1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines the interfaces that X86 uses to lower LLVM code into a
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// selection DAG.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "x86-isel"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "X86.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "X86InstrBuilder.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "X86ISelLowering.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "X86TargetMachine.h"
2019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "X86TargetObjectFile.h"
2119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "Utils/X86ShuffleDecode.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CallingConv.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalAlias.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalVariable.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instructions.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Intrinsics.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/LLVMContext.h"
3119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/CodeGen/IntrinsicLowering.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunction.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstrBuilder.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineJumpTableInfo.h"
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineModuleInfo.h"
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h"
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/PseudoSourceValue.h"
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCAsmInfo.h"
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCContext.h"
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCExpr.h"
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCSymbol.h"
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/BitVector.h"
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallSet.h"
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/Statistic.h"
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringExtras.h"
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/VectorExtras.h"
4819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/CallSite.h"
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Dwarf.h"
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/MathExtras.h"
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Target/TargetOptions.h"
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace dwarf;
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumTailCalls, "Number of tail calls");
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Forward declarations.
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       SDValue V2);
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue Insert128BitVector(SDValue Result,
6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SDValue Vec,
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SDValue Idx,
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SelectionDAG &DAG,
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  DebugLoc dl);
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue Extract128BitVector(SDValue Vec,
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   SDValue Idx,
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   SelectionDAG &DAG,
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   DebugLoc dl);
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Generate a DAG to grab 128-bits from a vector > 128 bits.  This
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// sets things up to match to an AVX VEXTRACTF128 instruction or a
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// simple subregister reference.  Idx is an index in the 128 bits we
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// want.  It need not be aligned to a 128-bit bounday.  That makes
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// lowering EXTRACT_VECTOR_ELT operations easier.
8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue Extract128BitVector(SDValue Vec,
8119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   SDValue Idx,
8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   SelectionDAG &DAG,
8319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   DebugLoc dl) {
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Vec.getValueType();
8519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT.getSizeInBits() == 256 && "Unexpected vector size!");
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ElVT = VT.getVectorElementType();
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int Factor = VT.getSizeInBits()/128;
8819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  VT.getVectorNumElements()/Factor);
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Extract from UNDEF is UNDEF.
9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Vec.getOpcode() == ISD::UNDEF)
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::UNDEF, dl, ResultVT);
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<ConstantSDNode>(Idx)) {
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Extract the relevant 128 bits.  Generate an EXTRACT_SUBVECTOR
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // we can match to VEXTRACTF128.
10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // This is the index of the first element of the 128-bit chunk
10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // we want.
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 * ElemsPerChunk);
10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 VecIdx);
11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Result;
11219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
11519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
11719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Generate a DAG to put 128-bits into a vector > 128 bits.  This
11819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// sets things up to match to an AVX VINSERTF128 instruction or a
11919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// simple superregister reference.  Idx is an index in the 128 bits
12019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// we want.  It need not be aligned to a 128-bit bounday.  That makes
12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// lowering INSERT_VECTOR_ELT operations easier.
12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue Insert128BitVector(SDValue Result,
12319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SDValue Vec,
12419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SDValue Idx,
12519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SelectionDAG &DAG,
12619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  DebugLoc dl) {
12719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<ConstantSDNode>(Idx)) {
12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT VT = Vec.getValueType();
12919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(VT.getSizeInBits() == 128 && "Unexpected vector size!");
13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT ElVT = VT.getVectorElementType();
13219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
13319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT ResultVT = Result.getValueType();
13419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert the relevant 128 bits.
13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // This is the index of the first element of the 128-bit chunk
13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // we want.
14019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 * ElemsPerChunk);
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue VecIdx = DAG.getConstant(NormalizedIdxVal, MVT::i32);
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Result = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         VecIdx);
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Result;
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool is64Bit = Subtarget->is64Bit();
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetEnvMacho()) {
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (is64Bit)
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return new X8664_MachoTargetObjectFile();
15919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return new TargetLoweringObjectFileMachO();
16019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
16119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetELF())
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return new TargetLoweringObjectFileELF();
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return new TargetLoweringObjectFileCOFF();
16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  llvm_unreachable("unknown subtarget type");
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  : TargetLowering(TM, createTLOF(TM)) {
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Subtarget = &TM.getSubtarget<X86Subtarget>();
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  X86ScalarSSEf64 = Subtarget->hasXMMInt();
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  X86ScalarSSEf32 = Subtarget->hasXMM();
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RegInfo = TM.getRegisterInfo();
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TD = getTargetData();
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Set up the TargetLowering object.
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86 is weird, it always uses i8 for shift amounts and setcc results.
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setBooleanContents(ZeroOrOneBooleanContent);
18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // For 64-bit since we have so many registers use the ILP scheduler, for
18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 32-bit code use the register pressure specific scheduling.
18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->is64Bit())
19019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setSchedulingPreference(Sched::ILP);
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else
19219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setSchedulingPreference(Sched::RegPressure);
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setStackPointerRegisterToSaveRestore(X86StackPtr);
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
19519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
19619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Setup Windows compiler runtime calls.
19719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::SDIV_I64, "_alldiv");
19819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::SREM_I64, "_allrem");
20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::UREM_I64, "_aullrem");
20119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::MUL_I64, "_allmul");
20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
20519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
20619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
20719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
20819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
21019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isTargetDarwin()) {
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setUseUnderscoreSetJmp(false);
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setUseUnderscoreLongJmp(false);
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (Subtarget->isTargetMingw()) {
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // MS runtime is weird: it exports _setjmp, but longjmp!
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setUseUnderscoreSetJmp(true);
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setUseUnderscoreLongJmp(false);
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setUseUnderscoreSetJmp(true);
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setUseUnderscoreLongJmp(true);
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Set up the register classes.
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRegisterClass(MVT::i8, X86::GR8RegisterClass);
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRegisterClass(MVT::i16, X86::GR16RegisterClass);
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRegisterClass(MVT::i32, X86::GR32RegisterClass);
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit())
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::i64, X86::GR64RegisterClass);
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We don't accept any truncstore of integer registers.
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::i64, MVT::i32, Expand);
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::i64, MVT::i16, Expand);
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::i32, MVT::i16, Expand);
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::i16, MVT::i8,  Expand);
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // SETOEQ and SETUNE require checking two conditions.
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // operation.
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Promote);
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UINT_TO_FP     , MVT::i64  , Expand);
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (!UseSoftFloat) {
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We have an algorithm for SSE2->double, and we turn this into a
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // 64-bit FILD followed by conditional FADD for other targets.
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UINT_TO_FP     , MVT::i64  , Custom);
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We have an algorithm for SSE2, and we turn this into a 64-bit
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FILD for other targets.
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Custom);
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this operation.
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!UseSoftFloat) {
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // SSE has no i16 to fp conversion, only i32
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (X86ScalarSSEf32) {
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // f32 and f64 cases are Legal, f80 case is not
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Custom);
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Promote);
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In 32-bit mode these are custom lowered.  In 64-bit mode F32 and F64
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // are Legal, f80 is custom lowered.
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_SINT     , MVT::i64  , Custom);
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SINT_TO_FP     , MVT::i64  , Custom);
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this operation.
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (X86ScalarSSEf32) {
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Promote);
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // f32 and f64 cases are Legal, f80 case is not
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Handle FP_TO_UINT by promoting the destination to a larger signed
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // conversion.
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_UINT     , MVT::i64  , Expand);
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Promote);
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (!UseSoftFloat) {
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Since AVX is a superset of SSE3, only check for SSE here.
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Expand FP_TO_UINT into a select.
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME: We would like to use a Custom expander here eventually to do
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the optimal thing for SSE vs. the default expansion in the legalizer.
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Expand);
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // With SSE3 we can use fisttpll to convert to a signed i64; without
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // SSE, we're stuck with a fistpll.
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Custom);
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: when we have SSE, these could be more efficient, by using movd/movq.
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!X86ScalarSSEf64) {
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::BITCAST        , MVT::f32  , Expand);
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::BITCAST        , MVT::i32  , Expand);
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget->is64Bit()) {
33519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::BITCAST      , MVT::f64  , Expand);
33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Without SSE, i64->f64 goes through memory.
33719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::BITCAST      , MVT::i64  , Expand);
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Scalar integer divide and remainder are lowered to use operations that
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // produce two results, to match the available instructions. This exposes
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the two-result form to trivial CSE, which is able to combine x/y and x%y
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // into a single instruction.
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Scalar integer multiply-high is also lowered to use two-result
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // operations, to match the available instructions. However, plain multiply
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // (low) operations are left as Legal, as there are single-result
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // instructions for this in x86. Using the two-result multiply instructions
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // when both high and low results are needed must be arranged by dagcombine.
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = 4; i != e; ++i) {
35219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MVT VT = IntVTs[i];
35319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::MULHS, VT, Expand);
35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::MULHU, VT, Expand);
35519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SDIV, VT, Expand);
35619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::UDIV, VT, Expand);
35719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SREM, VT, Expand);
35819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::UREM, VT, Expand);
35919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
36019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
36119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ADDC, VT, Custom);
36219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ADDE, VT, Custom);
36319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SUBC, VT, Custom);
36419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SUBE, VT, Custom);
36519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BR_JT            , MVT::Other, Expand);
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BRCOND           , MVT::Other, Custom);
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BR_CC            , MVT::Other, Expand);
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT_CC        , MVT::Other, Expand);
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit())
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Legal);
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Legal);
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FREM             , MVT::f32  , Expand);
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FREM             , MVT::f64  , Expand);
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FREM             , MVT::f80  , Expand);
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLT_ROUNDS_      , MVT::i32  , Custom);
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasBMI()) {
38319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTTZ           , MVT::i8   , Promote);
38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
38519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTTZ           , MVT::i8   , Custom);
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTTZ           , MVT::i16  , Custom);
38719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTTZ           , MVT::i32  , Custom);
38819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->is64Bit())
38919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::CTTZ         , MVT::i64  , Custom);
39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
39119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
39219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasLZCNT()) {
39319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTLZ           , MVT::i8   , Promote);
39419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
39519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTLZ           , MVT::i8   , Custom);
39619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTLZ           , MVT::i16  , Custom);
39719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTLZ           , MVT::i32  , Custom);
39819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->is64Bit())
39919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::CTLZ         , MVT::i64  , Custom);
40019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
40119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
40219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasPOPCNT()) {
40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTPOP          , MVT::i8   , Promote);
40419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
40519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTPOP          , MVT::i8   , Expand);
40619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTPOP          , MVT::i16  , Expand);
40719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CTPOP          , MVT::i32  , Expand);
40819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->is64Bit())
40919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::CTPOP        , MVT::i64  , Expand);
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BSWAP            , MVT::i16  , Expand);
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These should be promoted to a larger select which is supported.
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT          , MVT::i1   , Promote);
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86 wants to expand cmov itself.
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT          , MVT::i8   , Custom);
41919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SELECT          , MVT::i16  , Custom);
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT          , MVT::i32  , Custom);
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT          , MVT::f32  , Custom);
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT          , MVT::f64  , Custom);
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT          , MVT::f80  , Custom);
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC           , MVT::i8   , Custom);
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC           , MVT::i16  , Custom);
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC           , MVT::i32  , Custom);
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC           , MVT::f32  , Custom);
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC           , MVT::f64  , Custom);
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC           , MVT::f80  , Custom);
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SELECT        , MVT::i64  , Custom);
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SETCC         , MVT::i64  , Custom);
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
43419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::EH_RETURN       , MVT::Other, Custom);
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Darwin ABI issue.
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ConstantPool    , MVT::i32  , Custom);
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::JumpTable       , MVT::i32  , Custom);
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::GlobalAddress   , MVT::i32  , Custom);
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::GlobalTLSAddress, MVT::i32  , Custom);
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit())
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ExternalSymbol  , MVT::i32  , Custom);
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BlockAddress    , MVT::i32  , Custom);
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ConstantPool  , MVT::i64  , Custom);
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::JumpTable     , MVT::i64  , Custom);
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::GlobalAddress , MVT::i64  , Custom);
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ExternalSymbol, MVT::i64  , Custom);
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::BlockAddress  , MVT::i64  , Custom);
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SHL_PARTS       , MVT::i32  , Custom);
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SRA_PARTS       , MVT::i32  , Custom);
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SRL_PARTS       , MVT::i32  , Custom);
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SHL_PARTS     , MVT::i64  , Custom);
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SRA_PARTS     , MVT::i64  , Custom);
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SRL_PARTS     , MVT::i64  , Custom);
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasXMM())
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::PREFETCH      , MVT::Other, Legal);
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::MEMBARRIER    , MVT::Other, Custom);
46619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::ATOMIC_FENCE  , MVT::Other, Custom);
46719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // On X86 and X86-64, atomic operations are lowered to locked instructions.
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Locked instructions, in turn, have implicit fence semantics (all memory
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // operations are flushed before issuing the locked instruction, and they
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // are not buffered), so we can fold away the common pattern of
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // fence-atomic-fence.
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setShouldFoldAtomicFences(true);
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Expand certain atomics
47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = 4; i != e; ++i) {
47719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MVT VT = IntVTs[i];
47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
47919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget->is64Bit()) {
48419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
49419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasCmpxchg16b()) {
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
49619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
49719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME - use subtarget debug flags
49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->isTargetDarwin() &&
50019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !Subtarget->isTargetELF() &&
50119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !Subtarget->isTargetCygMing()) {
50219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
50319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
50419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
50519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
50619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::EHSELECTION,   MVT::i64, Expand);
50719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
50819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::EHSELECTION,   MVT::i32, Expand);
50919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->is64Bit()) {
51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setExceptionPointerRegister(X86::RAX);
51119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setExceptionSelectorRegister(X86::RDX);
51219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
51319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setExceptionPointerRegister(X86::EAX);
51419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setExceptionSelectorRegister(X86::EDX);
51519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
51919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
52019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::TRAP, MVT::Other, Legal);
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // VASTART needs to be custom lowered to use the VarArgsFrameIndex
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::VASTART           , MVT::Other, Custom);
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::VAEND             , MVT::Other, Expand);
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VAARG           , MVT::Other, Custom);
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VACOPY          , MVT::Other, Custom);
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VAARG           , MVT::Other, Expand);
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VACOPY          , MVT::Other, Expand);
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
53719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
53819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
53919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
54019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MVT::i64 : MVT::i32, Custom);
54119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else if (EnableSegmentedStacks)
54219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
54319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MVT::i64 : MVT::i32, Custom);
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
54519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
54619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MVT::i64 : MVT::i32, Expand);
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!UseSoftFloat && X86ScalarSSEf64) {
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // f32 and f64 use SSE.
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Set up the FP register classes.
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f32, X86::FR32RegisterClass);
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f64, X86::FR64RegisterClass);
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use ANDPD to simulate FABS.
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FABS , MVT::f64, Custom);
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FABS , MVT::f32, Custom);
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use XORP to simulate FNEG.
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG , MVT::f64, Custom);
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG , MVT::f32, Custom);
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use ANDPD and ORPD to simulate FCOPYSIGN.
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
56619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Lower this to FGETSIGNx86 plus an AND.
56719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
56819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
56919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We don't support sin/cos/fmod
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSIN , MVT::f64, Expand);
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOS , MVT::f64, Expand);
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSIN , MVT::f32, Expand);
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOS , MVT::f32, Expand);
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Expand FP immediates into loads from the stack, except for the special
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // cases we handle.
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+0.0)); // xorpd
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+0.0f)); // xorps
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (!UseSoftFloat && X86ScalarSSEf32) {
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use SSE for f32, x87 for f64.
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Set up the FP register classes.
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f32, X86::FR32RegisterClass);
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use ANDPS to simulate FABS.
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FABS , MVT::f32, Custom);
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use XORP to simulate FNEG.
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG , MVT::f32, Custom);
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UNDEF,     MVT::f64, Expand);
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use ANDPS and ORPS to simulate FCOPYSIGN.
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We don't support sin/cos/fmod
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSIN , MVT::f32, Expand);
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOS , MVT::f32, Expand);
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Special cases we handle for FP constants.
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+0.0f)); // xorps
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+0.0)); // FLD0
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+1.0)); // FLD1
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!UnsafeFPMath) {
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (!UseSoftFloat) {
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // f32 and f64 in x87.
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Set up the FP register classes.
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UNDEF,     MVT::f64, Expand);
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UNDEF,     MVT::f32, Expand);
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!UnsafeFPMath) {
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+0.0)); // FLD0
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+1.0)); // FLD1
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+0.0f)); // FLD0
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(+1.0f)); // FLD1
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
63819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // We don't support FMA.
63919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::FMA, MVT::f64, Expand);
64019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::FMA, MVT::f32, Expand);
64119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Long double always uses X87.
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!UseSoftFloat) {
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UNDEF,     MVT::f80, Expand);
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    {
64819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addLegalFPImmediate(TmpFlt);  // FLD0
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TmpFlt.changeSign();
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addLegalFPImmediate(TmpFlt);  // FLD0/FCHS
65219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
65319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool ignored;
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APFloat TmpFlt2(+1.0);
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      &ignored);
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addLegalFPImmediate(TmpFlt2);  // FLD1
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TmpFlt2.changeSign();
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addLegalFPImmediate(TmpFlt2);  // FLD1/FCHS
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!UnsafeFPMath) {
663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FSIN           , MVT::f80  , Expand);
664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::FCOS           , MVT::f80  , Expand);
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
66619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
66719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::FMA, MVT::f80, Expand);
668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Always use a library call for pow.
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FPOW             , MVT::f32  , Expand);
672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FPOW             , MVT::f64  , Expand);
673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FPOW             , MVT::f80  , Expand);
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG, MVT::f80, Expand);
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG2, MVT::f80, Expand);
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FLOG10, MVT::f80, Expand);
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FEXP, MVT::f80, Expand);
679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FEXP2, MVT::f80, Expand);
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First set operation action for all vector types to either promote
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // (for widening) or expand (for scalarization). Then we will selectively
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // turn on ones that can be effectively codegen'd.
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
70219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
70319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
72519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::TRUNCATE,  (MVT::SimpleValueType)VT, Expand);
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SIGN_EXTEND,  (MVT::SimpleValueType)VT, Expand);
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ZERO_EXTEND,  (MVT::SimpleValueType)VT, Expand);
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ANY_EXTEND,  (MVT::SimpleValueType)VT, Expand);
74019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,  (MVT::SimpleValueType)VT, Expand);
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setTruncStoreAction((MVT::SimpleValueType)VT,
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          (MVT::SimpleValueType)InnerVT, Expand);
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: In order to prevent SSE instructions being expanded to MMX ones
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // with -msoft-float, disable use of MMX as well.
75219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!UseSoftFloat && Subtarget->hasMMX()) {
75319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
75419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // No operations on x86mmx supported, everything uses intrinsics.
75519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
75619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
75719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // MMX-sized vectors (other than x86mmx) are expected to be expanded
75819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // into smaller operations.
75919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::MULHS,              MVT::v8i8,  Expand);
76019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::MULHS,              MVT::v4i16, Expand);
76119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::MULHS,              MVT::v2i32, Expand);
76219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::MULHS,              MVT::v1i64, Expand);
76319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::AND,                MVT::v8i8,  Expand);
76419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::AND,                MVT::v4i16, Expand);
76519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::AND,                MVT::v2i32, Expand);
76619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::AND,                MVT::v1i64, Expand);
76719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::OR,                 MVT::v8i8,  Expand);
76819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::OR,                 MVT::v4i16, Expand);
76919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::OR,                 MVT::v2i32, Expand);
77019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::OR,                 MVT::v1i64, Expand);
77119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::XOR,                MVT::v8i8,  Expand);
77219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::XOR,                MVT::v4i16, Expand);
77319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::XOR,                MVT::v2i32, Expand);
77419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::XOR,                MVT::v1i64, Expand);
77519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v8i8,  Expand);
77619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v4i16, Expand);
77719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v2i32, Expand);
77819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v1i64, Expand);
77919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v1i64, Expand);
78019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SELECT,             MVT::v8i8,  Expand);
78119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SELECT,             MVT::v4i16, Expand);
78219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SELECT,             MVT::v2i32, Expand);
78319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SELECT,             MVT::v1i64, Expand);
78419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::BITCAST,            MVT::v8i8,  Expand);
78519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::BITCAST,            MVT::v4i16, Expand);
78619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::BITCAST,            MVT::v2i32, Expand);
78719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::BITCAST,            MVT::v1i64, Expand);
78819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
78919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!UseSoftFloat && Subtarget->hasXMM()) {
790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FADD,               MVT::v4f32, Legal);
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSUB,               MVT::v4f32, Legal);
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FMUL,               MVT::v4f32, Legal);
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FDIV,               MVT::v4f32, Legal);
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSQRT,              MVT::v4f32, Legal);
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG,               MVT::v4f32, Custom);
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD,               MVT::v4f32, Legal);
799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::BUILD_VECTOR,       MVT::v4f32, Custom);
800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v4f32, Custom);
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SELECT,             MVT::v4f32, Custom);
80319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,              MVT::v4f32, Custom);
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
80619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!UseSoftFloat && Subtarget->hasXMMInt()) {
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // registers cannot be used even for integer operations.
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ADD,                MVT::v16i8, Legal);
817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ADD,                MVT::v8i16, Legal);
818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ADD,                MVT::v4i32, Legal);
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::ADD,                MVT::v2i64, Legal);
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::MUL,                MVT::v2i64, Custom);
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SUB,                MVT::v16i8, Legal);
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SUB,                MVT::v8i16, Legal);
823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SUB,                MVT::v4i32, Legal);
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SUB,                MVT::v2i64, Legal);
825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::MUL,                MVT::v8i16, Legal);
826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FADD,               MVT::v2f64, Legal);
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSUB,               MVT::v2f64, Legal);
828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FMUL,               MVT::v2f64, Legal);
829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FDIV,               MVT::v2f64, Legal);
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSQRT,              MVT::v2f64, Legal);
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG,               MVT::v2f64, Custom);
832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
83319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,              MVT::v2i64, Custom);
83419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,              MVT::v16i8, Custom);
83519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,              MVT::v8i16, Custom);
83619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,              MVT::v4i32, Custom);
837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v16i8, Custom);
839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v8i16, Custom);
840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4i32, Custom);
842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4f32, Custom);
843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v2f64, Custom);
845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v2i64, Custom);
846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v16i8, Custom);
847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v8i16, Custom);
848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v4i32, Custom);
849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT VT = (MVT::SimpleValueType)i;
853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Do not attempt to custom lower non-power-of-2 vectors
854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!isPowerOf2_32(VT.getVectorNumElements()))
855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Do not attempt to custom lower non-128-bit vectors
857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!VT.is128BitVector())
858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::BUILD_VECTOR,
860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         VT.getSimpleVT().SimpleTy, Custom);
861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::VECTOR_SHUFFLE,
862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         VT.getSimpleVT().SimpleTy, Custom);
863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::EXTRACT_VECTOR_ELT,
864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         VT.getSimpleVT().SimpleTy, Custom);
865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::BUILD_VECTOR,       MVT::v2f64, Custom);
868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::BUILD_VECTOR,       MVT::v2i64, Custom);
869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2f64, Custom);
870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2i64, Custom);
871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2f64, Custom);
872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget->is64Bit()) {
875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2i64, Custom);
876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT VT = SVT;
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Do not attempt to promote non-128-bit vectors
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!VT.is128BitVector())
886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
88719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::AND,    SVT, Promote);
889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddPromotedToType (ISD::AND,    SVT, MVT::v2i64);
890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::OR,     SVT, Promote);
891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddPromotedToType (ISD::OR,     SVT, MVT::v2i64);
892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::XOR,    SVT, Promote);
893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddPromotedToType (ISD::XOR,    SVT, MVT::v2i64);
894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::LOAD,   SVT, Promote);
895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddPromotedToType (ISD::LOAD,   SVT, MVT::v2i64);
896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::SELECT, SVT, Promote);
897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setTruncStoreAction(MVT::f64, MVT::f32, Expand);
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Custom lower v2i64 and v2f64 selects.
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD,               MVT::v2f64, Legal);
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD,               MVT::v2i64, Legal);
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SELECT,             MVT::v2f64, Custom);
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SELECT,             MVT::v2i64, Custom);
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FP_TO_SINT,         MVT::v4i32, Legal);
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SINT_TO_FP,         MVT::v4i32, Legal);
910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
91219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FFLOOR,             MVT::f32,   Legal);
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCEIL,              MVT::f32,   Legal);
915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FTRUNC,             MVT::f32,   Legal);
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FRINT,              MVT::f32,   Legal);
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEARBYINT,         MVT::f32,   Legal);
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FFLOOR,             MVT::f64,   Legal);
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FCEIL,              MVT::f64,   Legal);
920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FTRUNC,             MVT::f64,   Legal);
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FRINT,              MVT::f64,   Legal);
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEARBYINT,         MVT::f64,   Legal);
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Do we need to handle scalar-to-vector here?
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::MUL,                MVT::v4i32, Legal);
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Can turn SHL into an integer multiply.
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SHL,                MVT::v4i32, Custom);
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::SHL,                MVT::v16i8, Custom);
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
93119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v2f64, Legal);
93219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v2i64, Legal);
93319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v16i8, Legal);
93419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v4i32, Legal);
93519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v4f32, Legal);
93619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // i8 and i16 vectors are custom , because the source register and source
938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // source memory operand types are not the same width.  f32 vectors are
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // custom since the immediate controlling the insert encodes additional
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // information.
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v16i8, Custom);
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4i32, Custom);
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4f32, Custom);
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget->is64Bit()) {
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2i64, Legal);
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
95719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasXMMInt()) {
95819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v2i64, Custom);
95919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v4i32, Custom);
96019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v16i8, Custom);
96119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v8i16, Custom);
96219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
96319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v2i64, Custom);
96419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v4i32, Custom);
96519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v8i16, Custom);
96619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
96719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRA,               MVT::v4i32, Custom);
96819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRA,               MVT::v8i16, Custom);
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSE42() || Subtarget->hasAVX())
97219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,             MVT::v2i64, Custom);
97319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!UseSoftFloat && Subtarget->hasAVX()) {
97519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::v32i8,  X86::VR256RegisterClass);
97619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::v16i16, X86::VR256RegisterClass);
97719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::v8i32,  X86::VR256RegisterClass);
97819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::v8f32,  X86::VR256RegisterClass);
97919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::v4i64,  X86::VR256RegisterClass);
98019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    addRegisterClass(MVT::v4f64,  X86::VR256RegisterClass);
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD,               MVT::v8f32, Legal);
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD,               MVT::v4f64, Legal);
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::LOAD,               MVT::v4i64, Legal);
98519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FADD,               MVT::v8f32, Legal);
987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSUB,               MVT::v8f32, Legal);
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FMUL,               MVT::v8f32, Legal);
989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FDIV,               MVT::v8f32, Legal);
990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSQRT,              MVT::v8f32, Legal);
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG,               MVT::v8f32, Custom);
99219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FADD,               MVT::v4f64, Legal);
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSUB,               MVT::v4f64, Legal);
995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FMUL,               MVT::v4f64, Legal);
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FDIV,               MVT::v4f64, Legal);
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FSQRT,              MVT::v4f64, Legal);
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::FNEG,               MVT::v4f64, Custom);
999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
100019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::FP_TO_SINT,         MVT::v8i32, Legal);
100119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SINT_TO_FP,         MVT::v8i32, Legal);
100219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::FP_ROUND,           MVT::v4f32, Legal);
100319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
100419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v4f64,  Custom);
100519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v4i64,  Custom);
100619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v8f32,  Custom);
100719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v8i32,  Custom);
100819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v32i8,  Custom);
100919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::CONCAT_VECTORS,     MVT::v16i16, Custom);
101019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
101119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v4i64, Custom);
101219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v8i32, Custom);
101319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v16i16, Custom);
101419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRL,               MVT::v32i8, Custom);
101519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
101619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v4i64, Custom);
101719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v8i32, Custom);
101819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v16i16, Custom);
101919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SHL,               MVT::v32i8, Custom);
102019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
102119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRA,               MVT::v8i32, Custom);
102219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SRA,               MVT::v16i16, Custom);
102319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
102419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,             MVT::v32i8, Custom);
102519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,             MVT::v16i16, Custom);
102619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,             MVT::v8i32, Custom);
102719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SETCC,             MVT::v4i64, Custom);
102819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
102919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SELECT,            MVT::v4f64, Custom);
103019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SELECT,            MVT::v4i64, Custom);
103119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SELECT,            MVT::v8f32, Custom);
103219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
103319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v4f64, Legal);
103419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v4i64, Legal);
103519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v8i32, Legal);
103619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::VSELECT,            MVT::v8f32, Legal);
103719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
103819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ADD,               MVT::v4i64, Custom);
103919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ADD,               MVT::v8i32, Custom);
104019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ADD,               MVT::v16i16, Custom);
104119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::ADD,               MVT::v32i8, Custom);
104219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
104319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SUB,               MVT::v4i64, Custom);
104419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SUB,               MVT::v8i32, Custom);
104519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SUB,               MVT::v16i16, Custom);
104619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SUB,               MVT::v32i8, Custom);
104719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
104819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::MUL,               MVT::v4i64, Custom);
104919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::MUL,               MVT::v8i32, Custom);
105019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::MUL,               MVT::v16i16, Custom);
105119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Don't lower v32i8 because there is no 128-bit byte mul
105219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
105319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Custom lower several nodes for 256-bit types.
105419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
105519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
105619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
105719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EVT VT = SVT;
1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
105919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Extract subvector is special because the value type
106019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // (result) is 128-bit but the source is 256-bit wide.
106119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT.is128BitVector())
106219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        setOperationAction(ISD::EXTRACT_SUBVECTOR, SVT, Custom);
1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
106419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Do not attempt to custom lower other non-256-bit vectors
106519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!VT.is256BitVector())
1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
106819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::BUILD_VECTOR,       SVT, Custom);
106919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::VECTOR_SHUFFLE,     SVT, Custom);
107019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::INSERT_VECTOR_ELT,  SVT, Custom);
107119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::EXTRACT_VECTOR_ELT, SVT, Custom);
107219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::SCALAR_TO_VECTOR,   SVT, Custom);
107319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::INSERT_SUBVECTOR,   SVT, Custom);
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
107719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = (unsigned)MVT::v32i8; i != (unsigned)MVT::v4i64; ++i) {
107819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
107919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EVT VT = SVT;
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
108119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Do not attempt to promote non-256-bit vectors
108219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!VT.is256BitVector())
1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
108419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
108519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::AND,    SVT, Promote);
108619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AddPromotedToType (ISD::AND,    SVT, MVT::v4i64);
108719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::OR,     SVT, Promote);
108819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AddPromotedToType (ISD::OR,     SVT, MVT::v4i64);
108919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::XOR,    SVT, Promote);
109019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AddPromotedToType (ISD::XOR,    SVT, MVT::v4i64);
109119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::LOAD,   SVT, Promote);
109219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AddPromotedToType (ISD::LOAD,   SVT, MVT::v4i64);
109319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      setOperationAction(ISD::SELECT, SVT, Promote);
109419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AddPromotedToType (ISD::SELECT, SVT, MVT::v4i64);
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
109619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
109819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
109919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // of this type with custom code.
110019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
110119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; VT++) {
110219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, Custom);
1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We want to custom lower some of our intrinsics.
1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // handle type legalization for these operations here.
1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: We really should do custom legalization for addition and
1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // subtraction on x86-32 once PR3203 is fixed.  We really can't do much better
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // than generic legalization for 64-bit multiplication-with-overflow, though.
111519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
111619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Add/Sub/Mul with overflow operations are custom lowered.
111719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MVT VT = IntVTs[i];
111819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SADDO, VT, Custom);
111919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::UADDO, VT, Custom);
112019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SSUBO, VT, Custom);
112119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::USUBO, VT, Custom);
112219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::SMULO, VT, Custom);
112319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    setOperationAction(ISD::UMULO, VT, Custom);
1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // There are no 8-bit 3-address imul/mul instructions
112719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::SMULO, MVT::i8, Expand);
112819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::UMULO, MVT::i8, Expand);
112919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget->is64Bit()) {
1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These libcalls are not available in 32-bit.
1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setLibcallName(RTLIB::SHL_I128, 0);
1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setLibcallName(RTLIB::SRL_I128, 0);
1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setLibcallName(RTLIB::SRA_I128, 0);
1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We have target-specific dag combine patterns for the following nodes:
1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
114024809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  setTargetDAGCombine(ISD::BITCAST);
1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::BUILD_VECTOR);
114219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::VSELECT);
1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::SELECT);
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::SHL);
1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::SRA);
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::SRL);
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::OR);
114819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::AND);
114919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::ADD);
115019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::FADD);
115119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::FSUB);
115219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::SUB);
115319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::LOAD);
1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::STORE);
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTargetDAGCombine(ISD::ZERO_EXTEND);
115619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setTargetDAGCombine(ISD::SINT_TO_FP);
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit())
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setTargetDAGCombine(ISD::MUL);
1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  computeRegisterProperties();
1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
116219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // On Darwin, -Os means optimize for size without hurting performance,
116319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // do not reduce the limit.
1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
116519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
116719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
116819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
116919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setPrefLoopAlignment(16);
1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  benefitFromCodePlacementOpt = true;
117219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
117319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setPrefFunctionAlignment(4);
1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
117719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanEVT X86TargetLowering::getSetCCResultType(EVT VT) const {
117819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!VT.isVector()) return MVT::i8;
117919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return VT.changeVectorElementTypeToInteger();
1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the desired ByVal argument alignment.
118519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MaxAlign == 16)
1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
118819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VTy->getBitWidth() == 128)
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaxAlign = 16;
119119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned EltAlign = 0;
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getMaxByValAlign(ATy->getElementType(), EltAlign);
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EltAlign > MaxAlign)
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaxAlign = EltAlign;
119619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned EltAlign = 0;
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getMaxByValAlign(STy->getElementType(i), EltAlign);
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (EltAlign > MaxAlign)
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaxAlign = EltAlign;
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MaxAlign == 16)
1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return;
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// function arguments in the caller parameter area. For X86, aggregates
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// are at 4-byte boundaries.
121319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Max of 8 and alignment of type.
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned TyAlign = TD->getABITypeAlignment(Ty);
1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TyAlign > 8)
1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return TyAlign;
1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 8;
1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Align = 4;
122319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasXMM())
1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getMaxByValAlign(Ty, Align);
1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Align;
1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getOptimalMemOpType - Returns the target specific optimal type for load
1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// and store operations as a result of memset, memcpy, and memmove
1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// lowering. If DstAlign is zero that means it's safe to destination
1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// means there isn't a need to check it against alignment requirement,
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// probably because the source does not need to be loaded. If
1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 'NonScalarIntSafe' is true, that means it's safe to return a
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// constant so it does not need to be loaded.
1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// It returns EVT::Other if the type should be determined using generic
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// target-independent logic.
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanEVT
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::getOptimalMemOpType(uint64_t Size,
1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       unsigned DstAlign, unsigned SrcAlign,
1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       bool NonScalarIntSafe,
1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       bool MemcpyStrSrc,
1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       MachineFunction &MF) const {
1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // linux.  This is because the stack realignment code can't handle certain
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // cases like PR2962.  This should be removed when PR2962 is fixed.
1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function *F = MF.getFunction();
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NonScalarIntSafe &&
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !F->hasFnAttr(Attribute::NoImplicitFloat)) {
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Size >= 16 &&
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (Subtarget->isUnalignedMemAccessFast() ||
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         ((DstAlign == 0 || DstAlign >= 16) &&
1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (SrcAlign == 0 || SrcAlign >= 16))) &&
1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Subtarget->getStackAlignment() >= 16) {
125719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Subtarget->hasAVX() &&
125819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Subtarget->getStackAlignment() >= 32)
125919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return MVT::v8f32;
126019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Subtarget->hasXMMInt())
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return MVT::v4i32;
126219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Subtarget->hasXMM())
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return MVT::v4f32;
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (!MemcpyStrSrc && Size >= 8 &&
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               !Subtarget->is64Bit() &&
1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               Subtarget->getStackAlignment() >= 8 &&
126719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman               Subtarget->hasXMMInt()) {
1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Do not use f64 to lower memcpy if source is string constant. It's
1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // better to use i32 to avoid the loads.
1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return MVT::f64;
1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit() && Size >= 8)
1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return MVT::i64;
1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MVT::i32;
1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// current function.  The returned value is a member of the
1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MachineJumpTableInfo::JTEntryKind enum.
1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned X86TargetLowering::getJumpTableEncoding() const {
1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // symbol.
1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Subtarget->isPICStyleGOT())
1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return MachineJumpTableInfo::EK_Custom32;
128719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, use the normal jump table encoding heuristics.
1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::getJumpTableEncoding();
1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst MCExpr *
1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             const MachineBasicBlock *MBB,
1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             unsigned uid,MCContext &Ctx) const{
1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         Subtarget->isPICStyleGOT());
1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // entries.
1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MCSymbolRefExpr::Create(MBB->getSymbol(),
1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 MCSymbolRefExpr::VK_GOTOFF, Ctx);
1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// jumptable.
1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                    SelectionDAG &DAG) const {
1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget->is64Bit())
1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This doesn't have DebugLoc associated with it, but is not really the
1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // same as a Register.
1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Table;
1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MCExpr.
1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst MCExpr *X86TargetLowering::
1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumangetPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             MCContext &Ctx) const {
1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86-64 uses RIP relative addressing based on the jump table label.
1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleRIPRel())
1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, the reference is relative to the PIC base.
132619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
132919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// FIXME: Why this routine is here? Move to RegInfo!
1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<const TargetRegisterClass*, uint8_t>
1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::findRepresentativeClass(EVT VT) const{
1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *RRC = 0;
1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint8_t Cost = 1;
1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (VT.getSimpleVT().SimpleTy) {
1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TargetLowering::findRepresentativeClass(VT);
1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RRC = (Subtarget->is64Bit()
1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
134119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::x86mmx:
1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RRC = X86::VR64RegisterClass;
1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::f32: case MVT::f64:
1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v4f32: case MVT::v2f64:
1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v4f64:
1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RRC = X86::VR128RegisterClass;
1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return std::make_pair(RRC, Cost);
1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               unsigned &Offset) const {
1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget->isTargetLinux())
1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset = 0x28;
1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddressSpace = 256;
1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AddressSpace = 257;
1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // %gs:0x14 on i386
1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset = 0x14;
1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AddressSpace = 256;
1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//               Return Value Calling Convention Implementation
1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "X86GenCallingConv.inc"
1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
138219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool
138319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
138419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman				  MachineFunction &MF, bool isVarArg,
1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        const SmallVectorImpl<ISD::OutputArg> &Outs,
1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        LLVMContext &Context) const {
1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> RVLocs;
138819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 RVLocs, Context);
1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return CCInfo.CheckReturn(Outs, RetCC_X86);
1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerReturn(SDValue Chain,
1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               CallingConv::ID CallConv, bool isVarArg,
1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const SmallVectorImpl<ISD::OutputArg> &Outs,
1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const SmallVectorImpl<SDValue> &OutVals,
1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DebugLoc dl, SelectionDAG &DAG) const {
1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> RVLocs;
140319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 RVLocs, *DAG.getContext());
1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCInfo.AnalyzeReturn(Outs, RetCC_X86);
1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add the regs to the liveout set for the function.
1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != RVLocs.size(); ++i)
1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MRI.addLiveOut(RVLocs[i].getLocReg());
1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Flag;
1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 6> RetOps;
1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Operand #1 = Bytes To Pop
1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   MVT::i16));
1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Copy the result values into the output registers.
1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != RVLocs.size(); ++i) {
1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = RVLocs[i];
1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(VA.isRegLoc() && "Can only return in registers!");
1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue ValToCopy = OutVals[i];
1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT ValVT = ValToCopy.getValueType();
1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
142819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If this is x86-64, and we disabled SSE, we can't return FP values,
142919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // or SSE or MMX vectors.
143019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
143119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
143219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      report_fatal_error("SSE register return with SSE disabled");
1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Likewise we can't return F64 values with SSE1 only.  gcc does so, but
1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // llvm-gcc has never done it right and no one has noticed, so this
1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // should be OK for now.
1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ValVT == MVT::f64 &&
143919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      report_fatal_error("SSE2 register return with SSE2 disabled");
1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the RET instruction and handled by the FP Stackifier.
1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VA.getLocReg() == X86::ST0 ||
1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        VA.getLocReg() == X86::ST1) {
1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If this is a copy from an xmm register to ST(0), use an FPExtend to
1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // change the value to the FP stack register class.
1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (isScalarFPTypeInSSEReg(VA.getValVT()))
1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RetOps.push_back(ValToCopy);
1451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Don't emit a copytoreg.
1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // which is returned in RAX / RDX.
1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget->is64Bit()) {
145819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (ValVT == MVT::x86mmx) {
145919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
146019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  ValToCopy);
146319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // If we don't have SSE2 available, convert to v4f32 so the generated
146419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // register is legal.
146519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (!Subtarget->hasXMMInt())
146619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
146719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Flag = Chain.getValue(1);
1473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The x86-64 ABI for returning structs by value requires that we copy
1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the sret argument into %rax for the return. We saved the argument into
1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // a virtual register in the entry block, so now we copy the value out
1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // and into %rax.
1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit() &&
1480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineFunction &MF = DAG.getMachineFunction();
1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = FuncInfo->getSRetReturnReg();
148419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(Reg &&
1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "SRetReturnReg should have been set in LowerFormalArguments().");
1486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Flag = Chain.getValue(1);
1490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // RAX now acts like a return value.
1492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MRI.addLiveOut(X86::RAX);
1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RetOps[0] = Chain;  // Update chain.
1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add the flag if we have it.
1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Flag.getNode())
1499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RetOps.push_back(Flag);
1500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(X86ISD::RET_FLAG, dl,
1502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     MVT::Other, &RetOps[0], RetOps.size());
1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
150619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N->getNumValues() != 1)
150719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
150819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!N->hasNUsesOfValue(1, 0))
150919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
151019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
151119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDNode *Copy = *N->use_begin();
151219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Copy->getOpcode() != ISD::CopyToReg &&
151319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Copy->getOpcode() != ISD::FP_EXTEND)
151419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
151519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
151619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool HasRet = false;
151719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
151819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       UI != UE; ++UI) {
151919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (UI->getOpcode() != X86ISD::RET_FLAG)
152019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
152119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    HasRet = true;
152219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
152319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
152419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return HasRet;
152519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
152619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
152719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanEVT
152819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            ISD::NodeType ExtendKind) const {
153019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MVT ReturnMVT;
153119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // TODO: Is this also valid on 32-bit?
153219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ReturnMVT = MVT::i8;
153419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else
153519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ReturnMVT = MVT::i32;
153619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
153719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT MinVT = getRegisterType(Context, ReturnMVT);
153819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return VT.bitsLT(MinVT) ? MinVT : VT;
153919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
154019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerCallResult - Lower the result values of a call into the
1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// appropriate copies out of appropriate physical registers.
1543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
1545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   CallingConv::ID CallConv, bool isVarArg,
1547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const SmallVectorImpl<ISD::InputArg> &Ins,
1548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   DebugLoc dl, SelectionDAG &DAG,
1549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   SmallVectorImpl<SDValue> &InVals) const {
1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Assign locations to each value returned by this call.
1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> RVLocs;
1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Is64Bit = Subtarget->is64Bit();
155419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
155519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		 getTargetMachine(), RVLocs, *DAG.getContext());
1556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
1557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Copy all of the result registers out of their specified physreg.
1559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != RVLocs.size(); ++i) {
1560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = RVLocs[i];
1561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT CopyVT = VA.getValVT();
1562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is x86-64, and we disabled SSE, we can't return FP values
1564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
156519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
1566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      report_fatal_error("SSE register return with SSE disabled");
1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Val;
1570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is a call to a function that returns an fp value on the floating
1572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // point stack, we must guarantee the the value is popped from the stack, so
1573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a CopyFromReg is not good enough - the copy instruction may be eliminated
157419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // if the return value is not used. We use the FpPOP_RETVAL instruction
1575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // instead.
1576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If we prefer to use the value in xmm registers, copy it out as f80 and
1578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // use a truncate to move it from fp stack reg to xmm reg.
1579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Ops[] = { Chain, InFlag };
158119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
158219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         MVT::Other, MVT::Glue, Ops, 2), 1);
1583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Val = Chain.getValue(0);
1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Round the f80 to the right size, which also moves it to the appropriate
1586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // xmm register.
1587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CopyVT != VA.getValVT())
1588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          // This truncation won't change the value.
1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getIntPtrConstant(1));
1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 CopyVT, InFlag).getValue(1);
1594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Val = Chain.getValue(0);
1595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = Chain.getValue(2);
1597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InVals.push_back(Val);
1598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Chain;
1601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                C & StdCall & Fast Calling Convention implementation
1606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  StdCall calling convention seems to be standard for many Windows' API
1608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  routines and around. It differs from C calling convention just a little:
1609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  callee should clean up the stack, not caller. Symbols should be also
1610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  decorated in some fancy way :) It doesn't support any vector arguments.
1611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  For info on fast calling convention see Fast Calling Convention (tail call)
1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  implementation LowerX86_32FastCCCallTo.
1613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CallIsStructReturn - Determines whether a call uses struct return
1615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// semantics.
1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Outs.empty())
1618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Outs[0].Flags.isSRet();
1621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ArgsAreStructReturn - Determines whether a function uses struct
1624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// return semantics.
1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool
1626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ins.empty())
1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Ins[0].Flags.isSRet();
1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// by "Src" to address "Dst" with size and alignment information specified by
1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specific parameter attribute. The copy will be passed as a byval
1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// function parameter.
1637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue
1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanCreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DebugLoc dl) {
164119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
164219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       /*isVolatile*/false, /*AlwaysInline=*/true,
164519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo(), MachinePointerInfo());
1646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// IsTailCallConvention - Return true if the calling convention is one that
1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// supports tail call optimization.
1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool IsTailCallConvention(CallingConv::ID CC) {
1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
165419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
165519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!CI->isTailCall())
165619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
165719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
165819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CallSite CS(CI);
165919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CallingConv::ID CalleeCC = CS.getCallingConv();
166019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
166119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
166219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
166319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
166419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
166519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// a tailcall target by changing its ABI.
1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
1669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return GuaranteedTailCallOpt && IsTailCallConvention(CC);
1670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
1673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerMemArgument(SDValue Chain,
1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    CallingConv::ID CallConv,
1675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const SmallVectorImpl<ISD::InputArg> &Ins,
1676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    DebugLoc dl, SelectionDAG &DAG,
1677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const CCValAssign &VA,
1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    MachineFrameInfo *MFI,
1679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    unsigned i) const {
1680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the nodes corresponding to a load from this parameter slot.
1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ISD::ArgFlagsTy Flags = Ins[i].Flags;
1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT ValVT;
1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If value is passed by pointer we have address passed instead of the value
1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // itself.
1688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VA.getLocInfo() == CCValAssign::Indirect)
1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ValVT = VA.getLocVT();
1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ValVT = VA.getValVT();
1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: For now, all byval parameter objects are marked mutable. This can be
1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // changed with more analysis.
1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In case of tail call optimization mark all arguments mutable. Since they
1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // could be overwritten by lowering of arguments in case of a tail call.
1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Flags.isByVal()) {
169819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Bytes = Flags.getByValSize();
169919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
170019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getFrameIndex(FI, getPointerTy());
1702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    VA.getLocMemOffset(), isImmutable);
1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getLoad(ValVT, dl, Chain, FIN,
170719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo::getFixedStack(FI),
1708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       false, false, 0);
1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
1713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerFormalArguments(SDValue Chain,
1714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        CallingConv::ID CallConv,
1715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        bool isVarArg,
1716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      const SmallVectorImpl<ISD::InputArg> &Ins,
1717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        DebugLoc dl,
1718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        SelectionDAG &DAG,
1719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        SmallVectorImpl<SDValue> &InVals)
1720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          const {
1721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
1722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function* Fn = MF.getFunction();
1725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Fn->hasExternalLinkage() &&
1726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Subtarget->isTargetCygMing() &&
1727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Fn->getName() == "main")
1728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setForceFramePointer(true);
1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFrameInfo *MFI = MF.getFrameInfo();
1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Is64Bit = Subtarget->is64Bit();
1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool IsWin64 = Subtarget->isTargetWin64();
1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Var args not supported with calling convention fastcc or ghc");
1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Assign locations to all of the incoming arguments.
1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> ArgLocs;
173919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
1740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 ArgLocs, *DAG.getContext());
174119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
174219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Allocate shadow area for Win64
174319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (IsWin64) {
174419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCInfo.AllocateStack(32, 8);
174519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
174619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
174719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned LastVal = ~0U;
1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ArgValue;
1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = ArgLocs[i];
1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // places.
1755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(VA.getValNo() != LastVal &&
1756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Don't support value assigned to multiple locs yet");
175719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    (void)LastVal;
1758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LastVal = VA.getValNo();
1759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VA.isRegLoc()) {
1761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT RegVT = VA.getLocVT();
1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TargetRegisterClass *RC = NULL;
1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RegVT == MVT::i32)
1764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::GR32RegisterClass;
1765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (Is64Bit && RegVT == MVT::i64)
1766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::GR64RegisterClass;
1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (RegVT == MVT::f32)
1768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::FR32RegisterClass;
1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (RegVT == MVT::f64)
1770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::FR64RegisterClass;
1771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::VR256RegisterClass;
1773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
1774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::VR128RegisterClass;
177519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else if (RegVT == MVT::x86mmx)
1776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RC = X86::VR64RegisterClass;
1777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
1778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        llvm_unreachable("Unknown argument type!");
1779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If this is an 8 or 16-bit value, it is really passed promoted to 32
1784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // bits.  Insert an assert[sz]ext to capture this, then truncate to the
1785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // right size.
1786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VA.getLocInfo() == CCValAssign::SExt)
1787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getValueType(VA.getValVT()));
1789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (VA.getLocInfo() == CCValAssign::ZExt)
1790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getValueType(VA.getValVT()));
1792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (VA.getLocInfo() == CCValAssign::BCvt)
179319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
1794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VA.isExtInLoc()) {
1796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Handle MMX values passed in XMM regs.
1797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (RegVT.isVector()) {
179819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
179919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 ArgValue);
1800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else
1801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(VA.isMemLoc());
1805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
1806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If value is passed via pointer - do a load.
1809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VA.getLocInfo() == CCValAssign::Indirect)
181019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
181119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             MachinePointerInfo(), false, false, 0);
1812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InVals.push_back(ArgValue);
1814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The x86-64 ABI for returning structs by value requires that we copy
1817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the sret argument into %rax for the return. Save the argument into
1818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // a virtual register so that we can access it from the return points.
1819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
1820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = FuncInfo->getSRetReturnReg();
1822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Reg) {
1823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FuncInfo->setSRetReturnReg(Reg);
1825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
1827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
1828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned StackSize = CCInfo.getNextStackOffset();
1831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Align stack specially for tail calls.
1832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FuncIsMadeTailCallSafe(CallConv))
1833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
1834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the function takes variable number of arguments, make a frame index for
1836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the start of the first vararg value... for expansion of llvm.va_start.
1837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isVarArg) {
1838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    CallConv != CallingConv::X86_ThisCall)) {
1840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
1841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Is64Bit) {
1843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME: We should really autogenerate these arrays
1846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      static const unsigned GPR64ArgRegsWin64[] = {
1847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        X86::RCX, X86::RDX, X86::R8,  X86::R9
1848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      };
1849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      static const unsigned GPR64ArgRegs64Bit[] = {
1850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      };
1852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      static const unsigned XMMArgRegs64Bit[] = {
1853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      };
185619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      const unsigned *GPR64ArgRegs;
185719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned NumXMMRegs = 0;
1858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (IsWin64) {
186019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // The XMM registers which might contain var arg parameters are shadowed
186119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // in their paired GPR.  So we only need to save the GPR to their home
186219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // slots.
186319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        TotalNumIntRegs = 4;
1864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GPR64ArgRegs = GPR64ArgRegsWin64;
1865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
1866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GPR64ArgRegs = GPR64ArgRegs64Bit;
186819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
186919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
1870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       TotalNumIntRegs);
1873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
187519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
1876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             "SSE register cannot be used when SSE is disabled!");
1877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
1878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             "SSE register cannot be used when SSE is disabled!");
187919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
1880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Kernel mode asks for SSE to be disabled, so don't push them
1881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // on the stack.
1882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        TotalNumXMMRegs = 0;
1883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
188419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (IsWin64) {
188519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
188619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Get to the caller-allocated home save location.  Add 8 to account
188719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // for the return address.
188819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
188919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        FuncInfo->setRegSaveFrameIndex(
189019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
189119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Fixup to set vararg frame on shadow area (4 x i64).
189219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (NumIntRegs < 4)
189319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
189419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else {
189519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // For X86-64, if there are vararg parameters that are passed via
189619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // registers, then we must store them to their spots on the stack so they
189719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // may be loaded by deferencing the result of va_next.
189819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
189919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
190019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        FuncInfo->setRegSaveFrameIndex(
190119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               false));
190319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Store the integer parameter registers.
1906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SmallVector<SDValue, 8> MemOps;
1907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        getPointerTy());
1909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Offset = FuncInfo->getVarArgsGPOffset();
1910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
1911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  DAG.getIntPtrConstant(Offset));
1913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     X86::GR64RegisterClass);
1915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
1916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Store =
1917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          DAG.getStore(Val.getValue(1), dl, Val, FIN,
191819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo::getFixedStack(
191919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         FuncInfo->getRegSaveFrameIndex(), Offset),
192019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       false, false, 0);
1921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MemOps.push_back(Store);
1922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Offset += 8;
1923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Now store the XMM (fp + vector) parameter registers.
1927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SmallVector<SDValue, 11> SaveXMMOps;
1928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SaveXMMOps.push_back(Chain);
1929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SaveXMMOps.push_back(ALVal);
1933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SaveXMMOps.push_back(DAG.getIntPtrConstant(
1935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               FuncInfo->getRegSaveFrameIndex()));
1936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SaveXMMOps.push_back(DAG.getIntPtrConstant(
1937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               FuncInfo->getVarArgsFPOffset()));
1938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
194019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
1941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       X86::VR128RegisterClass);
1942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SaveXMMOps.push_back(Val);
1944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     MVT::Other,
1947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     &SaveXMMOps[0], SaveXMMOps.size()));
1948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!MemOps.empty())
1951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            &MemOps[0], MemOps.size());
1953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Some CCs need callee pop.
195719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt)) {
1958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
1959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
1961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is an sret function, the return should pop the hidden pointer.
1962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
1963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FuncInfo->setBytesToPopOnReturn(4);
1964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Is64Bit) {
1967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // RegSaveFrameIndex is X86-64 only.
1968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
1969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CallConv == CallingConv::X86_FastCall ||
1970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CallConv == CallingConv::X86_ThisCall)
1971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // fastcc functions can't have varargs.
1972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
1973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
197519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FuncInfo->setArgumentStackSize(StackSize);
197619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Chain;
1978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
1981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    SDValue StackPtr, SDValue Arg,
1983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    DebugLoc dl, SelectionDAG &DAG,
1984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const CCValAssign &VA,
1985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    ISD::ArgFlagsTy Flags) const {
198619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned LocMemOffset = VA.getLocMemOffset();
1987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
198919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Flags.isByVal())
1990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
199119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getStore(Chain, dl, Arg, PtrOff,
199319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      MachinePointerInfo::getStack(LocMemOffset),
1994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      false, false, 0);
1995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
1998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// optimization is performed and it is required.
1999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
2000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
2001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SDValue &OutRetAddr, SDValue Chain,
2002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           bool IsTailCall, bool Is64Bit,
2003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           int FPDiff, DebugLoc dl) const {
2004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Adjust the Return address stack slot.
2005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = getPointerTy();
2006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OutRetAddr = getReturnAddressFrameIndex(DAG);
2007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load the "old" Return address.
200919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
201019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           false, false, 0);
2011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue(OutRetAddr.getNode(), 1);
2012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
201419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
2015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// optimization is performed and it is required (FPDiff!=0).
2016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue
2017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanEmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
2018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         SDValue Chain, SDValue RetAddrFrIdx,
2019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         bool Is64Bit, int FPDiff, DebugLoc dl) {
2020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store the return address to the appropriate stack slot.
2021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!FPDiff) return Chain;
2022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Calculate the new stack slot for the return address.
2023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int SlotSize = Is64Bit ? 8 : 4;
2024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NewReturnAddrFI =
2025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
2026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
2027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
2028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
202919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo::getFixedStack(NewReturnAddrFI),
2030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       false, false, 0);
2031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Chain;
2032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
2035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
2036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             CallingConv::ID CallConv, bool isVarArg,
2037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             bool &isTailCall,
2038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const SmallVectorImpl<ISD::OutputArg> &Outs,
2039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const SmallVectorImpl<SDValue> &OutVals,
2040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const SmallVectorImpl<ISD::InputArg> &Ins,
2041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DebugLoc dl, SelectionDAG &DAG,
2042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             SmallVectorImpl<SDValue> &InVals) const {
2043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
2044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Is64Bit        = Subtarget->is64Bit();
204519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool IsWin64        = Subtarget->isTargetWin64();
2046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool IsStructRet    = CallIsStructReturn(Outs);
2047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool IsSibcall      = false;
2048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isTailCall) {
2050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check if it's really possible to do a tail call.
2051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
2052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
2053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   Outs, OutVals, Ins, DAG);
2054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Sibcalls are automatically detected tailcalls which do not require
2056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // ABI changes.
2057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!GuaranteedTailCallOpt && isTailCall)
2058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      IsSibcall = true;
2059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isTailCall)
2061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++NumTailCalls;
2062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Var args not supported with calling convention fastcc or ghc");
2066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Analyze operands of the call, assigning locations to each operand.
2068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> ArgLocs;
206919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
2070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 ArgLocs, *DAG.getContext());
207119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
207219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Allocate shadow area for Win64
207319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (IsWin64) {
207419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCInfo.AllocateStack(32, 8);
207519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
207619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
207719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get a count of how many bytes are to be pushed on the stack.
2080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBytes = CCInfo.getNextStackOffset();
2081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (IsSibcall)
2082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This is a sibcall. The memory operands are available in caller's
2083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // own caller's stack.
2084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumBytes = 0;
2085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
2086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
2087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int FPDiff = 0;
2089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isTailCall && !IsSibcall) {
2090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Lower arguments at fp - stackoffset + fpdiff.
2091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumBytesCallerPushed =
2092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FPDiff = NumBytesCallerPushed - NumBytes;
2094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Set the delta of movement of the returnaddr stackslot.
2096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // But only set if delta is greater than previous delta.
2097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!IsSibcall)
2102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
2103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RetAddrFrIdx;
210519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Load return address for tail calls.
2106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isTailCall && FPDiff)
2107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    Is64Bit, FPDiff, dl);
2109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 8> MemOpChains;
2112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue StackPtr;
2113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Walk the register/memloc assignments, inserting copies/loads.  In the case
2115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of tail call optimization arguments are handle later.
2116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = ArgLocs[i];
2118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT RegVT = VA.getLocVT();
2119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Arg = OutVals[i];
2120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD::ArgFlagsTy Flags = Outs[i].Flags;
2121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool isByVal = Flags.isByVal();
2122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Promote the value if needed.
2124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (VA.getLocInfo()) {
2125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("Unknown loc info!");
2126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::Full: break;
2127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::SExt:
2128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
2129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::ZExt:
2131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
2132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::AExt:
2134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
2135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Special case: passing MMX values in XMM registers.
213619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
2137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
2139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else
2140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::BCvt:
214319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
2144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::Indirect: {
2146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Store the argument.
2147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
2148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
2149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
215019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           MachinePointerInfo::getFixedStack(FI),
2151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           false, false, 0);
2152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Arg = SpillSlot;
2153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VA.isRegLoc()) {
2158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
215919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isVarArg && IsWin64) {
216019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Win64 ABI requires argument XMM reg to be copied to the corresponding
216119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // shadow reg if callee is a varargs function.
216219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned ShadowReg = 0;
216319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        switch (VA.getLocReg()) {
216419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case X86::XMM0: ShadowReg = X86::RCX; break;
216519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case X86::XMM1: ShadowReg = X86::RDX; break;
216619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case X86::XMM2: ShadowReg = X86::R8; break;
216719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case X86::XMM3: ShadowReg = X86::R9; break;
216819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
216919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (ShadowReg)
217019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
217119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
2172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (!IsSibcall && (!isTailCall || isByVal)) {
2173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(VA.isMemLoc());
2174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (StackPtr.getNode() == 0)
2175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             dl, DAG, VA, Flags));
2178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!MemOpChains.empty())
2182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        &MemOpChains[0], MemOpChains.size());
2184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Build a sequence of copy-to-reg nodes chained together with token chain
2186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // and flag operands which copy the outgoing args into registers.
2187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue InFlag;
2188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Tail call byval lowering might overwrite argument registers so in case of
2189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // tail call optimization the copies to registers are lowered later.
2190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTailCall)
2191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               RegsToPass[i].second, InFlag);
2194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InFlag = Chain.getValue(1);
2195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleGOT()) {
2198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // ELF / PIC requires GOT in the EBX register before function calls via PLT
2199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // GOT pointer.
2200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isTailCall) {
2201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
2202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getNode(X86ISD::GlobalBaseReg,
2203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           DebugLoc(), getPointerTy()),
2204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               InFlag);
2205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InFlag = Chain.getValue(1);
2206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
2207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If we are tail calling and generating PIC/GOT style code load the
2208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // address of the callee into ECX. The value in ecx is used as target of
2209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the tail jump. This is done to circumvent the ebx/callee-saved problem
2210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // for tail calls on PIC/GOT architectures. Normally we would just put the
2211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // address of GOT into ebx and then call target@PLT. But for tail calls
2212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // ebx would be restored (since ebx is callee saved) before jumping to the
2213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // target@PLT.
2214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Note: The actual moving to ECX is done further down.
2216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (G && !G->getGlobal()->hasHiddenVisibility() &&
2218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          !G->getGlobal()->hasProtectedVisibility())
2219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Callee = LowerGlobalAddress(Callee, DAG);
2220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (isa<ExternalSymbolSDNode>(Callee))
2221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Callee = LowerExternalSymbol(Callee, DAG);
2222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
222519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Is64Bit && isVarArg && !IsWin64) {
2226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // From AMD64 ABI document:
2227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // For calls that may call functions that use varargs or stdargs
2228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // (prototype-less calls or calls to functions containing ellipsis (...) in
2229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the declaration) %al is used as hidden argument to specify the number
2230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // of SSE registers used. The contents of %al do not need to match exactly
2231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the number of registers, but must be an ubound on the number of SSE
2232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // registers used and is in the range 0 - 8 inclusive.
2233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Count the number of XMM registers allocated.
2235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    static const unsigned XMMArgRegs[] = {
2236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    };
2239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
224019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((Subtarget->hasXMM() || !NumXMMRegs)
2241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           && "SSE registers cannot be used when SSE is disabled");
2242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
2244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
2245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = Chain.getValue(1);
2246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For tail calls lower the arguments to the 'real' stack slot.
2250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isTailCall) {
2251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Force all the incoming stack arguments to be loaded from the stack
2252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // before any new outgoing arguments are stored to the stack, because the
2253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // outgoing stack slots may alias the incoming argument stack slots, and
2254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the alias isn't otherwise explicit. This is slightly more conservative
2255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // than necessary, because it means that each store effectively depends
2256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // on every argument instead of just those arguments it would clobber.
2257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<SDValue, 8> MemOpChains2;
2260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue FIN;
2261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int FI = 0;
226219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Do not flag preceding copytoreg stuff together with the following stuff.
2263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = SDValue();
2264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (GuaranteedTailCallOpt) {
2265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CCValAssign &VA = ArgLocs[i];
2267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (VA.isRegLoc())
2268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
2269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        assert(VA.isMemLoc());
2270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Arg = OutVals[i];
2271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ISD::ArgFlagsTy Flags = Outs[i].Flags;
2272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Create frame index.
2273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        int32_t Offset = VA.getLocMemOffset()+FPDiff;
2274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
2275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
2276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        FIN = DAG.getFrameIndex(FI, getPointerTy());
2277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Flags.isByVal()) {
2279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Copy relative to framepointer.
2280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
2281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (StackPtr.getNode() == 0)
2282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
2283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          getPointerTy());
2284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
2285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                           ArgChain,
2288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                           Flags, DAG, dl));
2289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else {
2290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Store relative to framepointer.
2291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          MemOpChains2.push_back(
2292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            DAG.getStore(ArgChain, dl, Arg, FIN,
229319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo::getFixedStack(FI),
2294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         false, false, 0));
2295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!MemOpChains2.empty())
2300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          &MemOpChains2[0], MemOpChains2.size());
2302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Copy arguments to their registers.
2304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               RegsToPass[i].second, InFlag);
2307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InFlag = Chain.getValue(1);
2308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag =SDValue();
2310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Store the return address to the appropriate stack slot.
2312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
2313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     FPDiff, dl);
2314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // In the 64-bit large code model, we have to make all calls
2319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // through a register, since the call instruction's 32-bit
2320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // pc-relative offset may not be large enough to hold the whole
2321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // address.
2322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the callee is a GlobalAddress node (quite common, every direct call
2324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // it.
2326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We should use extra load for direct calls to dllimported functions in
2328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // non-JIT mode.
2329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const GlobalValue *GV = G->getGlobal();
2330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!GV->hasDLLImportLinkage()) {
2331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned char OpFlags = 0;
233219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool ExtraLoad = false;
233319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned WrapperKind = ISD::DELETED_NODE;
233419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
233519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
233619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // external symbols most go through the PLT in PIC mode.  If the symbol
233719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // has hidden or protected visibility, or if it is static or local, then
233819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // we don't need to use the PLT - we can directly call it.
233919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Subtarget->isTargetELF() &&
234019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
234119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
234219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpFlags = X86II::MO_PLT;
234319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else if (Subtarget->isPICStyleStubAny() &&
234419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 (GV->isDeclaration() || GV->isWeakForLinker()) &&
234519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 (!Subtarget->getTargetTriple().isMacOSX() ||
234619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
2347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // PC-relative references to external symbols should go through $stub,
2348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // unless we're building with the leopard linker or later, which
2349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // automatically synthesizes these stubs.
2350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        OpFlags = X86II::MO_DARWIN_STUB;
235119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else if (Subtarget->isPICStyleRIPRel() &&
235219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 isa<Function>(GV) &&
235319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
235419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // If the function is marked as non-lazy, generate an indirect call
235519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // which loads from the GOT directly. This avoids runtime overhead
235619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // at the cost of eager binding (and one extra byte of encoding).
235719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OpFlags = X86II::MO_GOTPCREL;
235819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        WrapperKind = X86ISD::WrapperRIP;
235919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ExtraLoad = true;
2360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
2363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          G->getOffset(), OpFlags);
236419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
236519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Add a wrapper if needed.
236619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (WrapperKind != ISD::DELETED_NODE)
236719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
236819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Add extra indirection if needed.
236919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (ExtraLoad)
237019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
237119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             MachinePointerInfo::getGOT(),
237219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             false, false, 0);
2373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned char OpFlags = 0;
2376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
237719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
237819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // external symbols should go through the PLT.
237919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->isTargetELF() &&
238019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        getTargetMachine().getRelocationModel() == Reloc::PIC_) {
238119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OpFlags = X86II::MO_PLT;
238219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else if (Subtarget->isPICStyleStubAny() &&
238319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman               (!Subtarget->getTargetTriple().isMacOSX() ||
238419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
2385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // PC-relative references to external symbols should go through $stub,
2386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // unless we're building with the leopard linker or later, which
2387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // automatically synthesizes these stubs.
2388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpFlags = X86II::MO_DARWIN_STUB;
2389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         OpFlags);
2393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Returns a chain & a flag for retval copy to use.
239619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 8> Ops;
2398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!IsSibcall && isTailCall) {
2400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getIntPtrConstant(0, true), InFlag);
2402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = Chain.getValue(1);
2403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Ops.push_back(Chain);
2406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Ops.push_back(Callee);
2407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isTailCall)
2409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
2410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add argument registers to the end of the list so that they are known live
2412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // into the call.
2413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  RegsToPass[i].second.getValueType()));
2416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add an implicit use GOT pointer in EBX.
2418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTailCall && Subtarget->isPICStyleGOT())
2419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
242119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
242219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Is64Bit && isVarArg && !IsWin64)
2423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
2424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InFlag.getNode())
2426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(InFlag);
2427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isTailCall) {
2429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We used to do:
2430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //// If this is the first return lowered for this function, add the regs
2431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //// to the liveout set for the function.
2432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This isn't right, although it's probably harmless on x86; liveouts
2433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // should be computed from returns not tail calls.  Consider a void
2434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // function making a tail call to a function returning int.
2435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::TC_RETURN, dl,
2436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       NodeTys, &Ops[0], Ops.size());
2437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
2440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InFlag = Chain.getValue(1);
2441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the CALLSEQ_END node.
2443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBytesForCalleeToPush;
244419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, GuaranteedTailCallOpt))
2445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumBytesForCalleeToPush = NumBytes;    // Callee pops everything
2446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
2447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is a call to a struct-return function, the callee
2448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // pops the hidden struct pointer, so we have to push it back.
2449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This is common for Darwin/X86, Linux & Mingw32 targets.
2450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumBytesForCalleeToPush = 4;
2451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
2452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumBytesForCalleeToPush = 0;  // Callee pops nothing.
2453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Returns a flag for retval copy to use.
2455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!IsSibcall) {
2456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCALLSEQ_END(Chain,
2457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getIntPtrConstant(NumBytes, true),
2458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     true),
2460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               InFlag);
2461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = Chain.getValue(1);
2462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Handle result values, copying them out of physregs into vregs that we
2465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // return.
2466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Ins, dl, DAG, InVals);
2468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                Fast Calling Convention (tail call) implementation
2473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  Like std call, callee cleans arguments, convention except that ECX is
2476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  reserved for storing the tail called function address. Only 2 registers are
2477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  free for argument passing (inreg). Tail call optimization is performed
2478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  provided:
2479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                * tailcallopt is enabled
2480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                * caller/callee are fastcc
2481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  On X86_64 architecture with GOT-style position independent code only local
2482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  (within module) calls are supported at the moment.
2483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  To keep the stack aligned according to platform abi the function
2484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  GetAlignedArgumentStackSize ensures that argument delta is always multiples
2485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
2486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  If a tail called function callee has more arguments than the caller the
2487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  caller needs to make sure that there is room to move the RETADDR to. This is
2488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  achieved by reserving an area the size of the argument delta right after the
2489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  original REtADDR, but before the saved framepointer or the spilled registers
2490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  stack layout:
2492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    arg1
2493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    arg2
2494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    RETADDR
2495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    [ new RETADDR
2496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//      move area ]
2497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    (possible EBP)
2498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    ESI
2499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    EDI
2500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//    local1 ..
2501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// for a 16 byte align requirement.
2504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned
2505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               SelectionDAG& DAG) const {
2507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
2508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetMachine &TM = MF.getTarget();
250919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetFrameLowering &TFI = *TM.getFrameLowering();
2510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned StackAlignment = TFI.getStackAlignment();
2511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t AlignMask = StackAlignment - 1;
2512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int64_t Offset = StackSize;
2513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t SlotSize = TD->getPointerSize();
2514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Number smaller than 12 so just add the difference.
2516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
2518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Mask out lower bits, add stackalignment once plus the 12 bytes.
2519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset = ((~AlignMask) & Offset) + StackAlignment +
2520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (StackAlignment-SlotSize);
2521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Offset;
2523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MatchingStackOffset - Return true if the given stack call argument is
2526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// already available in the same position (relatively) of the caller's
2527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// incoming argument stack.
2528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic
2529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         const X86InstrInfo *TII) {
2532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int FI = INT_MAX;
2534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Arg.getOpcode() == ISD::CopyFromReg) {
2535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
253619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!TargetRegisterInfo::isVirtualRegister(VR))
2537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
2538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineInstr *Def = MRI->getVRegDef(VR);
2539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Def)
2540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
2541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Flags.isByVal()) {
2542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!TII->isLoadFromStackSlot(Def, FI))
2543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
2544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
2545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Opcode = Def->getOpcode();
2546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Def->getOperand(1).isFI()) {
2548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        FI = Def->getOperand(1).getIndex();
2549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Bytes = Flags.getByValSize();
2550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else
2551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
2552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Flags.isByVal())
2555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // ByVal argument is passed in as a pointer but it's now being
2556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // dereferenced. e.g.
2557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // define @foo(%struct.X* %A) {
2558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      //   tail call @bar(%struct.X* byval %A)
2559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // }
2560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
2561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ptr = Ld->getBasePtr();
2562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!FINode)
2564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
2565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FI = FINode->getIndex();
256619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
256719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
256819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FI = FINode->getIndex();
256919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Bytes = Flags.getByValSize();
2570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else
2571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(FI != INT_MAX);
2574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!MFI->isFixedObjectIndex(FI))
2575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// for tail call optimization. Targets which want to do tail call
2581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// optimization should implement this function.
2582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
2583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     CallingConv::ID CalleeCC,
2585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     bool isVarArg,
2586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     bool isCalleeStructRet,
2587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     bool isCallerStructRet,
2588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
2589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const SmallVectorImpl<SDValue> &OutVals,
2590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const SmallVectorImpl<ISD::InputArg> &Ins,
2591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     SelectionDAG& DAG) const {
2592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!IsTailCallConvention(CalleeCC) &&
2593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CalleeCC != CallingConv::C)
2594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If -tailcallopt is specified, make fastcc functions tail-callable.
2597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineFunction &MF = DAG.getMachineFunction();
2598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function *CallerF = DAG.getMachineFunction().getFunction();
2599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CallingConv::ID CallerCC = CallerF->getCallingConv();
2600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool CCMatch = CallerCC == CalleeCC;
2601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GuaranteedTailCallOpt) {
2603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (IsTailCallConvention(CalleeCC) && CCMatch)
2604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
2605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look for obvious safe cases to perform tail call optimization that do not
2609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // require ABI changes. This is what gcc calls sibcall.
2610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // emit a special epilogue.
2613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RegInfo->needsStackRealignment(MF))
2614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Also avoid sibcall optimization if either caller or callee uses struct
2617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // return semantics.
2618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isCalleeStructRet || isCallerStructRet)
2619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
262119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // An stdcall caller is expected to clean up its arguments; the callee
262219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // isn't going to do that.
262319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
262419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
262519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
262619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Do not sibcall optimize vararg calls unless all arguments are passed via
262719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // registers.
262819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVarArg && !Outs.empty()) {
262919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
263019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Optimizing for varargs on Win64 is unlikely to be safe without
263119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // additional testing.
263219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->isTargetWin64())
263319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
263419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
263519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<CCValAssign, 16> ArgLocs;
263619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
263719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		   getTargetMachine(), ArgLocs, *DAG.getContext());
263819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
263919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCInfo.AnalyzeCallOperands(Outs, CC_X86);
264019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
264119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!ArgLocs[i].isRegLoc())
264219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return false;
264319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
264419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Therefore if it's not used by the call it is not safe to optimize this into
2647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // a sibcall.
2648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Unused = false;
2649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Ins[i].Used) {
2651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Unused = true;
2652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
2653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Unused) {
2656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<CCValAssign, 16> RVLocs;
265719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
265819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		   getTargetMachine(), RVLocs, *DAG.getContext());
2659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CCValAssign &VA = RVLocs[i];
2662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
2664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the calling conventions do not match, then we'd better make sure the
2668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // results are returned in the same way as what the caller expects.
2669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!CCMatch) {
2670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<CCValAssign, 16> RVLocs1;
267119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
267219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		    getTargetMachine(), RVLocs1, *DAG.getContext());
2673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<CCValAssign, 16> RVLocs2;
267619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
267719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		    getTargetMachine(), RVLocs2, *DAG.getContext());
2678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (RVLocs1.size() != RVLocs2.size())
2681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
2682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
2685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
2687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RVLocs1[i].isRegLoc()) {
2688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return false;
2690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
2691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return false;
2693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the callee takes no arguments then go on to check the results of the
2698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // call.
2699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Outs.empty()) {
2700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check if stack adjustment is needed. For now, do not do this if any
2701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // argument is passed on the stack.
2702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<CCValAssign, 16> ArgLocs;
270319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
270419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		   getTargetMachine(), ArgLocs, *DAG.getContext());
270519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
270619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Allocate shadow area for Win64
270719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->isTargetWin64()) {
270819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      CCInfo.AllocateStack(32, 8);
270919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
271019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
271119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CCInfo.getNextStackOffset()) {
2713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFunction &MF = DAG.getMachineFunction();
2714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
2716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Check if the arguments are already laid out in the right way as
2718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the caller's fixed stack objects.
2719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFrameInfo *MFI = MF.getFrameInfo();
2720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      const MachineRegisterInfo *MRI = &MF.getRegInfo();
2721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      const X86InstrInfo *TII =
2722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
2723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CCValAssign &VA = ArgLocs[i];
2725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Arg = OutVals[i];
2726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ISD::ArgFlagsTy Flags = Outs[i].Flags;
2727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (VA.getLocInfo() == CCValAssign::Indirect)
2728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return false;
2729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!VA.isRegLoc()) {
2730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   MFI, MRI, TII))
2732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return false;
2733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the tailcall address may be in a register, then make sure it's
2738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // possible to register allocate for it. In 32-bit, the call address can
2739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // only target EAX, EDX, or ECX since the tail call must be scheduled after
2740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // callee-saved registers are restored. These happen to be the same
2741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // registers used to pass 'inreg' arguments so watch out for those.
2742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Subtarget->is64Bit() &&
2743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        !isa<GlobalAddressSDNode>(Callee) &&
2744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        !isa<ExternalSymbolSDNode>(Callee)) {
2745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned NumInRegs = 0;
2746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CCValAssign &VA = ArgLocs[i];
2748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!VA.isRegLoc())
2749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
2750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned Reg = VA.getLocReg();
2751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (Reg) {
2752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        default: break;
2753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case X86::EAX: case X86::EDX: case X86::ECX:
2754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (++NumInRegs == 3)
2755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return false;
2756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
2757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
2758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
2763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
276519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanFastISel *
276619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
276719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return X86::createFastISel(funcInfo);
276819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
276919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                           Other Lowering Hooks
2773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
277519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool MayFoldLoad(SDValue Op) {
277619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
277719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
277819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
277919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool MayFoldIntoStore(SDValue Op) {
278019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
278119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
278219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
278319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isTargetShuffle(unsigned Opcode) {
278419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(Opcode) {
278519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: return false;
278619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFD:
278719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFHW:
278819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFLW:
278919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPD:
279019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PALIGN:
279119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPS:
279219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPS:
279319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPD:
279419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVHLPS:
279519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLPS:
279619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLPD:
279719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSHDUP:
279819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSLDUP:
279919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVDDUP:
280019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSS:
280119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSD:
280219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPS:
280319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPD:
280419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPSY:
280519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPDY:
280619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLWD:
280719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLBW:
280819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLDQ:
280919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLQDQ:
281019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPS:
281119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPD:
281219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKHPSY:
281319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKHPDY:
281419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHWD:
281519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHBW:
281619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHDQ:
281719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHQDQ:
281819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPS:
281919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPSY:
282019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPD:
282119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPDY:
282219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERM2F128:
282319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
282419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
282519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
282619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
282719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
282819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
282919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                               SDValue V1, SelectionDAG &DAG) {
283019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(Opc) {
283119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: llvm_unreachable("Unknown x86 shuffle node");
283219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSHDUP:
283319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSLDUP:
283419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVDDUP:
283519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(Opc, dl, VT, V1);
283619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
283719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
283819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
283919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
284019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
284119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
284219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
284319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(Opc) {
284419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: llvm_unreachable("Unknown x86 shuffle node");
284519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFD:
284619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFHW:
284719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFLW:
284819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPS:
284919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPSY:
285019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPD:
285119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPDY:
285219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
285319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
285419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
285519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
285619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
285719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
285819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
285919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman               SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
286019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(Opc) {
286119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: llvm_unreachable("Unknown x86 shuffle node");
286219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PALIGN:
286319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPD:
286419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPS:
286519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERM2F128:
286619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(Opc, dl, VT, V1, V2,
286719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getConstant(TargetMask, MVT::i8));
286819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
286919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
287019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
287119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
287219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
287319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    SDValue V1, SDValue V2, SelectionDAG &DAG) {
287419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(Opc) {
287519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: llvm_unreachable("Unknown x86 shuffle node");
287619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPS:
287719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPD:
287819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVHLPS:
287919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLPS:
288019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLPD:
288119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSS:
288219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSD:
288319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPS:
288419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPD:
288519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPSY:
288619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPDY:
288719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLWD:
288819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLBW:
288919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLDQ:
289019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLQDQ:
289119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPS:
289219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPD:
289319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKHPSY:
289419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKHPDY:
289519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHWD:
289619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHBW:
289719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHDQ:
289819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHQDQ:
289919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(Opc, dl, VT, V1, V2);
290019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
290119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
290219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
2903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
2905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
2906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int ReturnAddrIndex = FuncInfo->getRAIndex();
2908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ReturnAddrIndex == 0) {
2910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Set up a frame object for the return address.
2911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    uint64_t SlotSize = TD->getPointerSize();
2912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                           false);
2914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setRAIndex(ReturnAddrIndex);
2915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       bool hasSymbolicDisplacement) {
2923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Offset should fit into 32 bit immediate field.
2924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isInt<32>(Offset))
2925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we don't have a symbolic displacement - we don't have any extra
2928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // restrictions.
2929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!hasSymbolicDisplacement)
2930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
2931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Some tweaks might be needed for medium code model.
2933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (M != CodeModel::Small && M != CodeModel::Kernel)
2934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
2935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For small code model we assume that latest object is 16MB before end of 31
2937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // bits boundary. We may also accept pretty large negative constants knowing
2938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // that all objects are in the positive half of address space.
2939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (M == CodeModel::Small && Offset < 16*1024*1024)
2940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
2941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For kernel code model we know that all object resist in the negative half
2943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of 32bits address space. We may not accept negative offsets, since they may
2944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // be just off and we may accept pretty large positive ones.
2945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (M == CodeModel::Kernel && Offset > 0)
2946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
2947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
2949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
295119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isCalleePop - Determines whether the callee is required to pop its
295219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// own arguments. Callee pop is necessary to support tail calls.
295319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86::isCalleePop(CallingConv::ID CallingConv,
295419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      bool is64Bit, bool IsVarArg, bool TailCallOpt) {
295519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (IsVarArg)
295619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
295719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
295819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (CallingConv) {
295919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
296019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
296119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::X86_StdCall:
296219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return !is64Bit;
296319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::X86_FastCall:
296419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return !is64Bit;
296519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::X86_ThisCall:
296619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return !is64Bit;
296719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::Fast:
296819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return TailCallOpt;
296919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::GHC:
297019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return TailCallOpt;
297119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
297219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
297319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specific condition code, returning the condition code and the LHS/RHS of the
2976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// comparison to make.
2977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
2979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isFP) {
2980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // X > -1   -> X == 0, jump !sign.
2983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RHS = DAG.getConstant(0, RHS.getValueType());
2984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return X86::COND_NS;
2985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // X < 0   -> X == 0, jump on sign.
2987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return X86::COND_S;
2988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
2989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // X < 1   -> X <= 0
2990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RHS = DAG.getConstant(0, RHS.getValueType());
2991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return X86::COND_LE;
2992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
2993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (SetCCOpcode) {
2996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("Invalid integer condition!");
2997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETEQ:  return X86::COND_E;
2998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETGT:  return X86::COND_G;
2999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETGE:  return X86::COND_GE;
3000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETLT:  return X86::COND_L;
3001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETLE:  return X86::COND_LE;
3002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETNE:  return X86::COND_NE;
3003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETULT: return X86::COND_B;
3004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUGT: return X86::COND_A;
3005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETULE: return X86::COND_BE;
3006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUGE: return X86::COND_AE;
3007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
3008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First determine if it is required or is profitable to flip the operands.
3011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If LHS is a foldable load, but RHS is not, flip the condition.
301319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ISD::isNON_EXTLoad(LHS.getNode()) &&
301419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !ISD::isNON_EXTLoad(RHS.getNode())) {
3015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(LHS, RHS);
3017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (SetCCOpcode) {
3020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
3021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOLT:
3022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOLE:
3023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGT:
3024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGE:
3025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(LHS, RHS);
3026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
3027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // On a floating point condition, the flags are set as follows:
3030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ZF  PF  CF   op
3031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  0 | 0 | 0 | X > Y
3032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  0 | 0 | 1 | X < Y
3033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  1 | 0 | 0 | X == Y
3034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  1 | 1 | 1 | unordered
3035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (SetCCOpcode) {
3036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Condcode should be pre-legalized away");
3037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUEQ:
3038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETEQ:   return X86::COND_E;
3039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOLT:              // flipped
3040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOGT:
3041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGT:   return X86::COND_A;
3042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOLE:              // flipped
3043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOGE:
3044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGE:   return X86::COND_AE;
3045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGT:              // flipped
3046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULT:
3047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLT:   return X86::COND_B;
3048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGE:              // flipped
3049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULE:
3050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLE:   return X86::COND_BE;
3051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETONE:
3052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETNE:   return X86::COND_NE;
3053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUO:   return X86::COND_P;
3054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETO:    return X86::COND_NP;
3055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOEQ:
3056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUNE:  return X86::COND_INVALID;
3057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// code. Current x86 isa includes the following FP cmov instructions:
3062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
3063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool hasFPCMov(unsigned X86CC) {
3064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (X86CC) {
3065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
3066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_B:
3068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_BE:
3069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_E:
3070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_P:
3071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_A:
3072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_AE:
3073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_NE:
3074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_NP:
3075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
3076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isFPImmLegal - Returns true if the target can instruction select the
3080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified FP immediate natively. If false, the legalizer will
3081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// materialize the FP immediate as a load from a constant pool.
3082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
3086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
3088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specified range (L, H].
3092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isUndefOrInRange(int Val, int Low, int Hi) {
3093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (Val < 0) || (Val >= Low && Val < Hi);
3094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
309619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isUndefOrInRange - Return true if every element in Mask, begining
309719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// from position Pos and ending in Pos+Size, falls within the specified
309819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// range (L, L+Pos]. or is undef.
309919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isUndefOrInRange(const SmallVectorImpl<int> &Mask,
310019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             int Pos, int Size, int Low, int Hi) {
310119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = Pos, e = Pos+Size; i != e; ++i)
310219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], Low, Hi))
310319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
310419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
310519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
310619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified value.
3109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isUndefOrEqual(int Val, int CmpVal) {
3110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Val < 0 || Val == CmpVal)
3111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
3112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
3113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
311519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isSequentialOrUndefInRange - Return true if every element in Mask, begining
311619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// from position Pos and ending in Pos+Size, falls within the specified
311719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// sequential range (L, L+Pos]. or is undef.
311819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isSequentialOrUndefInRange(const SmallVectorImpl<int> &Mask,
311919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       int Pos, int Size, int Low) {
312019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = Pos, e = Pos+Size; i != e; ++i, ++Low)
312119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(Mask[i], Low))
312219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
312319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
312419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
312519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is suitable for input to PSHUFD or PSHUFW.  That is, it doesn't reference
3128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the second operand.
3129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
313019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT == MVT::v4f32 || VT == MVT::v4i32 )
3131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
3132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::v2f64 || VT == MVT::v2i64)
3133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (Mask[0] < 2 && Mask[1] < 2);
3134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
3135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
3138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isPSHUFDMask(M, N->getValueType(0));
3141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is suitable for input to PSHUFHW.
3145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
3146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::v8i16)
3147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Lower quadword copied in order or undef.
3150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i != 4; ++i)
3151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Mask[i] >= 0 && Mask[i] != i)
3152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Upper quadword shuffled.
3155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 4; i != 8; ++i)
3156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
3157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
3163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isPSHUFHWMask(M, N->getValueType(0));
3166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is suitable for input to PSHUFLW.
3170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
3171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::v8i16)
3172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Upper quadword copied in order.
3175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 4; i != 8; ++i)
3176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Mask[i] >= 0 && Mask[i] != i)
3177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Lower quadword shuffled.
3180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i != 4; ++i)
3181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Mask[i] >= 4)
3182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
3188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isPSHUFLWMask(M, N->getValueType(0));
3191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is suitable for input to PALIGNR.
3195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
319619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          bool hasSSSE3OrAVX) {
3197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int i, e = VT.getVectorNumElements();
319819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128 && VT.getSizeInBits() != 64)
319919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
320019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Do not handle v2i64 / v2f64 shuffles with palignr.
320219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (e < 4 || !hasSSSE3OrAVX)
3203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
320419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (i = 0; i != e; ++i)
3206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Mask[i] >= 0)
3207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
320819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // All undef, not a palignr.
3210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (i == e)
3211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
321319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Make sure we're shifting in the right direction.
321419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Mask[i] <= i)
321519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
3216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int s = Mask[i] - i;
321819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check the rest of the elements to see if they are consecutive.
3220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (++i; i != e; ++i) {
3221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int m = Mask[i];
322219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (m >= 0 && m != s+i)
3223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
322819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVSHUFPSYMask - Return true if the specified VECTOR_SHUFFLE operand
322919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to 256-bit
323019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// VSHUFPSY.
323119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVSHUFPSYMask(const SmallVectorImpl<int> &Mask, EVT VT,
323219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          const X86Subtarget *Subtarget) {
3233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElems = VT.getVectorNumElements();
323419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
323519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
3236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
323819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElems != 8)
323919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
324019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
324119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // VSHUFPSY divides the resulting vector into 4 chunks.
324219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The sources are also splitted into 4 chunks, and each destination
324319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // chunk must come from a different source chunk.
324419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
324519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  SRC1 =>   X7    X6    X5    X4    X3    X2    X1    X0
324619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  SRC2 =>   Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y9
324719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
324819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  DST  =>  Y7..Y4,   Y7..Y4,   X7..X4,   X7..X4,
324919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //           Y3..Y0,   Y3..Y0,   X3..X0,   X3..X0
325019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
325119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int QuarterSize = NumElems/4;
325219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = QuarterSize*2;
325319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i < QuarterSize; ++i)
325419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], 0, HalfSize))
325519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
325619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = QuarterSize; i < QuarterSize*2; ++i)
325719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
325819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
325919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
326019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The mask of the second half must be the same as the first but with
326119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the appropriate offsets. This works in the same way as VPERMILPS
326219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // works with masks.
326319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = QuarterSize*2; i < QuarterSize*3; ++i) {
326419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
326519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
326619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FstHalfIdx = i-HalfSize;
326719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Mask[FstHalfIdx] < 0)
326819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
326919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
327019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
327119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
327219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = QuarterSize*3; i < NumElems; ++i) {
327319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
327419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
327519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FstHalfIdx = i-HalfSize;
327619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Mask[FstHalfIdx] < 0)
327719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
327819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(Mask[i], Mask[FstHalfIdx]+HalfSize))
327919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
328019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
328119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
328219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
328319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
328419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
328519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
328619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getShuffleVSHUFPSYImmediate - Return the appropriate immediate to shuffle
328719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// the specified VECTOR_MASK mask with VSHUFPSY instruction.
328819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic unsigned getShuffleVSHUFPSYImmediate(SDNode *N) {
328919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
329019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
329119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
329219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
329319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(NumElems == 8 && VT.getSizeInBits() == 256 &&
329419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Only supports v8i32 and v8f32 types");
329519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
329619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = NumElems/2;
329719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Mask = 0;
329819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i != NumElems ; ++i) {
329919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SVOp->getMaskElt(i) < 0)
330019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
330119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // The mask of the first half must be equal to the second one.
330219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Shamt = (i%HalfSize)*2;
330319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Elt = SVOp->getMaskElt(i) % HalfSize;
330419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mask |= Elt << Shamt;
330519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
330619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
330719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Mask;
330819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
330919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
331019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVSHUFPDYMask - Return true if the specified VECTOR_SHUFFLE operand
331119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to 256-bit
331219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// VSHUFPDY. This shuffle doesn't have the same restriction as the PS
331319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// version and the mask of the second half isn't binded with the first
331419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// one.
331519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVSHUFPDYMask(const SmallVectorImpl<int> &Mask, EVT VT,
331619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           const X86Subtarget *Subtarget) {
331719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
331819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
331919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
332019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
332119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
332219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElems != 4)
332319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
332419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
332519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // VSHUFPSY divides the resulting vector into 4 chunks.
332619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The sources are also splitted into 4 chunks, and each destination
332719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // chunk must come from a different source chunk.
332819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
332919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  SRC1 =>      X3       X2       X1       X0
333019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  SRC2 =>      Y3       Y2       Y1       Y0
333119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
333219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  DST  =>  Y2..Y3,  X2..X3,  Y1..Y0,  X1..X0
333319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
333419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int QuarterSize = NumElems/4;
333519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = QuarterSize*2;
333619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i < QuarterSize; ++i)
333719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], 0, HalfSize))
333819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
333919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = QuarterSize; i < QuarterSize*2; ++i)
334019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], NumElems, NumElems+HalfSize))
334119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
334219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = QuarterSize*2; i < QuarterSize*3; ++i)
334319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], HalfSize, NumElems))
334419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
334519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = QuarterSize*3; i < NumElems; ++i)
334619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], NumElems+HalfSize, NumElems*2))
334719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
334819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
334919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
335019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
335119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
335219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getShuffleVSHUFPDYImmediate - Return the appropriate immediate to shuffle
335319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// the specified VECTOR_MASK mask with VSHUFPDY instruction.
335419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic unsigned getShuffleVSHUFPDYImmediate(SDNode *N) {
335519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
335619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
335719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
335819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
335919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(NumElems == 4 && VT.getSizeInBits() == 256 &&
336019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Only supports v4i64 and v4f64 types");
336119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
336219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = NumElems/2;
336319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Mask = 0;
336419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i != NumElems ; ++i) {
336519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SVOp->getMaskElt(i) < 0)
336619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
336719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int Elt = SVOp->getMaskElt(i) % HalfSize;
336819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mask |= Elt << i;
336919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
337019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
337119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Mask;
337219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
337319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
337419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
337519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to 128-bit
337619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// SHUFPS and SHUFPD.
337719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
337819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
337919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
338019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128)
338119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
338219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
338319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElems != 2 && NumElems != 4)
338419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
338519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
338619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int Half = NumElems / 2;
338719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i < Half; ++i)
338819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrInRange(Mask[i], 0, NumElems))
338919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
339019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = Half; i < NumElems; ++i)
3391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
3392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
3398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isSHUFPMask(M, N->getValueType(0));
3401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
3404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
3405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// half elements to come from vector 1 (which would equal the dest.) and
3406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the upper half to come from vector 2.
3407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
3408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElems = VT.getVectorNumElements();
3409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems != 2 && NumElems != 4)
3411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Half = NumElems / 2;
3414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i < Half; ++i)
3415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
3416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = Half; i < NumElems; ++i)
3418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrInRange(Mask[i], 0, NumElems))
3419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
3424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return isCommutedSHUFPMask(M, N->getValueType(0));
3427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
3431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
343219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
343319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
343419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
343519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128)
343619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
343719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
343819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElems != 4)
3439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
3442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return isUndefOrEqual(N->getMaskElt(0), 6) &&
3443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         isUndefOrEqual(N->getMaskElt(1), 7) &&
3444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         isUndefOrEqual(N->getMaskElt(2), 2) &&
3445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         isUndefOrEqual(N->getMaskElt(3), 3);
3446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// <2, 3, 2, 3>
3451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
345219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
345319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
345419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
345519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128)
345619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
345719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems != 4)
3459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
346019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return isUndefOrEqual(N->getMaskElt(0), 2) &&
346219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         isUndefOrEqual(N->getMaskElt(1), 3) &&
346319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         isUndefOrEqual(N->getMaskElt(2), 2) &&
346419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         isUndefOrEqual(N->getMaskElt(3), 3);
3465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
3469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = N->getValueType(0).getVectorNumElements();
3471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems != 2 && NumElems != 4)
3473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < NumElems/2; ++i)
3476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
3477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = NumElems/2; i < NumElems; ++i)
3480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), i))
3481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
3489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = N->getValueType(0).getVectorNumElements();
3490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
349119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((NumElems != 2 && NumElems != 4)
349219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      || N->getValueType(0).getSizeInBits() > 128)
3493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < NumElems/2; ++i)
3496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), i))
3497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < NumElems/2; ++i)
3500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
3501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to UNPCKL.
3508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
3509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         bool V2IsSplat = false) {
3510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElts = VT.getVectorNumElements();
351119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
351219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((VT.is128BitVector() || VT.is256BitVector()) &&
351319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported vector type for unpckh");
351419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
351519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
3516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
351819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
351919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // independently on 128-bit lanes.
352019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLanes = VT.getSizeInBits()/128;
352119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLaneElts = NumElts/NumLanes;
352219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
352319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Start = 0;
352419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned End = NumLaneElts;
352519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned s = 0; s < NumLanes; ++s) {
352619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = Start, j = s * NumLaneElts;
352719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         i != End;
352819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         i += 2, ++j) {
352919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int BitI  = Mask[i];
353019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int BitI1 = Mask[i+1];
353119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!isUndefOrEqual(BitI, j))
3532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
353319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (V2IsSplat) {
353419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!isUndefOrEqual(BitI1, NumElts))
353519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return false;
353619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else {
353719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!isUndefOrEqual(BitI1, j + NumElts))
353819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return false;
353919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
3540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
354119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Process the next 128 bits.
354219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Start += NumLaneElts;
354319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    End += NumLaneElts;
3544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
354519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
3553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to UNPCKH.
3557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
3558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         bool V2IsSplat = false) {
3559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElts = VT.getVectorNumElements();
356019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
356119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((VT.is128BitVector() || VT.is256BitVector()) &&
356219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported vector type for unpckh");
356319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
356419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8)
3565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
356719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
356819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // independently on 128-bit lanes.
356919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLanes = VT.getSizeInBits()/128;
357019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLaneElts = NumElts/NumLanes;
357119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
357219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Start = 0;
357319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned End = NumLaneElts;
357419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned l = 0; l != NumLanes; ++l) {
357519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = Start, j = (l*NumLaneElts)+NumLaneElts/2;
357619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             i != End; i += 2, ++j) {
357719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int BitI  = Mask[i];
357819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int BitI1 = Mask[i+1];
357919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!isUndefOrEqual(BitI, j))
3580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
358119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (V2IsSplat) {
358219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (isUndefOrEqual(BitI1, NumElts))
358319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return false;
358419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else {
358519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!isUndefOrEqual(BitI1, j+NumElts))
358619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return false;
358719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
3588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
358919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Process the next 128 bits.
359019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Start += NumLaneElts;
359119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    End += NumLaneElts;
3592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
3600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// <0, 0, 1, 1>
3605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
3606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElems = VT.getVectorNumElements();
3607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
361019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
361119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: Need a better way to get rid of this, there's no latency difference
361219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // between UNPCKLPD and MOVDDUP, the later should always be checked first and
361319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the former later. We should also remove the "_undef" special mask.
361419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElems == 4 && VT.getSizeInBits() == 256)
361519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
361619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
361719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
361819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // independently on 128-bit lanes.
361919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLanes = VT.getSizeInBits() / 128;
362019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLaneElts = NumElems / NumLanes;
362119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
362219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned s = 0; s < NumLanes; ++s) {
362319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = s * NumLaneElts, j = s * NumLaneElts;
362419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         i != NumLaneElts * (s + 1);
362519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         i += 2, ++j) {
362619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int BitI  = Mask[i];
362719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int BitI1 = Mask[i+1];
362819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
362919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!isUndefOrEqual(BitI, j))
363019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return false;
363119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!isUndefOrEqual(BitI1, j))
363219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return false;
363319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
3634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
363519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// <2, 2, 3, 3>
3648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
3649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElems = VT.getVectorNumElements();
3650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int BitI  = Mask[i];
3655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int BitI1 = Mask[i+1];
3656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(BitI, j))
3657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(BitI1, j))
3659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
3661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to MOVSS,
3672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MOVSD, and MOVD, i.e. setting the lowest element.
3673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
3674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.getVectorElementType().getSizeInBits() < 32)
3675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElts = VT.getVectorNumElements();
3678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isUndefOrEqual(Mask[0], NumElts))
3680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 1; i < NumElts; ++i)
3683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(Mask[i], i))
3684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ::isMOVLMask(M, N->getValueType(0));
3693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
369519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVPERM2F128Mask - Match 256-bit shuffles where the elements are considered
369619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// as permutations between 128-bit chunks or halves. As an example: this
369719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// shuffle bellow:
369819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///   vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
369919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// The first half comes from the second half of V1 and the second half from the
370019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// the second half of V2.
370119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVPERM2F128Mask(const SmallVectorImpl<int> &Mask, EVT VT,
370219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             const X86Subtarget *Subtarget) {
370319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256)
370419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
370519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
370619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The shuffle result is divided into half A and half B. In total the two
370719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // sources have 4 halves, namely: C, D, E, F. The final values of A and
370819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // B must come from C, D, E or F.
370919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = VT.getVectorNumElements()/2;
371019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool MatchA = false, MatchB = false;
371119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
371219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check if A comes from one of C, D, E, F.
371319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int Half = 0; Half < 4; ++Half) {
371419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
371519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MatchA = true;
371619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
371719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
371819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
371919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
372019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check if B comes from one of C, D, E, F.
372119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int Half = 0; Half < 4; ++Half) {
372219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
372319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MatchB = true;
372419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
372519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
372619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
372719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
372819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return MatchA && MatchB;
372919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
373019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
373119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getShuffleVPERM2F128Immediate - Return the appropriate immediate to shuffle
373219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// the specified VECTOR_MASK mask with VPERM2F128 instructions.
373319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic unsigned getShuffleVPERM2F128Immediate(SDNode *N) {
373419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
373519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
373619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
373719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = VT.getVectorNumElements()/2;
373819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
373919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int FstHalf = 0, SndHalf = 0;
374019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i < HalfSize; ++i) {
374119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SVOp->getMaskElt(i) > 0) {
374219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FstHalf = SVOp->getMaskElt(i)/HalfSize;
374319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
374419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
374519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
374619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = HalfSize; i < HalfSize*2; ++i) {
374719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SVOp->getMaskElt(i) > 0) {
374819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SndHalf = SVOp->getMaskElt(i)/HalfSize;
374919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
375019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
375119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
375219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
375319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return (FstHalf | (SndHalf << 4));
375419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
375519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
375619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVPERMILPDMask - Return true if the specified VECTOR_SHUFFLE operand
375719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
375819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Note that VPERMIL mask matching is different depending whether theunderlying
375919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
376019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// to the same elements of the low, but to the higher half of the source.
376119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// In VPERMILPD the two lanes could be shuffled independently of each other
376219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// with the same restriction that lanes can't be crossed.
376319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVPERMILPDMask(const SmallVectorImpl<int> &Mask, EVT VT,
376419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            const X86Subtarget *Subtarget) {
376519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElts = VT.getVectorNumElements();
376619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumLanes = VT.getSizeInBits()/128;
376719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
376819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasAVX())
376919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
377019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
377119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Only match 256-bit with 64-bit types
377219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 256 || NumElts != 4)
377319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
377419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
377519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The mask on the high lane is independent of the low. Both can match
377619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // any element in inside its own lane, but can't cross.
377719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int LaneSize = NumElts/NumLanes;
377819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int l = 0; l < NumLanes; ++l)
377919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
378019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int LaneStart = l*LaneSize;
378119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!isUndefOrInRange(Mask[i], LaneStart, LaneStart+LaneSize))
378219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return false;
378319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
378419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
378519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
378619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
378719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
378819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVPERMILPSMask - Return true if the specified VECTOR_SHUFFLE operand
378919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to VPERMILPS*.
379019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Note that VPERMIL mask matching is different depending whether theunderlying
379119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
379219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// to the same elements of the low, but to the higher half of the source.
379319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// In VPERMILPD the two lanes could be shuffled independently of each other
379419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// with the same restriction that lanes can't be crossed.
379519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVPERMILPSMask(const SmallVectorImpl<int> &Mask, EVT VT,
379619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            const X86Subtarget *Subtarget) {
379719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElts = VT.getVectorNumElements();
379819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumLanes = VT.getSizeInBits()/128;
379919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
380019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasAVX())
380119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
380219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
380319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Only match 256-bit with 32-bit types
380419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 256 || NumElts != 8)
380519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
380619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
380719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The mask on the high lane should be the same as the low. Actually,
380819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // they can differ if any of the corresponding index in a lane is undef
380919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // and the other stays in range.
381019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int LaneSize = NumElts/NumLanes;
381119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i < LaneSize; ++i) {
381219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int HighElt = i+LaneSize;
381319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool HighValid = isUndefOrInRange(Mask[HighElt], LaneSize, NumElts);
381419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool LowValid = isUndefOrInRange(Mask[i], 0, LaneSize);
381519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
381619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!HighValid || !LowValid)
381719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
381819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Mask[i] < 0 || Mask[HighElt] < 0)
381919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
382019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Mask[HighElt]-Mask[i] != LaneSize)
382119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
382219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
382319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
382419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
382519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
382619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
382719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getShuffleVPERMILPSImmediate - Return the appropriate immediate to shuffle
382819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// the specified VECTOR_MASK mask with VPERMILPS* instructions.
382919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic unsigned getShuffleVPERMILPSImmediate(SDNode *N) {
383019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
383119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
383219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
383319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElts = VT.getVectorNumElements();
383419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumLanes = VT.getSizeInBits()/128;
383519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int LaneSize = NumElts/NumLanes;
383619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
383719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Although the mask is equal for both lanes do it twice to get the cases
383819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // where a mask will match because the same mask element is undef on the
383919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // first half but valid on the second. This would get pathological cases
384019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // such as: shuffle <u, 0, 1, 2, 4, 4, 5, 6>, which is completely valid.
384119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Mask = 0;
384219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int l = 0; l < NumLanes; ++l) {
384319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = 0; i < LaneSize; ++i) {
384419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int MaskElt = SVOp->getMaskElt(i+(l*LaneSize));
384519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (MaskElt < 0)
384619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
384719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (MaskElt >= LaneSize)
384819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MaskElt -= LaneSize;
384919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Mask |= MaskElt << (i*2);
385019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
385119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
385219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
385319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Mask;
385419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
385519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
385619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getShuffleVPERMILPDImmediate - Return the appropriate immediate to shuffle
385719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// the specified VECTOR_MASK mask with VPERMILPD* instructions.
385819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic unsigned getShuffleVPERMILPDImmediate(SDNode *N) {
385919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
386019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
386119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
386219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElts = VT.getVectorNumElements();
386319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumLanes = VT.getSizeInBits()/128;
386419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
386519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Mask = 0;
386619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int LaneSize = NumElts/NumLanes;
386719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int l = 0; l < NumLanes; ++l)
386819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = l*LaneSize; i < LaneSize*(l+1); ++i) {
386919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int MaskElt = SVOp->getMaskElt(i);
387019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (MaskElt < 0)
387119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
387219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Mask |= (MaskElt-l*LaneSize) << i;
387319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
387419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
387519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Mask;
387619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
387719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of what x86 movss want. X86 movs requires the lowest  element to be lowest
3880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// element of vector 2 and the other elements to come from vector 1 in order.
3881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
3882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               bool V2IsSplat = false, bool V2IsUndef = false) {
3883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumOps = VT.getVectorNumElements();
3884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
3885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isUndefOrEqual(Mask[0], 0))
3888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 1; i < NumOps; ++i)
3891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
3894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
3897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
3900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           bool V2IsUndef = false) {
3901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> M;
3902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N->getMask(M);
3903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
3904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
390819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
390919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N,
391019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         const X86Subtarget *Subtarget) {
391119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
3912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
391419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The second vector must be undef
391519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N->getOperand(1).getOpcode() != ISD::UNDEF)
391619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
391719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
391819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
391919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
392019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
392119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
392219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (VT.getSizeInBits() == 256 && NumElems != 8))
392319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
3924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
392519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // "i+1" is the value the indexed mask element must have
392619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0; i < NumElems; i += 2)
392719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), i+1) ||
392819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        !isUndefOrEqual(N->getMaskElt(i+1), i+1))
3929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
393019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
393119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
3932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
393619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
393719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N,
393819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         const X86Subtarget *Subtarget) {
393919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasSSE3() && !Subtarget->hasAVX())
394019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
394119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
394219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The second vector must be undef
394319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N->getOperand(1).getOpcode() != ISD::UNDEF)
3944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
3945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
394619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
394719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
394819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
394919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
395019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (VT.getSizeInBits() == 256 && NumElems != 8))
395119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
395219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
395319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // "i" is the value the indexed mask element must have
395419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0; i < NumElems; i += 2)
395519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), i) ||
395619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        !isUndefOrEqual(N->getMaskElt(i+1), i))
3957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
395919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
396019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
396119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
396219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
396319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to 256-bit
396419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// version of MOVDDUP.
396519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isMOVDDUPYMask(ShuffleVectorSDNode *N,
396619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           const X86Subtarget *Subtarget) {
396719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
396819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElts = VT.getVectorNumElements();
396919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool V2IsUndef = N->getOperand(1).getOpcode() == ISD::UNDEF;
397019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
397119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasAVX() || VT.getSizeInBits() != 256 ||
397219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !V2IsUndef || NumElts != 4)
397319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
397419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
397519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i != NumElts/2; ++i)
397619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), 0))
3977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
397819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = NumElts/2; i != NumElts; ++i)
397919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), NumElts/2))
398019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
398119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
3982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
3983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
398519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// specifies a shuffle of elements that is suitable for input to 128-bit
398619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// version of MOVDDUP.
3987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
398819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
398919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
399019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128)
399119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
3992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
399319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int e = VT.getVectorNumElements() / 2;
3994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i < e; ++i)
3995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(N->getMaskElt(i), i))
3996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
3997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i < e; ++i)
3998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(N->getMaskElt(e+i), i))
3999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
400319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVEXTRACTF128Index - Return true if the specified
400419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
400519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// suitable for input to VEXTRACTF128.
400619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86::isVEXTRACTF128Index(SDNode *N) {
400719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
400819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
400919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
401019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The index should be aligned on a 128-bit boundary.
401119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Index =
401219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
401319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
401419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned VL = N->getValueType(0).getVectorNumElements();
401519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned VBits = N->getValueType(0).getSizeInBits();
401619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ElSize = VBits / VL;
401719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool Result = (Index * ElSize) % 128 == 0;
401819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
401919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Result;
402019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
402119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
402219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
402319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// operand specifies a subvector insert that is suitable for input to
402419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// VINSERTF128.
402519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86::isVINSERTF128Index(SDNode *N) {
402619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
402719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
402819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
402919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The index should be aligned on a 128-bit boundary.
403019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Index =
403119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
403219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
403319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned VL = N->getValueType(0).getVectorNumElements();
403419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned VBits = N->getValueType(0).getSizeInBits();
403519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ElSize = VBits / VL;
403619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool Result = (Index * ElSize) % 128 == 0;
403719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
403819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Result;
403919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
404019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
4042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
4043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned X86::getShuffleSHUFImmediate(SDNode *N) {
4044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumOperands = SVOp->getValueType(0).getVectorNumElements();
4046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Shift = (NumOperands == 4) ? 2 : 1;
4048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Mask = 0;
4049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i < NumOperands; ++i) {
4050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Val = SVOp->getMaskElt(NumOperands-i-1);
4051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val < 0) Val = 0;
4052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val >= NumOperands) Val -= NumOperands;
4053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask |= Val;
4054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i != NumOperands - 1)
4055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask <<= Shift;
4056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Mask;
4058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
4061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
4062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
4063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Mask = 0;
4065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 8 nodes, but we only care about the last 4.
4066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 7; i >= 4; --i) {
4067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Val = SVOp->getMaskElt(i);
4068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val >= 0)
4069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask |= (Val - 4);
4070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i != 4)
4071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask <<= 2;
4072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Mask;
4074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
4077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
4078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
4079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Mask = 0;
4081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 8 nodes, but we only care about the first 4.
4082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 3; i >= 0; --i) {
4083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Val = SVOp->getMaskElt(i);
4084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val >= 0)
4085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask |= Val;
4086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i != 0)
4087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask <<= 2;
4088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Mask;
4090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
4094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
4095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
4096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VVT = N->getValueType(0);
4097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
4098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Val = 0;
4099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned i, e;
4101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
4102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Val = SVOp->getMaskElt(i);
4103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val >= 0)
4104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
4105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
410619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Val - i > 0 && "PALIGNR imm should be positive");
4107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (Val - i) * EltSize;
4108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
411019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
411119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
411219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// instructions.
411319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
411419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
411519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
411619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
411719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Index =
411819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
411919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
412019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VecVT = N->getOperand(0).getValueType();
412119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ElVT = VecVT.getVectorElementType();
412219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
412319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
412419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Index / NumElemsPerChunk;
412519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
412619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
412719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getInsertVINSERTF128Immediate - Return the appropriate immediate
412819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
412919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// instructions.
413019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
413119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
413219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Illegal insert subvector for VINSERTF128");
413319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
413419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Index =
413519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
413619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
413719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VecVT = N->getValueType(0);
413819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ElVT = VecVT.getVectorElementType();
413919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
414019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
414119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Index / NumElemsPerChunk;
414219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
414319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// constant +0.0.
4146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86::isZeroNode(SDValue Elt) {
4147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ((isa<ConstantSDNode>(Elt) &&
4148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           cast<ConstantSDNode>(Elt)->isNullValue()) ||
4149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (isa<ConstantFPSDNode>(Elt) &&
4150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// their permute mask.
4155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    SelectionDAG &DAG) {
4157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = SVOp->getValueType(0);
4158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> MaskVec;
4160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != NumElems; ++i) {
4162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int idx = SVOp->getMaskElt(i);
4163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (idx < 0)
4164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(idx);
4165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (idx < (int)NumElems)
4166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(idx + NumElems);
4167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
4168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(idx - NumElems);
4169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              SVOp->getOperand(0), &MaskVec[0]);
4172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
4175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the two vector operands have swapped position.
4176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
4177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != NumElems; ++i) {
4179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int idx = Mask[i];
4180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (idx < 0)
4181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
4182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (idx < (int)NumElems)
4183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask[i] = idx + NumElems;
4184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
4185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask[i] = idx - NumElems;
4186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// match movhlps. The lower half elements should come from upper half of
4191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// V1 (and in order), and the upper half elements should come from the upper
4192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// half of V2 (and in order).
4193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
419419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op->getValueType(0);
419519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128)
419619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
419719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getVectorNumElements() != 4)
4198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = 2; i != e; ++i)
4200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
4201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 2; i != 4; ++i)
4203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
4204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isScalarLoadToVector - Returns true if the node is a scalar load that
4209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is promoted to a vector. It also returns the LoadSDNode by reference if
4210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// required.
4211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
4212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  N = N->getOperand(0).getNode();
4215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!ISD::isNON_EXTLoad(N))
4216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LD)
4218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    *LD = cast<LoadSDNode>(N);
4219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// match movlp{s|d}. The lower half elements should come from lower half of
4224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// V1 (and in order), and the upper half elements should come from the upper
4225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// half of V2 (and in order). And since V1 will become the source of the
4226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MOVLP, it must be either a vector load or a scalar load to vector.
4227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
4228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               ShuffleVectorSDNode *Op) {
422919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op->getValueType(0);
423019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() != 128)
423119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
423219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
4234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Is V2 is a vector load, don't do this transformation. We will try to use
4236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // load folding shufps op.
4237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ISD::isNON_EXTLoad(V2))
4238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
424019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
4241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems != 2 && NumElems != 4)
4243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = NumElems/2; i != e; ++i)
4245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(Op->getMaskElt(i), i))
4246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = NumElems/2; i != NumElems; ++i)
4248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
4249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// all the same.
4255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isSplatVector(SDNode *N) {
4256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N->getOpcode() != ISD::BUILD_VECTOR)
4257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue SplatValue = N->getOperand(0);
4260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N->getOperand(i) != SplatValue)
4262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
4267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to an zero vector.
4268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
4269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isZeroShuffle(ShuffleVectorSDNode *N) {
4270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = N->getOperand(0);
4271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V2 = N->getOperand(1);
4272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = N->getValueType(0).getVectorNumElements();
4273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != NumElems; ++i) {
4274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Idx = N->getMaskElt(i);
4275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx >= (int)NumElems) {
4276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Opc = V2.getOpcode();
4277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
4279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Opc != ISD::BUILD_VECTOR ||
4280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
4281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
4282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (Idx >= 0) {
4283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Opc = V1.getOpcode();
4284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
4286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Opc != ISD::BUILD_VECTOR ||
4287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          !X86::isZeroNode(V1.getOperand(Idx)))
4288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
4289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getZeroVector - Returns a vector of specified type with all zero elements.
4295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
429619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getZeroVector(EVT VT, bool HasXMMInt, SelectionDAG &DAG,
4297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DebugLoc dl) {
4298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(VT.isVector() && "Expected a vector type");
4299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
430019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Always build SSE zero vectors as <4 x i32> bitcasted
430119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to their dest type. This ensures they get CSE'd.
4302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Vec;
430319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 128) {  // SSE
430419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (HasXMMInt) {  // SSE2
430519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
430619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
430719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else { // SSE1
430819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
430919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
431019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
431119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (VT.getSizeInBits() == 256) { // AVX
431219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // 256-bit logic and arithmetic instructions in AVX are
431319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // all floating-point, no support for integer ops. Default
431419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // to emitting fp zeroed vectors then.
4315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
431619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
431719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
431919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
4320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getOnesVector - Returns a vector of specified type with all bits set.
432319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Always build ones vectors as <4 x i32>. For 256-bit types, use two
432419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// <4 x i32> inserted in a <8 x i32> appropriately. Then bitcast to their
432519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// original type, ensuring they get CSE'd.
4326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
4327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(VT.isVector() && "Expected a vector type");
432819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((VT.is128BitVector() || VT.is256BitVector())
432919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         && "Expected a 128-bit or 256-bit vector type");
4330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
433219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
433319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            Cst, Cst, Cst, Cst);
4334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
433519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.is256BitVector()) {
433619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, MVT::v8i32),
433719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              Vec, DAG.getConstant(0, MVT::i32), DAG, dl);
433819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Vec = Insert128BitVector(InsV, Vec,
433919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  DAG.getConstant(4 /* NumElems/2 */, MVT::i32), DAG, dl);
434019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
434119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
434219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
434319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
4344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// that point to V2 points to its first element.
4347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = SVOp->getValueType(0);
4349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
4352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> MaskVec;
4353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SVOp->getMask(MaskVec);
4354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != NumElems; ++i) {
4356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MaskVec[i] > (int)NumElems) {
4357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec[i] = NumElems;
4358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Changed = true;
4359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Changed)
4362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
4363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                SVOp->getOperand(1), &MaskVec[0]);
4364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue(SVOp, 0);
4365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// operation of specified width.
4369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
4370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       SDValue V2) {
4371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> Mask;
4373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Mask.push_back(NumElems);
4374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 1; i != NumElems; ++i)
4375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask.push_back(i);
4376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
4377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
4380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
4381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          SDValue V2) {
4382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> Mask;
4384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
4385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask.push_back(i);
4386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask.push_back(i + NumElems);
4387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
4389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
439119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
4392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
4393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          SDValue V2) {
4394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Half = NumElems/2;
4396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> Mask;
4397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != Half; ++i) {
4398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask.push_back(i + Half);
4399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask.push_back(i + NumElems + Half);
4400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
4402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
440419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
440519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// a generic shuffle instruction because the target has no such instructions.
440619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Generate shuffles which repeat i16 and i8 several times until they can be
440719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// represented by v4f32 and then be manipulated by target suported shuffles.
440819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
440919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = V.getValueType();
4410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int NumElems = VT.getVectorNumElements();
441119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = V.getDebugLoc();
4412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (NumElems > 4) {
4414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EltNo < NumElems/2) {
441519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      V = getUnpackl(DAG, dl, VT, V, V);
4416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
441719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      V = getUnpackh(DAG, dl, VT, V, V);
4418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EltNo -= NumElems/2;
4419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumElems >>= 1;
4421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
442219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return V;
442319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
442419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
442519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getLegalSplat - Generate a legal splat with supported x86 shuffles
442619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
442719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = V.getValueType();
442819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = V.getDebugLoc();
442919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256)
443019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         && "Vector size not supported");
443119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
443219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 128) {
443319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
443419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
443519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
443619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             &SplatMask[0]);
443719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
443819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // To use VPERMILPS to splat scalars, the second half of indicies must
443919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // refer to the higher part, which is a duplication of the lower one,
444019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // because VPERMILPS can only handle in-lane permutations.
444119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
444219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
444319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
444419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
444519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
444619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             &SplatMask[0]);
444719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
444819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
444919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT, V);
445019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
445119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
445219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PromoteSplat - Splat is promoted to target supported vector shuffles.
445319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
445419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT SrcVT = SV->getValueType(0);
445519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = SV->getOperand(0);
445619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = SV->getDebugLoc();
445719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
445819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int EltNo = SV->getSplatIndex();
445919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = SrcVT.getVectorNumElements();
446019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Size = SrcVT.getSizeInBits();
446119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
446219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(((Size == 128 && NumElems > 4) || Size == 256) &&
446319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          "Unknown how to promote splat for type");
446419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
446519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Extract the 128-bit part containing the splat element and update
446619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the splat element index when it refers to the higher register.
446719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Size == 256) {
446819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Idx = (EltNo > NumElems/2) ? NumElems/2 : 0;
446919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = Extract128BitVector(V1, DAG.getConstant(Idx, MVT::i32), DAG, dl);
447019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Idx > 0)
447119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EltNo -= NumElems/2;
447219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
447319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
447419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // All i16 and i8 vector types can't be used directly by a generic shuffle
447519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // instruction because the target has no such instruction. Generate shuffles
447619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // which repeat i16 and i8 several times until they fit in i32, and then can
447719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // be manipulated by target suported shuffles.
447819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT EltVT = SrcVT.getVectorElementType();
447919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (EltVT == MVT::i8 || EltVT == MVT::i16)
448019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = PromoteSplati8i16(V1, DAG, EltNo);
448119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
448219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Recreate the 256-bit vector and place the same 128-bit vector
448319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // into the low and high part. This is necessary because we want
448419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to use VPERM* to shuffle the vectors
448519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Size == 256) {
448619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue InsV = Insert128BitVector(DAG.getUNDEF(SrcVT), V1,
448719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         DAG.getConstant(0, MVT::i32), DAG, dl);
448819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = Insert128BitVector(InsV, V1,
448919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman               DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
449019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
4491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
449219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getLegalSplat(DAG, V1, EltNo);
4493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
4496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// vector of zero or undef vector.  This produces a shuffle where the low
4497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// element of V2 is swizzled into the zero/undef vector, landing at element
4498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Idx.  This produces a shuffle mask like 4,1,2,3 (idx=0) or  0,1,2,4 (idx=3).
4499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
450019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           bool isZero, bool HasXMMInt,
450119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           SelectionDAG &DAG) {
4502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = V2.getValueType();
4503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = isZero
450419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ? getZeroVector(VT, HasXMMInt, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
4505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
4506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 16> MaskVec;
4507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != NumElems; ++i)
4508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is the insertion idx, put the low elt of V2 here.
4509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MaskVec.push_back(i == Idx ? NumElems : i);
4510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
4511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
451319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getShuffleScalarElt - Returns the scalar element that will make up the ith
451419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// element of the result of the vector shuffle.
451519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
451619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   unsigned Depth) {
451719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Depth == 6)
451819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();  // Limit search depth.
451919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
452019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V = SDValue(N, 0);
452119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = V.getValueType();
452219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Opcode = V.getOpcode();
452319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
452419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
452519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
452619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Index = SV->getMaskElt(Index);
452719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
452819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Index < 0)
452919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getUNDEF(VT.getVectorElementType());
453019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
453119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int NumElems = VT.getVectorNumElements();
453219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
453319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
453419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
453519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
453619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Recurse into target specific vector shuffles to find scalars.
453719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isTargetShuffle(Opcode)) {
453819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int NumElems = VT.getVectorNumElements();
453919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<unsigned, 16> ShuffleMask;
454019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ImmN;
454119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
454219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch(Opcode) {
454319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::SHUFPS:
454419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::SHUFPD:
454519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
454619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeSHUFPSMask(NumElems,
454719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       cast<ConstantSDNode>(ImmN)->getZExtValue(),
454819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       ShuffleMask);
454919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
455019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKHBW:
455119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKHWD:
455219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKHDQ:
455319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKHQDQ:
455419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodePUNPCKHMask(NumElems, ShuffleMask);
455519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
455619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::UNPCKHPS:
455719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::UNPCKHPD:
455819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VUNPCKHPSY:
455919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VUNPCKHPDY:
456019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeUNPCKHPMask(NumElems, ShuffleMask);
456119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
456219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKLBW:
456319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKLWD:
456419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKLDQ:
456519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PUNPCKLQDQ:
456619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodePUNPCKLMask(VT, ShuffleMask);
456719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
456819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::UNPCKLPS:
456919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::UNPCKLPD:
457019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VUNPCKLPSY:
457119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VUNPCKLPDY:
457219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeUNPCKLPMask(VT, ShuffleMask);
4573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
457419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVHLPS:
457519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeMOVHLPSMask(NumElems, ShuffleMask);
457619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
457719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVLHPS:
457819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeMOVLHPSMask(NumElems, ShuffleMask);
457919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
458019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PSHUFD:
458119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
458219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodePSHUFMask(NumElems,
458319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      cast<ConstantSDNode>(ImmN)->getZExtValue(),
458419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      ShuffleMask);
458519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
458619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PSHUFHW:
458719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
458819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
458919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ShuffleMask);
459019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
459119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PSHUFLW:
459219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
459319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
459419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ShuffleMask);
459519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
459619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVSS:
459719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVSD: {
459819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // The index 0 always comes from the first element of the second source,
459919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // this is why MOVSS and MOVSD are used in the first place. The other
460019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // elements come from the other positions of the first source vector.
460119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned OpNum = (Index == 0) ? 1 : 0;
460219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
460319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 Depth+1);
460419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
460519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VPERMILPS:
460619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
460719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeVPERMILPSMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
460819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ShuffleMask);
460919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
461019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VPERMILPSY:
461119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
461219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeVPERMILPSMask(8, cast<ConstantSDNode>(ImmN)->getZExtValue(),
461319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ShuffleMask);
461419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
461519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VPERMILPD:
461619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
461719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeVPERMILPDMask(2, cast<ConstantSDNode>(ImmN)->getZExtValue(),
461819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ShuffleMask);
461919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
462019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VPERMILPDY:
462119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
462219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeVPERMILPDMask(4, cast<ConstantSDNode>(ImmN)->getZExtValue(),
462319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ShuffleMask);
462419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
462519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::VPERM2F128:
462619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ImmN = N->getOperand(N->getNumOperands()-1);
462719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DecodeVPERM2F128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(),
462819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           ShuffleMask);
462919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
463019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVDDUP:
463119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVLHPD:
463219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVLPD:
463319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVLPS:
463419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVSHDUP:
463519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::MOVSLDUP:
463619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::PALIGN:
463719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue(); // Not yet implemented.
463819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    default:
463919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(0 && "unknown target shuffle node");
464019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
464119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
464219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
464319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Index = ShuffleMask[Index];
464419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Index < 0)
464519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getUNDEF(VT.getVectorElementType());
464619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
464719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
464819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
464919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Depth+1);
4650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
465219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Actual nodes that may contain scalar elements
465319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Opcode == ISD::BITCAST) {
465419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
465519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT SrcVT = V.getValueType();
465619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumElems = VT.getVectorNumElements();
4657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
465819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
465919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
466019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
466119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
466219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
466319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return (Index == 0) ? V.getOperand(0)
466419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          : DAG.getUNDEF(VT.getVectorElementType());
466519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
466619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.getOpcode() == ISD::BUILD_VECTOR)
466719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return V.getOperand(Index);
466819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
466919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
467019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
467119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
467219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getNumOfConsecutiveZeros - Return the number of elements of a vector
467319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// shuffle operation which come from a consecutively from a zero. The
467419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// search can start in two different directions, from left or right.
467519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
467619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
467719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  bool ZerosFromLeft, SelectionDAG &DAG) {
467819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int i = 0;
467919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
468019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  while (i < NumElems) {
468119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
468219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
468319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!(Elt.getNode() &&
468419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
468519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
468619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ++i;
4687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
468819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
468919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return i;
469019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
469119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
469219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
469319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// MaskE correspond consecutively to elements from one of the vector operands,
469419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// starting from its index OpIdx. Also tell OpNum which source vector operand.
469519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
469619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
469719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              int OpIdx, int NumElems, unsigned &OpNum) {
4698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool SeenV1 = false;
4699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool SeenV2 = false;
470019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
470119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
470219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int Idx = SVOp->getMaskElt(i);
470319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Ignore undef indicies
470419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Idx < 0)
4705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
470619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx < NumElems)
4708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SeenV1 = true;
470919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else
4710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SeenV2 = true;
471119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
471219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Only accept consecutive elements from the same vector
471319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
4715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
471619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
471719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  OpNum = SeenV1 ? 0 : 1;
471819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
471919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
472019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
472119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
472219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// logical left shift of a vector.
472319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
472419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
472519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
472619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
472719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              false /* check zeros from right */, DAG);
472819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned OpSrc;
472919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
473019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!NumZeros)
473119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
473219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
473319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Considering the elements in the mask that are not consecutive zeros,
473419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // check if they consecutively come from only one of the source vectors.
473519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
473619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //               V1 = {X, A, B, C}     0
473719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //                         \  \  \    /
473819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   vector_shuffle V1, V2 <1, 2, 3, X>
473919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
474019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isShuffleMaskConsecutive(SVOp,
474119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            0,                   // Mask Start Index
474219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumElems-NumZeros-1, // Mask End Index
474319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumZeros,            // Where to start looking in the src vector
474419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumElems,            // Number of elements in vector
474519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            OpSrc))              // Which source operand ?
474619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
474719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
474819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  isLeft = false;
474919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShAmt = NumZeros;
475019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShVal = SVOp->getOperand(OpSrc);
475119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
475219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
475319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
475419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
475519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// logical left shift of a vector.
475619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
475719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
475819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
475919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
476019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              true /* check zeros from left */, DAG);
476119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned OpSrc;
476219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
476319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!NumZeros)
476419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
476519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
476619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Considering the elements in the mask that are not consecutive zeros,
476719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // check if they consecutively come from only one of the source vectors.
476819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
476919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //                           0    { A, B, X, X } = V2
477019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //                          / \    /  /
477119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   vector_shuffle V1, V2 <X, X, 4, 5>
477219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
477319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isShuffleMaskConsecutive(SVOp,
477419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumZeros,     // Mask Start Index
477519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumElems-1,   // Mask End Index
477619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            0,            // Where to start looking in the src vector
477719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            NumElems,     // Number of elements in vector
477819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            OpSrc))       // Which source operand ?
4779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
4780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
478119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  isLeft = true;
4782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ShAmt = NumZeros;
478319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShVal = SVOp->getOperand(OpSrc);
4784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
4785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
478719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVectorShift - Returns true if the shuffle can be implemented as a
478819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// logical left or right shift of a vector.
478919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
479019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
479119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Although the logic below support any bitwidth size, there are no
479219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // shift instructions which handle more than 128-bit vectors.
479319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SVOp->getValueType(0).getSizeInBits() > 128)
479419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
479519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
479619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
479719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
479819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
479919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
480019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
480119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
4802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
4805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
4806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       unsigned NumNonZero, unsigned NumZero,
4807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       SelectionDAG &DAG,
4808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       const TargetLowering &TLI) {
4809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumNonZero > 8)
4810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
4811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
4813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V(0, 0);
4814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool First = true;
4815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < 16; ++i) {
4816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ThisIsNonZero && First) {
4818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NumZero)
4819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        V = getZeroVector(MVT::v8i16, true, DAG, dl);
4820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
4821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        V = DAG.getUNDEF(MVT::v8i16);
4822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      First = false;
4823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((i & 1) != 0) {
4826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue ThisElt(0, 0), LastElt(0, 0);
4827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (LastIsNonZero) {
4829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
4830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MVT::i16, Op.getOperand(i-1));
4831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
4832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ThisIsNonZero) {
4833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              ThisElt, DAG.getConstant(8, MVT::i8));
4836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (LastIsNonZero)
4837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
4838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else
4839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ThisElt = LastElt;
4840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ThisElt.getNode())
4842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
4843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        DAG.getIntPtrConstant(i/2));
4844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
484719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
4848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
4851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
4852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
4853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     unsigned NumNonZero, unsigned NumZero,
4854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     SelectionDAG &DAG,
4855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     const TargetLowering &TLI) {
4856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumNonZero > 4)
4857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
4858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
4860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V(0, 0);
4861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool First = true;
4862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < 8; ++i) {
4863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool isNonZero = (NonZeros & (1 << i)) != 0;
4864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isNonZero) {
4865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (First) {
4866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (NumZero)
4867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          V = getZeroVector(MVT::v8i16, true, DAG, dl);
4868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        else
4869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          V = DAG.getUNDEF(MVT::v8i16);
4870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        First = false;
4871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
4872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
4873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      MVT::v8i16, V, Op.getOperand(i),
4874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      DAG.getIntPtrConstant(i));
4875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return V;
4879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getVShift - Return a vector logical shift node.
4882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
4883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
4884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         unsigned NumBits, SelectionDAG &DAG,
4885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         const TargetLowering &TLI, DebugLoc dl) {
488619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT.getSizeInBits() == 128 && "Unknown type for VShift");
488719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ShVT = MVT::v2i64;
4888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
488919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
489019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT,
4891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getNode(Opc, dl, ShVT, SrcOp,
489219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             DAG.getConstant(NumBits,
489319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  TLI.getShiftAmountTy(SrcOp.getValueType()))));
4894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
4897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
4898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          SelectionDAG &DAG) const {
489919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check if the scalar load can be widened into a vector load. And if
4901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the address is "base + cst" see if the cst can be "absorbed" into
4902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the shuffle mask.
4903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ptr = LD->getBasePtr();
4905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
4907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT PVT = LD->getValueType(0);
4908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PVT != MVT::i32 && PVT != MVT::f32)
4909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
4910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int FI = -1;
4912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int64_t Offset = 0;
4913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FI = FINode->getIndex();
4915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Offset = 0;
491619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else if (DAG.isBaseWithConstantOffset(Ptr) &&
4917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Offset = Ptr.getConstantOperandVal(1);
4920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ptr = Ptr.getOperand(0);
4921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
4922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
4923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
492519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // FIXME: 256-bit vector instructions don't require a strict alignment,
492619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // improve this code to support it better.
492719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned RequiredAlign = VT.getSizeInBits()/8;
4928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Chain = LD->getChain();
492919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Make sure the stack object alignment is at least 16 or 32.
4930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
493119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
4932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MFI->isFixedObjectIndex(FI)) {
4933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Can't change the alignment. FIXME: It's possible to compute
4934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // the exact stack offset and reference FI + adjust offset instead.
4935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If someone *really* cares about this. That's the way to implement it.
4936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return SDValue();
4937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
493819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MFI->setObjectAlignment(FI, RequiredAlign);
4939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
4940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
4941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
494219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // (Offset % 16 or 32) must be multiple of 4. Then address is then
4943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Ptr + (Offset & ~15).
4944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Offset < 0)
4945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
494619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((Offset % RequiredAlign) & 3)
4947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
494819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int64_t StartOffset = Offset & ~(RequiredAlign-1);
4949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (StartOffset)
4950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int EltNo = (Offset - StartOffset) >> 2;
495419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int NumElems = VT.getVectorNumElements();
495519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
495619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT CanonVT = VT.getSizeInBits() == 128 ? MVT::v4i32 : MVT::v8i32;
495719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
495819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
495919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             LD->getPointerInfo().getWithOffset(StartOffset),
4960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             false, false, 0);
496119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
496219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Canonicalize it to a v4i32 or v8i32 shuffle.
496319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<int, 8> Mask;
496419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = 0; i < NumElems; ++i)
496519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Mask.push_back(EltNo);
496619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
496719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = DAG.getNode(ISD::BITCAST, dl, CanonVT, V1);
496819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::BITCAST, dl, NVT,
496919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getVectorShuffle(CanonVT, dl, V1,
497019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            DAG.getUNDEF(CanonVT),&Mask[0]));
4971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
4972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
4974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
4975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
497619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
497719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// vector of type 'VT', see if the elements can be replaced by a single large
4978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// load which has the same value as a build_vector whose operands are 'elts'.
4979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
4980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
498119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///
4982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FIXME: we'd also like to handle the case where the last elements are zero
4983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// There's even a handy isZeroNode for that purpose.
4985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
498619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        DebugLoc &DL, SelectionDAG &DAG) {
4987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltVT = VT.getVectorElementType();
4988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = Elts.size();
498919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LoadSDNode *LDBase = NULL;
4991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned LastLoadedElt = -1U;
499219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For each element in the initializer, see if we've found a load or an undef.
499419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we don't find an initial load element, or later load elements are
4995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // non-consecutive, bail out.
4996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < NumElems; ++i) {
4997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Elt = Elts[i];
499819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Elt.getNode() ||
5000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
5002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!LDBase) {
5003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return SDValue();
5005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LDBase = cast<LoadSDNode>(Elt.getNode());
5006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LastLoadedElt = i;
5007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Elt.getOpcode() == ISD::UNDEF)
5010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LoadSDNode *LD = cast<LoadSDNode>(Elt);
5013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
5015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LastLoadedElt = i;
5016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we have found an entire vector of loads and undefs, then return a large
5019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // load of the entire vector width starting at the base pointer.  If we found
5020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // consecutive loads for the low half, generate a vzext_load node.
5021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LastLoadedElt == NumElems - 1) {
5022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
502319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
502419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         LDBase->getPointerInfo(),
5025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
502619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
502719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       LDBase->getPointerInfo(),
5028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       LDBase->isVolatile(), LDBase->isNonTemporal(),
5029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       LDBase->getAlignment());
503019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (NumElems == 4 && LastLoadedElt == 1 &&
503119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
5032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
503419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ResNode =
503519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
503619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                LDBase->getPointerInfo(),
503719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                LDBase->getAlignment(),
503819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false/*isVolatile*/, true/*ReadMem*/,
503919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false/*WriteMem*/);
504019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
5041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
5043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
5046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
5047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
504819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
504919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
505019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ExtVT = VT.getVectorElementType();
505119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = Op.getNumOperands();
505219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
505319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Vectors containing all zeros can be matched by pxor and xorps later
505419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ISD::isBuildVectorAllZeros(Op.getNode())) {
505519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
505619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
505719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Op.getValueType() == MVT::v4i32 ||
505819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Op.getValueType() == MVT::v8i32)
5059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Op;
5060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
506119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getZeroVector(Op.getValueType(), Subtarget->hasXMMInt(), DAG, dl);
506219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
506319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
506419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
506519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // vectors or broken into v4i32 operations on 256-bit vectors.
506619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ISD::isBuildVectorAllOnes(Op.getNode())) {
506719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Op.getValueType() == MVT::v4i32)
506819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return Op;
506919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
507019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getOnesVector(Op.getValueType(), DAG, dl);
5071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned EVTBits = ExtVT.getSizeInBits();
5074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumZero  = 0;
5076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumNonZero = 0;
5077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NonZeros = 0;
5078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool IsAllConstants = true;
5079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallSet<SDValue, 8> Values;
5080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < NumElems; ++i) {
5081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Elt = Op.getOperand(i);
5082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Elt.getOpcode() == ISD::UNDEF)
5083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Values.insert(Elt);
5085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Elt.getOpcode() != ISD::Constant &&
5086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Elt.getOpcode() != ISD::ConstantFP)
5087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      IsAllConstants = false;
5088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (X86::isZeroNode(Elt))
5089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NumZero++;
5090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else {
5091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NonZeros |= (1 << i);
5092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NumNonZero++;
5093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
509619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // All undef vector. Return an UNDEF.  All zero vectors were handled above.
509719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumNonZero == 0)
5098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getUNDEF(VT);
5099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Special case for single non-zero, non-undef, element.
5101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumNonZero == 1) {
5102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Idx = CountTrailingZeros_32(NonZeros);
5103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Item = Op.getOperand(Idx);
5104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is an insertion of an i64 value on x86-32, and if the top bits of
5106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the value are obviously zero, truncate the value to i32 and do the
5107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // insertion that way.  Only do this if the value is non-constant or if the
5108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // value is a constant being inserted into element 0.  It is cheaper to do
5109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a constant pool load than it is to do a movd + shuffle.
5110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
5111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (!IsAllConstants || Idx == 0)) {
5112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
511319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Handle SSE only.
511419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        assert(VT == MVT::v2i64 && "Expected an SSE value type!");
511519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT VecVT = MVT::v4i32;
511619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned VecElts = 4;
5117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Truncate the value (which may itself be a constant) to i32, and
5119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // convert it to a vector with movd (S2V+shuffle to zero extend).
5120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
5121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
5122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = getShuffleVectorZeroOrUndef(Item, 0, true,
512319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           Subtarget->hasXMMInt(), DAG);
5124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Now we have our 32-bit value zero extended in the low element of
5126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // a vector.  If Idx != 0, swizzle it into place.
5127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Idx != 0) {
5128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SmallVector<int, 4> Mask;
5129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Mask.push_back(Idx);
5130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          for (unsigned i = 1; i != VecElts; ++i)
5131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Mask.push_back(i);
5132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Item = DAG.getVectorShuffle(VecVT, dl, Item,
5133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      DAG.getUNDEF(Item.getValueType()),
5134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      &Mask[0]);
5135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
513619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
5137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we have a constant or non-constant insertion into the low element of
5141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the rest of the elements.  This will be matched as movd/movq/movss/movsd
5143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // depending on what the source datatype is.
5144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx == 0) {
5145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NumZero == 0) {
5146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
5148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
5149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
515119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return getShuffleVectorZeroOrUndef(Item, 0, true,Subtarget->hasXMMInt(),
5152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           DAG);
5153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
5154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
515519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
515619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT MiddleVT = MVT::v4i32;
5157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
5158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Item = getShuffleVectorZeroOrUndef(Item, 0, true,
515919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           Subtarget->hasXMMInt(), DAG);
516019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return DAG.getNode(ISD::BITCAST, dl, VT, Item);
5161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Is it a vector logical left shift?
5165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NumElems == 2 && Idx == 1 &&
5166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        X86::isZeroNode(Op.getOperand(0)) &&
5167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        !X86::isZeroNode(Op.getOperand(1))) {
5168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned NumBits = VT.getSizeInBits();
5169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return getVShift(true, VT,
5170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   VT, Op.getOperand(1)),
5172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       NumBits/2, DAG, *this, dl);
5173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (IsAllConstants) // Otherwise, it's better to do a constpool load.
5176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
5177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, if this is a vector with i32 or f32 elements, and the element
5179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // is a non-constant being inserted into an element other than the low one,
5180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // we can't use a constant pool load.  Instead, use SCALAR_TO_VECTOR (aka
5181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // movd/movss) to move this into the low element, then shuffle it into
5182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // place.
5183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EVTBits == 32) {
5184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Turn it into a shuffle of zero and zero-extended scalar to vector.
5187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
518819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         Subtarget->hasXMMInt(), DAG);
5189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SmallVector<int, 8> MaskVec;
5190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0; i < NumElems; i++)
5191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskVec.push_back(i == Idx ? 0 : 1);
5192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
5193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Splat is obviously ok. Let legalizer expand it to a shuffle.
5197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Values.size() == 1) {
5198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EVTBits == 32) {
5199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Instead of a shuffle like this:
5200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Check if it's possible to issue this instead.
5202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Idx = CountTrailingZeros_32(NonZeros);
5204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Item = Op.getOperand(Idx);
5205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
5209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // A vector full of immediates; various special cases are already
5212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // handled, so this is best done with a single constant-pool load.
5213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (IsAllConstants)
5214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
5215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
521619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // For AVX-length vectors, build the individual 128-bit pieces and use
521719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // shuffles to put them in place.
521819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256 && !ISD::isBuildVectorAllZeros(Op.getNode())) {
521919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<SDValue, 32> V;
522019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i < NumElems; ++i)
522119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      V.push_back(Op.getOperand(i));
522219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
522319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
522419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
522519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Build both the lower and upper subvector.
522619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
522719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
522819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                NumElems/2);
522919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
523019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Recreate the wider vector with the lower and upper part.
523119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Vec = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), Lower,
523219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG.getConstant(0, MVT::i32), DAG, dl);
523319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Insert128BitVector(Vec, Upper, DAG.getConstant(NumElems/2, MVT::i32),
523419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG, dl);
523519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
523619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
5237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Let legalizer expand 2-wide build_vectors.
5238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EVTBits == 64) {
5239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NumNonZero == 1) {
5240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // One half is zero or undef.
5241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Idx = CountTrailingZeros_32(NonZeros);
5242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
5243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 Op.getOperand(Idx));
5244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return getShuffleVectorZeroOrUndef(V2, Idx, true,
524519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         Subtarget->hasXMMInt(), DAG);
5246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
5248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If element VT is < 32 bits, convert it to inserts into a zero vector.
5251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EVTBits == 8 && NumElems == 16) {
5252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
5253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        *this);
5254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V.getNode()) return V;
5255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EVTBits == 16 && NumElems == 8) {
5258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
525919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      *this);
5260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V.getNode()) return V;
5261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If element VT is == 32 bits, turn it into a number of shuffles.
5264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 8> V;
5265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  V.resize(NumElems);
5266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElems == 4 && NumZero > 0) {
5267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < 4; ++i) {
5268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool isZero = !(NonZeros & (1 << i));
5269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (isZero)
527019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V[i] = getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
5271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
5272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < 2; ++i) {
5276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        default: break;
5278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case 0:
5279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          V[i] = V[i*2];  // Must be a zero vector.
5280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
5281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case 1:
5282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
5283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
5284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case 2:
5285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
5286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
5287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case 3:
5288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
5289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
5290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<int, 8> MaskVec;
5294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Reverse = (NonZeros & 0x3) == 2;
5295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < 2; ++i)
5296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(Reverse ? 1-i : i);
5297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < 2; ++i)
5299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
5300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
5301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Values.size() > 1 && VT.getSizeInBits() == 128) {
5304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check for a build vector of consecutive loads.
5305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < NumElems; ++i)
5306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V[i] = Op.getOperand(i);
530719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
5308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check for elements which are consecutive loads.
5309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LD.getNode())
5311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return LD;
531219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
531319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // For SSE 4.1, use insertps to put the high elements into the low element.
531419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (getSubtarget()->hasSSE41() || getSubtarget()->hasAVX()) {
531519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Result;
531619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
531719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
531819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else
531919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Result = DAG.getUNDEF(VT);
532019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
532119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (unsigned i = 1; i < NumElems; ++i) {
532219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
532319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
5324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Op.getOperand(i), DAG.getIntPtrConstant(i));
532519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
532619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return Result;
532719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
532819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
532919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Otherwise, expand into a number of unpckl*, start by extending each of
533019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // our (non-undef) elements to the full vector width with the element in the
533119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // bottom slot of the vector (which generates no code for SSE).
533219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i < NumElems; ++i) {
533319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
533419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
533519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else
533619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V[i] = DAG.getUNDEF(VT);
5337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
533819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
533919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Next, we iteratively mix elements, e.g. for v4f32:
5340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //   Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //         : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //   Step 2: unpcklps X, Y ==>    <3, 2, 1, 0>
534319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned EltStride = NumElems >> 1;
534419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    while (EltStride != 0) {
534519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (unsigned i = 0; i < EltStride; ++i) {
534619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // If V[i+EltStride] is undef and this is the first round of mixing,
534719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // then it is safe to just drop this shuffle: V[i] is already in the
534819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // right place, the one element (since it's the first round) being
534919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // inserted as undef can be dropped.  This isn't safe for successive
535019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // rounds because they will permute elements within both vectors.
535119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
535219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            EltStride == NumElems/2)
535319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          continue;
535419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
535519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
535619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
535719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EltStride >>= 1;
5358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return V[0];
5360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
5362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
536419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// LowerMMXCONCAT_VECTORS - We support concatenate two MMX registers and place
536519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// them in a MMX register.  This is better than doing a stack convert.
536619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue LowerMMXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
5368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT ResVT = Op.getValueType();
536919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
5370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
5371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
5372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Mask[2];
537319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
5374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InVec = Op.getOperand(1);
5376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumElts = ResVT.getVectorNumElements();
537819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
5379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
5380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
5381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
538219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
5383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
5384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask[0] = 0; Mask[1] = 2;
5385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
5386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
538719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
538819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
538919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
539019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
539119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// to create 256-bit vectors from two other 128-bit ones.
539219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
539319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
539419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ResVT = Op.getValueType();
539519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
539619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(ResVT.getSizeInBits() == 256 && "Value type must be 256-bit wide");
539719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
539819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = Op.getOperand(0);
539919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V2 = Op.getOperand(1);
540019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = ResVT.getVectorNumElements();
540119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
540219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, ResVT), V1,
540319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 DAG.getConstant(0, MVT::i32), DAG, dl);
540419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
540519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            DAG, dl);
540619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
540719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
540819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue
540919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
541019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ResVT = Op.getValueType();
541119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
541219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Op.getNumOperands() == 2);
541319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((ResVT.getSizeInBits() == 128 || ResVT.getSizeInBits() == 256) &&
541419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported CONCAT_VECTORS for value type");
541519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
541619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // We support concatenate two MMX registers and place them in a MMX register.
541719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // This is better than doing a stack convert.
541819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ResVT.is128BitVector())
541919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return LowerMMXCONCAT_VECTORS(Op, DAG);
542019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
542119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
542219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // from two other 128-bit ones.
542319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return LowerAVXCONCAT_VECTORS(Op, DAG);
5424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// v8i16 shuffles - Prefer shuffles in the following order:
5427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1. [all]   pshuflw, pshufhw, optional move
5428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 2. [ssse3] 1 x pshufb
5429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3. [ssse3] 2 x pshufb + 1 x por
5430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 4. [all]   mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
543119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue
543219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
543319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            SelectionDAG &DAG) const {
543419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = SVOp->getOperand(0);
5436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V2 = SVOp->getOperand(1);
5437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = SVOp->getDebugLoc();
5438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> MaskVals;
5439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Determine if more than 1 of the words in each of the low and high quadwords
5441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of the result come from the same quadword of one of the two inputs.  Undef
5442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // mask values count as coming from any quadword, for better codegen.
5443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<unsigned, 4> LoQuad(4);
5444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<unsigned, 4> HiQuad(4);
5445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitVector InputQuads(4);
5446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < 8; ++i) {
5447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
5448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int EltIdx = SVOp->getMaskElt(i);
5449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MaskVals.push_back(EltIdx);
5450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EltIdx < 0) {
5451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Quad[0];
5452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Quad[1];
5453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Quad[2];
5454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Quad[3];
5455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ++Quad[EltIdx / 4];
5458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InputQuads.set(EltIdx / 4);
5459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int BestLoQuad = -1;
5462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned MaxQuad = 1;
5463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < 4; ++i) {
5464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LoQuad[i] > MaxQuad) {
5465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestLoQuad = i;
5466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaxQuad = LoQuad[i];
5467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int BestHiQuad = -1;
5471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MaxQuad = 1;
5472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < 4; ++i) {
5473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (HiQuad[i] > MaxQuad) {
5474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestHiQuad = i;
5475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaxQuad = HiQuad[i];
5476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For SSSE3, If all 8 words of the result come from only 1 quadword of each
5480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of the two input vectors, shuffle them into one input vector so only a
5481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // single pshufb instruction is necessary. If There are more than 2 input
5482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // quads, disable the next transformation since it does not help SSSE3.
5483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool V1Used = InputQuads[0] || InputQuads[1];
5484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool V2Used = InputQuads[2] || InputQuads[3];
548519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
5486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (InputQuads.count() == 2 && V1Used && V2Used) {
5487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestLoQuad = InputQuads.find_first();
5488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestHiQuad = InputQuads.find_next(BestLoQuad);
5489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (InputQuads.count() > 2) {
5491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestLoQuad = -1;
5492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestHiQuad = -1;
5493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the shuffle mask.  If a quad is scored as -1, that means that it contains
5498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // words from all 4 input quadwords.
5499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue NewV;
5500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BestLoQuad >= 0 || BestHiQuad >= 0) {
5501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<int, 8> MaskV;
5502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
5503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
5504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
550519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
550619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
550719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
5508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // source words for the shuffle, to aid later transformations.
5511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool AllWordsInNewV = true;
5512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool InOrder[2] = { true, true };
5513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 8; ++i) {
5514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int idx = MaskVals[i];
5515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (idx != (int)i)
5516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InOrder[i/4] = false;
5517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
5518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
5519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AllWordsInNewV = false;
5520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
5521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AllWordsInNewV) {
5525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (int i = 0; i != 8; ++i) {
5526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        int idx = MaskVals[i];
5527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (idx < 0)
5528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
5529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
5530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if ((idx != i) && idx < 4)
5531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          pshufhw = false;
5532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if ((idx != i) && idx > 3)
5533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          pshuflw = false;
5534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V1 = NewV;
5536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V2Used = false;
5537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestLoQuad = 0;
5538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BestHiQuad = 1;
5539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we've eliminated the use of V2, and the new mask is a pshuflw or
5542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // pshufhw, that's as cheap as it gets.  Return the new shuffle.
5543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
554419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
554519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned TargetMask = 0;
554619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
5547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
554819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
554919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             X86::getShufflePSHUFLWImmediate(NewV.getNode());
555019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      V1 = NewV.getOperand(0);
555119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
5552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we have SSSE3, and all words of the result are from 1 input vector,
5556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // case 2 is generated, otherwise case 3 is generated.  If no SSSE3
5557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // is present, fall back to case 4.
555819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
5559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<SDValue,16> pshufbMask;
5560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we have elements from both input vectors, set the high bit of the
5562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // shuffle mask element to zero out elements that come from V2 in the V1
5563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // mask, and elements that come from V1 in the V2 mask, so that the two
5564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // results can be OR'd together.
5565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool TwoInputs = V1Used && V2Used;
5566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 8; ++i) {
5567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int EltIdx = MaskVals[i] * 2;
5568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (TwoInputs && (EltIdx >= 16)) {
5569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
5572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pshufbMask.push_back(DAG.getConstant(EltIdx,   MVT::i8));
5574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
5575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
557619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
5577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
5578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getNode(ISD::BUILD_VECTOR, dl,
5579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 MVT::v16i8, &pshufbMask[0], 16));
5580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!TwoInputs)
558119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Calculate the shuffle mask for the second input, shuffle it, and
5584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // OR it with the first shuffled input.
5585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    pshufbMask.clear();
5586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 8; ++i) {
5587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int EltIdx = MaskVals[i] * 2;
5588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (EltIdx < 16) {
5589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
5592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
5595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
559619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
5597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
5598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getNode(ISD::BUILD_VECTOR, dl,
5599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 MVT::v16i8, &pshufbMask[0], 16));
5600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
560119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // and update MaskVals with new element order.
5606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitVector InOrder(8);
5607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BestLoQuad >= 0) {
5608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<int, 8> MaskV;
5609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (int i = 0; i != 4; ++i) {
5610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int idx = MaskVals[i];
5611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (idx < 0) {
5612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskV.push_back(-1);
5613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InOrder.set(i);
5614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if ((idx / 4) == BestLoQuad) {
5615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskV.push_back(idx & 3);
5616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InOrder.set(i);
5617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
5618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskV.push_back(-1);
5619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 4; i != 8; ++i)
5622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskV.push_back(i);
5623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
5624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                &MaskV[0]);
562519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
562619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
562719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
562819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
562919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               NewV.getOperand(0),
563019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               X86::getShufflePSHUFLWImmediate(NewV.getNode()),
563119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               DAG);
5632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // and update MaskVals with the new element order.
5636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BestHiQuad >= 0) {
5637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<int, 8> MaskV;
5638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 4; ++i)
5639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskV.push_back(i);
5640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 4; i != 8; ++i) {
5641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int idx = MaskVals[i];
5642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (idx < 0) {
5643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskV.push_back(-1);
5644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InOrder.set(i);
5645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if ((idx / 4) == BestHiQuad) {
5646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskV.push_back((idx & 3) + 4);
5647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InOrder.set(i);
5648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
5649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MaskV.push_back(-1);
5650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
5653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                &MaskV[0]);
565419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
565519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE &&
565619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (Subtarget->hasSSSE3() || Subtarget->hasAVX()))
565719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
565819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              NewV.getOperand(0),
565919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              X86::getShufflePSHUFHWImmediate(NewV.getNode()),
566019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG);
5661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In case BestHi & BestLo were both -1, which means each quadword has a word
5664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // from each of the four input quadwords, calculate the InOrder bitvector now
5665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // before falling through to the insert/extract cleanup.
5666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BestLoQuad == -1 && BestHiQuad == -1) {
5667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewV = V1;
5668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (int i = 0; i != 8; ++i)
5669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MaskVals[i] < 0 || MaskVals[i] == i)
5670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InOrder.set(i);
5671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The other elements are put in the right place using pextrw and pinsrw.
5674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != 8; ++i) {
5675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (InOrder[i])
5676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int EltIdx = MaskVals[i];
5678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EltIdx < 0)
5679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue ExtOp = (EltIdx < 8)
5681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  DAG.getIntPtrConstant(EltIdx))
5683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
5684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  DAG.getIntPtrConstant(EltIdx - 8));
5685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
5686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getIntPtrConstant(i));
5687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return NewV;
5689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// v16i8 shuffles - Prefer shuffles in the following order:
5692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1. [ssse3] 1 x pshufb
5693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 2. [ssse3] 2 x pshufb + 1 x por
5694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3. [all]   v8i16 shuffle + N x pextrw + rotate + pinsrw
5695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic
5696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
5697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 SelectionDAG &DAG,
5698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 const X86TargetLowering &TLI) {
5699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = SVOp->getOperand(0);
5700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V2 = SVOp->getOperand(1);
5701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = SVOp->getDebugLoc();
5702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 16> MaskVals;
5703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SVOp->getMask(MaskVals);
5704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we have SSSE3, case 1 is generated when all result bytes come from
5706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // one of  the inputs.  Otherwise, case 2 is generated.  If no SSSE3 is
5707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // present, fall back to case 3.
5708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: kill V2Only once shuffles are canonizalized by getNode.
5709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool V1Only = true;
5710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool V2Only = true;
5711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < 16; ++i) {
5712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int EltIdx = MaskVals[i];
5713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EltIdx < 0)
5714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (EltIdx < 16)
5716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V2Only = false;
5717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
5718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V1Only = false;
5719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
572219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (TLI.getSubtarget()->hasSSSE3() || TLI.getSubtarget()->hasAVX()) {
5723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<SDValue,16> pshufbMask;
5724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all result elements are from one input vector, then only translate
5726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // undef mask values to 0x80 (zero out result) in the pshufb mask.
5727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //
5728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, we have elements from both input vectors, and must zero out
5729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // elements that come from V2 in the first mask, and V1 in the second mask
5730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // so that we can OR them together.
5731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool TwoInputs = !(V1Only || V2Only);
5732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 16; ++i) {
5733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int EltIdx = MaskVals[i];
5734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
5735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
5737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
5739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If all the elements are from V2, assign it to V1 and return after
5741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // building the first pshufb.
5742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V2Only)
5743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V1 = V2;
5744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
5745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getNode(ISD::BUILD_VECTOR, dl,
5746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 MVT::v16i8, &pshufbMask[0], 16));
5747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!TwoInputs)
5748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return V1;
5749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Calculate the shuffle mask for the second input, shuffle it, and
5751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // OR it with the first shuffled input.
5752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    pshufbMask.clear();
5753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 16; ++i) {
5754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int EltIdx = MaskVals[i];
5755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (EltIdx < 16) {
5756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
5757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
5758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
5760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
5762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getNode(ISD::BUILD_VECTOR, dl,
5763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 MVT::v16i8, &pshufbMask[0], 16));
5764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
5765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // No SSSE3 - Calculate in place words and then fix all out of place words
5768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // With 0-16 extracts & inserts.  Worst case is 16 bytes out of order from
5769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the 16 different words that comprise the two doublequadword input vectors.
577019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
577119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
5772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue NewV = V2Only ? V2 : V1;
5773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 0; i != 8; ++i) {
5774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Elt0 = MaskVals[i*2];
5775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Elt1 = MaskVals[i*2+1];
5776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This word of the result is all undef, skip it.
5778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Elt0 < 0 && Elt1 < 0)
5779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This word of the result is already in the correct place, skip it.
5782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
5783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
5785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue InsElt;
5790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If Elt0 and Elt1 are defined, are consecutive, and can be load
5792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // using a single extract together, load it and store it.
5793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
5794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
5795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getIntPtrConstant(Elt1 / 2));
5796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
5797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        DAG.getIntPtrConstant(i));
5798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
5799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If Elt1 is defined, extract it from the appropriate source.  If the
5802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // source byte is not also odd, shift the extracted word left 8 bits
5803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // otherwise clear the bottom 8 bits if we need to do an or.
5804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Elt1 >= 0) {
5805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
5806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getIntPtrConstant(Elt1 / 2));
5807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((Elt1 & 1) == 0)
5808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
580919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             DAG.getConstant(8,
581019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  TLI.getShiftAmountTy(InsElt.getValueType())));
5811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (Elt0 >= 0)
5812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getConstant(0xFF00, MVT::i16));
5814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If Elt0 is defined, extract it from the appropriate source.  If the
5816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // source byte is not also even, shift the extracted word right 8 bits. If
5817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Elt1 was also defined, OR the extracted values together before
5818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // inserting them in the result.
5819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Elt0 >= 0) {
5820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
5821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((Elt0 & 1) != 0)
5823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
582419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG.getConstant(8,
582519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 TLI.getShiftAmountTy(InsElt0.getValueType())));
5826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (Elt1 >= 0)
5827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
5828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getConstant(0x00FF, MVT::i16));
5829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
5830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         : InsElt0;
5831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
5833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getIntPtrConstant(i));
5834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
583519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
5836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
583919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
5840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// done when every pair / quad of shuffle mask elements point to elements in
5841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the right sequence. e.g.
584219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
5843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic
5844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
584519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 SelectionDAG &DAG, DebugLoc dl) {
5846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = SVOp->getValueType(0);
5847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = SVOp->getOperand(0);
5848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V2 = SVOp->getOperand(1);
5849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
5850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NewWidth = (NumElems == 4) ? 2 : 4;
585119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT NewVT;
5852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (VT.getSimpleVT().SimpleTy) {
5853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: assert(false && "Unexpected!");
5854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v4f32: NewVT = MVT::v2f64; break;
5855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v4i32: NewVT = MVT::v2i64; break;
5856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v8i16: NewVT = MVT::v4i32; break;
5857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v16i8: NewVT = MVT::v4i32; break;
5858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Scale = NumElems / NewWidth;
5861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> MaskVec;
5862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < NumElems; i += Scale) {
5863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int StartIdx = -1;
5864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (int j = 0; j < Scale; ++j) {
5865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int EltIdx = SVOp->getMaskElt(i+j);
5866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (EltIdx < 0)
5867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
5868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (StartIdx == -1)
5869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        StartIdx = EltIdx - (EltIdx % Scale);
5870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (EltIdx != StartIdx + j)
5871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return SDValue();
5872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (StartIdx == -1)
5874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(-1);
5875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
5876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskVec.push_back(StartIdx / Scale);
5877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
587919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
588019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
5881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
5882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getVZextMovL - Return a zero-extending vector move low node.
5885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
5886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue getVZextMovL(EVT VT, EVT OpVT,
5887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            SDValue SrcOp, SelectionDAG &DAG,
5888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            const X86Subtarget *Subtarget, DebugLoc dl) {
5889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::v2f64 || VT == MVT::v4f32) {
5890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LoadSDNode *LD = NULL;
5891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
5892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LD = dyn_cast<LoadSDNode>(SrcOp);
5893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!LD) {
5894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // movssrr and movsdrr do not clear top bits. Try to use movd, movq
5895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // instead.
5896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
589719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
5898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
589919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
5900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
5901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // PR2108
5902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
590319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return DAG.getNode(ISD::BITCAST, dl, VT,
5904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
5905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
5906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   OpVT,
5907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   SrcOp.getOperand(0)
5908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                          .getOperand(0))));
5909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
5910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
5911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
5912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT,
5914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
591519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 DAG.getNode(ISD::BITCAST, dl,
5916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             OpVT, SrcOp)));
5917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
5918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// areShuffleHalvesWithinDisjointLanes - Check whether each half of a vector
592019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// shuffle node referes to only one lane in the sources.
592119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool areShuffleHalvesWithinDisjointLanes(ShuffleVectorSDNode *SVOp) {
592219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
592319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
592419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int HalfSize = NumElems/2;
592519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<int, 16> M;
592619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SVOp->getMask(M);
592719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool MatchA = false, MatchB = false;
592819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
592919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int l = 0; l < NumElems*2; l += HalfSize) {
593019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isUndefOrInRange(M, 0, HalfSize, l, l+HalfSize)) {
593119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MatchA = true;
593219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
593319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
593419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
593519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
593619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int l = 0; l < NumElems*2; l += HalfSize) {
593719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isUndefOrInRange(M, HalfSize, HalfSize, l, l+HalfSize)) {
593819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MatchB = true;
593919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
594019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
594119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
594219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
594319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return MatchA && MatchB;
594419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
594519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
594619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
594719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// which could not be matched by any known target speficic shuffle
594819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue
594919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanLowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
595019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (areShuffleHalvesWithinDisjointLanes(SVOp)) {
595119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If each half of a vector shuffle node referes to only one lane in the
595219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // source vectors, extract each used 128-bit lane and shuffle them using
595319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // 128-bit shuffles. Then, concatenate the results. Otherwise leave
595419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // the work to the legalizer.
595519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc dl = SVOp->getDebugLoc();
595619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT VT = SVOp->getValueType(0);
595719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int NumElems = VT.getVectorNumElements();
595819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int HalfSize = NumElems/2;
595919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
596019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Extract the reference for each half
596119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FstVecExtractIdx = 0, SndVecExtractIdx = 0;
596219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FstVecOpNum = 0, SndVecOpNum = 0;
596319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = 0; i < HalfSize; ++i) {
596419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int Elt = SVOp->getMaskElt(i);
596519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (SVOp->getMaskElt(i) < 0)
596619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
596719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FstVecOpNum = Elt/NumElems;
596819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FstVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
596919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
597019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
597119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = HalfSize; i < NumElems; ++i) {
597219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int Elt = SVOp->getMaskElt(i);
597319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (SVOp->getMaskElt(i) < 0)
597419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
597519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SndVecOpNum = Elt/NumElems;
597619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SndVecExtractIdx = Elt % NumElems < HalfSize ? 0 : HalfSize;
597719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
597819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
597919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
598019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Extract the subvectors
598119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V1 = Extract128BitVector(SVOp->getOperand(FstVecOpNum),
598219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      DAG.getConstant(FstVecExtractIdx, MVT::i32), DAG, dl);
598319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V2 = Extract128BitVector(SVOp->getOperand(SndVecOpNum),
598419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      DAG.getConstant(SndVecExtractIdx, MVT::i32), DAG, dl);
598519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
598619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Generate 128-bit shuffles
598719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<int, 16> MaskV1, MaskV2;
598819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = 0; i < HalfSize; ++i) {
598919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int Elt = SVOp->getMaskElt(i);
599019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MaskV1.push_back(Elt < 0 ? Elt : Elt % HalfSize);
599119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
599219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = HalfSize; i < NumElems; ++i) {
599319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int Elt = SVOp->getMaskElt(i);
599419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MaskV2.push_back(Elt < 0 ? Elt : Elt % HalfSize);
599519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
599619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
599719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT NVT = V1.getValueType();
599819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &MaskV1[0]);
599919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V2 = DAG.getVectorShuffle(NVT, dl, V2, DAG.getUNDEF(NVT), &MaskV2[0]);
600019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
600119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Concatenate the result back
600219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT), V1,
600319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   DAG.getConstant(0, MVT::i32), DAG, dl);
600419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Insert128BitVector(V, V2, DAG.getConstant(NumElems/2, MVT::i32),
600519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG, dl);
600619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
600719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
600819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
600919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
601019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
601119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
601219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// 4 elements, and match them with several different shuffle types.
6013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue
601419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanLowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
6015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = SVOp->getOperand(0);
6016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V2 = SVOp->getOperand(1);
6017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = SVOp->getDebugLoc();
6018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = SVOp->getValueType(0);
6019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
602019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT.getSizeInBits() == 128 && "Unsupported vector size");
602119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<std::pair<int, int>, 8> Locs;
6023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Locs.resize(4);
6024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> Mask1(4U, -1);
6025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> PermMask;
6026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SVOp->getMask(PermMask);
6027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumHi = 0;
6029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumLo = 0;
6030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != 4; ++i) {
6031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Idx = PermMask[i];
6032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx < 0) {
6033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Locs[i] = std::make_pair(-1, -1);
6034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
6035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Idx < 4) {
6037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Locs[i] = std::make_pair(0, NumLo);
6038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Mask1[NumLo] = Idx;
6039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NumLo++;
6040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
6041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Locs[i] = std::make_pair(1, NumHi);
6042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (2+NumHi < 4)
6043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Mask1[2+NumHi] = Idx;
6044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NumHi++;
6045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
6046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumLo <= 2 && NumHi <= 2) {
6050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If no more than two elements come from either vector. This can be
6051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // implemented with two shuffles. First shuffle gather the elements.
6052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // The second shuffle, which takes the first shuffle as both of its
6053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // vector operands, put the elements into the right order.
6054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
6055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<int, 8> Mask2(4U, -1);
6057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != 4; ++i) {
6059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Locs[i].first == -1)
6060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
6061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else {
6062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned Idx = (i < 2) ? 0 : 4;
6063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Idx += Locs[i].first * 2 + Locs[i].second;
6064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Mask2[i] = Idx;
6065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
6066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
6069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (NumLo == 3 || NumHi == 3) {
6070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, we must have three elements from one vector, call it X, and
6071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // one element from the other, call it Y.  First, use a shufps to build an
6072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // intermediate vector with the one element from Y and the element from X
6073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // that will be in the same half in the final destination (the indexes don't
6074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // matter). Then, use a shufps to build the final vector, taking the half
6075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // containing the element from Y from the intermediate, and the other half
6076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // from X.
6077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NumHi == 3) {
6078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Normalize it so the 3 elements come from V1.
6079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CommuteVectorShuffleMask(PermMask, VT);
6080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::swap(V1, V2);
6081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Find the element from V2.
6084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned HiIndex;
6085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
6086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int Val = PermMask[HiIndex];
6087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Val < 0)
6088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
6089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Val >= 4)
6090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
6091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask1[0] = PermMask[HiIndex];
6094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask1[1] = -1;
6095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask1[2] = PermMask[HiIndex^1];
6096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Mask1[3] = -1;
6097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
6098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (HiIndex >= 2) {
6100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[0] = PermMask[0];
6101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[1] = PermMask[1];
6102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[2] = HiIndex & 1 ? 6 : 4;
6103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[3] = HiIndex & 1 ? 4 : 6;
6104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
6105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
6106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[0] = HiIndex & 1 ? 2 : 0;
6107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[1] = HiIndex & 1 ? 0 : 2;
6108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[2] = PermMask[2];
6109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask1[3] = PermMask[3];
6110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Mask1[2] >= 0)
6111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Mask1[2] += 4;
6112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Mask1[3] >= 0)
6113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Mask1[3] += 4;
6114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
6115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Break it into (shuffle shuffle_hi, shuffle_lo).
6119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Locs.clear();
612019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Locs.resize(4);
6121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int,8> LoMask(4U, -1);
6122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int,8> HiMask(4U, -1);
6123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int,8> *MaskPtr = &LoMask;
6125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned MaskIdx = 0;
6126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned LoIdx = 0;
6127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned HiIdx = 2;
6128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != 4; ++i) {
6129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i == 2) {
6130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskPtr = &HiMask;
6131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskIdx = 1;
6132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LoIdx = 0;
6133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HiIdx = 2;
6134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Idx = PermMask[i];
6136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx < 0) {
6137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Locs[i] = std::make_pair(-1, -1);
6138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (Idx < 4) {
6139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Locs[i] = std::make_pair(MaskIdx, LoIdx);
6140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (*MaskPtr)[LoIdx] = Idx;
6141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LoIdx++;
6142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
6143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Locs[i] = std::make_pair(MaskIdx, HiIdx);
6144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (*MaskPtr)[HiIdx] = Idx;
6145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HiIdx++;
6146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
6151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<int, 8> MaskOps;
6152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != 4; ++i) {
6153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Locs[i].first == -1) {
6154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskOps.push_back(-1);
6155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
6156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Idx = Locs[i].first * 4 + Locs[i].second;
6157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MaskOps.push_back(Idx);
6158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
6161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
6162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
616319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool MayFoldVectorLoad(SDValue V) {
616419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
616519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
616619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
616719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
616819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MayFoldLoad(V))
616919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
617019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
617119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
617219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
617319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// FIXME: the version above should always be used. Since there's
617419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// a bug where several vector shuffles can't be folded because the
617519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// DAG is not updated during lowering and a node claims to have two
617619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// uses while it only has one, use this version, and let isel match
617719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// another instruction if the load really happens to have more than
617819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// one use. Remove this version after this bug get fixed.
617919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// rdar://8434668, PR8156
618019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool RelaxedMayFoldVectorLoad(SDValue V) {
618119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
618219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
618319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
618419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
618519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ISD::isNormalLoad(V.getNode()))
618619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
618719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
618819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
618919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
619019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
619119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// a vector extract, and if both can be later optimized into a single load.
619219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
619319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// here because otherwise a target specific shuffle node is going to be
619419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// emitted for this shuffle, and the optimization not done.
619519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// FIXME: This is probably not the best approach, but fix the problem
619619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// until the right path is decided.
619719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
619819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
619919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         const TargetLowering &TLI) {
620019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = V.getValueType();
620119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
620219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
620319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Be sure that the vector shuffle is present in a pattern like this:
620419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
620519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!V.hasOneUse())
620619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
620719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
620819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDNode *N = *V.getNode()->use_begin();
620919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
621019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
621119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
621219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue EltNo = N->getOperand(1);
621319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isa<ConstantSDNode>(EltNo))
621419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
621519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
621619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the bit convert changed the number of elements, it is unsafe
621719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to examine the mask.
621819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool HasShuffleIntoBitcast = false;
621919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.getOpcode() == ISD::BITCAST) {
622019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT SrcVT = V.getOperand(0).getValueType();
622119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
622219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
622319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
622419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    HasShuffleIntoBitcast = true;
622519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
622619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
622719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Select the input vector, guarding against out of range extract vector.
622819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
622919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
623019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
623119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
623219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
623319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Skip one more bit_convert if necessary
623419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.getOpcode() == ISD::BITCAST)
623519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
623619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
623719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ISD::isNormalLoad(V.getNode())) {
623819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Is the original load suitable?
623919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    LoadSDNode *LN0 = cast<LoadSDNode>(V);
624019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
624119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // FIXME: avoid the multi-use bug that is preventing lots of
624219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // of foldings to be detected, this is still wrong of course, but
624319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // give the temporary desired behavior, and if it happens that
624419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // the load has real more uses, during isel it will not fold, and
624519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // will generate poor code.
624619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
624719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
624819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
624919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!HasShuffleIntoBitcast)
625019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return true;
625119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
625219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If there's a bitcast before the shuffle, check if the load type and
625319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // alignment is valid.
625419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Align = LN0->getAlignment();
625519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NewAlign =
625619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TLI.getTargetData()->getABITypeAlignment(
625719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    VT.getTypeForEVT(*DAG.getContext()));
625819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
625919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
626019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
626119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
626219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
626319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
626419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
626519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
626619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
626719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
626819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
626919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
627019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Canonizalize to v2f64.
627119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
627219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT,
627319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
627419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          V1, DAG));
627519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
627619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
627719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
627819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
627919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        bool HasXMMInt) {
628019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = Op.getOperand(0);
628119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V2 = Op.getOperand(1);
628219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
628319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
628419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT != MVT::v2i64 && "unsupported shuffle type");
628519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
628619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (HasXMMInt && VT == MVT::v2f64)
628719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
628819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
628919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
629019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, VT,
629119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
629219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
629319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
629419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
629519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
629619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
629719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
629819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = Op.getOperand(0);
629919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V2 = Op.getOperand(1);
630019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
630119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
630219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
630319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "unsupported shuffle type");
630419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
630519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V2.getOpcode() == ISD::UNDEF)
630619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V2 = V1;
630719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
630819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // v4i32 or v4f32
630919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
631019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
631119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
631219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic inline unsigned getSHUFPOpcode(EVT VT) {
631319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(VT.getSimpleVT().SimpleTy) {
631419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8i32: // Use fp unit for int unpack.
631519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8f32:
631619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i32: // Use fp unit for int unpack.
631719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f32: return X86ISD::SHUFPS;
631819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i64: // Use fp unit for int unpack.
631919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f64:
632019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2i64: // Use fp unit for int unpack.
632119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2f64: return X86ISD::SHUFPD;
632219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
632319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Unknown type for shufp*");
632419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
632519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return 0;
632619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
632719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
632819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
632919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasXMMInt) {
6330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V1 = Op.getOperand(0);
6331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue V2 = Op.getOperand(1);
6332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
6333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElems = VT.getVectorNumElements();
633419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
633519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
633619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // operand of these instructions is only memory, so check if there's a
633719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
633819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // same masks.
633919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool CanFoldLoad = false;
634019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
634119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Trivial case, when V2 comes from a load.
634219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MayFoldVectorLoad(V2))
634319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CanFoldLoad = true;
634419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
634519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // When V1 is a load, it can be folded later into a store in isel, example:
634619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
634719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //    turns into:
634819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  (MOVLPSmr addr:$src1, VR128:$src2)
634919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // So, recognize this potential and also use MOVLPS or MOVLPD
635019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
635119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CanFoldLoad = true;
635219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
635319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Both of them can't be memory operations though.
635419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MayFoldVectorLoad(V1) && MayFoldVectorLoad(V2))
635519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CanFoldLoad = false;
635619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
635719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CanFoldLoad) {
635819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (HasXMMInt && NumElems == 2)
635919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
636019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
636119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NumElems == 4)
636219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
636319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
636419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
636519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
636619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // movl and movlp will both match v2i64, but v2i64 is never matched by
636719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // movl earlier because we make it strict to avoid messing with the movlp load
636819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // folding logic (see the code above getMOVLP call). Match it here then,
636919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // this is horrible, but will stay like this until we move all shuffle
637019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // matching to x86 specific nodes. Note that for the 1st condition all
637119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // types are matched with movsd.
637219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (HasXMMInt) {
637319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // FIXME: isMOVLMask should be checked and matched before getMOVLP,
637419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // as to remove this logic from here, as much as possible
637519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NumElems == 2 || !X86::isMOVLMask(SVOp))
637619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
637719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
637819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
637919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
638019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT != MVT::v4i32 && "unsupported shuffle type");
638119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
638219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Invert the operand order and use SHUFPS to match it.
638319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V2, V1,
638419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              X86::getShuffleSHUFImmediate(SVOp), DAG);
638519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
638619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
638719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic inline unsigned getUNPCKLOpcode(EVT VT) {
638819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(VT.getSimpleVT().SimpleTy) {
638919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i32: return X86ISD::PUNPCKLDQ;
639019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
639119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f32: return X86ISD::UNPCKLPS;
639219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2f64: return X86ISD::UNPCKLPD;
639319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8i32: // Use fp unit for int unpack.
639419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8f32: return X86ISD::VUNPCKLPSY;
639519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i64: // Use fp unit for int unpack.
639619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f64: return X86ISD::VUNPCKLPDY;
639719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v16i8: return X86ISD::PUNPCKLBW;
639819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8i16: return X86ISD::PUNPCKLWD;
639919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
640019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Unknown type for unpckl");
640119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
640219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return 0;
640319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
640419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
640519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic inline unsigned getUNPCKHOpcode(EVT VT) {
640619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(VT.getSimpleVT().SimpleTy) {
640719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i32: return X86ISD::PUNPCKHDQ;
640819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
640919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f32: return X86ISD::UNPCKHPS;
641019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2f64: return X86ISD::UNPCKHPD;
641119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8i32: // Use fp unit for int unpack.
641219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8f32: return X86ISD::VUNPCKHPSY;
641319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i64: // Use fp unit for int unpack.
641419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f64: return X86ISD::VUNPCKHPDY;
641519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v16i8: return X86ISD::PUNPCKHBW;
641619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8i16: return X86ISD::PUNPCKHWD;
641719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
641819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Unknown type for unpckh");
641919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
642019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return 0;
642119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
642219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
642319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic inline unsigned getVPERMILOpcode(EVT VT) {
642419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch(VT.getSimpleVT().SimpleTy) {
642519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i32:
642619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f32: return X86ISD::VPERMILPS;
642719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2i64:
642819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v2f64: return X86ISD::VPERMILPD;
642919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8i32:
643019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v8f32: return X86ISD::VPERMILPSY;
643119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4i64:
643219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MVT::v4f64: return X86ISD::VPERMILPDY;
643319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
643419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Unknown type for vpermil");
643519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
643619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return 0;
643719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
643819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
643919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isVectorBroadcast - Check if the node chain is suitable to be xformed to
644019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// a vbroadcast node. The nodes are suitable whenever we can fold a load coming
644119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// from a 32 or 64 bit scalar. Update Op to the desired load to be folded.
644219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isVectorBroadcast(SDValue &Op) {
644319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
644419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool Is256 = VT.getSizeInBits() == 256;
644519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
644619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((VT.getSizeInBits() == 128 || Is256) &&
644719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported type for vbroadcast node");
644819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
644919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V = Op;
645019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
645119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(0);
645219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
645319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Is256 && !(V.hasOneUse() &&
645419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 V.getOpcode() == ISD::INSERT_SUBVECTOR &&
645519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 V.getOperand(0).getOpcode() == ISD::UNDEF))
645619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
645719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
645819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Is256)
645919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = V.getOperand(1);
646019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
646119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!V.hasOneUse())
646219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
646319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
646419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check the source scalar_to_vector type. 256-bit broadcasts are
646519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // supported for 32/64-bit sizes, while 128-bit ones are only supported
646619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // for 32-bit scalars.
646719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V.getOpcode() != ISD::SCALAR_TO_VECTOR)
646819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
646919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
647019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ScalarSize = V.getOperand(0).getValueType().getSizeInBits();
647119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ScalarSize != 32 && ScalarSize != 64)
647219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
647319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Is256 && ScalarSize == 64)
647419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
647519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
647619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  V = V.getOperand(0);
647719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!MayFoldLoad(V))
647819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
647919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
648019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Return the load node
648119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Op = V;
648219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
648319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
648419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
648519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic
648619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
648719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               const TargetLowering &TLI,
648819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               const X86Subtarget *Subtarget) {
648919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
649019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
649119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
649219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = Op.getOperand(0);
649319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V2 = Op.getOperand(1);
6494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isZeroShuffle(SVOp))
649619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getZeroVector(VT, Subtarget->hasXMMInt(), DAG, dl);
6497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
649819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle splat operations
6499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SVOp->isSplat()) {
650019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumElem = VT.getVectorNumElements();
650119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int Size = VT.getSizeInBits();
650219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Special case, this is the only place now where it's allowed to return
650319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // a vector_shuffle operation without using a target specific node, because
650419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // *hopefully* it will be optimized away by the dag combiner. FIXME: should
650519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // this be moved to DAGCombine instead?
650619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NumElem <= 4 && CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
6507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Op;
650819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
650919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Use vbroadcast whenever the splat comes from a foldable load
651019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->hasAVX() && isVectorBroadcast(V1))
651119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(X86ISD::VBROADCAST, dl, VT, V1);
651219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
651319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Handle splats by matching through known shuffle masks
651419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((Size == 128 && NumElem <= 4) ||
651519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (Size == 256 && NumElem < 8))
651619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
651719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
651819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // All remaning splats are promoted to target supported vector shuffles.
651919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return PromoteSplat(SVOp, DAG);
6520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the shuffle can be profitably rewritten as a narrower shuffle, then
6523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // do it!
6524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::v8i16 || VT == MVT::v16i8) {
652519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewOp.getNode())
652719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
652819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if ((VT == MVT::v4i32 ||
652919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             (VT == MVT::v4f32 && Subtarget->hasXMMInt()))) {
6530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Figure out a cleaner way to do this.
6531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Try to make use of movq to zero out the top part.
6532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ISD::isBuildVectorAllZeros(V2.getNode())) {
653319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NewOp.getNode()) {
6535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
6536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
6537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG, Subtarget, dl);
6538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
6539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
654019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
6542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
6543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            DAG, Subtarget, dl);
6544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
654619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
654719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
654819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
654919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue
655019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
655119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
655219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = Op.getOperand(0);
655319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V2 = Op.getOperand(1);
655419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
655519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
655619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NumElems = VT.getVectorNumElements();
655719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool isMMX = VT.getSizeInBits() == 64;
655819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
655919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
656019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool V1IsSplat = false;
656119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool V2IsSplat = false;
656219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool HasXMMInt = Subtarget->hasXMMInt();
656319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFunction &MF = DAG.getMachineFunction();
656419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
6565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
656619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Shuffle operations on MMX not supported.
656719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isMMX)
6568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
6569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
657019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Vector shuffle lowering takes 3 steps:
657119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
657219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
657319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //    narrowing and commutation of operands should be handled.
657419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 2) Matching of shuffles with known shuffle masks to x86 target specific
657519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //    shuffle nodes.
657619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 3) Rewriting of unmatched masks into new generic shuffle operations,
657719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //    so the shuffle can be broken into other shuffles and the legalizer can
657819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //    try the lowering again.
657919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
658019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The general ideia is that no vector_shuffle operation should be left to
658119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // be matched during isel, all of them must be converted to a target specific
658219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // node here.
658319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
658419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Normalize the input vectors. Here splats, zeroed vectors, profitable
658519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // narrowing and commutation of operands should be handled. The actual code
658619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // doesn't include all of those, work in progress...
658719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
658819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NewOp.getNode())
658919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return NewOp;
659019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
659119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
659219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // unpckh_undef). Only use pshufd if speed is more important than size.
659319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
659419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
659519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
659619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
659719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
659819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVDDUPMask(SVOp) &&
659919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
660019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      V2IsUndef && RelaxedMayFoldVectorLoad(V1))
660119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getMOVDDup(Op, dl, V1, DAG);
660219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
660319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVHLPS_v_undef_Mask(SVOp))
660419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getMOVHighToLow(Op, dl, DAG);
660519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
660619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Use to match splats
660719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (HasXMMInt && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
660819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (VT == MVT::v2f64 || VT == MVT::v2i64))
660919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
661019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
661119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isPSHUFDMask(SVOp)) {
661219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // The actual implementation will match the mask in the if above and then
661319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // during isel it can match several different instructions, not only pshufd
661419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // as its name says, sad but true, emulate the behavior for now...
661519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
661619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
661719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
661819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
661919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
662019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (HasXMMInt && (VT == MVT::v4f32 || VT == MVT::v4i32))
662119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
662219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
662319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V1,
662419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                TargetMask, DAG);
662519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
662619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check if this can be converted into a logical shift.
6628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLeft = false;
6629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned ShAmt = 0;
6630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShVal;
663119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool isShift = getSubtarget()->hasXMMInt() &&
663219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
6633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isShift && ShVal.hasOneUse()) {
6634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the shifted value has multiple uses, it may be cheaper to use
6635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // v_set0 + movlhps or movhlps, etc.
6636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT EltVT = VT.getVectorElementType();
6637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShAmt *= EltVT.getSizeInBits();
6638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
6639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (X86::isMOVLMask(SVOp)) {
6642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V1IsUndef)
6643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return V2;
6644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ISD::isBuildVectorAllZeros(V1.getNode()))
6645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
664619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!X86::isMOVLPMask(SVOp)) {
664719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (HasXMMInt && (VT == MVT::v2i64 || VT == MVT::v2f64))
664819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
664919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
665019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v4i32 || VT == MVT::v4f32)
665119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
665219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
6653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: fold these into legal mask.
665619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
665719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getMOVLowToHigh(Op, dl, DAG, HasXMMInt);
665819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
665919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVHLPSMask(SVOp))
666019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getMOVHighToLow(Op, dl, DAG);
666119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
666219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVSHDUPMask(SVOp, Subtarget))
666319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
666419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
666519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVSLDUPMask(SVOp, Subtarget))
666619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
666719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
666819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isMOVLPMask(SVOp))
666919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getMOVLP(Op, dl, DAG, HasXMMInt);
6670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShouldXformToMOVHLPS(SVOp) ||
6672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
6673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return CommuteVectorShuffle(SVOp, DAG);
6674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isShift) {
6676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // No better options. Use a vshl / vsrl.
6677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT EltVT = VT.getVectorElementType();
6678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShAmt *= EltVT.getSizeInBits();
6679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
6680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Commuted = false;
6683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: This should also accept a bitcast of a splat?  Be careful, not
6684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 1,1,1,1 -> v8i16 though.
6685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  V1IsSplat = isSplatVector(V1.getNode());
6686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  V2IsSplat = isSplatVector(V2.getNode());
6687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Canonicalize the splat or undef, if present, to be on the RHS.
6689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
6690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = CommuteVectorShuffle(SVOp, DAG);
6691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SVOp = cast<ShuffleVectorSDNode>(Op);
6692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V1 = SVOp->getOperand(0);
6693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V2 = SVOp->getOperand(1);
6694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(V1IsSplat, V2IsSplat);
6695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(V1IsUndef, V2IsUndef);
6696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Commuted = true;
6697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
6700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Shuffling low element of v1 into undef, just return v1.
6701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V2IsUndef)
6702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return V1;
6703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the instruction selector will not match, so get a canonical MOVL with
6705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // swapped operands to undo the commute.
6706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getMOVL(DAG, dl, VT, V2, V1);
6707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
670919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isUNPCKLMask(SVOp))
671019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
671119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
671219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isUNPCKHMask(SVOp))
671319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
6714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (V2IsSplat) {
6716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Normalize mask so all entries that point to V2 points to its first
6717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // element then try to match unpck{h|l} again. If match, return a
6718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // new vector_shuffle with the corrected mask.
6719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewMask = NormalizeMask(SVOp, DAG);
6720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
6721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NSVOp != SVOp) {
6722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (X86::isUNPCKLMask(NSVOp, true)) {
6723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return NewMask;
6724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (X86::isUNPCKHMask(NSVOp, true)) {
6725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return NewMask;
6726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
6727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
6728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Commuted) {
6731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Commute is back and try unpck* again.
6732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: this seems wrong.
6733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
6734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
6735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
673619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (X86::isUNPCKLMask(NewSVOp))
673719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
673819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
673919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (X86::isUNPCKHMask(NewSVOp))
674019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
674119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
6742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Normalize the node to match x86 shuffle ops if needed
674419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
6745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return CommuteVectorShuffle(SVOp, DAG);
6746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
674719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The checks below are all present in isShuffleMaskLegal, but they are
674819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // inlined here right now to enable us to directly emit target specific
674919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // nodes, and remove one by one until they don't return Op anymore.
675019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<int, 16> M;
675119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SVOp->getMask(M);
675219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
675319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()))
675419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
675519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                X86::getShufflePALIGNRImmediate(SVOp),
675619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG);
675719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
675819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
675919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SVOp->getSplatIndex() == 0 && V2IsUndef) {
676019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VT == MVT::v2f64)
676119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
676219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VT == MVT::v2i64)
676319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
676419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
676519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
676619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isPSHUFHWMask(M, VT))
676719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
676819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                X86::getShufflePSHUFHWImmediate(SVOp),
676919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG);
677019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
677119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isPSHUFLWMask(M, VT))
677219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
677319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                X86::getShufflePSHUFLWImmediate(SVOp),
677419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG);
677519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
677619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isSHUFPMask(M, VT))
677719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
677819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                X86::getShuffleSHUFImmediate(SVOp), DAG);
677919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
678019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isUNPCKL_v_undef_Mask(SVOp))
678119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
678219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isUNPCKH_v_undef_Mask(SVOp))
678319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
678419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
678519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //===--------------------------------------------------------------------===//
678619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Generate target specific nodes for 128 or 256-bit shuffles only
678719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // supported in the AVX instruction set.
678819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
678919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
679019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle VMOVDDUPY permutations
679119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isMOVDDUPYMask(SVOp, Subtarget))
679219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
679319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
679419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle VPERMILPS* permutations
679519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVPERMILPSMask(M, VT, Subtarget))
679619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
679719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                getShuffleVPERMILPSImmediate(SVOp), DAG);
679819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
679919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle VPERMILPD* permutations
680019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVPERMILPDMask(M, VT, Subtarget))
680119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getVPERMILOpcode(VT), dl, VT, V1,
680219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                getShuffleVPERMILPDImmediate(SVOp), DAG);
680319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
680419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle VPERM2F128 permutations
680519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVPERM2F128Mask(M, VT, Subtarget))
680619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(X86ISD::VPERM2F128, dl, VT, V1, V2,
680719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                getShuffleVPERM2F128Immediate(SVOp), DAG);
680819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
680919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle VSHUFPSY permutations
681019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVSHUFPSYMask(M, VT, Subtarget))
681119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
681219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                getShuffleVSHUFPSYImmediate(SVOp), DAG);
681319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
681419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle VSHUFPDY permutations
681519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isVSHUFPDYMask(M, VT, Subtarget))
681619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getTargetShuffleNode(getSHUFPOpcode(VT), dl, VT, V1, V2,
681719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                getShuffleVSHUFPDYImmediate(SVOp), DAG);
681819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
681919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //===--------------------------------------------------------------------===//
682019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Since no target specific shuffle was selected for this generic one,
682119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // lower it into other known shuffles. FIXME: this isn't true yet, but
682219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // this is the plan.
682319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
6824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::v8i16) {
682719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewOp.getNode())
6829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return NewOp;
6830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::v16i8) {
6833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewOp.getNode())
6835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return NewOp;
6836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
683819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle all 128-bit wide vectors with 4 elements, and match them with
683919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // several different shuffle types.
684019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumElems == 4 && VT.getSizeInBits() == 128)
684119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
684219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
684319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Handle general 256-bit shuffles
684419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.is256BitVector())
684519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
6848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
6849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
6851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
6852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                SelectionDAG &DAG) const {
6853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
6854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
685519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
685619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op.getOperand(0).getValueType().getSizeInBits() != 128)
685719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
685819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.getSizeInBits() == 8) {
6860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
6861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    Op.getOperand(0), Op.getOperand(1));
6862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Assert  = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
6863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    DAG.getValueType(VT));
6864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
6865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (VT.getSizeInBits() == 16) {
6866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx == 0)
6869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
687119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     DAG.getNode(ISD::BITCAST, dl,
6872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MVT::v4i32,
6873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Op.getOperand(0)),
6874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     Op.getOperand(1)));
6875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
6876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    Op.getOperand(0), Op.getOperand(1));
6877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Assert  = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
6878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    DAG.getValueType(VT));
6879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
6880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (VT == MVT::f32) {
6881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the result back to FR32 register. It's only worth matching if the
6883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // result has a single use which is a store or a bitcast to i32.  And in
6884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the case of a store, it's not worth it if the index is a constant 0,
6885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // because a MOVSSmr can be used instead, which is smaller and faster.
6886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Op.hasOneUse())
6887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
6888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDNode *User = *Op.getNode()->use_begin();
6889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((User->getOpcode() != ISD::STORE ||
6890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         (isa<ConstantSDNode>(Op.getOperand(1)) &&
6891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
689219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (User->getOpcode() != ISD::BITCAST ||
6893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         User->getValueType(0) != MVT::i32))
6894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
6895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
689619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
6897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              Op.getOperand(0)),
6898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              Op.getOperand(1));
689919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
6900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (VT == MVT::i32) {
6901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // ExtractPS works with constant index.
6902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<ConstantSDNode>(Op.getOperand(1)))
6903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Op;
6904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
6906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
6907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
6910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
6912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isa<ConstantSDNode>(Op.getOperand(1)))
6913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
6914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
691519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Vec = Op.getOperand(0);
691619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VecVT = Vec.getValueType();
691719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
691819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is a 256-bit vector result, first extract the 128-bit vector and
691919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // then extract the element from the 128-bit vector.
692019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VecVT.getSizeInBits() == 256) {
692119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc dl = Op.getNode()->getDebugLoc();
692219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumElems = VecVT.getVectorNumElements();
692319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Idx = Op.getOperand(1);
692419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
692519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
692619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Get the 128-bit vector.
692719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool Upper = IdxVal >= NumElems/2;
692819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Vec = Extract128BitVector(Vec,
692919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32), DAG, dl);
693019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
693119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
693219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : Idx);
693319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
693419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
693519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Vec.getValueSizeInBits() <= 128 && "Unexpected vector length");
693619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
693719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSE41() || Subtarget->hasAVX()) {
6938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
6939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Res.getNode())
6940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Res;
6941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
6944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
6945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: handle v16i8.
6946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.getSizeInBits() == 16) {
6947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Vec = Op.getOperand(0);
6948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx == 0)
6950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
695219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     DAG.getNode(ISD::BITCAST, dl,
6953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MVT::v4i32, Vec),
6954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     Op.getOperand(1)));
6955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Transform it so it match pextrw which produces a 32-bit result.
6956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT EltVT = MVT::i32;
6957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
6958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    Op.getOperand(0), Op.getOperand(1));
6959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Assert  = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
6960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    DAG.getValueType(VT));
6961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
6962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (VT.getSizeInBits() == 32) {
6963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx == 0)
6965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Op;
6966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // SHUFPS the element to the lowest double word, then movss.
696819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
6969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT VVT = Op.getOperand(0).getValueType();
6970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
6971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DAG.getUNDEF(VVT), Mask);
6972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
6973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getIntPtrConstant(0));
6974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (VT.getSizeInBits() == 64) {
6975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
6976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
6977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //        to match extract_elt for f64.
6978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Idx == 0)
6980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Op;
6981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // UNPCKHPD the element to the lowest double word, then movsd.
6983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
6984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // to a f64mem, the whole operation is folded into a single MOVHPDmr.
6985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Mask[2] = { 1, -1 };
6986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT VVT = Op.getOperand(0).getValueType();
6987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
6988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DAG.getUNDEF(VVT), Mask);
6989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
6990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getIntPtrConstant(0));
6991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
6994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
6995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
6997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
6998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               SelectionDAG &DAG) const {
6999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
7000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltVT = VT.getVectorElementType();
7001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N0 = Op.getOperand(0);
7004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N1 = Op.getOperand(1);
7005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N2 = Op.getOperand(2);
7006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
700719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256)
700819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
700919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
7011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isa<ConstantSDNode>(N2)) {
7012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Opc;
7013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v8i16)
7014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::PINSRW;
7015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (VT == MVT::v16i8)
7016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::PINSRB;
7017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
7018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::PINSRB;
7019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // argument.
7022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N1.getValueType() != MVT::i32)
7023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N2.getValueType() != MVT::i32)
7025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
7026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(Opc, dl, VT, N0, N1, N2);
7027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
7028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Bits [7:6] of the constant are the source select.  This will always be
7029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //  zero here.  The DAG Combiner may combine an extract_elt index into these
7030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //  bits.  For example (insert (extract, 3), 2) could be matched by putting
7031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //  the '3' into bits [7:6] of X86ISD::INSERTPS.
7032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Bits [5:4] of the constant are the destination select.  This is the
7033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //  value of the incoming immediate.
7034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Bits [3:0] of the constant are the zero mask.  The DAG Combiner may
7035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //   combine either bitwise AND or insert of float 0.0 to set these bits.
7036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
7037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Create this as a scalar to vector..
7038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
7039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
7040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
7041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // PINSR* works with constant index.
7042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
7043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
7045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
7049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
7050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltVT = VT.getVectorElementType();
7051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N0 = Op.getOperand(0);
7054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N1 = Op.getOperand(1);
7055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N2 = Op.getOperand(2);
7056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
705719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is a 256-bit vector result, first extract the 128-bit vector,
705819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // insert the element into the extracted half and then place it back.
705919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256) {
706019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isa<ConstantSDNode>(N2))
706119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
706219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
706319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Get the desired 128-bit vector half.
706419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumElems = VT.getVectorNumElements();
706519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
706619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool Upper = IdxVal >= NumElems/2;
706719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ins128Idx = DAG.getConstant(Upper ? NumElems/2 : 0, MVT::i32);
706819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V = Extract128BitVector(N0, Ins128Idx, DAG, dl);
706919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
707019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert the element into the desired half.
707119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V,
707219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 N1, Upper ? DAG.getConstant(IdxVal-NumElems/2, MVT::i32) : N2);
707319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
707419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert the changed part back to the 256-bit vector
707519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Insert128BitVector(N0, V, Ins128Idx, DAG, dl);
707619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
707719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
707819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSE41() || Subtarget->hasAVX())
707919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
708019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
708119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (EltVT == MVT::i8)
708219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
708319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
7085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // as its second argument.
7087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N1.getValueType() != MVT::i32)
7088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N2.getValueType() != MVT::i32)
7090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
709119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
7092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
7094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
709819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContext *Context = DAG.getContext();
7099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
710019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT OpVT = Op.getValueType();
710119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
710219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is a 256-bit vector result, first insert into a 128-bit
710319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // vector and then insert into the 256-bit vector.
710419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (OpVT.getSizeInBits() > 128) {
710519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert into a 128-bit vector.
710619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT VT128 = EVT::getVectorVT(*Context,
710719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 OpVT.getVectorElementType(),
710819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 OpVT.getVectorNumElements() / 2);
710919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
711019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
711119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
711219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert the 128-bit vector.
711319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, OpVT), Op,
711419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG.getConstant(0, MVT::i32),
711519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              DAG, dl);
711619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
711719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getValueType() == MVT::v1i64 &&
7119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op.getOperand(0).getValueType() == MVT::i64)
7120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
7121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
712319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
712419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Expected an SSE type!");
712519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
712619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
712719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
712819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
712919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Lower a node with an EXTRACT_SUBVECTOR opcode.  This may result in
713019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// a simple subregister reference or explicit instructions to grab
713119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// upper bits of a vector.
713219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue
713319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
713419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasAVX()) {
713519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc dl = Op.getNode()->getDebugLoc();
713619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Vec = Op.getNode()->getOperand(0);
713719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Idx = Op.getNode()->getOperand(1);
713819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
713919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Op.getNode()->getValueType(0).getSizeInBits() == 128
714019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        && Vec.getNode()->getValueType(0).getSizeInBits() == 256) {
714119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return Extract128BitVector(Vec, Idx, DAG, dl);
714219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
714319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
714419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
714519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
714619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
714719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Lower a node with an INSERT_SUBVECTOR opcode.  This may result in a
714819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// simple superregister reference or explicit instructions to insert
714919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// the upper bits of a vector.
715019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue
715119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
715219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasAVX()) {
715319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc dl = Op.getNode()->getDebugLoc();
715419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Vec = Op.getNode()->getOperand(0);
715519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue SubVec = Op.getNode()->getOperand(1);
715619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Idx = Op.getNode()->getOperand(2);
715719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
715819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Op.getNode()->getValueType(0).getSizeInBits() == 256
715919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
716019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return Insert128BitVector(Vec, SubVec, Idx, DAG, dl);
716119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
7162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
716319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
7164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// one of the above mentioned nodes. It has to be wrapped because otherwise
7169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// be used to form addressing mode. These wrapped nodes will be selected
7171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// into MOV32ri.
7172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
7174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
7175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // global base reg.
7178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char OpFlag = 0;
7179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned WrapperKind = X86ISD::Wrapper;
7180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeModel::Model M = getTargetMachine().getCodeModel();
7181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleRIPRel() &&
7183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (M == CodeModel::Small || M == CodeModel::Kernel))
7184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WrapperKind = X86ISD::WrapperRIP;
7185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Subtarget->isPICStyleGOT())
7186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpFlag = X86II::MO_GOTOFF;
7187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Subtarget->isPICStyleStubPIC())
7188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpFlag = X86II::MO_PIC_BASE_OFFSET;
7189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
7191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             CP->getAlignment(),
7192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             CP->getOffset(), OpFlag);
7193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = CP->getDebugLoc();
7194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
7195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // With PIC, the address is actually $g + Offset.
7196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpFlag) {
7197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(X86ISD::GlobalBaseReg,
7199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     DebugLoc(), getPointerTy()),
7200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Result);
7201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
7204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
7207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
7208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // global base reg.
7211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char OpFlag = 0;
7212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned WrapperKind = X86ISD::Wrapper;
7213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeModel::Model M = getTargetMachine().getCodeModel();
7214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleRIPRel() &&
7216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (M == CodeModel::Small || M == CodeModel::Kernel))
7217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WrapperKind = X86ISD::WrapperRIP;
7218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Subtarget->isPICStyleGOT())
7219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpFlag = X86II::MO_GOTOFF;
7220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Subtarget->isPICStyleStubPIC())
7221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpFlag = X86II::MO_PIC_BASE_OFFSET;
7222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          OpFlag);
7225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = JT->getDebugLoc();
7226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
7227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // With PIC, the address is actually $g + Offset.
722919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (OpFlag)
7230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(X86ISD::GlobalBaseReg,
7232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     DebugLoc(), getPointerTy()),
7233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Result);
7234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
7236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
7240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
7241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // global base reg.
7244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char OpFlag = 0;
7245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned WrapperKind = X86ISD::Wrapper;
7246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeModel::Model M = getTargetMachine().getCodeModel();
7247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleRIPRel() &&
724919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (M == CodeModel::Small || M == CodeModel::Kernel)) {
725019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
725119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OpFlag = X86II::MO_GOTPCREL;
7252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WrapperKind = X86ISD::WrapperRIP;
725319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (Subtarget->isPICStyleGOT()) {
725419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OpFlag = X86II::MO_GOT;
725519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (Subtarget->isPICStyleStubPIC()) {
725619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
725719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (Subtarget->isPICStyleStubNoDynamic()) {
725819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OpFlag = X86II::MO_DARWIN_NONLAZY;
725919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
7260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
7262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = Op.getDebugLoc();
7264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
7265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // With PIC, the address is actually $g + Offset.
7268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
7269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !Subtarget->is64Bit()) {
7270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(X86ISD::GlobalBaseReg,
7272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     DebugLoc(), getPointerTy()),
7273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Result);
7274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
727619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // For symbols that require a load from a stub to get the address, emit the
727719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // load.
727819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isGlobalStubReference(OpFlag))
727919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
728019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo::getGOT(), false, false, 0);
728119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
7283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
7287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the TargetBlockAddressAddress node.
7288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char OpFlags =
7289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Subtarget->ClassifyBlockAddressReference();
7290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeModel::Model M = getTargetMachine().getCodeModel();
7291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
7292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       /*isTarget=*/true, OpFlags);
7295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleRIPRel() &&
7297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (M == CodeModel::Small || M == CodeModel::Kernel))
7298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
7300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
7301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // With PIC, the address is actually $g + Offset.
7303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isGlobalRelativeToPICBase(OpFlags)) {
7304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Result);
7307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
7310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
7314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      int64_t Offset,
7315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      SelectionDAG &DAG) const {
7316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the TargetGlobalAddress node, folding in the constant
7317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // offset if it is legal.
7318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char OpFlags =
7319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
7320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeModel::Model M = getTargetMachine().getCodeModel();
7321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result;
7322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpFlags == X86II::MO_NO_FLAG &&
7323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86::isOffsetSuitableForCodeModel(Offset, M)) {
7324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // A direct static reference to a global.
7325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
7326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset = 0;
7327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
7328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
7329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->isPICStyleRIPRel() &&
7332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (M == CodeModel::Small || M == CodeModel::Kernel))
7333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
7335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
7336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // With PIC, the address is actually $g + Offset.
7338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isGlobalRelativeToPICBase(OpFlags)) {
7339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Result);
7342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For globals that require a load from a stub to get the address, emit the
7345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // load.
7346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isGlobalStubReference(OpFlags))
7347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
734819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo::getGOT(), false, false, 0);
7349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If there was a non-zero offset that we didn't fold, create an explicit
7351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // addition for it.
7352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Offset != 0)
7353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
7354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Offset, getPointerTy()));
7355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
7357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
7361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
7362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
7363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
7364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue
7367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
7368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
7369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           unsigned char OperandFlags) {
7370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
737119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = GA->getDebugLoc();
7373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           GA->getValueType(0),
7375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           GA->getOffset(),
7376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           OperandFlags);
7377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InFlag) {
7378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[] = { Chain,  TGA, *InFlag };
7379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
7380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
7381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[]  = { Chain, TGA };
7382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
7383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
7386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MFI->setAdjustsStack(true);
7387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Flag = Chain.getValue(1);
7389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
7390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
7393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue
7394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanLowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
7395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                const EVT PtrVT) {
7396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue InFlag;
7397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = GA->getDebugLoc();  // ? function entry point might be better
7398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     DAG.getNode(X86ISD::GlobalBaseReg,
7400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 DebugLoc(), PtrVT), InFlag);
7401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InFlag = Chain.getValue(1);
7402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
7404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
7407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue
7408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanLowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
7409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                const EVT PtrVT) {
7410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    X86::RAX, X86II::MO_TLSGD);
7412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
7415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// "local exec" model.
7416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
7417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const EVT PtrVT, TLSModel::Model model,
7418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   bool is64Bit) {
7419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = GA->getDebugLoc();
7420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
742119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
742219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
742319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                         is64Bit ? 257 : 256));
742419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
742519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
742619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      DAG.getIntPtrConstant(0),
742719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      MachinePointerInfo(Ptr), false, false, 0);
7428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char OperandFlags = 0;
7430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Most TLS accesses are not RIP relative, even on x86-64.  One exception is
7431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // initialexec.
7432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned WrapperKind = X86ISD::Wrapper;
7433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (model == TLSModel::LocalExec) {
7434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
7435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (is64Bit) {
7436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(model == TLSModel::InitialExec);
7437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandFlags = X86II::MO_GOTTPOFF;
7438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WrapperKind = X86ISD::WrapperRIP;
7439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
7440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(model == TLSModel::InitialExec);
7441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandFlags = X86II::MO_INDNTPOFF;
7442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
7445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // exec)
744619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           GA->getValueType(0),
7448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           GA->getOffset(), OperandFlags);
7449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (model == TLSModel::InitialExec)
7452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
745319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo::getGOT(), false, false, 0);
7454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The address of the thread local variable is the add of the thread
7456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // pointer with the offset of the variable.
7457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
7458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
7461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
746219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
7464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const GlobalValue *GV = GA->getGlobal();
7465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
746619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetELF()) {
746719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // TODO: implement the "local dynamic" model
746819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // TODO: implement the "initial exec"model for pic executables
746919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
747019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If GV is an alias then use the aliasee for determining
747119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // thread-localness.
747219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
747319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      GV = GA->resolveAliasedGlobal(false);
747419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
747519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TLSModel::Model model
747619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      = getTLSModel(GV, getTargetMachine().getRelocationModel());
747719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
747819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch (model) {
747919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      case TLSModel::GeneralDynamic:
748019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      case TLSModel::LocalDynamic: // not implemented
748119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Subtarget->is64Bit())
748219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
748319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
748419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
748519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      case TLSModel::InitialExec:
748619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      case TLSModel::LocalExec:
748719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
748819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   Subtarget->is64Bit());
748919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
749019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (Subtarget->isTargetDarwin()) {
7491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Darwin only has one model of TLS.  Lower to that.
7492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned char OpFlag = 0;
7493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           X86ISD::WrapperRIP : X86ISD::Wrapper;
749519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // global base reg.
7498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  !Subtarget->is64Bit();
7500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PIC32)
7501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpFlag = X86II::MO_TLVP_PIC_BASE;
7502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
7503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpFlag = X86II::MO_TLVP;
750419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc DL = Op.getDebugLoc();
7505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
750619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                GA->getValueType(0),
7507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                GA->getOffset(), OpFlag);
7508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
750919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // With PIC32, the address is actually $g + Offset.
7511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PIC32)
7512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getNode(X86ISD::GlobalBaseReg,
7514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DebugLoc(), getPointerTy()),
7515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           Offset);
751619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Lowering the machine isd will make sure everything is in the right
7518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // location.
751919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Chain = DAG.getEntryNode();
752019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
752119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Args[] = { Chain, Offset };
752219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
752319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MFI->setAdjustsStack(true);
7527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // And our return value (tls address) is in the standard call return value
7529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // location.
7530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
753119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
753219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              Chain.getValue(1));
7533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
753419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(false &&
7536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "TLS not implemented for this target.");
7537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  llvm_unreachable("Unreachable");
7539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
7540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
754319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values and
7544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// take a 2 x i32 value to shift plus a shift amount.
754519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const {
7546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getNumOperands() == 3 && "Not a double-shift!");
7547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
7548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned VTBits = VT.getSizeInBits();
7549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
7551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShOpLo = Op.getOperand(0);
7552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShOpHi = Op.getOperand(1);
7553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShAmt  = Op.getOperand(2);
7554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
7555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     DAG.getConstant(VTBits - 1, MVT::i8))
7556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       : DAG.getConstant(0, VT);
7557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Tmp2, Tmp3;
7559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getOpcode() == ISD::SHL_PARTS) {
7560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
7562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
7563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
7565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                DAG.getConstant(VTBits, MVT::i8));
7569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
7570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             AndNode, DAG.getConstant(0, MVT::i8));
7571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Hi, Lo;
7573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
7574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
7576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getOpcode() == ISD::SHL_PARTS) {
7578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
7580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
7581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
7583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[2] = { Lo, Hi };
7586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getMergeValues(Ops, 2, dl);
7587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
7591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT SrcVT = Op.getOperand(0).getValueType();
7592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
759319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SrcVT.isVector())
7594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
7595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
7597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Unknown SINT_TO_FP to lower!");
7598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These are really Legal; return the operand so the caller accepts it as
7600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Legal.
7601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
7602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
7603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
7604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Subtarget->is64Bit()) {
7605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
7606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Size = SrcVT.getSizeInBits()/8;
7610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
7611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
7612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
7614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               StackSlot,
761519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               MachinePointerInfo::getFixedStack(SSFI),
7616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               false, false, 0);
7617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
762119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     SDValue StackSlot,
7622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     SelectionDAG &DAG) const {
7623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Build the FILD
762419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
7625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList Tys;
7626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
7627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (useSSE)
762819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
7629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
7630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
763119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
763219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ByteSize = SrcVT.getSizeInBits()/8;
763319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
763419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
763519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineMemOperand *MMO;
763619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (FI) {
763719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int SSFI = FI->getIndex();
763819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MMO =
763919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getMachineFunction()
764019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
764119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachineMemOperand::MOLoad, ByteSize, ByteSize);
764219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
764319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
764419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StackSlot = StackSlot.getOperand(1);
764519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
7646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
764719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
764819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           X86ISD::FILD, DL,
764919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           Tys, Ops, array_lengthof(Ops),
765019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           SrcVT, MMO);
7651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (useSSE) {
7653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = Result.getValue(1);
7654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue InFlag = Result.getValue(2);
7655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // shouldn't be necessary except that RFP cannot be live across
7658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // multiple blocks. When stackifier is fixed, they can be uncoupled.
7659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineFunction &MF = DAG.getMachineFunction();
766019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
766119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
7662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Tys = DAG.getVTList(MVT::Other);
7664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[] = {
7665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    };
766719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineMemOperand *MMO =
766819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getMachineFunction()
766919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
767019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachineMemOperand::MOStore, SSFISize, SSFISize);
767119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
767219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
767319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    Ops, array_lengthof(Ops),
767419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    Op.getValueType(), MMO);
767519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
767619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo::getFixedStack(SSFI),
7677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         false, false, 0);
7678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
7681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
7684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               SelectionDAG &DAG) const {
7686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This algorithm is not obvious. Here it is in C code, more or less:
7687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /*
7688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    double uint64_to_double( uint32_t hi, uint32_t lo ) {
7689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      static const __m128i exp = { 0x4330000045300000ULL, 0 };
7690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
7691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Copy ints to xmm registers.
7693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      __m128i xh = _mm_cvtsi32_si128( hi );
7694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      __m128i xl = _mm_cvtsi32_si128( lo );
7695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Combine into low half of a single xmm register.
7697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      __m128i x = _mm_unpacklo_epi32( xh, xl );
7698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      __m128d d;
7699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      double sd;
7700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Merge in appropriate exponents to give the integer bits the right
7702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // magnitude.
7703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      x = _mm_unpacklo_epi32( x, exp );
7704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Subtract away the biases to deal with the IEEE-754 double precision
7706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // implicit 1.
7707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      d = _mm_sub_pd( (__m128d) x, bias );
7708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // All conversions up to here are exact. The correctly rounded result is
7710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // calculated using the current rounding mode using the following
7711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // horizontal add.
7712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
7713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      _mm_store_sd( &sd, d );   // Because we are returning doubles in XMM, this
7714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                // store doesn't really need to be here (except
7715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                // maybe to zero the other double)
7716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return sd;
7717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
7718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  */
7719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext *Context = DAG.getContext();
7722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Build some magic constants.
7724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> CV0;
7725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
7726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
7727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
7729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C0 = ConstantVector::get(CV0);
7730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
7731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> CV1;
7733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV1.push_back(
7734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV1.push_back(
7736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
7737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C1 = ConstantVector::get(CV1);
7738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
7739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
7742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        Op.getOperand(0),
7743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        DAG.getIntPtrConstant(1)));
7744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
7745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
7746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        Op.getOperand(0),
7747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        DAG.getIntPtrConstant(0)));
7748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
7749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
775019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachinePointerInfo::getConstantPool(),
7751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              false, false, 16);
7752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
775319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
7754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
775519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachinePointerInfo::getConstantPool(),
7756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              false, false, 16);
7757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
7758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add the halves; easiest way is to swap them into another reg first.
7760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int ShufMask[2] = { 1, -1 };
7761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
7762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      DAG.getUNDEF(MVT::v2f64), ShufMask);
7763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
7764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
7765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getIntPtrConstant(0));
7766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
7769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               SelectionDAG &DAG) const {
7771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FP constant to bias correct the final result.
7773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
7774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   MVT::f64);
7775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load the 32-bit value into an XMM register.
7777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
777819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             Op.getOperand(0));
777919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
778019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Zero out the upper parts of the register.
778119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget->hasXMMInt(),
778219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     DAG);
7783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
778519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
7786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getIntPtrConstant(0));
7787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Or the load with the bias.
7789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
779019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
7791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
7792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   MVT::v2f64, Load)),
779319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
7794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
7795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   MVT::v2f64, Bias)));
7796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
779719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
7798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   DAG.getIntPtrConstant(0));
7799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Subtract the bias.
7801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
7802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Handle final rounding.
7804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT DestVT = Op.getValueType();
7805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DestVT.bitsLT(MVT::f64)) {
7807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
7808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getIntPtrConstant(0));
7809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (DestVT.bitsGT(MVT::f64)) {
7810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
7811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Handle final rounding.
7814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Sub;
7815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
7819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N0 = Op.getOperand(0);
7820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
7821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
7823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the optimization here.
7825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DAG.SignBitIsZero(N0))
7826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
7827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT SrcVT = N0.getValueType();
7829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT DstVT = Op.getValueType();
7830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
7831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return LowerUINT_TO_FP_i64(Op, DAG);
7832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
7833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return LowerUINT_TO_FP_i32(Op, DAG);
7834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Make a 64-bit buffer, and use it to build an FILD.
7836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
7837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT == MVT::i32) {
7838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue WordOff = DAG.getConstant(4, getPointerTy());
7839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     getPointerTy(), StackSlot, WordOff);
7841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
784219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  StackSlot, MachinePointerInfo(),
784319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  false, false, 0);
7844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
784519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  OffsetSlot, MachinePointerInfo(),
784619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  false, false, 0);
7847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Fild;
7849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
785319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                StackSlot, MachinePointerInfo(),
785419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               false, false, 0);
7855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For i64 source, we need to add the appropriate power of 2 if the input
7856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // was negative.  This is the same as the optimization in
7857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
7858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // we must be careful to do the computation in x87 extended precision, not
7859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // in SSE. (The generic code can't know it's OK to do this, or how to.)
786019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
786119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineMemOperand *MMO =
786219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getMachineFunction()
786319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
786419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          MachineMemOperand::MOLoad, 8, 8);
786519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
7867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
786819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
786919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         MVT::i64, MMO);
7870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt FF(32, 0x5F800000ULL);
7872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check whether the sign bit is set.
7874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
7875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
7876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 ISD::SETLT);
7877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
7879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FudgePtr = DAG.getConstantPool(
7880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             ConstantInt::get(*DAG.getContext(), FF.zext(64)),
7881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         getPointerTy());
7882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
7884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Zero = DAG.getIntPtrConstant(0);
7885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Four = DAG.getIntPtrConstant(4);
7886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
7887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Zero, Four);
7888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
7889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load the value out, extending it from f32 to f80.
7891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Avoid the extend by constructing the right constant pool?
789219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
789319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 FudgePtr, MachinePointerInfo::getConstantPool(),
789419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 MVT::f32, false, false, 4);
7895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Extend everything to 80 bits to force it to be done on x87.
7896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
7897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
7898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<SDValue,SDValue> X86TargetLowering::
7901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanFP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
790219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
7903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT DstTy = Op.getValueType();
7905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!IsSigned) {
7907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
7908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DstTy = MVT::i64;
7909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(DstTy.getSimpleVT() <= MVT::i64 &&
7912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         DstTy.getSimpleVT() >= MVT::i16 &&
7913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Unknown FP_TO_SINT to lower!");
7914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // These are really Legal.
7916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DstTy == MVT::i32 &&
7917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
7918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return std::make_pair(SDValue(), SDValue());
7919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit() &&
7920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DstTy == MVT::i64 &&
7921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
7922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return std::make_pair(SDValue(), SDValue());
7923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
7925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // stack slot.
7926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
7927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned MemSize = DstTy.getSizeInBits()/8;
7928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
7929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
793119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
793219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc;
7934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (DstTy.getSimpleVT().SimpleTy) {
7935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
7936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
7937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
7938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
7939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = DAG.getEntryNode();
7942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Value = Op.getOperand(0);
794319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT TheVT = Op.getOperand(0).getValueType();
794419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isScalarFPTypeInSSEReg(TheVT)) {
7945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
794619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getStore(Chain, DL, Value, StackSlot,
794719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo::getFixedStack(SSFI),
7948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         false, false, 0);
7949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
7950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[] = {
795119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Chain, StackSlot, DAG.getValueType(TheVT)
7952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    };
795319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
795419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineMemOperand *MMO =
795519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
795619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachineMemOperand::MOLoad, MemSize, MemSize);
795719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
795819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    DstTy, MMO);
7959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = Value.getValue(1);
7960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
7961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
7962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
796419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineMemOperand *MMO =
796519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
796619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachineMemOperand::MOStore, MemSize, MemSize);
796719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Build the FP_TO_INT*_IN_MEM
7969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = { Chain, Value, StackSlot };
797019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
797119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         Ops, 3, DstTy, MMO);
7972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return std::make_pair(FIST, StackSlot);
7974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
7977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
797819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op.getValueType().isVector())
7979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
7980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
7982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FIST = Vals.first, StackSlot = Vals.second;
7983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
7984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FIST.getNode() == 0) return Op;
7985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load the result.
7987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
798819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     FIST, StackSlot, MachinePointerInfo(), false, false, 0);
7989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
7990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
7992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
7993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
7994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FIST = Vals.first, StackSlot = Vals.second;
7995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(FIST.getNode() && "Unexpected failure");
7996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load the result.
7998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
799919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     FIST, StackSlot, MachinePointerInfo(), false, false, 0);
8000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFABS(SDValue Op,
8003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     SelectionDAG &DAG) const {
8004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext *Context = DAG.getContext();
8005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
8007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltVT = VT;
8008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.isVector())
8009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EltVT = VT.getVectorElementType();
8010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> CV;
8011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EltVT == MVT::f64) {
8012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
8016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = ConstantVector::get(CV);
8023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
802519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             MachinePointerInfo::getConstantPool(),
8026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             false, false, 16);
8027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
8028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
8031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext *Context = DAG.getContext();
8032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
8034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltVT = VT;
8035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.isVector())
8036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EltVT = VT.getVectorElementType();
8037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> CV;
8038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EltVT == MVT::f64) {
8039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
8043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(C);
8048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = ConstantVector::get(CV);
8050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
805219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             MachinePointerInfo::getConstantPool(),
8053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             false, false, 16);
8054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.isVector()) {
805519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::BITCAST, dl, VT,
8056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getNode(ISD::XOR, dl, MVT::v2i64,
805719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
8058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                Op.getOperand(0)),
805919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
8060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
8061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
8062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
8066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext *Context = DAG.getContext();
8067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op0 = Op.getOperand(0);
8068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op1 = Op.getOperand(1);
8069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
8071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT SrcVT = Op1.getValueType();
8072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If second operand is smaller, extend it first.
8074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT.bitsLT(VT)) {
8075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
8076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SrcVT = VT;
8077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // And if it is bigger, shrink it first.
8079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT.bitsGT(VT)) {
8080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
8081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SrcVT = VT;
8082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // At this point the operands and the result should have the same
8085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // type, and that won't be f80 since that is not custom lowered.
8086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First get the sign bit of second operand.
8088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> CV;
8089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT == MVT::f64) {
8090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
8092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
8093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = ConstantVector::get(CV);
8099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
810119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachinePointerInfo::getConstantPool(),
8102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              false, false, 16);
8103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
8104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Shift sign bit right or left if the two operands have different types.
8106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT.bitsGT(VT)) {
8107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Op0 is MVT::f32, Op1 is MVT::f64.
8108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getConstant(32, MVT::i32));
811119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
8112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
8113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          DAG.getIntPtrConstant(0));
8114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Clear first operand sign bit.
8117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CV.clear();
8118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::f64) {
8119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
8121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
8122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  C = ConstantVector::get(CV);
8128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
813019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachinePointerInfo::getConstantPool(),
8131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              false, false, 16);
8132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
8133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Or the value with the sign bit.
8135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
8136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
813819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
813919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue N0 = Op.getOperand(0);
814019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
814119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
814219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
814319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
814419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
814519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  DAG.getConstant(1, VT));
814619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
814719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
814819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Emit nodes that will be selected as "test Op0,Op0", or something
8150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// equivalent.
8151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
8152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    SelectionDAG &DAG) const {
8153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // CF and OF aren't always set the way we want. Determine which
8156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of these we need.
8157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NeedCF = false;
8158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NeedOF = false;
8159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (X86CC) {
8160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
8161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_A: case X86::COND_AE:
8162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_B: case X86::COND_BE:
8163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NeedCF = true;
8164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
8165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_G: case X86::COND_GE:
8166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_L: case X86::COND_LE:
8167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::COND_O: case X86::COND_NO:
8168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NeedOF = true;
8169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
8170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // See if we can use the EFLAGS value from the operand instead of
8173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // we prove that the arithmetic won't overflow, we can't use OF or CF.
8175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getResNo() != 0 || NeedOF || NeedCF)
8176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Emit a CMP with 0, which is the TEST pattern.
8177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(0, Op.getValueType()));
8179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opcode = 0;
8181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumOperands = 0;
8182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getNode()->getOpcode()) {
8183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ADD:
8184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Due to an isel shortcoming, be conservative if this add is likely to be
8185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // selected as part of a load-modify-store instruction. When the root node
8186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // in a match is a store, isel doesn't know how to remap non-chain non-flag
8187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // uses of other nodes in the match, such as the ADD in this case. This
8188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // leads to the ADD being left around and reselected, with the result being
8189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // two adds in the output.  Alas, even if none our users are stores, that
8190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // doesn't prove we're O.K.  Ergo, if we have any parents that aren't
8191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // CopyToReg or SETCC, eschew INC/DEC.  A better fix seems to require
8192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // climbing the DAG back to the root, and it doesn't seem to be worth the
8193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // effort.
8194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           UE = Op.getNode()->use_end(); UI != UE; ++UI)
8196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
8197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        goto default_case;
8198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C =
8200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
8201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // An add of one will be selected as an INC.
8202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getAPIntValue() == 1) {
8203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::INC;
8204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NumOperands = 1;
8205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
8206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // An add of negative one (subtract of one) will be selected as a DEC.
8209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getAPIntValue().isAllOnesValue()) {
8210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::DEC;
8211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NumOperands = 1;
8212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
8213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise use a regular EFLAGS-setting add.
8217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opcode = X86ISD::ADD;
8218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumOperands = 2;
8219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
8220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::AND: {
8221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the primary and result isn't used, don't bother using X86ISD::AND,
8222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // because a TEST instruction will be better.
8223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool NonFlagUse = false;
8224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDNode *User = *UI;
8227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned UOpNo = UI.getOperandNo();
8228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Look pass truncate.
8230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        UOpNo = User->use_begin().getOperandNo();
8231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        User = *User->use_begin();
8232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (User->getOpcode() != ISD::BRCOND &&
8235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          User->getOpcode() != ISD::SETCC &&
8236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
8237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NonFlagUse = true;
8238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
8239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!NonFlagUse)
8243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
8244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FALL THROUGH
8246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SUB:
8247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::OR:
8248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::XOR:
8249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Due to the ISEL shortcoming noted above, be conservative if this op is
8250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // likely to be selected as part of a load-modify-store instruction.
8251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           UE = Op.getNode()->use_end(); UI != UE; ++UI)
8253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (UI->getOpcode() == ISD::STORE)
8254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        goto default_case;
8255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise use a regular EFLAGS-setting instruction.
8257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Op.getNode()->getOpcode()) {
8258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("unexpected operator!");
8259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SUB: Opcode = X86ISD::SUB; break;
8260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::OR:  Opcode = X86ISD::OR;  break;
8261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::XOR: Opcode = X86ISD::XOR; break;
8262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::AND: Opcode = X86ISD::AND; break;
8263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NumOperands = 2;
8266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
8267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ADD:
8268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SUB:
8269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::INC:
8270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::DEC:
8271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::OR:
8272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::XOR:
8273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::AND:
8274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue(Op.getNode(), 1);
8275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
8276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default_case:
8277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
8278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Opcode == 0)
8281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Emit a CMP with 0, which is the TEST pattern.
8282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(0, Op.getValueType()));
8284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 4> Ops;
8287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != NumOperands; ++i)
8288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(Op.getOperand(i));
8289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DAG.ReplaceAllUsesWith(Op, New);
8292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue(New.getNode(), 1);
8293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// equivalent.
8297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
8298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   SelectionDAG &DAG) const {
8299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C->getAPIntValue() == 0)
8301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return EmitTest(Op0, X86CC, DAG);
8302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op0.getDebugLoc();
8304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
8305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// if it's possible.
8309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     DebugLoc dl, SelectionDAG &DAG) const {
8311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op0 = And.getOperand(0);
8312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op1 = And.getOperand(1);
8313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op0.getOpcode() == ISD::TRUNCATE)
8314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = Op0.getOperand(0);
8315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op1.getOpcode() == ISD::TRUNCATE)
8316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = Op1.getOperand(0);
8317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue LHS, RHS;
8319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op1.getOpcode() == ISD::SHL)
8320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(Op0, Op1);
8321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op0.getOpcode() == ISD::SHL) {
8322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (And00C->getZExtValue() == 1) {
8324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If we looked past a truncate, check that it's only truncating away
8325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // known zeros.
8326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned BitWidth = Op0.getValueSizeInBits();
8327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned AndBitWidth = And.getValueSizeInBits();
8328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (BitWidth > AndBitWidth) {
8329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
8330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
8331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return SDValue();
8333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
8334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        LHS = Op1;
8335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        RHS = Op0.getOperand(1);
8336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (Op1.getOpcode() == ISD::Constant) {
8338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
8339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue AndLHS = Op0;
8340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
8341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LHS = AndLHS.getOperand(0);
8342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RHS = AndLHS.getOperand(1);
8343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LHS.getNode()) {
8347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If LHS is i8, promote it to i32 with any_extend.  There is no i8 BT
8348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // instruction.  Since the shift amount is in-range-or-undefined, we know
8349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // that doing a bittest on the i32 value is ok.  We extend to i32 because
8350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the encoding for the i16 version is larger than the i32 version.
8351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Also promote i16 to i32 for performance / code size reason.
8352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LHS.getValueType() == MVT::i8 ||
8353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        LHS.getValueType() == MVT::i16)
8354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
8355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the operand types disagree, extend the shift amount to match.  Since
8357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // BT ignores high bits (like shifts) we can use anyextend.
8358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LHS.getValueType() != RHS.getValueType())
8359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
8360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Cond, MVT::i8), BT);
8365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
8368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
837119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
837219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
837319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op0 = Op.getOperand(0);
8376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op1 = Op.getOperand(1);
8377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Optimize to BT if possible.
8381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Lower (X & (1 << N)) == 0 to BT(X, N).
8382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Lower ((X >>s N) & 1) != 0 to BT(X, N).
838419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
8385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op1.getOpcode() == ISD::Constant &&
8386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      cast<ConstantSDNode>(Op1)->isNullValue() &&
8387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewSetCC.getNode())
8390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return NewSetCC;
8391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
839319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look for X == 0, X == 1, X != 0, or X != 1.  We can simplify some forms of
839419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // these.
839519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op1.getOpcode() == ISD::Constant &&
8396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
8397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       cast<ConstantSDNode>(Op1)->isNullValue()) &&
8398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (CC == ISD::SETEQ || CC == ISD::SETNE)) {
839919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
840019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If the input is a setcc, then reuse the input setcc or use a new one with
840119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // the inverted condition.
840219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Op0.getOpcode() == X86ISD::SETCC) {
840319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
840419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool Invert = (CC == ISD::SETNE) ^
840519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        cast<ConstantSDNode>(Op1)->isNullValue();
840619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!Invert) return Op0;
840719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CCode = X86::GetOppositeBranchCondition(CCode);
840919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
841019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
841119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
8412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isFP = Op1.getValueType().isFloatingPoint();
8415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
8416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (X86CC == X86::COND_INVALID)
8417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
8418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
841919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
842019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
842119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(X86CC, MVT::i8), EFLAGS);
842219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
8423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
842419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
842519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// ones, and then concatenate the result back.
842619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
842719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
8428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
842919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT.getSizeInBits() == 256 && Op.getOpcode() == ISD::SETCC &&
843019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported value type for operation");
843119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
843219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
843319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
843419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue CC = Op.getOperand(2);
843519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Idx0 = DAG.getConstant(0, MVT::i32);
843619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
843719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
843819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Extract the LHS vectors
843919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS = Op.getOperand(0);
844019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
844119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
844219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
844319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Extract the RHS vectors
844419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS = Op.getOperand(1);
844519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
844619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
844719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
844819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Issue the operation on the smaller types and concatenate the result back
844919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MVT EltVT = VT.getVectorElementType().getSimpleVT();
845019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
845119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
845219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
845319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
845619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
8458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Cond;
8459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op0 = Op.getOperand(0);
8460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op1 = Op.getOperand(1);
8461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CC = Op.getOperand(2);
8462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
8463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
8465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isFP) {
8468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned SSECC = 8;
846919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT EltVT = Op0.getValueType().getVectorElementType();
847019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(EltVT == MVT::f32 || EltVT == MVT::f64);
847119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
847219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Opc = EltVT == MVT::f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
8473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Swap = false;
8474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
847519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // SSE Condition code mapping:
847619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  0 - EQ
847719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  1 - LT
847819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  2 - LE
847919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  3 - UNORD
848019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  4 - NEQ
848119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  5 - NLT
848219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  6 - NLE
848319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  7 - ORD
8484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (SetCCOpcode) {
8485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: break;
8486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETOEQ:
8487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETEQ:  SSECC = 0; break;
8488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETOGT:
8489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETGT: Swap = true; // Fallthrough
8490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETLT:
8491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETOLT: SSECC = 1; break;
8492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETOGE:
8493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETGE: Swap = true; // Fallthrough
8494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETLE:
8495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETOLE: SSECC = 2; break;
8496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUO:  SSECC = 3; break;
8497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUNE:
8498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETNE:  SSECC = 4; break;
8499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETULE: Swap = true;
8500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUGE: SSECC = 5; break;
8501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETULT: Swap = true;
8502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETUGT: SSECC = 6; break;
8503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ISD::SETO:   SSECC = 7; break;
8504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Swap)
8506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::swap(Op0, Op1);
8507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // In the two special cases we can't handle, emit two comparisons.
8509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SSECC == 8) {
8510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SetCCOpcode == ISD::SETUEQ) {
8511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue UNORD, EQ;
8512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
8513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
8514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
8515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (SetCCOpcode == ISD::SETONE) {
8517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue ORD, NEQ;
8518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
8519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
8520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
8521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      llvm_unreachable("Illegal FP comparison");
8523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Handle all other FP comparisons here.
8525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
8526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
852819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Break 256-bit integer vector compare into smaller ones.
852919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isFP && VT.getSizeInBits() == 256)
853019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Lower256IntVSETCC(Op, DAG);
853119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We are handling one of the integer comparisons here.  Since SSE only has
8533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // GT and EQ comparisons for integer, swapping operands and multiple
8534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // operations may be required for some comparisons.
8535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
8536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Swap = false, Invert = false, FlipSigns = false;
8537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (VT.getSimpleVT().SimpleTy) {
8539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
8540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
8541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
8542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
8543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
8544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (SetCCOpcode) {
8547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
8548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETNE:  Invert = true;
8549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETEQ:  Opc = EQOpc; break;
8550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLT:  Swap = true;
8551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGT:  Opc = GTOpc; break;
8552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGE:  Swap = true;
8553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLE:  Opc = GTOpc; Invert = true; break;
8554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULT: Swap = true;
8555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
8556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGE: Swap = true;
8557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
8558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Swap)
8560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(Op0, Op1);
8561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
856219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check that the operation in question is available (most are plain SSE2,
856319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // but PCMPGTQ and PCMPEQQ have different requirements).
856419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Opc == X86ISD::PCMPGTQ && !Subtarget->hasSSE42() && !Subtarget->hasAVX())
856519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
856619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Opc == X86ISD::PCMPEQQ && !Subtarget->hasSSE41() && !Subtarget->hasAVX())
856719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
856819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Since SSE has no unsigned integer comparisons, we need to flip  the sign
8570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // bits of the inputs before performing those operations.
8571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FlipSigns) {
8572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT EltVT = VT.getVectorElementType();
8573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      EltVT);
8575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
8576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    SignBits.size());
8578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
8580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
8583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the logical-not of the result is required, perform that now.
8585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Invert)
8586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getNOT(dl, Result, VT);
8587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
8589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
8592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isX86LogicalCmp(SDValue Op) {
8593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc = Op.getNode()->getOpcode();
8594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
8595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
8596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getResNo() == 1 &&
8597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (Opc == X86ISD::ADD ||
8598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::SUB ||
859919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       Opc == X86ISD::ADC ||
860019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       Opc == X86ISD::SBB ||
8601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::SMUL ||
8602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::UMUL ||
8603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::INC ||
8604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::DEC ||
8605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::OR ||
8606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::XOR ||
8607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       Opc == X86ISD::AND))
8608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
8609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
861019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
861119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
861219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
8614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
861619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isZero(SDValue V) {
861719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
861819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return C && C->isNullValue();
861919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
862019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
862119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isAllOnes(SDValue V) {
862219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
862319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return C && C->isAllOnesValue();
862419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
862519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
8627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool addTest = true;
8628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Cond  = Op.getOperand(0);
862919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Op1 = Op.getOperand(1);
863019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Op2 = Op.getOperand(2);
863119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
8632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CC;
8633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == ISD::SETCC) {
8635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewCond = LowerSETCC(Cond, DAG);
8636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewCond.getNode())
8637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = NewCond;
8638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
864019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
864119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
864219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
864319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
8644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == X86ISD::SETCC &&
864519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
864619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      isZero(Cond.getOperand(1).getOperand(1))) {
8647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Cmp = Cond.getOperand(1);
864819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
864919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
865019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
865119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
865219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
865319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
865419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
865519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue CmpOp0 = Cmp.getOperand(0);
865619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
865719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
865819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
865919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Res =   // Res = 0 or -1.
866019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
866119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
866219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
866319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isAllOnes(Op1) != (CondCode == X86::COND_E))
866419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Res = DAG.getNOT(DL, Res, Res.getValueType());
866519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
866719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (N2C == 0 || !N2C->isNullValue())
866819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
866919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return Res;
8670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
867319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look past (and (setcc_carry (cmp ...)), 1).
8674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == ISD::AND &&
8675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
867719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (C && C->getAPIntValue() == 1)
8678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cond.getOperand(0);
8679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If condition flag is set by a X86ISD::CMP, then use it as the condition
8682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // setting operand in place of the X86ISD::SETCC.
8683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == X86ISD::SETCC ||
8684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond.getOpcode() == X86ISD::SETCC_CARRY) {
8685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CC = Cond.getOperand(0);
8686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Cmp = Cond.getOperand(1);
8688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Opc = Cmp.getOpcode();
8689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT VT = Op.getValueType();
8690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool IllegalFPCMov = false;
8692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT.isFloatingPoint() && !VT.isVector() &&
8693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        !isScalarFPTypeInSSEReg(VT))  // FPStack?
8694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
8695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opc == X86ISD::BT) { // FIXME
8698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cmp;
8699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addTest = false;
8700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (addTest) {
8704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Look pass the truncate.
8705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond.getOpcode() == ISD::TRUNCATE)
8706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cond.getOperand(0);
8707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We know the result of AND is compared against zero. Try to match
8709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // it to BT.
871019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
871119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
8712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NewSetCC.getNode()) {
8713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CC = NewSetCC.getOperand(0);
8714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = NewSetCC.getOperand(1);
8715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        addTest = false;
8716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (addTest) {
8721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = EmitTest(Cond, X86::COND_NE, DAG);
8723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
872519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // a <  b ? -1 :  0 -> RES = ~setcc_carry
872619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // a <  b ?  0 : -1 -> RES = setcc_carry
872719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // a >= b ? -1 :  0 -> RES = setcc_carry
872819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // a >= b ?  0 : -1 -> RES = ~setcc_carry
872919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Cond.getOpcode() == X86ISD::CMP) {
873019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
873119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
873219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
873319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
873419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
873519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG.getConstant(X86::COND_B, MVT::i8), Cond);
873619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isAllOnes(Op1) != (CondCode == X86::COND_B))
873719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return DAG.getNOT(DL, Res, Res.getValueType());
873819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return Res;
873919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
874019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
874119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
8742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
8743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // condition is true.
874419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
8745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = { Op2, Op1, CC, Cond };
874619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
8747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
8750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
8751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// from the AND / OR.
8752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
8753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Opc = Op.getOpcode();
8754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Opc != ISD::OR && Opc != ISD::AND)
8755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
8756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOperand(0).hasOneUse() &&
8758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
8759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op.getOperand(1).hasOneUse());
8760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
8763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1 and that the SETCC node has a single use.
8764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isXor1OfSetCC(SDValue Op) {
8765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getOpcode() != ISD::XOR)
8766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
8767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N1C && N1C->getAPIntValue() == 1) {
8769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
8770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op.getOperand(0).hasOneUse();
8771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
8773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
8776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool addTest = true;
8777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = Op.getOperand(0);
8778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Cond  = Op.getOperand(1);
8779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Dest  = Op.getOperand(2);
8780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CC;
8782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == ISD::SETCC) {
8784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewCond = LowerSETCC(Cond, DAG);
8785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NewCond.getNode())
8786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = NewCond;
8787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if 0
8789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: LowerXALUO doesn't handle these!!
8790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Cond.getOpcode() == X86ISD::ADD  ||
8791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           Cond.getOpcode() == X86ISD::SUB  ||
8792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           Cond.getOpcode() == X86ISD::SMUL ||
8793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           Cond.getOpcode() == X86ISD::UMUL)
8794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = LowerXALUO(Cond, DAG);
8795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
8796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look pass (and (setcc_carry (cmp ...)), 1).
8798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == ISD::AND &&
8799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
880119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (C && C->getAPIntValue() == 1)
8802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cond.getOperand(0);
8803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If condition flag is set by a X86ISD::CMP, then use it as the condition
8806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // setting operand in place of the X86ISD::SETCC.
8807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.getOpcode() == X86ISD::SETCC ||
8808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond.getOpcode() == X86ISD::SETCC_CARRY) {
8809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CC = Cond.getOperand(0);
8810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Cmp = Cond.getOperand(1);
8812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Opc = Cmp.getOpcode();
8813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
8814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
8815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cmp;
8816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addTest = false;
8817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
8818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
8819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
8820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::COND_O:
8821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::COND_B:
8822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // These can only come from an arithmetic instruction with overflow,
8823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // e.g. SADDO, UADDO.
8824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = Cond.getNode()->getOperand(1);
8825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        addTest = false;
8826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
8827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
8830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned CondOpc;
8831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
8832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Cmp = Cond.getOperand(0).getOperand(1);
8833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CondOpc == ISD::OR) {
8834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Also, recognize the pattern generated by an FCMP_UNE. We can emit
8835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // two branches instead of an explicit OR instruction with a
8836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // separate test.
8837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Cmp == Cond.getOperand(1).getOperand(1) &&
8838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isX86LogicalCmp(Cmp)) {
8839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          CC = Cond.getOperand(0).getOperand(0);
8840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              Chain, Dest, CC, Cmp);
8842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          CC = Cond.getOperand(1).getOperand(0);
8843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = Cmp;
8844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          addTest = false;
8845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
8846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else { // ISD::AND
8847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
8848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // two branches instead of an explicit AND instruction with a
8849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // separate test. However, we only do this if this block doesn't
8850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // have a fall-through edge, because this requires an explicit
8851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // jmp when the condition is false.
8852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Cmp == Cond.getOperand(1).getOperand(1) &&
8853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isX86LogicalCmp(Cmp) &&
8854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Op.getNode()->hasOneUse()) {
8855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          X86::CondCode CCode =
8856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          CCode = X86::GetOppositeBranchCondition(CCode);
8858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          CC = DAG.getConstant(CCode, MVT::i8);
8859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDNode *User = *Op.getNode()->use_begin();
8860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Look for an unconditional branch following this conditional branch.
8861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // We need this because we need to reverse the successors in order
8862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // to implement FCMP_OEQ.
8863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (User->getOpcode() == ISD::BR) {
8864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            SDValue FalseBB = User->getOperand(1);
8865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            SDNode *NewBR =
8866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
8867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            assert(NewBR == User);
8868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            (void)NewBR;
8869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Dest = FalseBB;
8870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                Chain, Dest, CC, Cmp);
8873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            X86::CondCode CCode =
8874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
8875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            CCode = X86::GetOppositeBranchCondition(CCode);
8876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            CC = DAG.getConstant(CCode, MVT::i8);
8877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Cond = Cmp;
8878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            addTest = false;
8879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
8880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
8881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
8883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
8884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // It should be transformed during dag combiner except when the condition
8885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // is set by a arithmetics with overflow node.
8886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86::CondCode CCode =
8887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
8888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CCode = X86::GetOppositeBranchCondition(CCode);
8889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = DAG.getConstant(CCode, MVT::i8);
8890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cond.getOperand(0).getOperand(1);
8891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addTest = false;
8892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (addTest) {
8896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Look pass the truncate.
8897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Cond.getOpcode() == ISD::TRUNCATE)
8898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Cond = Cond.getOperand(0);
8899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We know the result of AND is compared against zero. Try to match
8901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // it to BT.
890219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
8903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
8904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NewSetCC.getNode()) {
8905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CC = NewSetCC.getOperand(0);
8906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = NewSetCC.getOperand(1);
8907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        addTest = false;
8908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
8909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
8910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (addTest) {
8913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CC = DAG.getConstant(X86::COND_NE, MVT::i8);
8914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = EmitTest(Cond, X86::COND_NE, DAG);
8915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
8916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
8917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Chain, Dest, CC, Cond);
8918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
8922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Calls to _alloca is needed to probe the stack when allocating more than 4k
8923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// bytes in one go. Touching the stack at 4K increments is necessary to ensure
8924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// that the guard pages used by the OS virtual memory manager are allocated in
8925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// correct sequence.
8926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
8927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
8928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
892919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
893019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          EnableSegmentedStacks) &&
893119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "This should be used only on Windows targets or when segmented stacks "
893219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "are being used");
893319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
8934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
8935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the inputs.
8937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = Op.getOperand(0);
8938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Size  = Op.getOperand(1);
8939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Ensure alignment here
8940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
894119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool Is64Bit = Subtarget->is64Bit();
894219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
8943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
894419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (EnableSegmentedStacks) {
894519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineFunction &MF = DAG.getMachineFunction();
894619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineRegisterInfo &MRI = MF.getRegInfo();
8947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
894819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Is64Bit) {
894919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // The 64 bit implementation of segmented stacks needs to clobber both r10
895019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // r11. This makes it impossible to use it along with nested parameters.
895119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      const Function *F = MF.getFunction();
895219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
895319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
895419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           I != E; I++)
895519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (I->hasNestAttr())
895619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          report_fatal_error("Cannot use segmented stacks with functions that "
895719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             "have nested arguments.");
895819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
895919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
896019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const TargetRegisterClass *AddrRegClass =
896119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
896219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
896319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
896419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
896519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG.getRegister(Vreg, SPTy));
896619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ops1[2] = { Value, Chain };
896719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getMergeValues(Ops1, 2, dl);
896819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
896919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Flag;
897019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
8971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
897219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
897319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Flag = Chain.getValue(1);
897419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
897619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
897719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Flag = Chain.getValue(1);
8978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
897919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
8980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
898119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ops1[2] = { Chain.getValue(0), Chain };
898219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getMergeValues(Ops1, 2, dl);
898319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
8984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
8985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
8987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
8988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
8989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
899119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
8992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
899319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
8994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // vastart just stores the address of the VarArgsFrameIndex slot into the
8995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // memory location argument.
8996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
8997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   getPointerTy());
899819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
899919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        MachinePointerInfo(SV), false, false, 0);
9000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // __va_list_tag:
9003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   gp_offset         (0 - 6 * 8)
9004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   fp_offset         (48 - 48 + 8 * 16)
9005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   overflow_arg_area (point to parameters coming in memory).
9006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   reg_save_area
9007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 8> MemOps;
9008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FIN = Op.getOperand(1);
9009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store gp_offset
901019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Store = DAG.getStore(Op.getOperand(0), DL,
9011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               MVT::i32),
901319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               FIN, MachinePointerInfo(SV), false, false, 0);
9014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MemOps.push_back(Store);
9015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store fp_offset
901719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
9018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    FIN, DAG.getIntPtrConstant(4));
901919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Store = DAG.getStore(Op.getOperand(0), DL,
9020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       MVT::i32),
902219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       FIN, MachinePointerInfo(SV, 4), false, false, 0);
9023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MemOps.push_back(Store);
9024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store ptr to overflow_arg_area
902619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
9027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    FIN, DAG.getIntPtrConstant(4));
9028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    getPointerTy());
903019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
903119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo(SV, 8),
9032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       false, false, 0);
9033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MemOps.push_back(Store);
9034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store ptr to reg_save_area.
903619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
9037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    FIN, DAG.getIntPtrConstant(8));
9038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    getPointerTy());
904019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
904119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo(SV, 16), false, false, 0);
9042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MemOps.push_back(Store);
904319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
9044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     &MemOps[0], MemOps.size());
9045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
904819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Subtarget->is64Bit() &&
904919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "LowerVAARG only handles 64-bit va_arg!");
905019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((Subtarget->isTargetLinux() ||
905119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Subtarget->isTargetDarwin()) &&
905219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          "Unhandled target in LowerVAARG");
905319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Op.getNode()->getNumOperands() == 4);
905419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Chain = Op.getOperand(0);
905519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue SrcPtr = Op.getOperand(1);
905619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
905719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Align = Op.getConstantOperandVal(3);
905819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
9059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
906019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ArgVT = Op.getNode()->getValueType(0);
906119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
906219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
906319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint8_t ArgMode;
906419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
906519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Decide which area this value should be read from.
906619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // TODO: Implement the AMD64 ABI in its entirety. This simple
906719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // selection mechanism works only for the basic types.
906819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ArgVT == MVT::f80) {
906919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("va_arg for f80 not yet implemented");
907019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
907119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ArgMode = 2;  // Argument passed in XMM register. Use fp_offset.
907219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
907319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ArgMode = 1;  // Argument passed in GPR64 register(s). Use gp_offset.
907419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
907519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("Unhandled argument type in LowerVAARG");
907619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
907719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
907819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ArgMode == 2) {
907919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Sanity Check: Make sure using fp_offset makes sense.
908019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(!UseSoftFloat &&
908119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           !(DAG.getMachineFunction()
908219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
908319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           Subtarget->hasXMM());
908419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
908519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
908619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Insert VAARG_64 node into the DAG
908719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // VAARG_64 returns two values: Variable Argument Address, Chain
908819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<SDValue, 11> InstOps;
908919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InstOps.push_back(Chain);
909019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InstOps.push_back(SrcPtr);
909119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
909219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
909319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  InstOps.push_back(DAG.getConstant(Align, MVT::i32));
909419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
909519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
909619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          VTs, &InstOps[0], InstOps.size(),
909719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          MVT::i64,
909819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          MachinePointerInfo(SV),
909919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          /*Align=*/0,
910019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          /*Volatile=*/false,
910119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          /*ReadMem=*/true,
910219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          /*WriteMem=*/true);
910319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Chain = VAARG.getValue(1);
910419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
910519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Load the next argument and return it
910619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getLoad(ArgVT, dl,
910719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     Chain,
910819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     VAARG,
910919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     MachinePointerInfo(),
911019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     false, false, 0);
9111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
9114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
9115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
9116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = Op.getOperand(0);
9117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue DstPtr = Op.getOperand(1);
9118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue SrcPtr = Op.getOperand(2);
9119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
912119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
9122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
912319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
9124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
912519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       false,
912619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
9127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
9130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
9131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
9132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (IntNo) {
9134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: return SDValue();    // Don't custom lower most intrinsics.
9135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Comparison intrinsics.
9136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_comieq_ss:
9137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_comilt_ss:
9138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_comile_ss:
9139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_comigt_ss:
9140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_comige_ss:
9141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_comineq_ss:
9142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_ucomieq_ss:
9143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_ucomilt_ss:
9144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_ucomile_ss:
9145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_ucomigt_ss:
9146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_ucomige_ss:
9147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse_ucomineq_ss:
9148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_comieq_sd:
9149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_comilt_sd:
9150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_comile_sd:
9151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_comigt_sd:
9152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_comige_sd:
9153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_comineq_sd:
9154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_ucomieq_sd:
9155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_ucomilt_sd:
9156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_ucomile_sd:
9157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_ucomigt_sd:
9158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_ucomige_sd:
9159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_ucomineq_sd: {
9160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Opc = 0;
9161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD::CondCode CC = ISD::SETCC_INVALID;
9162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (IntNo) {
9163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: break;
9164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_comieq_ss:
9165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_comieq_sd:
9166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::COMI;
9167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETEQ;
9168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_comilt_ss:
9170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_comilt_sd:
9171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::COMI;
9172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETLT;
9173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_comile_ss:
9175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_comile_sd:
9176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::COMI;
9177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETLE;
9178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_comigt_ss:
9180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_comigt_sd:
9181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::COMI;
9182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETGT;
9183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_comige_ss:
9185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_comige_sd:
9186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::COMI;
9187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETGE;
9188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_comineq_ss:
9190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_comineq_sd:
9191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::COMI;
9192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETNE;
9193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_ucomieq_ss:
9195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_ucomieq_sd:
9196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::UCOMI;
9197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETEQ;
9198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_ucomilt_ss:
9200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_ucomilt_sd:
9201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::UCOMI;
9202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETLT;
9203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_ucomile_ss:
9205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_ucomile_sd:
9206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::UCOMI;
9207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETLE;
9208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_ucomigt_ss:
9210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_ucomigt_sd:
9211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::UCOMI;
9212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETGT;
9213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_ucomige_ss:
9215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_ucomige_sd:
9216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::UCOMI;
9217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETGE;
9218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse_ucomineq_ss:
9220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_ucomineq_sd:
9221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = X86ISD::UCOMI;
9222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC = ISD::SETNE;
9223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LHS = Op.getOperand(1);
9227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue RHS = Op.getOperand(2);
9228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
9229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
9230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                DAG.getConstant(X86CC, MVT::i8), Cond);
9233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
9234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
923519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Arithmetic intrinsics.
923619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_sse3_hadd_ps:
923719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_sse3_hadd_pd:
923819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_avx_hadd_ps_256:
923919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_avx_hadd_pd_256:
924019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::FHADD, dl, Op.getValueType(),
924119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       Op.getOperand(1), Op.getOperand(2));
924219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_sse3_hsub_ps:
924319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_sse3_hsub_pd:
924419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_avx_hsub_ps_256:
924519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Intrinsic::x86_avx_hsub_pd_256:
924619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::FHSUB, dl, Op.getValueType(),
924719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       Op.getOperand(1), Op.getOperand(2));
9248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ptest and testp intrinsics. The intrinsic these come from are designed to
9249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // return an integer value, not just an instruction so lower it to the ptest
9250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // or testp pattern and a setcc for the result.
9251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse41_ptestz:
9252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse41_ptestc:
9253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse41_ptestnzc:
9254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_ptestz_256:
9255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_ptestc_256:
9256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_ptestnzc_256:
9257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestz_ps:
9258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestc_ps:
9259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestnzc_ps:
9260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestz_pd:
9261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestc_pd:
9262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestnzc_pd:
9263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestz_ps_256:
9264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestc_ps_256:
9265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestnzc_ps_256:
9266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestz_pd_256:
9267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestc_pd_256:
9268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_avx_vtestnzc_pd_256: {
9269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool IsTestPacked = false;
9270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned X86CC = 0;
9271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (IntNo) {
9272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
9273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestz_ps:
9274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestz_pd:
9275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestz_ps_256:
9276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestz_pd_256:
9277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      IsTestPacked = true; // Fallthrough
9278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse41_ptestz:
9279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_ptestz_256:
9280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // ZF = 1
9281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86CC = X86::COND_E;
9282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestc_ps:
9284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestc_pd:
9285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestc_ps_256:
9286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestc_pd_256:
9287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      IsTestPacked = true; // Fallthrough
9288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse41_ptestc:
9289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_ptestc_256:
9290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // CF = 1
9291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86CC = X86::COND_B;
9292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestnzc_ps:
9294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestnzc_pd:
9295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestnzc_ps_256:
9296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_vtestnzc_pd_256:
9297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      IsTestPacked = true; // Fallthrough
9298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse41_ptestnzc:
9299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_avx_ptestnzc_256:
9300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // ZF and CF = 0
9301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      X86CC = X86::COND_A;
9302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LHS = Op.getOperand(1);
9306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue RHS = Op.getOperand(2);
9307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
9309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
9312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fix vector shift instructions where the last operand is a non-immediate
9315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // i32 value.
9316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_pslli_w:
9317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_pslli_d:
9318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_pslli_q:
9319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_psrli_w:
9320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_psrli_d:
9321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_psrli_q:
9322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_psrai_w:
9323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_sse2_psrai_d:
9324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_pslli_w:
9325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_pslli_d:
9326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_pslli_q:
9327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_psrli_w:
9328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_psrli_d:
9329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_psrli_q:
9330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_psrai_w:
9331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Intrinsic::x86_mmx_psrai_d: {
9332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue ShAmt = Op.getOperand(2);
9333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<ConstantSDNode>(ShAmt))
9334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
9335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NewIntNo = 0;
9337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT ShAmtVT = MVT::v4i32;
9338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (IntNo) {
9339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_pslli_w:
9340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psll_w;
9341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_pslli_d:
9343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psll_d;
9344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_pslli_q:
9346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psll_q;
9347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_psrli_w:
9349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psrl_w;
9350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_psrli_d:
9352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psrl_d;
9353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_psrli_q:
9355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psrl_q;
9356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_psrai_w:
9358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psra_w;
9359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Intrinsic::x86_sse2_psrai_d:
9361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewIntNo = Intrinsic::x86_sse2_psra_d;
9362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: {
9364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ShAmtVT = MVT::v2i32;
9365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (IntNo) {
9366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_pslli_w:
9367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psll_w;
9368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_pslli_d:
9370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psll_d;
9371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_pslli_q:
9373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psll_q;
9374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_psrli_w:
9376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psrl_w;
9377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_psrli_d:
9379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psrl_d;
9380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_psrli_q:
9382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psrl_q;
9383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_psrai_w:
9385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psra_w;
9386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Intrinsic::x86_mmx_psrai_d:
9388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewIntNo = Intrinsic::x86_mmx_psra_d;
9389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
9390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
9391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
9392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // The vector shift intrinsics with scalars uses 32b shift amounts but
9397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
9398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // to be zero.
9399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue ShOps[4];
9400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShOps[0] = ShAmt;
9401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShOps[1] = DAG.getConstant(0, MVT::i32);
9402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ShAmtVT == MVT::v4i32) {
9403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ShOps[2] = DAG.getUNDEF(MVT::i32);
9404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ShOps[3] = DAG.getUNDEF(MVT::i32);
9405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ShAmt =  DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
9406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
9407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ShAmt =  DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
940819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// FIXME this must be lowered to get rid of the invalid type.
9409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT VT = Op.getValueType();
941219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
9413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(NewIntNo, MVT::i32),
9415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       Op.getOperand(1), ShAmt);
9416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
9421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
9422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MFI->setReturnAddressIsTaken(true);
9424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
9427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Depth > 0) {
9429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
9430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Offset =
9431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DAG.getConstant(TD->getPointerSize(),
9432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
9433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
9434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getNode(ISD::ADD, dl, getPointerTy(),
9435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   FrameAddr, Offset),
943619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       MachinePointerInfo(), false, false, 0);
9437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Just load the return address.
9440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
9441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
944219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     RetAddrFI, MachinePointerInfo(), false, false, 0);
9443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
9446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MFI->setFrameAddressIsTaken(true);
9448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
9450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();  // FIXME probably not meaningful
9451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
9453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
9454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (Depth--)
945519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
945619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachinePointerInfo(),
9457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            false, false, 0);
9458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return FrameAddr;
9459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
9462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     SelectionDAG &DAG) const {
9463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getIntPtrConstant(2*TD->getPointerSize());
9464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
9467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
9468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain     = Op.getOperand(0);
9469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Offset    = Op.getOperand(1);
9470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Handler   = Op.getOperand(2);
9471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl       = Op.getDebugLoc();
9472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
947319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
947419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     Subtarget->is64Bit() ? X86::RBP : X86::EBP,
947519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     getPointerTy());
9476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
9477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
947819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
947919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  DAG.getIntPtrConstant(TD->getPointerSize()));
9480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
948119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
948219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       false, false, 0);
9483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
9484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MF.getRegInfo().addLiveOut(StoreAddrReg);
9485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(X86ISD::EH_RETURN, dl,
9487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     MVT::Other,
9488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
9489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
949119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
949219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  SelectionDAG &DAG) const {
949319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Op.getOperand(0);
949419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
949519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
949619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
949719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                SelectionDAG &DAG) const {
9498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Root = Op.getOperand(0);
9499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Trmp = Op.getOperand(1); // trampoline
9500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FPtr = Op.getOperand(2); // nested function
9501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Nest = Op.getOperand(3); // 'nest' parameter value
9502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl  = Op.getDebugLoc();
9503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
9505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
9507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue OutChains[6];
9508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Large code-model.
9510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const unsigned char JMP64r  = 0xFF; // 64-bit jmp through register opcode.
9511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
9512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
951319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
951419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
9515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
9517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Load the pointer to the nested function into R11.
9519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
9520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Addr = Trmp;
9521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
952219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                Addr, MachinePointerInfo(TrmpAddr),
952319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 0);
9524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(2, MVT::i64));
952719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
952819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(TrmpAddr, 2),
9529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                false, false, 2);
9530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Load the 'nest' parameter value into R10.
9532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // R10 is specified in X86CallingConv.td
9533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
9534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(10, MVT::i64));
9536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
953719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                Addr, MachinePointerInfo(TrmpAddr, 10),
953819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 0);
9539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(12, MVT::i64));
954219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
954319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(TrmpAddr, 12),
9544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                false, false, 2);
9545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Jump to the nested function.
9547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
9548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(20, MVT::i64));
9550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
955119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                Addr, MachinePointerInfo(TrmpAddr, 20),
955219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 0);
9553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
9555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
9556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(22, MVT::i64));
9557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
955819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(TrmpAddr, 22),
955919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 0);
9560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
956119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
9562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
9563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Function *Func =
9564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
9565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CallingConv::ID CC = Func->getCallingConv();
9566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NestReg;
9567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (CC) {
9569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default:
9570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      llvm_unreachable("Unsupported calling convention");
9571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::C:
9572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::X86_StdCall: {
9573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Pass 'nest' parameter in ECX.
9574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Must be kept in sync with X86CallingConv.td
9575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NestReg = X86::ECX;
9576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Check that ECX wasn't needed by an 'inreg' parameter.
957819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FunctionType *FTy = Func->getFunctionType();
9579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      const AttrListPtr &Attrs = Func->getAttributes();
9580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!Attrs.isEmpty() && !Func->isVarArg()) {
9582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned InRegCount = 0;
9583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned Idx = 1;
9584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (FunctionType::param_iterator I = FTy->param_begin(),
9586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             E = FTy->param_end(); I != E; ++I, ++Idx)
9587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Attrs.paramHasAttr(Idx, Attribute::InReg))
9588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // FIXME: should only count parameters that are lowered to integers.
9589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
9590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (InRegCount > 2) {
9592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          report_fatal_error("Nest register in use - reduce number of inreg"
9593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             " parameters!");
9594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
9595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
9596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::X86_FastCall:
9599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::X86_ThisCall:
9600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::Fast:
9601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Pass 'nest' parameter in EAX.
9602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Must be kept in sync with X86CallingConv.td
9603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NestReg = X86::EAX;
9604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
9605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
9606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue OutChains[4];
9608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Addr, Disp;
9609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(10, MVT::i32));
9612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
9613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This is storing the opcode for MOV32ri.
9615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
961619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
9617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OutChains[0] = DAG.getStore(Root, dl,
9618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
961919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                Trmp, MachinePointerInfo(TrmpAddr),
962019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 0);
9621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(1, MVT::i32));
962419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
962519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(TrmpAddr, 1),
9626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                false, false, 1);
9627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
9629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(5, MVT::i32));
9631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
963219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(TrmpAddr, 5),
963319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 1);
9634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
9636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(6, MVT::i32));
963719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
963819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(TrmpAddr, 6),
9639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                false, false, 1);
9640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
964119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
9642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
9646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            SelectionDAG &DAG) const {
9647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /*
9648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   The rounding mode is in bits 11:10 of FPSR, and has the following
9649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   settings:
9650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     00 Round to nearest
9651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     01 Round to -inf
9652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     10 Round to +inf
9653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     11 Round to 0
9654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FLT_ROUNDS, on the other hand, expects the following:
9656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    -1 Undefined
9657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     0 Round to 0
9658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     1 Round to nearest
9659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     2 Round to +inf
9660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     3 Round to -inf
9661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  To perform the conversion, we do:
9663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
9664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  */
9665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
9667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetMachine &TM = MF.getTarget();
966819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetFrameLowering &TFI = *TM.getFrameLowering();
9669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned StackAlignment = TFI.getStackAlignment();
9670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
967119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
9672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Save FP Control Word to stack slot
9674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
9675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
9676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
967719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
967819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineMemOperand *MMO =
967919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman   MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
968019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           MachineMemOperand::MOStore, 2, 2);
968119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
968219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
968319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
968419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          DAG.getVTList(MVT::Other),
968519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          Ops, 2, MVT::i16, MMO);
9686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load FP Control Word from stack slot
968819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
968919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachinePointerInfo(), false, false, 0);
9690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transform as necessary
9692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CWD1 =
969319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getNode(ISD::SRL, DL, MVT::i16,
969419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                DAG.getNode(ISD::AND, DL, MVT::i16,
9695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            CWD, DAG.getConstant(0x800, MVT::i16)),
9696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                DAG.getConstant(11, MVT::i8));
9697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CWD2 =
969819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getNode(ISD::SRL, DL, MVT::i16,
969919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                DAG.getNode(ISD::AND, DL, MVT::i16,
9700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            CWD, DAG.getConstant(0x400, MVT::i16)),
9701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                DAG.getConstant(9, MVT::i8));
9702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RetVal =
970419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getNode(ISD::AND, DL, MVT::i16,
970519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                DAG.getNode(ISD::ADD, DL, MVT::i16,
970619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
9707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            DAG.getConstant(1, MVT::i16)),
9708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                DAG.getConstant(3, MVT::i16));
9709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode((VT.getSizeInBits() < 16 ?
971219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
9713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
9716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
9717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT OpVT = VT;
9718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits = VT.getSizeInBits();
9719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
9720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = Op.getOperand(0);
9722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::i8) {
9723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Zero extend to i32 since there is not an i8 bsr.
9724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpVT = MVT::i32;
9725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
9726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
9729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
9730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
9731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If src is zero (i.e. bsr sets ZF), returns NumBits.
9733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = {
9734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op,
9735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getConstant(NumBits+NumBits-1, OpVT),
9736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getConstant(X86::COND_E, MVT::i8),
9737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op.getValue(1)
9738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
9739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
9740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Finally xor with NumBits-1.
9742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
9743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::i8)
9745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
9746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Op;
9747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
9750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
9751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT OpVT = VT;
9752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits = VT.getSizeInBits();
9753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
9754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = Op.getOperand(0);
9756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::i8) {
9757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OpVT = MVT::i32;
9758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
9759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
9760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Issue a bsf (scan bits forward) which also sets EFLAGS.
9762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
9763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
9764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If src is zero (i.e. bsf sets ZF), returns NumBits.
9766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = {
9767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op,
9768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getConstant(NumBits, OpVT),
9769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getConstant(X86::COND_E, MVT::i8),
9770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op.getValue(1)
9771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
9772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
9773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT == MVT::i8)
9775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
9776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Op;
9777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
977919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
978019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// ones, and then concatenate the result back.
978119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
978219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
978319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
978419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VT.getSizeInBits() == 256 && VT.isInteger() &&
978519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported value type for operation");
978619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
978719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
978819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
978919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Idx0 = DAG.getConstant(0, MVT::i32);
979019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Idx1 = DAG.getConstant(NumElems/2, MVT::i32);
979119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
979219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Extract the LHS vectors
979319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS = Op.getOperand(0);
979419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS1 = Extract128BitVector(LHS, Idx0, DAG, dl);
979519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS2 = Extract128BitVector(LHS, Idx1, DAG, dl);
979619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
979719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Extract the RHS vectors
979819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS = Op.getOperand(1);
979919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS1 = Extract128BitVector(RHS, Idx0, DAG, dl);
980019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS2 = Extract128BitVector(RHS, Idx1, DAG, dl);
980119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
980219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MVT EltVT = VT.getVectorElementType().getSimpleVT();
980319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
980419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
980519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
980619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
980719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
980819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
980919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
981019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
981119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Op.getValueType().getSizeInBits() == 256 &&
981219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         Op.getValueType().isInteger() &&
981319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Only handle AVX 256-bit vector integer operation");
981419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Lower256IntArith(Op, DAG);
981519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
981619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
981719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
981819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Op.getValueType().getSizeInBits() == 256 &&
981919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         Op.getValueType().isInteger() &&
982019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Only handle AVX 256-bit vector integer operation");
982119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Lower256IntArith(Op, DAG);
982219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
982319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
982419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
9825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
982619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
982719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Decompose 256-bit ops into smaller 128-bit ops.
982819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256)
982919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Lower256IntArith(Op, DAG);
983019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
9832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
9833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
9835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
9836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
9837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
9838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
9839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
9840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
9841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
9842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  return AloBlo + AloBhi + AhiBlo;
9843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue A = Op.getOperand(0);
9845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue B = Op.getOperand(1);
9846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       A, DAG.getConstant(32, MVT::i32));
9850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
9852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       B, DAG.getConstant(32, MVT::i32));
9853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
9855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       A, B);
9856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
9858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       A, Bhi);
9859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
9861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       Ahi, B);
9862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       AloBhi, DAG.getConstant(32, MVT::i32));
9865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
9867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       AhiBlo, DAG.getConstant(32, MVT::i32));
9868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
9869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
9870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Res;
9871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
9872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
987319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
987419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
987519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
987619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
987719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue R = Op.getOperand(0);
987819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Amt = Op.getOperand(1);
987919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContext *Context = DAG.getContext();
988019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
988119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasXMMInt())
988219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
988319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
988419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Decompose 256-bit shifts into smaller 128-bit shifts.
988519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256) {
988619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int NumElems = VT.getVectorNumElements();
988719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MVT EltVT = VT.getVectorElementType().getSimpleVT();
988819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
988919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
989019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Extract the two vectors
989119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V1 = Extract128BitVector(R, DAG.getConstant(0, MVT::i32), DAG, dl);
989219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V2 = Extract128BitVector(R, DAG.getConstant(NumElems/2, MVT::i32),
989319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     DAG, dl);
989419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
989519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Recreate the shift amount vectors
989619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Amt1, Amt2;
989719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
989819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Constant shift amount
989919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SmallVector<SDValue, 4> Amt1Csts;
990019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SmallVector<SDValue, 4> Amt2Csts;
990119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (int i = 0; i < NumElems/2; ++i)
990219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Amt1Csts.push_back(Amt->getOperand(i));
990319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (int i = NumElems/2; i < NumElems; ++i)
990419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Amt2Csts.push_back(Amt->getOperand(i));
990519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
990619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
990719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 &Amt1Csts[0], NumElems/2);
990819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
990919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 &Amt2Csts[0], NumElems/2);
991019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
991119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Variable shift amount
991219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Amt1 = Extract128BitVector(Amt, DAG.getConstant(0, MVT::i32), DAG, dl);
991319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Amt2 = Extract128BitVector(Amt, DAG.getConstant(NumElems/2, MVT::i32),
991419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 DAG, dl);
991519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
991619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
991719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Issue new vector shifts for the smaller types
991819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
991919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
9920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
992119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Concatenate the result back
992219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
992319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
992419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
992519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Optimize shl/srl/sra with constant shift amount.
992619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isSplatVector(Amt.getNode())) {
992719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue SclrAmt = Amt->getOperand(0);
992819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
992919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      uint64_t ShiftAmt = C->getZExtValue();
993019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
993119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SHL)
993219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
993319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
993419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
993519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
993619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SHL)
993719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
993819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
993919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
994019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
994119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SHL)
994219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
994319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
994419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
994519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
994619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v2i64 && Op.getOpcode() == ISD::SRL)
994719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
994819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
994919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
995019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
995119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRL)
995219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
995319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
995419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
995519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
995619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRL)
995719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
995819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
995919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
9960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
996119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v4i32 && Op.getOpcode() == ISD::SRA)
996219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
996319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
996419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
9965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
996619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::v8i16 && Op.getOpcode() == ISD::SRA)
996719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
996819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
996919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     R, DAG.getConstant(ShiftAmt, MVT::i32));
997019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
997119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
997219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
997319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Lower SHL with variable shift amount.
997419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
9975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
9977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Op.getOperand(1), DAG.getConstant(23, MVT::i32));
9978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
998019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<Constant*> CV(4, CI);
9982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = ConstantVector::get(CV);
9983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
9984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
998519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 MachinePointerInfo::getConstantPool(),
9986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 false, false, 16);
9987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
998919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
9990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
9991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::MUL, dl, VT, Op, R);
9992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
999319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
9994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a = a << 5;
9995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
9996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
9997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Op.getOperand(1), DAG.getConstant(5, MVT::i32));
9998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
10000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
10001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<Constant*> CVM1(16, CM1);
10003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<Constant*> CVM2(16, CM2);
10004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = ConstantVector::get(CVM1);
10005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
1000719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachinePointerInfo::getConstantPool(),
10008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            false, false, 16);
10009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // r = pblendv(r, psllw(r & (char16)15, 4), a);
10011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    M = DAG.getNode(ISD::AND, dl, VT, R, M);
10012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    DAG.getConstant(4, MVT::i32));
1001519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
10016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a += a
10017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
1001819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    C = ConstantVector::get(CVM2);
10020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
1002219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    MachinePointerInfo::getConstantPool(),
1002319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    false, false, 16);
1002419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // r = pblendv(r, psllw(r & (char16)63, 2), a);
10026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    M = DAG.getNode(ISD::AND, dl, VT, R, M);
10027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
10028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
10029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    DAG.getConstant(2, MVT::i32));
1003019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    R = DAG.getNode(ISD::VSELECT, dl, VT, Op, R, M);
10031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a += a
10032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
1003319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // return pblendv(r, r+r, a);
1003519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    R = DAG.getNode(ISD::VSELECT, dl, VT, Op,
1003619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    R, DAG.getNode(ISD::ADD, dl, VT, R, R));
10037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return R;
10038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
10040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
10043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
10045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // looks for this combo and may remove the "setcc" instruction if the "setcc"
10046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // has only one use.
10047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDNode *N = Op.getNode();
10048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue LHS = N->getOperand(0);
10049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RHS = N->getOperand(1);
10050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BaseOp = 0;
10051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Cond = 0;
1005219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
10053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
10054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Unknown ovf instruction!");
10055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SADDO:
10056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // A subtract of one will be selected as a INC. Note that INC doesn't
10057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // set CF, so we can't do this for UADDO.
1005819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
1005919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->isOne()) {
10060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BaseOp = X86ISD::INC;
10061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = X86::COND_O;
10062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
10063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
10064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseOp = X86ISD::ADD;
10065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = X86::COND_O;
10066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::UADDO:
10068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseOp = X86ISD::ADD;
10069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = X86::COND_B;
10070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SSUBO:
10072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // A subtract of one will be selected as a DEC. Note that DEC doesn't
10073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // set CF, so we can't do this for USUBO.
1007419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
1007519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->isOne()) {
10076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BaseOp = X86ISD::DEC;
10077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = X86::COND_O;
10078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
10079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
10080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseOp = X86ISD::SUB;
10081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = X86::COND_O;
10082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::USUBO:
10084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseOp = X86ISD::SUB;
10085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = X86::COND_B;
10086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SMULO:
10088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseOp = X86ISD::SMUL;
10089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Cond = X86::COND_O;
10090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1009119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
1009219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
1009319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 MVT::i32);
1009419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
1009519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1009619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue SetCC =
1009719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
1009819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  DAG.getConstant(X86::COND_O, MVT::i32),
1009919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  SDValue(Sum.getNode(), 2));
1010019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1010119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
1010219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
10103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Also sets EFLAGS.
10106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
1010719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
10108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue SetCC =
1011019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
1011119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                DAG.getConstant(Cond, MVT::i32),
1011219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                SDValue(Sum.getNode(), 1));
1011319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1011419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
1011519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1011619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1011719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const{
1011819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
1011919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDNode* Node = Op.getNode();
1012019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1012119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Node->getValueType(0);
1012219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasXMMInt() && VT.isVector()) {
1012319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
1012419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        ExtraVT.getScalarType().getSizeInBits();
1012519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
1012619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1012719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned SHLIntrinsicsID = 0;
1012819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned SRAIntrinsicsID = 0;
1012919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch (VT.getSimpleVT().SimpleTy) {
1013019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      default:
1013119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return SDValue();
1013219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      case MVT::v4i32: {
1013319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_d;
1013419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_d;
1013519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        break;
1013619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1013719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      case MVT::v8i16: {
1013819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SHLIntrinsicsID = Intrinsic::x86_sse2_pslli_w;
1013919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SRAIntrinsicsID = Intrinsic::x86_sse2_psrai_w;
1014019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        break;
1014119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1014219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1014319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1014419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Tmp1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
1014519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         DAG.getConstant(SHLIntrinsicsID, MVT::i32),
1014619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         Node->getOperand(0), ShAmt);
1014719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1014819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // In case of 1 bit sext, no need to shr
1014919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ExtraVT.getScalarType().getSizeInBits() == 1) return Tmp1;
1015019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1015119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
1015219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getConstant(SRAIntrinsicsID, MVT::i32),
1015319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       Tmp1, ShAmt);
1015419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
10155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1015619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
10157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1015919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
10161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
1016219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1016319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
1016419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // There isn't any reason to disable it if the target processor supports it.
1016519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasXMMInt() && !Subtarget->is64Bit()) {
1016619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Chain = Op.getOperand(0);
1016719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Zero = DAG.getConstant(0, MVT::i32);
1016819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ops[] = {
1016919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getRegister(X86::ESP, MVT::i32), // Base
1017019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getTargetConstant(1, MVT::i8),   // Scale
1017119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getRegister(0, MVT::i32),        // Index
1017219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getTargetConstant(0, MVT::i32),  // Disp
1017319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getRegister(0, MVT::i32),        // Segment.
1017419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Zero,
1017519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Chain
1017619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    };
1017719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDNode *Res =
1017819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
1017919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          array_lengthof(Ops));
1018019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue(Res, 0);
1018119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1018219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
1018419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!isDev)
10185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1018619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1018719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1018819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
1018919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
1019019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
1019119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1019219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
1019319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Op1 && !Op2 && !Op3 && Op4)
1019419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
1019519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1019619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
1019719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op1 && !Op2 && !Op3 && !Op4)
1019819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
1019919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1020019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
1020119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //           (MFENCE)>;
1020219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
1020319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1020419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1020519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
1020619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             SelectionDAG &DAG) const {
1020719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
1020819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
1020919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
1021019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
1021119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
1021219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1021319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The only fence that needs an instruction is a sequentially-consistent
1021419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // cross-thread fence.
1021519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
1021619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
1021719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // no-sse2). There isn't any reason to disable it if the target processor
1021819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // supports it.
1021919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->hasXMMInt() || Subtarget->is64Bit())
1022019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
1022119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1022219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Chain = Op.getOperand(0);
1022319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Zero = DAG.getConstant(0, MVT::i32);
1022419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ops[] = {
1022519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getRegister(X86::ESP, MVT::i32), // Base
1022619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getTargetConstant(1, MVT::i8),   // Scale
1022719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getRegister(0, MVT::i32),        // Index
1022819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getTargetConstant(0, MVT::i32),  // Disp
1022919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getRegister(0, MVT::i32),        // Segment.
1023019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Zero,
1023119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Chain
1023219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    };
1023319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDNode *Res =
1023419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
1023519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         array_lengthof(Ops));
1023619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue(Res, 0);
10237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1023819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1023919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // MEMBARRIER is a compiler barrier; it codegens to a no-op.
1024019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
10241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1024319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
10245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT T = Op.getValueType();
1024619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = Op.getDebugLoc();
10247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg = 0;
10248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned size = 0;
10249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch(T.getSimpleVT().SimpleTy) {
10250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
10251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(false && "Invalid value type!");
10252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i8:  Reg = X86::AL;  size = 1; break;
10253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i16: Reg = X86::AX;  size = 2; break;
10254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i32: Reg = X86::EAX; size = 4; break;
10255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MVT::i64:
10256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(Subtarget->is64Bit() && "Node not type legal!");
10257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reg = X86::RAX; size = 8;
10258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1026019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
10261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    Op.getOperand(2), SDValue());
10262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = { cpIn.getValue(0),
10263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    Op.getOperand(1),
10264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    Op.getOperand(3),
10265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    DAG.getTargetConstant(size, MVT::i8),
10266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    cpIn.getValue(1) };
1026719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
1026819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
1026919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
1027019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           Ops, 5, T, MMO);
10271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue cpOut =
1027219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
10273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return cpOut;
10274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
10277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 SelectionDAG &DAG) const {
10278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Subtarget->is64Bit() && "Result not type legalized?");
1027919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
10280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue TheChain = Op.getOperand(0);
10281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
10282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
10283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
10284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
10285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   rax.getValue(2));
10286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
10287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            DAG.getConstant(32, MVT::i8));
10288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = {
10289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
10290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    rdx.getValue(1)
10291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
10292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getMergeValues(Ops, 2, dl);
10293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1029519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue X86TargetLowering::LowerBITCAST(SDValue Op,
10296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            SelectionDAG &DAG) const {
10297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT SrcVT = Op.getOperand(0).getValueType();
10298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT DstVT = Op.getValueType();
1029919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Subtarget->is64Bit() && !Subtarget->hasXMMInt() &&
1030019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         Subtarget->hasMMX() && "Unexpected custom BITCAST");
1030119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((DstVT == MVT::i64 ||
10302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
1030319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unexpected custom BITCAST");
10304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // i64 <=> MMX conversions are Legal.
10305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT==MVT::i64 && DstVT.isVector())
10306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
10307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DstVT==MVT::i64 && SrcVT.isVector())
10308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
10309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // MMX <=> MMX conversions are Legal.
10310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcVT.isVector() && DstVT.isVector())
10311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Op;
10312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // All other conversions need to be expanded.
10313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
10314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1031519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
10317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDNode *Node = Op.getNode();
10318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Node->getDebugLoc();
10319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT T = Node->getValueType(0);
10320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
10321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getConstant(0, T), Node->getOperand(2));
10322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
10323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       cast<AtomicSDNode>(Node)->getMemoryVT(),
10324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       Node->getOperand(0),
10325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       Node->getOperand(1), negOp,
10326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       cast<AtomicSDNode>(Node)->getSrcValue(),
1032719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       cast<AtomicSDNode>(Node)->getAlignment(),
1032819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       cast<AtomicSDNode>(Node)->getOrdering(),
1032919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       cast<AtomicSDNode>(Node)->getSynchScope());
1033019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1033119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1033219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
1033319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDNode *Node = Op.getNode();
1033419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Node->getDebugLoc();
1033519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
1033619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1033719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Convert seq_cst store -> xchg
1033819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
1033919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: On 32-bit, store -> fist or movq would be more efficient
1034019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //        (The only way to get a 16-byte store is cmpxchg16b)
1034119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
1034219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
1034319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
1034419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
1034519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 cast<AtomicSDNode>(Node)->getMemoryVT(),
1034619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 Node->getOperand(0),
1034719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 Node->getOperand(1), Node->getOperand(2),
1034819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 cast<AtomicSDNode>(Node)->getMemOperand(),
1034919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 cast<AtomicSDNode>(Node)->getOrdering(),
1035019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 cast<AtomicSDNode>(Node)->getSynchScope());
1035119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Swap.getValue(1);
1035219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1035319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Other atomic stores have a simple pattern.
1035419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Op;
1035519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1035619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1035719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
1035819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getNode()->getValueType(0);
1035919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1036019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Let legalize expand this if it isn't a legal type yet.
1036119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
1036219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1036319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1036419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList VTs = DAG.getVTList(VT, MVT::i32);
1036519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1036619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Opc;
1036719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool ExtraOp = false;
1036819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (Op.getOpcode()) {
1036919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: assert(0 && "Invalid code");
1037019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADDC: Opc = X86ISD::ADD; break;
1037119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
1037219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUBC: Opc = X86ISD::SUB; break;
1037319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
1037419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1037519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1037619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!ExtraOp)
1037719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
1037819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       Op.getOperand(1));
1037919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
1038019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     Op.getOperand(1), Op.getOperand(2));
10381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerOperation - Provide custom lowering hooks for some operations.
10384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
10385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
10387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Should not custom lower this!");
1038819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SIGN_EXTEND_INREG:  return LowerSIGN_EXTEND_INREG(Op,DAG);
10389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::MEMBARRIER:         return LowerMEMBARRIER(Op,DAG);
1039019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ATOMIC_FENCE:       return LowerATOMIC_FENCE(Op,DAG);
10391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_CMP_SWAP:    return LowerCMP_SWAP(Op,DAG);
10392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_SUB:    return LowerLOAD_SUB(Op,DAG);
1039319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ATOMIC_STORE:       return LowerATOMIC_STORE(Op,DAG);
10394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::BUILD_VECTOR:       return LowerBUILD_VECTOR(Op, DAG);
10395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::CONCAT_VECTORS:     return LowerCONCAT_VECTORS(Op, DAG);
10396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::VECTOR_SHUFFLE:     return LowerVECTOR_SHUFFLE(Op, DAG);
10397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
10398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::INSERT_VECTOR_ELT:  return LowerINSERT_VECTOR_ELT(Op, DAG);
1039919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::EXTRACT_SUBVECTOR:  return LowerEXTRACT_SUBVECTOR(Op, DAG);
1040019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::INSERT_SUBVECTOR:   return LowerINSERT_SUBVECTOR(Op, DAG);
10401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SCALAR_TO_VECTOR:   return LowerSCALAR_TO_VECTOR(Op, DAG);
10402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
10403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
10404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
10405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ExternalSymbol:     return LowerExternalSymbol(Op, DAG);
10406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
10407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SHL_PARTS:
10408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRA_PARTS:
1040919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SRL_PARTS:          return LowerShiftParts(Op, DAG);
10410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG);
10411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::UINT_TO_FP:         return LowerUINT_TO_FP(Op, DAG);
10412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FP_TO_SINT:         return LowerFP_TO_SINT(Op, DAG);
10413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FP_TO_UINT:         return LowerFP_TO_UINT(Op, DAG);
10414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FABS:               return LowerFABS(Op, DAG);
10415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FNEG:               return LowerFNEG(Op, DAG);
10416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FCOPYSIGN:          return LowerFCOPYSIGN(Op, DAG);
1041719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::FGETSIGN:           return LowerFGETSIGN(Op, DAG);
10418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETCC:              return LowerSETCC(Op, DAG);
10419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SELECT:             return LowerSELECT(Op, DAG);
10420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::BRCOND:             return LowerBRCOND(Op, DAG);
10421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
10422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::VASTART:            return LowerVASTART(Op, DAG);
10423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::VAARG:              return LowerVAARG(Op, DAG);
10424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::VACOPY:             return LowerVACOPY(Op, DAG);
10425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
10426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
10427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
10428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FRAME_TO_ARGS_OFFSET:
10429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
10430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
1043119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::EH_RETURN:          return LowerEH_RETURN(Op, DAG);
1043219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::INIT_TRAMPOLINE:    return LowerINIT_TRAMPOLINE(Op, DAG);
1043319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADJUST_TRAMPOLINE:  return LowerADJUST_TRAMPOLINE(Op, DAG);
10434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FLT_ROUNDS_:        return LowerFLT_ROUNDS_(Op, DAG);
10435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::CTLZ:               return LowerCTLZ(Op, DAG);
10436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::CTTZ:               return LowerCTTZ(Op, DAG);
1043719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::MUL:                return LowerMUL(Op, DAG);
1043819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SRA:
1043919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SRL:
1044019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SHL:                return LowerShift(Op, DAG);
10441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SADDO:
10442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::UADDO:
10443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SSUBO:
10444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::USUBO:
10445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SMULO:
10446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::UMULO:              return LowerXALUO(Op, DAG);
10447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::READCYCLECOUNTER:   return LowerREADCYCLECOUNTER(Op, DAG);
1044819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::BITCAST:            return LowerBITCAST(Op, DAG);
1044919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADDC:
1045019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADDE:
1045119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUBC:
1045219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUBE:               return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
1045319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADD:                return LowerADD(Op, DAG);
1045419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUB:                return LowerSUB(Op, DAG);
10455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1045819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic void ReplaceATOMIC_LOAD(SDNode *Node,
1045919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SmallVectorImpl<SDValue> &Results,
1046019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  SelectionDAG &DAG) {
1046119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Node->getDebugLoc();
1046219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
1046319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1046419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Convert wide load -> cmpxchg8b/cmpxchg16b
1046519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: On 32-bit, load -> fild or movq would be more efficient
1046619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //        (The only way to get a 16-byte load is cmpxchg16b)
1046719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
1046819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Zero = DAG.getConstant(0, VT);
1046919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
1047019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Node->getOperand(0),
1047119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Node->getOperand(1), Zero, Zero,
1047219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               cast<AtomicSDNode>(Node)->getMemOperand(),
1047319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               cast<AtomicSDNode>(Node)->getOrdering(),
1047419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               cast<AtomicSDNode>(Node)->getSynchScope());
1047519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Results.push_back(Swap.getValue(0));
1047619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Results.push_back(Swap.getValue(1));
1047719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1047819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
10479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid X86TargetLowering::
10480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
10481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        SelectionDAG &DAG, unsigned NewOp) const {
10482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Node->getDebugLoc();
1048319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert (Node->getValueType(0) == MVT::i64 &&
1048419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          "Only know how to expand i64 atomics");
10485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = Node->getOperand(0);
10487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue In1 = Node->getOperand(1);
10488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
10489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Node->getOperand(2), DAG.getIntPtrConstant(0));
10490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
10491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Node->getOperand(2), DAG.getIntPtrConstant(1));
10492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[] = { Chain, In1, In2L, In2H };
10493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
10494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result =
10495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
10496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            cast<MemSDNode>(Node)->getMemOperand());
10497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
10498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
10499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Results.push_back(Result.getValue(2));
10500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ReplaceNodeResults - Replace a node with an illegal result type
10503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// with a new node built out of custom code.
10504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid X86TargetLowering::ReplaceNodeResults(SDNode *N,
10505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SmallVectorImpl<SDValue>&Results,
10506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           SelectionDAG &DAG) const {
10507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = N->getDebugLoc();
10508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (N->getOpcode()) {
10509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
10510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(false && "Do not know how to custom type legalize this operation!");
10511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1051219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SIGN_EXTEND_INREG:
1051319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADDC:
1051419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADDE:
1051519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUBC:
1051619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUBE:
1051719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // We don't want to expand or promote these.
1051819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
10519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FP_TO_SINT: {
10520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::pair<SDValue,SDValue> Vals =
10521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        FP_TO_INTHelper(SDValue(N, 0), DAG, true);
10522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue FIST = Vals.first, StackSlot = Vals.second;
10523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (FIST.getNode() != 0) {
10524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT VT = N->getValueType(0);
10525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Return a load from the stack slot.
1052619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
1052719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    MachinePointerInfo(), false, false, 0));
10528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
10529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::READCYCLECOUNTER: {
1053219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
10533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue TheChain = N->getOperand(0);
10534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
10535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
10536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     rd.getValue(1));
10537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
10538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     eax.getValue(2));
10539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Use a buildpair to merge the two 32-bit values into a 64-bit one.
10540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[] = { eax, edx };
10541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
10542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Results.push_back(edx.getValue(1));
10543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_CMP_SWAP: {
10546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EVT T = N->getValueType(0);
1054719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
1054819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool Regs64bit = T == MVT::i128;
1054919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
10550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue cpInL, cpInH;
1055119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
1055219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        DAG.getConstant(0, HalfT));
1055319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
1055419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                        DAG.getConstant(1, HalfT));
1055519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
1055619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             Regs64bit ? X86::RAX : X86::EAX,
1055719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             cpInL, SDValue());
1055819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
1055919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             Regs64bit ? X86::RDX : X86::EDX,
1056019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             cpInH, cpInL.getValue(1));
10561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue swapInL, swapInH;
1056219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
1056319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          DAG.getConstant(0, HalfT));
1056419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
1056519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          DAG.getConstant(1, HalfT));
1056619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
1056719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Regs64bit ? X86::RBX : X86::EBX,
1056819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               swapInL, cpInH.getValue(1));
1056919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
1057019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Regs64bit ? X86::RCX : X86::ECX,
1057119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               swapInH, swapInL.getValue(1));
10572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[] = { swapInH.getValue(0),
10573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      N->getOperand(1),
10574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      swapInH.getValue(1) };
1057519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
1057619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
1057719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
1057819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  X86ISD::LCMPXCHG8_DAG;
1057919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
1058019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             Ops, 3, T, MMO);
1058119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
1058219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        Regs64bit ? X86::RAX : X86::EAX,
1058319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        HalfT, Result.getValue(1));
1058419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
1058519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        Regs64bit ? X86::RDX : X86::EDX,
1058619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        HalfT, cpOutL.getValue(2));
10587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
1058819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
10589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Results.push_back(cpOutH.getValue(1));
10590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_ADD:
10593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
10594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_AND:
10596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
10597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_NAND:
10599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
10600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_OR:
10602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
10603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_SUB:
10605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
10606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_LOAD_XOR:
10608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
10609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
10610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ATOMIC_SWAP:
10611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
10612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1061319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ATOMIC_LOAD:
1061419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ReplaceATOMIC_LOAD(N, Results, DAG);
10615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
10619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Opcode) {
10620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: return NULL;
10621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::BSF:                return "X86ISD::BSF";
10622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::BSR:                return "X86ISD::BSR";
10623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SHLD:               return "X86ISD::SHLD";
10624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SHRD:               return "X86ISD::SHRD";
10625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FAND:               return "X86ISD::FAND";
10626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FOR:                return "X86ISD::FOR";
10627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FXOR:               return "X86ISD::FXOR";
10628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FSRL:               return "X86ISD::FSRL";
10629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FILD:               return "X86ISD::FILD";
10630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FILD_FLAG:          return "X86ISD::FILD_FLAG";
10631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
10632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
10633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
10634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FLD:                return "X86ISD::FLD";
10635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FST:                return "X86ISD::FST";
10636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::CALL:               return "X86ISD::CALL";
10637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
10638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::BT:                 return "X86ISD::BT";
10639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::CMP:                return "X86ISD::CMP";
10640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::COMI:               return "X86ISD::COMI";
10641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::UCOMI:              return "X86ISD::UCOMI";
10642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SETCC:              return "X86ISD::SETCC";
10643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SETCC_CARRY:        return "X86ISD::SETCC_CARRY";
1064419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::FSETCCsd:           return "X86ISD::FSETCCsd";
1064519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::FSETCCss:           return "X86ISD::FSETCCss";
10646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::CMOV:               return "X86ISD::CMOV";
10647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::BRCOND:             return "X86ISD::BRCOND";
10648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
10649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::REP_STOS:           return "X86ISD::REP_STOS";
10650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::REP_MOVS:           return "X86ISD::REP_MOVS";
10651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::GlobalBaseReg:      return "X86ISD::GlobalBaseReg";
10652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::Wrapper:            return "X86ISD::Wrapper";
10653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::WrapperRIP:         return "X86ISD::WrapperRIP";
10654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PEXTRB:             return "X86ISD::PEXTRB";
10655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PEXTRW:             return "X86ISD::PEXTRW";
10656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::INSERTPS:           return "X86ISD::INSERTPS";
10657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PINSRB:             return "X86ISD::PINSRB";
10658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PINSRW:             return "X86ISD::PINSRW";
10659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PSHUFB:             return "X86ISD::PSHUFB";
1066019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::ANDNP:              return "X86ISD::ANDNP";
1066119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSIGNB:             return "X86ISD::PSIGNB";
1066219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSIGNW:             return "X86ISD::PSIGNW";
1066319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSIGND:             return "X86ISD::PSIGND";
10664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FMAX:               return "X86ISD::FMAX";
10665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FMIN:               return "X86ISD::FMIN";
10666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FRSQRT:             return "X86ISD::FRSQRT";
10667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FRCP:               return "X86ISD::FRCP";
1066819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::FHADD:              return "X86ISD::FHADD";
1066919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::FHSUB:              return "X86ISD::FHSUB";
10670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::TLSADDR:            return "X86ISD::TLSADDR";
10671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::TLSCALL:            return "X86ISD::TLSCALL";
10672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::EH_RETURN:          return "X86ISD::EH_RETURN";
10673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::TC_RETURN:          return "X86ISD::TC_RETURN";
10674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FNSTCW16m:          return "X86ISD::FNSTCW16m";
10675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::LCMPXCHG_DAG:       return "X86ISD::LCMPXCHG_DAG";
10676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::LCMPXCHG8_DAG:      return "X86ISD::LCMPXCHG8_DAG";
10677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ATOMADD64_DAG:      return "X86ISD::ATOMADD64_DAG";
10678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ATOMSUB64_DAG:      return "X86ISD::ATOMSUB64_DAG";
10679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ATOMOR64_DAG:       return "X86ISD::ATOMOR64_DAG";
10680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ATOMXOR64_DAG:      return "X86ISD::ATOMXOR64_DAG";
10681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ATOMAND64_DAG:      return "X86ISD::ATOMAND64_DAG";
10682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ATOMNAND64_DAG:     return "X86ISD::ATOMNAND64_DAG";
10683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::VZEXT_MOVL:         return "X86ISD::VZEXT_MOVL";
10684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::VZEXT_LOAD:         return "X86ISD::VZEXT_LOAD";
10685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::VSHL:               return "X86ISD::VSHL";
10686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::VSRL:               return "X86ISD::VSRL";
10687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::CMPPD:              return "X86ISD::CMPPD";
10688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::CMPPS:              return "X86ISD::CMPPS";
10689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPEQB:            return "X86ISD::PCMPEQB";
10690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPEQW:            return "X86ISD::PCMPEQW";
10691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPEQD:            return "X86ISD::PCMPEQD";
10692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPEQQ:            return "X86ISD::PCMPEQQ";
10693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPGTB:            return "X86ISD::PCMPGTB";
10694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPGTW:            return "X86ISD::PCMPGTW";
10695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPGTD:            return "X86ISD::PCMPGTD";
10696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PCMPGTQ:            return "X86ISD::PCMPGTQ";
10697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ADD:                return "X86ISD::ADD";
10698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SUB:                return "X86ISD::SUB";
1069919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::ADC:                return "X86ISD::ADC";
1070019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SBB:                return "X86ISD::SBB";
10701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SMUL:               return "X86ISD::SMUL";
10702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::UMUL:               return "X86ISD::UMUL";
10703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::INC:                return "X86ISD::INC";
10704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::DEC:                return "X86ISD::DEC";
10705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::OR:                 return "X86ISD::OR";
10706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::XOR:                return "X86ISD::XOR";
10707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::AND:                return "X86ISD::AND";
1070819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::ANDN:               return "X86ISD::ANDN";
10709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::MUL_IMM:            return "X86ISD::MUL_IMM";
10710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::PTEST:              return "X86ISD::PTEST";
10711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::TESTP:              return "X86ISD::TESTP";
1071219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PALIGN:             return "X86ISD::PALIGN";
1071319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFD:             return "X86ISD::PSHUFD";
1071419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFHW:            return "X86ISD::PSHUFHW";
1071519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFHW_LD:         return "X86ISD::PSHUFHW_LD";
1071619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFLW:            return "X86ISD::PSHUFLW";
1071719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFLW_LD:         return "X86ISD::PSHUFLW_LD";
1071819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPS:             return "X86ISD::SHUFPS";
1071919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPD:             return "X86ISD::SHUFPD";
1072019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPS:            return "X86ISD::MOVLHPS";
1072119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPD:            return "X86ISD::MOVLHPD";
1072219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVHLPS:            return "X86ISD::MOVHLPS";
1072319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVHLPD:            return "X86ISD::MOVHLPD";
1072419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLPS:             return "X86ISD::MOVLPS";
1072519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLPD:             return "X86ISD::MOVLPD";
1072619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVDDUP:            return "X86ISD::MOVDDUP";
1072719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSHDUP:           return "X86ISD::MOVSHDUP";
1072819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSLDUP:           return "X86ISD::MOVSLDUP";
1072919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSHDUP_LD:        return "X86ISD::MOVSHDUP_LD";
1073019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSLDUP_LD:        return "X86ISD::MOVSLDUP_LD";
1073119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSD:              return "X86ISD::MOVSD";
1073219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSS:              return "X86ISD::MOVSS";
1073319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPS:           return "X86ISD::UNPCKLPS";
1073419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPD:           return "X86ISD::UNPCKLPD";
1073519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPDY:         return "X86ISD::VUNPCKLPDY";
1073619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPS:           return "X86ISD::UNPCKHPS";
1073719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPD:           return "X86ISD::UNPCKHPD";
1073819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLBW:          return "X86ISD::PUNPCKLBW";
1073919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLWD:          return "X86ISD::PUNPCKLWD";
1074019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLDQ:          return "X86ISD::PUNPCKLDQ";
1074119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLQDQ:         return "X86ISD::PUNPCKLQDQ";
1074219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHBW:          return "X86ISD::PUNPCKHBW";
1074319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHWD:          return "X86ISD::PUNPCKHWD";
1074419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHDQ:          return "X86ISD::PUNPCKHDQ";
1074519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHQDQ:         return "X86ISD::PUNPCKHQDQ";
1074619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VBROADCAST:         return "X86ISD::VBROADCAST";
1074719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPS:          return "X86ISD::VPERMILPS";
1074819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPSY:         return "X86ISD::VPERMILPSY";
1074919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPD:          return "X86ISD::VPERMILPD";
1075019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPDY:         return "X86ISD::VPERMILPDY";
1075119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERM2F128:         return "X86ISD::VPERM2F128";
10752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
1075319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VAARG_64:           return "X86ISD::VAARG_64";
1075419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::WIN_ALLOCA:         return "X86ISD::WIN_ALLOCA";
1075519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MEMBARRIER:         return "X86ISD::MEMBARRIER";
1075619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SEG_ALLOCA:         return "X86ISD::SEG_ALLOCA";
10757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// isLegalAddressingMode - Return true if the addressing mode represented
10761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// by AM is legal for this target, for a load/store of the specified type.
10762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
1076319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                              Type *Ty) const {
10764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86 supports extremely general addressing modes.
10765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeModel::Model M = getTargetMachine().getCodeModel();
1076619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Reloc::Model R = getTargetMachine().getRelocationModel();
10767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // X86 allows a sign-extended 32-bit immediate field as a displacement.
10769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
10770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
10771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AM.BaseGV) {
10773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned GVFlags =
10774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
10775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If a reference to this global requires an extra load, we can't fold it.
10777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isGlobalStubReference(GVFlags))
10778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
10779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If BaseGV requires a register for the PIC base, we cannot also have a
10781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // BaseReg specified.
10782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
10783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
10784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If lower 4G is not available, then we must use rip-relative addressing.
1078619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((M != CodeModel::Small || R != Reloc::Static) &&
1078719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
10788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
10789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (AM.Scale) {
10792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 0:
10793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 1:
10794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 2:
10795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 4:
10796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 8:
10797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These scales always work.
10798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 3:
10800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 5:
10801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 9:
10802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These scales are formed with basereg+scalereg.  Only accept if there is
10803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // no basereg yet.
10804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AM.HasBaseReg)
10805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
10806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
10807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:  // Other stuff never works.
10808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
10809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
10812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1081519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
10816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
10818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
10819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
10820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumBits1 <= NumBits2)
10821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
10822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
10823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
10826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!VT1.isInteger() || !VT2.isInteger())
10827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
10828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits1 = VT1.getSizeInBits();
10829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits2 = VT2.getSizeInBits();
10830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumBits1 <= NumBits2)
10831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
10832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
10833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1083519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
10836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
10837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
10838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
10841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
10842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
10843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
10846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // i16 instructions are longer (0x66 prefix) and potentially slower.
10847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return !(VT1 == MVT::i32 && VT2 == MVT::i16);
10848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isShuffleMaskLegal - Targets can use this to indicate that they only
10851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
10852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
10853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// are assumed to be legal.
10854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
10855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
10856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      EVT VT) const {
10857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Very little shuffling can be done for 64-bit vectors right now.
10858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.getSizeInBits() == 64)
1085919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX());
10860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: pshufb, blends, shifts.
10862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (VT.getVectorNumElements() == 2 ||
10863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
10864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isMOVLMask(M, VT) ||
10865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isSHUFPMask(M, VT) ||
10866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isPSHUFDMask(M, VT) ||
10867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isPSHUFHWMask(M, VT) ||
10868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isPSHUFLWMask(M, VT) ||
1086919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          isPALIGNRMask(M, VT, Subtarget->hasSSSE3() || Subtarget->hasAVX()) ||
10870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isUNPCKLMask(M, VT) ||
10871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isUNPCKHMask(M, VT) ||
10872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isUNPCKL_v_undef_Mask(M, VT) ||
10873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isUNPCKH_v_undef_Mask(M, VT));
10874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
10877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
10878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          EVT VT) const {
10879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumElts = VT.getVectorNumElements();
10880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: This collection of masks seems suspect.
10881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElts == 2)
10882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
10883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumElts == 4 && VT.getSizeInBits() == 128) {
10884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (isMOVLMask(Mask, VT)  ||
10885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isCommutedMOVLMask(Mask, VT, true) ||
10886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isSHUFPMask(Mask, VT) ||
10887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isCommutedSHUFPMask(Mask, VT));
10888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
10890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
10891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
10893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                           X86 Scheduler Hooks
10894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
10895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// private utility function
10897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
10898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
10899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       MachineBasicBlock *MBB,
10900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned regOpc,
10901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned immOpc,
10902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned LoadOpc,
10903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned CXchgOpc,
10904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned notOpc,
10905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned EAXreg,
10906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       TargetRegisterClass *RC,
10907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       bool invSrc) const {
10908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For the atomic bitwise operator, we generate
10909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   thisMBB:
10910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   newMBB:
10911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     ld  t1 = [bitinstr.addr]
10912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     op  t2 = t1, [bitinstr.val]
10913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     mov EAX = t1
10914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     lcs dest = [bitinstr.addr], t2  [EAX is implicit]
10915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     bz  newMBB
10916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     fallthrough -->nextMBB
10917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *LLVM_BB = MBB->getBasicBlock();
10919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator MBBIter = MBB;
10920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++MBBIter;
10921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// First build the CFG
10923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = MBB->getParent();
10924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *thisMBB = MBB;
10925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
10926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
10927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, newMBB);
10928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, nextMBB);
10929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transfer the remainder of thisMBB and its successor edges to nextMBB.
10931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nextMBB->splice(nextMBB->begin(), thisMBB,
10932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  llvm::next(MachineBasicBlock::iterator(bInstr)),
10933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  thisMBB->end());
10934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
10935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update thisMBB to fall through to newMBB
10937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  thisMBB->addSuccessor(newMBB);
10938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // newMBB jumps to itself and fall through to nextMBB
10940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  newMBB->addSuccessor(nextMBB);
10941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  newMBB->addSuccessor(newMBB);
10942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Insert instructions into newMBB based on incoming instruction
10944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
10945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "unexpected number of operands");
10946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = bInstr->getDebugLoc();
10947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand& destOper = bInstr->getOperand(0);
10948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand* argOpers[2 + X86::AddrNumOperands];
10949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int numArgs = bInstr->getNumOperands() - 1;
10950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i < numArgs; ++i)
10951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    argOpers[i] = &bInstr->getOperand(i+1);
10952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // x86 address has 4 operands: base, index, scale, and displacement
10954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
10955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int valArgIndx = lastAddrIndx + 1;
10956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
10958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
10959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx; ++i)
10960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
10961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned tt = F->getRegInfo().createVirtualRegister(RC);
10963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (invSrc) {
10964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
10965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
10966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
10967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    tt = t1;
10968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
10970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((argOpers[valArgIndx]->isReg() ||
10971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          argOpers[valArgIndx]->isImm()) &&
10972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "invalid operand");
10973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (argOpers[valArgIndx]->isReg())
10974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
10975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
10976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
10977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(tt);
10978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).addOperand(*argOpers[valArgIndx]);
10979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
10981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t1);
10982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
10984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx; ++i)
10985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
10986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t2);
10987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
10988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).setMemRefs(bInstr->memoperands_begin(),
10989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    bInstr->memoperands_end());
10990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
10992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(EAXreg);
10993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // insert branch
10995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
10996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bInstr->eraseFromParent();   // The pseudo instruction is gone now.
10998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return nextMBB;
10999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// private utility function:  64 bit atomics on 32 bit host.
11002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
11003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       MachineBasicBlock *MBB,
11005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned regOpcL,
11006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned regOpcH,
11007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned immOpcL,
11008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned immOpcH,
11009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       bool invSrc) const {
11010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For the atomic bitwise operator, we generate
11011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   thisMBB (instructions are in pairs, except cmpxchg8b)
11012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     ld t1,t2 = [bitinstr.addr]
11013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   newMBB:
11014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     op  t5, t6 <- out1, out2, [bitinstr.val]
11016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //      (for SWAP, substitute:  mov t5, t6 <- [bitinstr.val])
11017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     mov ECX, EBX <- t5, t6
11018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     mov EAX, EDX <- t1, t2
11019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     cmpxchg8b [bitinstr.addr]  [EAX, EDX, EBX, ECX implicit]
11020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     mov t3, t4 <- EAX, EDX
11021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     bz  newMBB
11022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     result in out1, out2
11023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     fallthrough -->nextMBB
11024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *RC = X86::GR32RegisterClass;
11026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const unsigned LoadOpc = X86::MOV32rm;
11027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const unsigned NotOpc = X86::NOT32r;
11028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator MBBIter = MBB;
11031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++MBBIter;
11032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// First build the CFG
11034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = MBB->getParent();
11035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *thisMBB = MBB;
11036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, newMBB);
11039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, nextMBB);
11040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nextMBB->splice(nextMBB->begin(), thisMBB,
11043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  llvm::next(MachineBasicBlock::iterator(bInstr)),
11044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  thisMBB->end());
11045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update thisMBB to fall through to newMBB
11048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  thisMBB->addSuccessor(newMBB);
11049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // newMBB jumps to itself and fall through to nextMBB
11051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  newMBB->addSuccessor(nextMBB);
11052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  newMBB->addSuccessor(newMBB);
11053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = bInstr->getDebugLoc();
11055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Insert instructions into newMBB based on incoming instruction
11056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
11057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
11058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "unexpected number of operands");
11059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand& dest1Oper = bInstr->getOperand(0);
11060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand& dest2Oper = bInstr->getOperand(1);
11061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand* argOpers[2 + X86::AddrNumOperands];
11062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
11063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    argOpers[i] = &bInstr->getOperand(i+2);
11064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We use some of the operands multiple times, so conservatively just
11066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // clear any kill flags that might be present.
11067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (argOpers[i]->isReg() && argOpers[i]->isUse())
11068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      argOpers[i]->setIsKill(false);
11069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
11070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // x86 address has 5 operands: base, index, scale, displacement, and segment.
11072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
11073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
11075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
11076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx; ++i)
11077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
11078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
11079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
11080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // add 4 to displacement.
11081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx-2; ++i)
11082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
11083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand newOp3 = *(argOpers[3]);
11084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (newOp3.isImm())
11085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    newOp3.setImm(newOp3.getImm()+4);
11086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
11087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    newOp3.setOffset(newOp3.getOffset()+4);
11088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).addOperand(newOp3);
11089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).addOperand(*argOpers[lastAddrIndx]);
11090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // t3/4 are defined later, at the bottom of the loop
11092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
11094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
11095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
11096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
11097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The subsequent operations should be using the destination registers of
11100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //the PHI instructions.
11101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (invSrc) {
11102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    t1 = F->getRegInfo().createVirtualRegister(RC);
11103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    t2 = F->getRegInfo().createVirtualRegister(RC);
11104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
11105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
11106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
11107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    t1 = dest1Oper.getReg();
11108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    t2 = dest2Oper.getReg();
11109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
11110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int valArgIndx = lastAddrIndx + 1;
11112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((argOpers[valArgIndx]->isReg() ||
11113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          argOpers[valArgIndx]->isImm()) &&
11114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "invalid operand");
11115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
11117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (argOpers[valArgIndx]->isReg())
11118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
11119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
11120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
11121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (regOpcL != X86::MOV32rr)
11122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB.addReg(t1);
11123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).addOperand(*argOpers[valArgIndx]);
11124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(argOpers[valArgIndx + 1]->isReg() ==
11125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         argOpers[valArgIndx]->isReg());
11126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(argOpers[valArgIndx + 1]->isImm() ==
11127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         argOpers[valArgIndx]->isImm());
11128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (argOpers[valArgIndx + 1]->isReg())
11129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
11130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
11131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
11132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (regOpcH != X86::MOV32rr)
11133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB.addReg(t2);
11134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).addOperand(*argOpers[valArgIndx + 1]);
11135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
11137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t1);
11138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
11139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t2);
11140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
11142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t5);
11143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
11144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t6);
11145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
11147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx; ++i)
11148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
11149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
11151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).setMemRefs(bInstr->memoperands_begin(),
11152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    bInstr->memoperands_end());
11153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
11155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(X86::EAX);
11156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
11157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(X86::EDX);
11158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // insert branch
11160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
11161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bInstr->eraseFromParent();   // The pseudo instruction is gone now.
11163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return nextMBB;
11164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// private utility function
11167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
11168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
11169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                      MachineBasicBlock *MBB,
11170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                      unsigned cmovOpc) const {
11171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For the atomic min/max operator, we generate
11172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   thisMBB:
11173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   newMBB:
11174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     ld t1 = [min/max.addr]
11175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     mov t2 = [min/max.val]
11176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     cmp  t1, t2
11177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     cmov[cond] t2 = t1
11178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     mov EAX = t1
11179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     lcs dest = [bitinstr.addr], t2  [EAX is implicit]
11180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     bz   newMBB
11181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //     fallthrough -->nextMBB
11182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
11183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator MBBIter = MBB;
11186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++MBBIter;
11187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// First build the CFG
11189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = MBB->getParent();
11190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *thisMBB = MBB;
11191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, newMBB);
11194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, nextMBB);
11195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nextMBB->splice(nextMBB->begin(), thisMBB,
11198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  llvm::next(MachineBasicBlock::iterator(mInstr)),
11199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  thisMBB->end());
11200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
11201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update thisMBB to fall through to newMBB
11203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  thisMBB->addSuccessor(newMBB);
11204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // newMBB jumps to newMBB and fall through to nextMBB
11206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  newMBB->addSuccessor(nextMBB);
11207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  newMBB->addSuccessor(newMBB);
11208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = mInstr->getDebugLoc();
11210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Insert instructions into newMBB based on incoming instruction
11211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
11212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "unexpected number of operands");
11213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand& destOper = mInstr->getOperand(0);
11214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineOperand* argOpers[2 + X86::AddrNumOperands];
11215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int numArgs = mInstr->getNumOperands() - 1;
11216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i < numArgs; ++i)
11217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    argOpers[i] = &mInstr->getOperand(i+1);
11218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // x86 address has 4 operands: base, index, scale, and displacement
11220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
11221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int valArgIndx = lastAddrIndx + 1;
11222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
11224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
11225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx; ++i)
11226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
11227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We only support register and immediate values
11229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((argOpers[valArgIndx]->isReg() ||
11230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          argOpers[valArgIndx]->isImm()) &&
11231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "invalid operand");
11232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
11234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (argOpers[valArgIndx]->isReg())
11235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
11236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
11237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
11238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).addOperand(*argOpers[valArgIndx]);
11239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
11241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t1);
11242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
11244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t1);
11245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t2);
11246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Generate movc
11248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
11249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
11250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t2);
11251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t1);
11252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Cmp and exchange if none has modified the memory location
11254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
11255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i=0; i <= lastAddrIndx; ++i)
11256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (*MIB).addOperand(*argOpers[i]);
11257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(t3);
11258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
11259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  (*MIB).setMemRefs(mInstr->memoperands_begin(),
11260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    mInstr->memoperands_end());
11261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
11263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MIB.addReg(X86::EAX);
11264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // insert branch
11266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
11267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  mInstr->eraseFromParent();   // The pseudo instruction is gone now.
11269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return nextMBB;
11270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
11273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// or XMM0_V32I8 in AVX all of this code can be replaced with that
11274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in the .td file.
11275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
11276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
11277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            unsigned numArgs, bool memArg) const {
11278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
11279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Target must have SSE4.2 or AVX features enabled");
11280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = MI->getDebugLoc();
11282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc;
11284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget->hasAVX()) {
11285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (memArg)
11286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
11287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
11288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
11289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
11290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (memArg)
11291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
11292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
11293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
11294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
11295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1129619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
11297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < numArgs; ++i) {
11298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineOperand &Op = MI->getOperand(i+1);
11299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!(Op.isReg() && Op.isImplicit()))
11300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MIB.addOperand(Op);
11301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1130219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl,
1130319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TII->get(Subtarget->hasAVX() ? X86::VMOVAPSrr : X86::MOVAPSrr),
1130419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             MI->getOperand(0).getReg())
11305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(X86::XMM0);
11306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->eraseFromParent();
1130819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return BB;
1130919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1131019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1131119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMachineBasicBlock *
1131219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
1131319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = MI->getDebugLoc();
1131419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1131519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1131619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Address into RAX/EAX, other two args into ECX, EDX.
1131719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1131819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
1131919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
1132019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0; i < X86::AddrNumOperands; ++i)
1132119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MIB.addOperand(MI->getOperand(i));
1132219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1132319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ValOps = X86::AddrNumOperands;
1132419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
1132519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(MI->getOperand(ValOps).getReg());
1132619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
1132719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(MI->getOperand(ValOps+1).getReg());
1132819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1132919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The instruction doesn't actually take any operands though.
1133019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
1133119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1133219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI->eraseFromParent(); // The pseudo is gone now.
1133319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return BB;
1133419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1133519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1133619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMachineBasicBlock *
1133719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
1133819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = MI->getDebugLoc();
1133919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1134019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1134119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // First arg in ECX, the second in EAX.
1134219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
1134319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(MI->getOperand(0).getReg());
1134419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
1134519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(MI->getOperand(1).getReg());
11346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1134719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The instruction doesn't actually take any operands though.
1134819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
1134919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1135019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI->eraseFromParent(); // The pseudo is gone now.
11351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BB;
11352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
1135519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::EmitVAARG64WithCustomInserter(
1135619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   MachineInstr *MI,
1135719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   MachineBasicBlock *MBB) const {
1135819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Emit va_arg instruction on X86-64.
1135919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1136019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Operands to this pseudo-instruction:
1136119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 0  ) Output        : destination address (reg)
1136219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 1-5) Input         : va_list address (addr, i64mem)
1136319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 6  ) ArgSize       : Size (in bytes) of vararg type
1136419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 7  ) ArgMode       : 0=overflow only, 1=use gp_offset, 2=use fp_offset
1136519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 8  ) Align         : Alignment of type
1136619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 9  ) EFLAGS (implicit-def)
1136719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1136819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
1136919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
1137019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1137119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned DestReg = MI->getOperand(0).getReg();
1137219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineOperand &Base = MI->getOperand(1);
1137319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineOperand &Scale = MI->getOperand(2);
1137419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineOperand &Index = MI->getOperand(3);
1137519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineOperand &Disp = MI->getOperand(4);
1137619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineOperand &Segment = MI->getOperand(5);
1137719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ArgSize = MI->getOperand(6).getImm();
1137819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ArgMode = MI->getOperand(7).getImm();
1137919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Align = MI->getOperand(8).getImm();
1138019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1138119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Memory Reference
1138219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
1138319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
1138419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
1138519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1138619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Machine Information
1138719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1138819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1138919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
1139019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
1139119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = MI->getDebugLoc();
1139219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1139319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // struct va_list {
1139419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   i32   gp_offset
1139519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   i32   fp_offset
1139619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   i64   overflow_area (address)
1139719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   i64   reg_save_area (address)
1139819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // }
1139919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // sizeof(va_list) = 24
1140019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // alignment(va_list) = 8
1140119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1140219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned TotalNumIntRegs = 6;
1140319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned TotalNumXMMRegs = 8;
1140419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool UseGPOffset = (ArgMode == 1);
1140519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool UseFPOffset = (ArgMode == 2);
1140619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned MaxOffset = TotalNumIntRegs * 8 +
1140719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
1140819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1140919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /* Align ArgSize to a multiple of 8 */
1141019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
1141119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool NeedsAlign = (Align > 8);
1141219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1141319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *thisMBB = MBB;
1141419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *overflowMBB;
1141519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *offsetMBB;
1141619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *endMBB;
1141719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1141819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned OffsetDestReg = 0;    // Argument address computed by offsetMBB
1141919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned OverflowDestReg = 0;  // Argument address computed by overflowMBB
1142019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned OffsetReg = 0;
1142119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1142219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!UseGPOffset && !UseFPOffset) {
1142319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If we only pull from the overflow region, we don't create a branch.
1142419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // We don't need to alter control flow.
1142519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OffsetDestReg = 0; // unused
1142619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OverflowDestReg = DestReg;
1142719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1142819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    offsetMBB = NULL;
1142919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    overflowMBB = thisMBB;
1143019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    endMBB = thisMBB;
1143119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
1143219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // First emit code to check if gp_offset (or fp_offset) is below the bound.
1143319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If so, pull the argument from reg_save_area. (branch to offsetMBB)
1143419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If not, pull from overflow_area. (branch to overflowMBB)
1143519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //
1143619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //       thisMBB
1143719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //         |     .
1143819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //         |        .
1143919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //     offsetMBB   overflowMBB
1144019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //         |        .
1144119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //         |     .
1144219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //        endMBB
1144319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1144419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Registers for the PHI in endMBB
1144519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
1144619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
1144719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1144819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const BasicBlock *LLVM_BB = MBB->getBasicBlock();
1144919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineFunction *MF = MBB->getParent();
1145019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1145119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1145219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1145319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1145419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineFunction::iterator MBBIter = MBB;
1145519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ++MBBIter;
1145619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1145719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert the new basic blocks
1145819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MF->insert(MBBIter, offsetMBB);
1145919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MF->insert(MBBIter, overflowMBB);
1146019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MF->insert(MBBIter, endMBB);
1146119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1146219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Transfer the remainder of MBB and its successor edges to endMBB.
1146319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    endMBB->splice(endMBB->begin(), thisMBB,
1146419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    llvm::next(MachineBasicBlock::iterator(MI)),
1146519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    thisMBB->end());
1146619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
1146719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1146819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Make offsetMBB and overflowMBB successors of thisMBB
1146919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    thisMBB->addSuccessor(offsetMBB);
1147019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    thisMBB->addSuccessor(overflowMBB);
1147119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1147219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // endMBB is a successor of both offsetMBB and overflowMBB
1147319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    offsetMBB->addSuccessor(endMBB);
1147419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    overflowMBB->addSuccessor(endMBB);
1147519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1147619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Load the offset value into a register
1147719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
1147819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
1147919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Base)
1148019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Scale)
1148119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Index)
1148219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addDisp(Disp, UseFPOffset ? 4 : 0)
1148319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Segment)
1148419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .setMemRefs(MMOBegin, MMOEnd);
1148519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1148619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Check if there is enough room left to pull this argument.
1148719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
1148819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OffsetReg)
1148919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(MaxOffset + 8 - ArgSizeA8);
1149019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1149119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Branch to "overflowMBB" if offset >= max
1149219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Fall through to "offsetMBB" otherwise
1149319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
1149419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addMBB(overflowMBB);
1149519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1149619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1149719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // In offsetMBB, emit code to use the reg_save_area.
1149819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (offsetMBB) {
1149919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(OffsetReg != 0);
1150019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1150119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Read the reg_save_area address.
1150219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
1150319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
1150419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Base)
1150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Scale)
1150619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Index)
1150719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addDisp(Disp, 16)
1150819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Segment)
1150919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .setMemRefs(MMOBegin, MMOEnd);
1151019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1151119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Zero-extend the offset
1151219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
1151319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
1151419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addImm(0)
1151519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(OffsetReg)
1151619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addImm(X86::sub_32bit);
1151719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1151819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Add the offset to the reg_save_area to get the final address.
1151919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
1152019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OffsetReg64)
1152119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(RegSaveReg);
1152219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1152319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Compute the offset for the next argument
1152419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
1152519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
1152619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OffsetReg)
1152719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(UseFPOffset ? 16 : 8);
1152819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Store it back into the va_list.
1153019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
1153119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Base)
1153219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Scale)
1153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Index)
1153419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addDisp(Disp, UseFPOffset ? 4 : 0)
1153519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addOperand(Segment)
1153619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(NextOffsetReg)
1153719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .setMemRefs(MMOBegin, MMOEnd);
1153819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1153919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Jump to endMBB
1154019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
1154119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addMBB(endMBB);
1154219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1154319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1154419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1154519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Emit code to use overflow area
1154619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1154719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1154819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Load the overflow_area address into a register.
1154919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
1155019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
1155119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Base)
1155219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Scale)
1155319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Index)
1155419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addDisp(Disp, 8)
1155519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Segment)
1155619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .setMemRefs(MMOBegin, MMOEnd);
1155719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1155819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we need to align it, do so. Otherwise, just copy the address
1155919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to OverflowDestReg.
1156019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NeedsAlign) {
1156119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Align the overflow address
1156219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
1156319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
1156419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1156519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // aligned_addr = (addr + (align-1)) & ~(align-1)
1156619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
1156719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OverflowAddrReg)
1156819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(Align-1);
1156919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1157019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
1157119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(TmpReg)
1157219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(~(uint64_t)(Align-1));
1157319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
1157419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
1157519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OverflowAddrReg);
1157619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1157719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1157819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Compute the next overflow address after this argument.
1157919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // (the overflow address should be kept 8-byte aligned)
1158019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
1158119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
1158219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(OverflowDestReg)
1158319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addImm(ArgSizeA8);
1158419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1158519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Store the new overflow address.
1158619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
1158719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Base)
1158819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Scale)
1158919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Index)
1159019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addDisp(Disp, 8)
1159119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addOperand(Segment)
1159219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(NextAddrReg)
1159319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .setMemRefs(MMOBegin, MMOEnd);
1159419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1159519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we branched, emit the PHI to the front of endMBB.
1159619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (offsetMBB) {
1159719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(*endMBB, endMBB->begin(), DL,
1159819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            TII->get(X86::PHI), DestReg)
1159919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OffsetDestReg).addMBB(offsetMBB)
1160019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(OverflowDestReg).addMBB(overflowMBB);
1160119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1160219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1160319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Erase the pseudo instruction
1160419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI->eraseFromParent();
1160519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1160619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return endMBB;
1160719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1160819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1160919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMachineBasicBlock *
11610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
11611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MachineInstr *MI,
11612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MachineBasicBlock *MBB) const {
11613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Emit code to save XMM registers to the stack. The ABI says that the
11614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // number of registers to save is given in %al, so it's theoretically
11615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // possible to do an indirect jump trick to avoid saving all of them,
11616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // however this code takes a simpler approach and just executes all
11617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // of the stores if %al is non-zero. It's less code, and it's probably
11618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // easier on the hardware branch predictor, and stores aren't all that
11619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // expensive anyway.
11620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the new basic blocks. One block contains all the XMM stores,
11622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // and one block is the final destination regardless of whether any
11623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // stores were performed.
11624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = MBB->getParent();
11626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator MBBIter = MBB;
11627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++MBBIter;
11628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
11629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
11630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, XMMSaveMBB);
11631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(MBBIter, EndMBB);
11632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transfer the remainder of MBB and its successor edges to EndMBB.
11634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EndMBB->splice(EndMBB->begin(), MBB,
11635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 llvm::next(MachineBasicBlock::iterator(MI)),
11636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 MBB->end());
11637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
11638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The original block will now fall through to the XMM save block.
11640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB->addSuccessor(XMMSaveMBB);
11641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The XMMSaveMBB will fall through to the end block.
11642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  XMMSaveMBB->addSuccessor(EndMBB);
11643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Now add the instructions.
11645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = MI->getDebugLoc();
11647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned CountReg = MI->getOperand(0).getReg();
11649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
11650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
11651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget->isTargetWin64()) {
11653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If %al is 0, branch around the XMM save block.
11654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
11655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
11656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MBB->addSuccessor(EndMBB);
11657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
11658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1165919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
11660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In the XMM save block, save all the XMM argument registers.
11661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
11662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
11663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineMemOperand *MMO =
11664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      F->getMachineMemOperand(
1166519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
1166619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MachineMemOperand::MOStore,
11667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        /*Size=*/16, /*Align=*/16);
1166819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
11669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addFrameIndex(RegSaveFrameIndex)
11670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(/*Scale=*/1)
11671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(/*IndexReg=*/0)
11672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(/*Disp=*/Offset)
11673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(/*Segment=*/0)
11674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(MI->getOperand(i).getReg())
11675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addMemOperand(MMO);
11676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
11677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->eraseFromParent();   // The pseudo instruction is gone now.
11679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return EndMBB;
11681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
11684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
11685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     MachineBasicBlock *BB) const {
11686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = MI->getDebugLoc();
11688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // To "insert" a SELECT_CC instruction, we actually have to insert the
11690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // diamond control-flow pattern.  The incoming instruction knows the
11691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // destination vreg to set, the condition code register to branch on, the
11692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // true/false values to select between, and a branch opcode to use.
11693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *LLVM_BB = BB->getBasicBlock();
11694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator It = BB;
11695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++It;
11696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  thisMBB:
11698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ...
11699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   TrueVal = ...
11700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   cmpTY ccX, r1, r2
11701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   bCC copy1MBB
11702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   fallthrough --> copy0MBB
11703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *thisMBB = BB;
11704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = BB->getParent();
11705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
11706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
11707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(It, copy0MBB);
11708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->insert(It, sinkMBB);
11709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the EFLAGS register isn't dead in the terminator, then claim that it's
11711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // live into the sink and copy blocks.
1171219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!MI->killsRegister(X86::EFLAGS)) {
1171319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    copy0MBB->addLiveIn(X86::EFLAGS);
1171419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    sinkMBB->addLiveIn(X86::EFLAGS);
11715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
11716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transfer the remainder of BB and its successor edges to sinkMBB.
11718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sinkMBB->splice(sinkMBB->begin(), BB,
11719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  llvm::next(MachineBasicBlock::iterator(MI)),
11720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  BB->end());
11721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
11722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add the true and fallthrough blocks as its successors.
11724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB->addSuccessor(copy0MBB);
11725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB->addSuccessor(sinkMBB);
11726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the conditional branch instruction.
11728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc =
11729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
11730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
11731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  copy0MBB:
11733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   %FalseValue = ...
11734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   # fallthrough to sinkMBB
11735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  copy0MBB->addSuccessor(sinkMBB);
11736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  sinkMBB:
11738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
11739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ...
11740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(*sinkMBB, sinkMBB->begin(), DL,
11741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          TII->get(X86::PHI), MI->getOperand(0).getReg())
11742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
11743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
11744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->eraseFromParent();   // The pseudo instruction is gone now.
11746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return sinkMBB;
11747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
1175019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
1175119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        bool Is64Bit) const {
1175219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1175319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = MI->getDebugLoc();
1175419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFunction *MF = BB->getParent();
1175519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const BasicBlock *LLVM_BB = BB->getBasicBlock();
1175619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1175719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(EnableSegmentedStacks);
1175819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1175919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
1176019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
1176119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1176219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // BB:
1176319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  ... [Till the alloca]
1176419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If stacklet is not large enough, jump to mallocMBB
1176519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1176619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // bumpMBB:
1176719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  Allocate by subtracting from RSP
1176819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  Jump to continueMBB
1176919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1177019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // mallocMBB:
1177119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  Allocate by call to runtime
1177219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1177319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // continueMBB:
1177419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  ...
1177519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  [rest of original BB]
1177619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1177719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1177819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1177919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1178019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1178119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1178219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineRegisterInfo &MRI = MF->getRegInfo();
1178319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetRegisterClass *AddrRegClass =
1178419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
1178519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1178619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
1178719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
1178819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
1178919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    sizeVReg = MI->getOperand(1).getReg(),
1179019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    physSPReg = Is64Bit ? X86::RSP : X86::ESP;
1179119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1179219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFunction::iterator MBBIter = BB;
1179319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ++MBBIter;
1179419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1179519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MF->insert(MBBIter, bumpMBB);
1179619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MF->insert(MBBIter, mallocMBB);
1179719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MF->insert(MBBIter, continueMBB);
1179819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1179919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  continueMBB->splice(continueMBB->begin(), BB, llvm::next
1180019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      (MachineBasicBlock::iterator(MI)), BB->end());
1180119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  continueMBB->transferSuccessorsAndUpdatePHIs(BB);
1180219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1180319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Add code to the main basic block to check if the stack limit has been hit,
1180419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // and if so, jump to mallocMBB otherwise to bumpMBB.
1180519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
1180619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), tmpSPVReg)
1180719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(tmpSPVReg).addReg(sizeVReg);
1180819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
1180919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg)
1181019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(tmpSPVReg);
1181119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
1181219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1181319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // bumpMBB simply decreases the stack pointer, since we know the current
1181419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // stacklet has enough space.
1181519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
1181619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(tmpSPVReg);
1181719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
1181819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(tmpSPVReg);
1181919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
1182019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1182119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Calls into a routine in libgcc to allocate more space from the heap.
1182219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Is64Bit) {
1182319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
1182419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(sizeVReg);
1182519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
1182619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addExternalSymbol("__morestack_allocate_stack_space").addReg(X86::RDI);
1182719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
1182819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
1182919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(12);
1183019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
1183119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
1183219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addExternalSymbol("__morestack_allocate_stack_space");
1183319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1183419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1183519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Is64Bit)
1183619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
1183719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(16);
1183819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1183919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
1184019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(Is64Bit ? X86::RAX : X86::EAX);
1184119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
1184219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1184319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Set up the CFG correctly.
1184419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BB->addSuccessor(bumpMBB);
1184519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BB->addSuccessor(mallocMBB);
1184619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  mallocMBB->addSuccessor(continueMBB);
1184719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bumpMBB->addSuccessor(continueMBB);
1184819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1184919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Take care of the PHI nodes.
1185019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
1185119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MI->getOperand(0).getReg())
1185219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(mallocPtrVReg).addMBB(mallocMBB)
1185319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
1185419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1185519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Delete the original pseudo instruction.
1185619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI->eraseFromParent();
1185719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1185819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // And we're done.
1185919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return continueMBB;
1186019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1186119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1186219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMachineBasicBlock *
1186319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanX86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
11864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          MachineBasicBlock *BB) const {
11865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = MI->getDebugLoc();
11867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1186819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(!Subtarget->isTargetEnvMacho());
1186919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
11870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The lowering is pretty easy: we're just emitting the call to _alloca.  The
11871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // non-trivial part is impdef of ESP.
11872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1187319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->isTargetWin64()) {
1187419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->isTargetCygMing()) {
1187519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // ___chkstk(Mingw64):
1187619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Clobbers R10, R11, RAX and EFLAGS.
1187719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Updates RSP.
1187819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
1187919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addExternalSymbol("___chkstk")
1188019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RAX, RegState::Implicit)
1188119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RSP, RegState::Implicit)
1188219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RAX, RegState::Define | RegState::Implicit)
1188319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RSP, RegState::Define | RegState::Implicit)
1188419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
1188519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
1188619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // __chkstk(MSVCRT): does not update stack pointer.
1188719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Clobbers R10, R11 and EFLAGS.
1188819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // FIXME: RAX(allocated size) might be reused and not killed.
1188919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
1189019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addExternalSymbol("__chkstk")
1189119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RAX, RegState::Implicit)
1189219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
1189319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // RAX has the offset to subtracted from RSP.
1189419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
1189519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RSP)
1189619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        .addReg(X86::RAX);
1189719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1189819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
1189919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const char *StackProbeSymbol =
1190019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
1190119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1190219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
1190319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addExternalSymbol(StackProbeSymbol)
1190419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(X86::EAX, RegState::Implicit)
1190519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(X86::ESP, RegState::Implicit)
1190619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(X86::EAX, RegState::Define | RegState::Implicit)
1190719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(X86::ESP, RegState::Define | RegState::Implicit)
1190819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
1190919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
11910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->eraseFromParent();   // The pseudo instruction is gone now.
11912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BB;
11913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
11916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
11917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      MachineBasicBlock *BB) const {
11918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This is pretty easy.  We're taking the value that we received from
11919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // our load from the relocation, sticking it in either RDI (x86-64)
11920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // or EAX and doing an indirect call.  The return value will then
11921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // be in the normal return register.
1192219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const X86InstrInfo *TII
11923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
11924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = MI->getDebugLoc();
11925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = BB->getParent();
1192619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1192719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
11928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(MI->getOperand(3).isGlobal() && "This should be a global");
1192919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
11930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget->is64Bit()) {
11931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      TII->get(X86::MOV64rm), X86::RDI)
11933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(X86::RIP)
11934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addImm(0).addReg(0)
1193519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
11936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      MI->getOperand(3).getTargetFlags())
11937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(0);
11938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
11939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addDirectMem(MIB, X86::RDI);
11940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
11941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      TII->get(X86::MOV32rm), X86::EAX)
11943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(0)
11944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addImm(0).addReg(0)
1194519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
11946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      MI->getOperand(3).getTargetFlags())
11947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(0);
11948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
11949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addDirectMem(MIB, X86::EAX);
11950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
11951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
11952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      TII->get(X86::MOV32rm), X86::EAX)
11953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(TII->getGlobalBaseReg(F))
11954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addImm(0).addReg(0)
1195519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
11956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      MI->getOperand(3).getTargetFlags())
11957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(0);
11958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
11959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addDirectMem(MIB, X86::EAX);
11960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1196119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
11962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->eraseFromParent(); // The pseudo instruction is gone now.
11963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BB;
11964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
11965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
11967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
11968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               MachineBasicBlock *BB) const {
11969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (MI->getOpcode()) {
1197019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: assert(0 && "Unexpected instr type to insert");
1197119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::TAILJMPd64:
1197219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::TAILJMPr64:
1197319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::TAILJMPm64:
1197419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(0 && "TAILJMP64 would not be touched here.");
1197519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::TCRETURNdi64:
1197619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::TCRETURNri64:
1197719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::TCRETURNmi64:
1197819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
1197919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // On AMD64, additional defs should be added before register allocation.
1198019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!Subtarget->isTargetWin64()) {
1198119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::RSI);
1198219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::RDI);
1198319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM6);
1198419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM7);
1198519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM8);
1198619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM9);
1198719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM10);
1198819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM11);
1198919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM12);
1199019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM13);
1199119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM14);
1199219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MI->addRegisterDefined(X86::XMM15);
1199319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1199419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return BB;
1199519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::WIN_ALLOCA:
1199619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return EmitLoweredWinAlloca(MI, BB);
1199719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::SEG_ALLOCA_32:
1199819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return EmitLoweredSegAlloca(MI, BB, false);
1199919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::SEG_ALLOCA_64:
1200019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return EmitLoweredSegAlloca(MI, BB, true);
12001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::TLSCall_32:
12002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::TLSCall_64:
12003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitLoweredTLSCall(MI, BB);
12004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_GR8:
12005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_FR32:
12006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_FR64:
12007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_V4F32:
12008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_V2F64:
12009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_V2I64:
1201019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::CMOV_V8F32:
1201119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::CMOV_V4F64:
1201219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::CMOV_V4I64:
12013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_GR16:
12014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_GR32:
12015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_RFP32:
12016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_RFP64:
12017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::CMOV_RFP80:
12018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitLoweredSelect(MI, BB);
12019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP32_TO_INT16_IN_MEM:
12021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP32_TO_INT32_IN_MEM:
12022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP32_TO_INT64_IN_MEM:
12023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP64_TO_INT16_IN_MEM:
12024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP64_TO_INT32_IN_MEM:
12025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP64_TO_INT64_IN_MEM:
12026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP80_TO_INT16_IN_MEM:
12027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP80_TO_INT32_IN_MEM:
12028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::FP80_TO_INT64_IN_MEM: {
12029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DebugLoc DL = MI->getDebugLoc();
12031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Change the floating point control register to use "round towards zero"
12033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // mode when truncating to an integer value.
12034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineFunction *F = BB->getParent();
12035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
12036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFrameReference(BuildMI(*BB, MI, DL,
12037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              TII->get(X86::FNSTCW16m)), CWFrameIdx);
12038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Load the old value of the high byte of the control word...
12040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned OldCW =
12041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
12042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
12043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      CWFrameIdx);
12044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Set the high part to be round to zero...
12046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
12047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(0xC7F);
12048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Reload the modified control word now...
12050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFrameReference(BuildMI(*BB, MI, DL,
12051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              TII->get(X86::FLDCW16m)), CWFrameIdx);
12052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Restore the memory image of control word to original value
12054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
12055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(OldCW);
12056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Get the X86 opcode to use.
12058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Opc;
12059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (MI->getOpcode()) {
12060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("illegal opcode!");
12061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
12067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
12070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
12071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    X86AddressMode AM;
12073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineOperand &Op = MI->getOperand(0);
12074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.isReg()) {
12075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.BaseType = X86AddressMode::RegBase;
12076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.Base.Reg = Op.getReg();
12077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
12078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.BaseType = X86AddressMode::FrameIndexBase;
12079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.Base.FrameIndex = Op.getIndex();
12080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
12081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = MI->getOperand(1);
12082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.isImm())
12083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.Scale = Op.getImm();
12084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = MI->getOperand(2);
12085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.isImm())
12086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.IndexReg = Op.getImm();
12087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = MI->getOperand(3);
12088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.isGlobal()) {
12089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.GV = Op.getGlobal();
12090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
12091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AM.Disp = Op.getImm();
12092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
12093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
12094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
12095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Reload the original control word now.
12097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addFrameReference(BuildMI(*BB, MI, DL,
12098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              TII->get(X86::FLDCW16m)), CWFrameIdx);
12099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MI->eraseFromParent();   // The pseudo instruction is gone now.
12101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return BB;
12102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // String/text processing lowering.
12104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::PCMPISTRM128REG:
12105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::VPCMPISTRM128REG:
12106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitPCMP(MI, BB, 3, false /* in-mem */);
12107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::PCMPISTRM128MEM:
12108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::VPCMPISTRM128MEM:
12109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitPCMP(MI, BB, 3, true /* in-mem */);
12110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::PCMPESTRM128REG:
12111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::VPCMPESTRM128REG:
12112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitPCMP(MI, BB, 5, false /* in mem */);
12113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::PCMPESTRM128MEM:
12114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::VPCMPESTRM128MEM:
12115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitPCMP(MI, BB, 5, true /* in mem */);
12116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1211719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Thread synchronization.
1211819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::MONITOR:
1211919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return EmitMonitor(MI, BB);
1212019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::MWAIT:
1212119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return EmitMwait(MI, BB);
1212219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
12123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Atomic Lowering.
12124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMAND32:
12125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
12126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND32ri, X86::MOV32rm,
12127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG32,
12128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT32r, X86::EAX,
12129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR32RegisterClass);
12130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMOR32:
12131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
12132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::OR32ri, X86::MOV32rm,
12133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG32,
12134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT32r, X86::EAX,
12135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR32RegisterClass);
12136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMXOR32:
12137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
12138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::XOR32ri, X86::MOV32rm,
12139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG32,
12140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT32r, X86::EAX,
12141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR32RegisterClass);
12142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMNAND32:
12143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
12144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND32ri, X86::MOV32rm,
12145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG32,
12146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT32r, X86::EAX,
12147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR32RegisterClass, true);
12148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMMIN32:
12149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
12150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMMAX32:
12151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
12152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMUMIN32:
12153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
12154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMUMAX32:
12155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
12156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMAND16:
12158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND16ri, X86::MOV16rm,
12160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG16,
12161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT16r, X86::AX,
12162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR16RegisterClass);
12163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMOR16:
12164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
12165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::OR16ri, X86::MOV16rm,
12166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG16,
12167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT16r, X86::AX,
12168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR16RegisterClass);
12169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMXOR16:
12170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
12171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::XOR16ri, X86::MOV16rm,
12172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG16,
12173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT16r, X86::AX,
12174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR16RegisterClass);
12175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMNAND16:
12176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
12177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND16ri, X86::MOV16rm,
12178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG16,
12179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT16r, X86::AX,
12180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR16RegisterClass, true);
12181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMMIN16:
12182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
12183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMMAX16:
12184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
12185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMUMIN16:
12186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
12187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMUMAX16:
12188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
12189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMAND8:
12191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND8ri, X86::MOV8rm,
12193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG8,
12194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT8r, X86::AL,
12195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR8RegisterClass);
12196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMOR8:
12197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
12198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::OR8ri, X86::MOV8rm,
12199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG8,
12200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT8r, X86::AL,
12201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR8RegisterClass);
12202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMXOR8:
12203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
12204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::XOR8ri, X86::MOV8rm,
12205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG8,
12206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT8r, X86::AL,
12207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR8RegisterClass);
12208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMNAND8:
12209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
12210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND8ri, X86::MOV8rm,
12211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG8,
12212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT8r, X86::AL,
12213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR8RegisterClass, true);
12214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
12215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This group is for 64-bit host.
12216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMAND64:
12217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND64ri32, X86::MOV64rm,
12219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG64,
12220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT64r, X86::RAX,
12221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR64RegisterClass);
12222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMOR64:
12223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
12224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::OR64ri32, X86::MOV64rm,
12225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG64,
12226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT64r, X86::RAX,
12227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR64RegisterClass);
12228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMXOR64:
12229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
12230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::XOR64ri32, X86::MOV64rm,
12231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG64,
12232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT64r, X86::RAX,
12233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR64RegisterClass);
12234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMNAND64:
12235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
12236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND64ri32, X86::MOV64rm,
12237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::LCMPXCHG64,
12238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::NOT64r, X86::RAX,
12239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::GR64RegisterClass, true);
12240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMMIN64:
12241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
12242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMMAX64:
12243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
12244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMUMIN64:
12245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
12246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMUMAX64:
12247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
12248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This group does 64-bit operations on a 32-bit host.
12250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMAND6432:
12251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND32rr, X86::AND32rr,
12253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND32ri, X86::AND32ri,
12254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               false);
12255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMOR6432:
12256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::OR32rr, X86::OR32rr,
12258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::OR32ri, X86::OR32ri,
12259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               false);
12260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMXOR6432:
12261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::XOR32rr, X86::XOR32rr,
12263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::XOR32ri, X86::XOR32ri,
12264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               false);
12265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMNAND6432:
12266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND32rr, X86::AND32rr,
12268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::AND32ri, X86::AND32ri,
12269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               true);
12270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMADD6432:
12271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::ADD32rr, X86::ADC32rr,
12273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::ADD32ri, X86::ADC32ri,
12274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               false);
12275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMSUB6432:
12276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::SUB32rr, X86::SBB32rr,
12278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::SUB32ri, X86::SBB32ri,
12279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               false);
12280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::ATOMSWAP6432:
12281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitAtomicBit6432WithCustomInserter(MI, BB,
12282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::MOV32rr, X86::MOV32rr,
12283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               X86::MOV32ri, X86::MOV32ri,
12284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               false);
12285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86::VASTART_SAVE_XMM_REGS:
12286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
1228719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1228819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86::VAARG_64:
1228919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return EmitVAARG64WithCustomInserter(MI, BB);
12290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
12292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
12294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                           X86 Optimization Hooks
12295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
12296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
12298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       const APInt &Mask,
12299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       APInt &KnownZero,
12300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       APInt &KnownOne,
12301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       const SelectionDAG &DAG,
12302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       unsigned Depth) const {
12303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc = Op.getOpcode();
12304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Opc >= ISD::BUILTIN_OP_END ||
12305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Opc == ISD::INTRINSIC_WO_CHAIN ||
12306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Opc == ISD::INTRINSIC_W_CHAIN ||
12307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Opc == ISD::INTRINSIC_VOID) &&
12308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Should use MaskedValueIsZero if you don't know whether Op"
12309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         " is a target node!");
12310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);   // Don't know anything.
12312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Opc) {
12313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
12314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::ADD:
12315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SUB:
1231619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::ADC:
1231719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SBB:
12318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SMUL:
12319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::UMUL:
12320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::INC:
12321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::DEC:
12322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::OR:
12323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::XOR:
12324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::AND:
12325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // These nodes' second result is a boolean.
12326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Op.getResNo() == 0)
12327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
12328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Fallthrough
12329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::SETCC:
12330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
12331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       Mask.getBitWidth() - 1);
12332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1233319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::INTRINSIC_WO_CHAIN: {
1233419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1233519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumLoBits = 0;
1233619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch (IntId) {
1233719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    default: break;
1233819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Intrinsic::x86_sse_movmsk_ps:
1233919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Intrinsic::x86_avx_movmsk_ps_256:
1234019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Intrinsic::x86_sse2_movmsk_pd:
1234119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Intrinsic::x86_avx_movmsk_pd_256:
1234219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Intrinsic::x86_mmx_pmovmskb:
1234319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Intrinsic::x86_sse2_pmovmskb_128: {
1234419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // High bits of movmskp{s|d}, pmovmskb are known zero.
1234519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      switch (IntId) {
1234619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_sse_movmsk_ps:      NumLoBits = 4; break;
1234719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_avx_movmsk_ps_256:  NumLoBits = 8; break;
1234819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_sse2_movmsk_pd:     NumLoBits = 2; break;
1234919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_avx_movmsk_pd_256:  NumLoBits = 4; break;
1235019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_mmx_pmovmskb:       NumLoBits = 8; break;
1235119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_sse2_pmovmskb_128:  NumLoBits = 16; break;
1235219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1235319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      KnownZero = APInt::getHighBitsSet(Mask.getBitWidth(),
1235419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        Mask.getBitWidth() - NumLoBits);
1235519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
1235619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1235719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1235819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
12359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1236019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1236119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1236219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1236319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
1236419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                         unsigned Depth) const {
1236519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
1236619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op.getOpcode() == X86ISD::SETCC_CARRY)
1236719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Op.getValueType().getScalarType().getSizeInBits();
1236819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1236919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Fallback case.
1237019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return 1;
12371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
12372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
12374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// node is a GlobalAddress + offset.
12375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isGAPlusOffset(SDNode *N,
12376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       const GlobalValue* &GA,
12377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       int64_t &Offset) const {
12378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N->getOpcode() == X86ISD::Wrapper) {
12379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
12380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
12381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
12382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
12383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
12384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::isGAPlusOffset(N, GA, Offset);
12386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
12387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1238819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
1238919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// same as extracting the high 128-bit part of 256-bit vector and then
1239019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// inserting the result into the low part of a new 256-bit vector
1239119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
1239219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
1239319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
1239419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1239519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
1239619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = 0, j = NumElems/2; i < NumElems/2; ++i, ++j)
1239719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
1239819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SVOp->getMaskElt(j) >= 0)
1239919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
1240019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1240119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
1240219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1240319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1240419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
1240519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// same as extracting the low 128-bit part of 256-bit vector and then
1240619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// inserting the result into the high part of a new 256-bit vector
1240719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
1240819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
1240919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
1241019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1241119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
1241219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (int i = NumElems/2, j = 0; i < NumElems; ++i, ++j)
1241319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
1241419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SVOp->getMaskElt(j) >= 0)
1241519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
1241619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1241719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
1241819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1241919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1242019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
1242119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
1242219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        TargetLowering::DAGCombinerInfo &DCI) {
1242319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = N->getDebugLoc();
1242419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
1242519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V1 = SVOp->getOperand(0);
1242619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V2 = SVOp->getOperand(1);
1242719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = SVOp->getValueType(0);
1242819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int NumElems = VT.getVectorNumElements();
1242919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1243019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
1243119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      V2.getOpcode() == ISD::CONCAT_VECTORS) {
1243219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //
1243319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //                   0,0,0,...
1243419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //                      |
1243519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //    V      UNDEF    BUILD_VECTOR    UNDEF
1243619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //     \      /           \           /
1243719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //  CONCAT_VECTOR         CONCAT_VECTOR
1243819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //         \                  /
1243919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //          \                /
1244019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //          RESULT: V + zero extended
1244119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //
1244219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
1244319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V2.getOperand(1).getOpcode() != ISD::UNDEF ||
1244419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V1.getOperand(1).getOpcode() != ISD::UNDEF)
1244519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
1244619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1244719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
1244819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
1244919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1245019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // To match the shuffle mask, the first half of the mask should
1245119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // be exactly the first vector, and all the rest a splat with the
1245219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // first element of the second one.
1245319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (int i = 0; i < NumElems/2; ++i)
1245419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
1245519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
1245619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return SDValue();
1245719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1245819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Emit a zeroed vector and insert the desired subvector on its
1245919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // first half.
1246019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Zeros = getZeroVector(VT, true /* HasXMMInt */, DAG, dl);
1246119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0),
1246219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         DAG.getConstant(0, MVT::i32), DAG, dl);
1246319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DCI.CombineTo(N, InsV);
1246419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1246519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1246619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //===--------------------------------------------------------------------===//
1246719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Combine some shuffles into subvector extracts and inserts:
1246819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //
1246919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1247019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
1247119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isShuffleHigh128VectorInsertLow(SVOp)) {
1247219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V = Extract128BitVector(V1, DAG.getConstant(NumElems/2, MVT::i32),
1247319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    DAG, dl);
1247419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
1247519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      V, DAG.getConstant(0, MVT::i32), DAG, dl);
1247619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DCI.CombineTo(N, InsV);
1247719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1247819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1247919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
1248019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isShuffleLow128VectorInsertHigh(SVOp)) {
1248119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V = Extract128BitVector(V1, DAG.getConstant(0, MVT::i32), DAG, dl);
1248219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue InsV = Insert128BitVector(DAG.getNode(ISD::UNDEF, dl, VT),
1248319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                             V, DAG.getConstant(NumElems/2, MVT::i32), DAG, dl);
1248419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DCI.CombineTo(N, InsV);
1248519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1248619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1248719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1248819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1248919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1249019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformShuffleCombine - Performs several different shuffle combines.
12491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
1249219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     TargetLowering::DAGCombinerInfo &DCI,
1249319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     const X86Subtarget *Subtarget) {
12494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = N->getDebugLoc();
12495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
12496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1249719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Don't create instructions with illegal types after legalize types has run.
1249819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1249919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
1250019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1250119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1250219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
1250319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasAVX() && VT.getSizeInBits() == 256 &&
1250419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      N->getOpcode() == ISD::VECTOR_SHUFFLE)
1250519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return PerformShuffleCombine256(N, DAG, DCI);
1250619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1250719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Only handle 128 wide vector from here on.
12508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.getSizeInBits() != 128)
12509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
12510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1251119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
1251219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
1251319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // consecutive, non-overlapping, and in the right order.
12514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 16> Elts;
12515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1251619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
1251719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
12518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
12519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
12520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1252124809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens/// \brief Detect bitcasts between i32 to x86mmx low word. Since MMX types are
1252224809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens/// special and don't usually play with other vector types, it's better to
1252324809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens/// handle them early to be sure we emit efficient code by avoiding
1252424809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens/// store-load conversions.
1252524809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capensstatic SDValue PerformBITCASTCombine(SDNode *N, SelectionDAG &DAG) {
1252624809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  if (N->getValueType(0) != MVT::x86mmx ||
1252724809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens      N->getOperand(0)->getOpcode() != ISD::BUILD_VECTOR ||
1252824809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens      N->getOperand(0)->getValueType(0) != MVT::v2i32)
1252924809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens    return SDValue();
1253024809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens
1253124809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  SDValue V = N->getOperand(0);
1253224809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  ConstantSDNode *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
1253324809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  if (C && C->getZExtValue() == 0 && V.getOperand(0).getValueType() == MVT::i32)
1253424809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens    return DAG.getNode(X86ISD::MMX_MOVW2D, V.getOperand(0).getDebugLoc(),
1253524809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens                       N->getValueType(0), V.getOperand(0));
1253624809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens
1253724809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  return SDValue();
1253824809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens}
1253924809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens
1254019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
1254119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// generation and convert it from being a bunch of shuffles and extracts
1254219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// to a simple store and scalar loads to extract the elements.
12543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
12544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                const TargetLowering &TLI) {
12545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue InputVector = N->getOperand(0);
1254624809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  // Detect whether we are trying to convert from mmx to i32 and the bitcast
1254724809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  // from mmx to v2i32 has a single usage.
1254824809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
1254924809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens      InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
1255024809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens      InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
1255124809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens    return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
1255224809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens                       N->getValueType(0),
1255324809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens                       InputVector.getNode()->getOperand(0));
12554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Only operate on vectors of 4 elements, where the alternative shuffling
12556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // gets to be more expensive.
12557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InputVector.getValueType() != MVT::v4i32)
12558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
12559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
12561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // single use which is a sign-extend or zero-extend, and all elements are
12562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // used.
12563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDNode *, 4> Uses;
12564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned ExtractedElements = 0;
12565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
12566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
12567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (UI.getUse().getResNo() != InputVector.getResNo())
12568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
12569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDNode *Extract = *UI;
12571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
12573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Extract->getValueType(0) != MVT::i32)
12575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
12576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Extract->hasOneUse())
12577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
12578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
12579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
12580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
12581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isa<ConstantSDNode>(Extract->getOperand(1)))
12582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
12583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Record which element was extracted.
12585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ExtractedElements |=
12586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
12587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Uses.push_back(Extract);
12589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If not all the elements were used, this may not be worthwhile.
12592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ExtractedElements != 15)
12593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
12594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Ok, we've now decided to do the transformation.
12596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = InputVector.getDebugLoc();
12597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store the value to a temporary stack slot.
12599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
1260019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
1260119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachinePointerInfo(), false, false, 0);
12602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Replace each use (extract) with a load of the appropriate element.
12604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
12605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       UE = Uses.end(); UI != UE; ++UI) {
12606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDNode *Extract = *UI;
12607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1260819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // cOMpute the element's address.
12609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Idx = Extract->getOperand(1);
12610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned EltSize =
12611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
12612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
12613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
12614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1261519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
1261619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     StackPtr, OffsetVal);
12617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Load the scalar.
12619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
1262019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     ScalarAddr, MachinePointerInfo(),
1262119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     false, false, 0);
12622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Replace the exact with the load.
12624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
12625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The replacement was made in place; don't return anything.
12628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
12629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
12630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1263119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
1263219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// nodes.
12633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
12634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const X86Subtarget *Subtarget) {
12635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = N->getDebugLoc();
12636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Cond = N->getOperand(0);
12637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the LHS/RHS of the select.
12638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue LHS = N->getOperand(1);
12639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RHS = N->getOperand(2);
1264019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = LHS.getValueType();
12641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we have SSE[12] support, try to form min/max nodes. SSE min/max
12643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // instructions match the semantics of the common C idiom x<y?x:y but not
12644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // x<=y?x:y, because of how they handle negative zero (which can be
12645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ignored in unsafe-math mode).
1264619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
1264719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
1264819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (Subtarget->hasXMMInt() ||
1264919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
12650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
12651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Opcode = 0;
12653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check for x CC y ? x : y.
12654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
12655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        DAG.isEqualTo(RHS, Cond.getOperand(1))) {
12656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (CC) {
12657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
12658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETULT:
12659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a min would handle NaNs incorrectly, and swapping
12660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // the operands would cause it to handle comparisons between positive
12661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // and negative zero incorrectly.
12662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
12663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!UnsafeFPMath &&
12664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
12666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          std::swap(LHS, RHS);
12667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMIN;
12669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOLE:
12671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a min would handle comparisons between positive
12672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // and negative zero incorrectly.
12673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!UnsafeFPMath &&
12674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
12676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMIN;
12677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETULE:
12679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a min would handle both negative zeros and NaNs
12680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // incorrectly, but we can swap the operands to fix both.
12681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        std::swap(LHS, RHS);
12682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOLT:
12683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETLT:
12684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETLE:
12685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMIN;
12686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOGE:
12689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a max would handle comparisons between positive
12690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // and negative zero incorrectly.
12691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!UnsafeFPMath &&
1269219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
12693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
12694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMAX;
12695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETUGT:
12697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a max would handle NaNs incorrectly, and swapping
12698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // the operands would cause it to handle comparisons between positive
12699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // and negative zero incorrectly.
12700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
12701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!UnsafeFPMath &&
12702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
12703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
12704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          std::swap(LHS, RHS);
12705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMAX;
12707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETUGE:
12709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a max would handle both negative zeros and NaNs
12710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // incorrectly, but we can swap the operands to fix both.
12711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        std::swap(LHS, RHS);
12712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOGT:
12713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETGT:
12714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETGE:
12715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMAX;
12716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check for x CC y ? y : x -- a min/max with reversed arms.
12719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
12720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               DAG.isEqualTo(RHS, Cond.getOperand(0))) {
12721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (CC) {
12722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
12723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOGE:
12724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a min would handle comparisons between positive
12725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // and negative zero incorrectly, and swapping the operands would
12726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // cause it to handle NaNs incorrectly.
12727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!UnsafeFPMath &&
12728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
12729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
12730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
12731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          std::swap(LHS, RHS);
12732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMIN;
12734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETUGT:
12736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a min would handle NaNs incorrectly.
12737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!UnsafeFPMath &&
12738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
12739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
12740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMIN;
12741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETUGE:
12743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a min would handle both negative zeros and NaNs
12744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // incorrectly, but we can swap the operands to fix both.
12745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        std::swap(LHS, RHS);
12746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOGT:
12747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETGT:
12748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETGE:
12749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMIN;
12750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETULT:
12753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a max would handle NaNs incorrectly.
12754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
12755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
12756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMAX;
12757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOLE:
12759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a max would handle comparisons between positive
12760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // and negative zero incorrectly, and swapping the operands would
12761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // cause it to handle NaNs incorrectly.
12762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!UnsafeFPMath &&
12763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
12764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
12765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
12766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          std::swap(LHS, RHS);
12767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMAX;
12769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETULE:
12771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Converting this to a max would handle both negative zeros and NaNs
12772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // incorrectly, but we can swap the operands to fix both.
12773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        std::swap(LHS, RHS);
12774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETOLT:
12775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETLT:
12776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case ISD::SETLE:
12777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Opcode = X86ISD::FMAX;
12778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
12779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
12781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Opcode)
12783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
12784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is a select between two integer constants, try to do some
12787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // optimizations.
12788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
12789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
12790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Don't do this for crazy integer types.
12791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
12792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If this is efficiently invertible, canonicalize the LHSC/RHSC values
12793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // so that TrueC (the true value) is larger than FalseC.
12794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        bool NeedsCondInvert = false;
12795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
12797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // Efficiently invertible.
12798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            (Cond.getOpcode() == ISD::SETCC ||  // setcc -> invertible.
12799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             (Cond.getOpcode() == ISD::XOR &&   // xor(X, C) -> invertible.
12800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              isa<ConstantSDNode>(Cond.getOperand(1))))) {
12801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          NeedsCondInvert = true;
12802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          std::swap(TrueC, FalseC);
12803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Optimize C ? 8 : 0 -> zext(C) << 3.  Likewise for any pow2/0.
12806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (FalseC->getAPIntValue() == 0 &&
12807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            TrueC->getAPIntValue().isPowerOf2()) {
12808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (NeedsCondInvert) // Invert the condition if needed.
12809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getConstant(1, Cond.getValueType()));
12811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Zero extend the condition if needed.
12813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
12814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
12817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getConstant(ShAmt, MVT::i8));
12818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
12821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
12822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (NeedsCondInvert) // Invert the condition if needed.
12823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getConstant(1, Cond.getValueType()));
12825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Zero extend the condition if needed.
12827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             FalseC->getValueType(0), Cond);
12829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             SDValue(FalseC, 0));
12831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Optimize cases that will turn into an LEA instruction.  This requires
12834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
12835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
12836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
12837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
12838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          bool isFastMultiplier = false;
12840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Diff < 10) {
12841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            switch ((unsigned char)Diff) {
12842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              default: break;
12843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 1:  // result = add base, cond
12844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 2:  // result = lea base(    , cond*2)
12845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 3:  // result = lea base(cond, cond*2)
12846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 4:  // result = lea base(    , cond*4)
12847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 5:  // result = lea base(cond, cond*4)
12848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 8:  // result = lea base(    , cond*8)
12849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              case 9:  // result = lea base(cond, cond*8)
12850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                isFastMultiplier = true;
12851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                break;
12852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            }
12853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
12854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (isFastMultiplier) {
12856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            if (NeedsCondInvert) // Invert the condition if needed.
12858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
12859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 DAG.getConstant(1, Cond.getValueType()));
12860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // Zero extend the condition if needed.
12862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Cond);
12864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // Scale the condition by the difference.
12865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            if (Diff != 1)
12866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 DAG.getConstant(Diff, Cond.getValueType()));
12868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            // Add the base if non-zero.
12870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            if (FalseC->getAPIntValue() != 0)
12871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 SDValue(FalseC, 0));
12873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return Cond;
12874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
12875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
12880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
12881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
12883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
12884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  TargetLowering::DAGCombinerInfo &DCI) {
12885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = N->getDebugLoc();
12886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the flag operand isn't dead, don't touch this CMOV.
12888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
12889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
12890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1289119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue FalseOp = N->getOperand(0);
1289219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue TrueOp = N->getOperand(1);
1289319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
1289419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Cond = N->getOperand(3);
1289519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CC == X86::COND_E || CC == X86::COND_NE) {
1289619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch (Cond.getOpcode()) {
1289719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    default: break;
1289819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::BSR:
1289919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case X86ISD::BSF:
1290019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
1290119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (DAG.isKnownNeverZero(Cond.getOperand(0)))
1290219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return (CC == X86::COND_E) ? FalseOp : TrueOp;
1290319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1290419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1290519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
12906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is a select between two integer constants, try to do some
12907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // optimizations.  Note that the operands are ordered the opposite of SELECT
12908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // operands.
1290919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
1291019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
12911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
12912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // larger than FalseC (the false value).
12913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
12914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CC = X86::GetOppositeBranchCondition(CC);
12915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        std::swap(TrueC, FalseC);
12916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3.  Likewise for any pow2/0.
12919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // This is efficient for any integer data type (including i8/i16) and
12920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // shift amount.
12921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
12922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(CC, MVT::i8), Cond);
12924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Zero extend the condition if needed.
12926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
12927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned ShAmt = TrueC->getAPIntValue().logBase2();
12929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
12930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(ShAmt, MVT::i8));
12931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (N->getNumValues() == 2)  // Dead flag value?
12932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DCI.CombineTo(N, Cond, SDValue());
12933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return Cond;
12934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.  This is efficient
12937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // for any integer data type, including i8/i16.
12938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
12939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(CC, MVT::i8), Cond);
12941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Zero extend the condition if needed.
12943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
12944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           FalseC->getValueType(0), Cond);
12945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           SDValue(FalseC, 0));
12947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (N->getNumValues() == 2)  // Dead flag value?
12949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return DCI.CombineTo(N, Cond, SDValue());
12950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return Cond;
12951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Optimize cases that will turn into an LEA instruction.  This requires
12954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
12955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
12956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
12957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
12958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        bool isFastMultiplier = false;
12960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Diff < 10) {
12961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          switch ((unsigned char)Diff) {
12962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          default: break;
12963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 1:  // result = add base, cond
12964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 2:  // result = lea base(    , cond*2)
12965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 3:  // result = lea base(cond, cond*2)
12966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 4:  // result = lea base(    , cond*4)
12967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 5:  // result = lea base(cond, cond*4)
12968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 8:  // result = lea base(    , cond*8)
12969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case 9:  // result = lea base(cond, cond*8)
12970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            isFastMultiplier = true;
12971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
12972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
12973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (isFastMultiplier) {
12976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
12977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getConstant(CC, MVT::i8), Cond);
12979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Zero extend the condition if needed.
12980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
12981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Cond);
12982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Scale the condition by the difference.
12983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Diff != 1)
12984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
12985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DAG.getConstant(Diff, Cond.getValueType()));
12986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Add the base if non-zero.
12988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (FalseC->getAPIntValue() != 0)
12989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
12990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               SDValue(FalseC, 0));
12991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (N->getNumValues() == 2)  // Dead flag value?
12992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            return DCI.CombineTo(N, Cond, SDValue());
12993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return Cond;
12994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
12995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
12996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
12997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
12998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
12999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PerformMulCombine - Optimize a single multiply with constant into two
13003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// in order to implement it with two cheaper instructions, e.g.
13004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LEA + SHL, LEA + LEA.
13005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
13006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 TargetLowering::DAGCombinerInfo &DCI) {
13007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
13008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
13011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::i64)
13012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
13015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!C)
13016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t MulAmt = C->getZExtValue();
13018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
13019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t MulAmt1 = 0;
13022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t MulAmt2 = 0;
13023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((MulAmt % 9) == 0) {
13024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MulAmt1 = 9;
13025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MulAmt2 = MulAmt / 9;
13026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if ((MulAmt % 5) == 0) {
13027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MulAmt1 = 5;
13028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MulAmt2 = MulAmt / 5;
13029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if ((MulAmt % 3) == 0) {
13030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MulAmt1 = 3;
13031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MulAmt2 = MulAmt / 3;
13032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MulAmt2 &&
13034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
13035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DebugLoc DL = N->getDebugLoc();
13036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isPowerOf2_64(MulAmt2) &&
13038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
13039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If second multiplifer is pow2, issue it first. We want the multiply by
13040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // 3, 5, or 9 to be folded into the addressing mode unless the lone use
13041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // is an add.
13042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::swap(MulAmt1, MulAmt2);
13043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewMul;
13045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isPowerOf2_64(MulAmt1))
13046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
13047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
13048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
13049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
13050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(MulAmt1, VT));
13051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isPowerOf2_64(MulAmt2))
13053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
13054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
13055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
13056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
13057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getConstant(MulAmt2, VT));
13058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Do not add new nodes to DAG combiner worklist.
13060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DCI.CombineTo(N, NewMul, false);
13061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
13063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
13066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N0 = N->getOperand(0);
13067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N1 = N->getOperand(1);
13068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
13069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N0.getValueType();
13070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
13072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // since the result of setcc_c is all zero's or all ones.
13073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N1C && N0.getOpcode() == ISD::AND &&
13074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0.getOperand(1).getOpcode() == ISD::Constant) {
13075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N00 = N0.getOperand(0);
13076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
13077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ((N00.getOpcode() == ISD::ANY_EXTEND ||
13078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          N00.getOpcode() == ISD::ZERO_EXTEND) &&
13079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
13080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
13081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt ShAmt = N1C->getAPIntValue();
13082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Mask = Mask.shl(ShAmt);
13083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Mask != 0)
13084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
13085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           N00, DAG.getConstant(Mask, VT));
13086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
13090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
13093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///                       when possible.
13094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
13095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const X86Subtarget *Subtarget) {
13096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
13097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!VT.isVector() && VT.isInteger() &&
13098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N->getOpcode() == ISD::SHL)
13099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return PerformSHLCombine(N, DAG);
13100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // On X86 with SSE2 support, we can transform this to a vector shift if
13102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // all elements are shifted by the same amount.  We can't do this in legalize
13103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // because the a constant vector is typically transformed to a constant pool
13104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // so we have no knowledge of the shift amount.
1310519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Subtarget->hasXMMInt())
13106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
13109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShAmtOp = N->getOperand(1);
13112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT EltVT = VT.getVectorElementType();
13113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = N->getDebugLoc();
13114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue BaseShAmt = SDValue();
13115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
13116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumElts = VT.getVectorNumElements();
13117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned i = 0;
13118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (; i != NumElts; ++i) {
13119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Arg = ShAmtOp.getOperand(i);
13120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Arg.getOpcode() == ISD::UNDEF) continue;
13121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BaseShAmt = Arg;
13122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
13123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (; i != NumElts; ++i) {
13125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Arg = ShAmtOp.getOperand(i);
13126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Arg.getOpcode() == ISD::UNDEF) continue;
13127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Arg != BaseShAmt) {
13128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return SDValue();
13129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
13130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
13132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
13133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue InVec = ShAmtOp.getOperand(0);
13134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
13135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned NumElts = InVec.getValueType().getVectorNumElements();
13136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned i = 0;
13137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (; i != NumElts; ++i) {
13138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue Arg = InVec.getOperand(i);
13139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Arg.getOpcode() == ISD::UNDEF) continue;
13140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BaseShAmt = Arg;
13141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
13142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
13143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
13145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
13146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         if (C->getZExtValue() == SplatIdx)
13147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           BaseShAmt = InVec.getOperand(1);
13148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       }
13149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (BaseShAmt.getNode() == 0)
13151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
13152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              DAG.getIntPtrConstant(0));
13153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else
13154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The shift amount is an i32.
13157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EltVT.bitsGT(MVT::i32))
13158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
13159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (EltVT.bitsLT(MVT::i32))
13160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
13161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The shift amount is identical so we can do a vector shift.
13163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue  ValOp = N->getOperand(0);
13164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (N->getOpcode()) {
13165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
13166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Unknown shift opcode!");
13167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
13168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SHL:
13169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v2i64)
13170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
13172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v4i32)
13174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
13176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v8i16)
13178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
13180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
13182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRA:
13183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v4i32)
13184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
13186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v8i16)
13188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
13190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
13192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRL:
13193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v2i64)
13194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
13196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::v4i32)
13198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
13200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT ==  MVT::v8i16)
13202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
13203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
13204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         ValOp, BaseShAmt);
13205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
13206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
13208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1321019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1321119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// CMPEQCombine - Recognize the distinctive  (AND (setcc ...) (setcc ..))
1321219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
1321319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// and friends.  Likewise for OR -> CMPNEQSS.
1321419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
1321519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            TargetLowering::DAGCombinerInfo &DCI,
1321619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            const X86Subtarget *Subtarget) {
1321719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned opcode;
1321819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1321919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
1322019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // we're requiring SSE2 for both.
1322119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasXMMInt() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
1322219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue N0 = N->getOperand(0);
1322319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue N1 = N->getOperand(1);
1322419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue CMP0 = N0->getOperand(1);
1322519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue CMP1 = N1->getOperand(1);
1322619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc DL = N->getDebugLoc();
1322719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1322819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // The SETCCs should both refer to the same CMP.
1322919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
1323019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return SDValue();
1323119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1323219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue CMP00 = CMP0->getOperand(0);
1323319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue CMP01 = CMP0->getOperand(1);
1323419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT     VT    = CMP00.getValueType();
1323519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1323619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VT == MVT::f32 || VT == MVT::f64) {
1323719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      bool ExpectingFlags = false;
1323819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Check for any users that want flags:
1323919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (SDNode::use_iterator UI = N->use_begin(),
1324019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             UE = N->use_end();
1324119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman           !ExpectingFlags && UI != UE; ++UI)
1324219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        switch (UI->getOpcode()) {
1324319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        default:
1324419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::BR_CC:
1324519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::BRCOND:
1324619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::SELECT:
1324719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ExpectingFlags = true;
1324819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
1324919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::CopyToReg:
1325019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::SIGN_EXTEND:
1325119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::ZERO_EXTEND:
1325219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case ISD::ANY_EXTEND:
1325319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
1325419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1325519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1325619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!ExpectingFlags) {
1325719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
1325819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
1325919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1326019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
1326119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          X86::CondCode tmp = cc0;
1326219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          cc0 = cc1;
1326319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          cc1 = tmp;
1326419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1326519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1326619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if ((cc0 == X86::COND_E  && cc1 == X86::COND_NP) ||
1326719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
1326819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          bool is64BitFP = (CMP00.getValueType() == MVT::f64);
1326919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          X86ISD::NodeType NTOperator = is64BitFP ?
1327019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            X86ISD::FSETCCsd : X86ISD::FSETCCss;
1327119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // FIXME: need symbolic constants for these magic numbers.
1327219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          // See X86ATTInstPrinter.cpp:printSSECC().
1327319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
1327419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
1327519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                              DAG.getConstant(x86cc, MVT::i8));
1327619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
1327719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                              OnesOrZeroesF);
1327819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
1327919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      DAG.getConstant(1, MVT::i32));
1328019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
1328119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return OneBitOfTruth;
1328219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1328319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1328419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1328519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1328619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1328719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1328819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1328919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
1329019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// so it can be folded inside ANDNP.
1329119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CanFoldXORWithAllOnes(const SDNode *N) {
1329219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
1329319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1329419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Match direct AllOnes for 128 and 256-bit vectors
1329519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ISD::isBuildVectorAllOnes(N))
1329619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
1329719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1329819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look through a bit convert.
1329919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N->getOpcode() == ISD::BITCAST)
1330019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    N = N->getOperand(0).getNode();
1330119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1330219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Sometimes the operand may come from a insert_subvector building a 256-bit
1330319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // allones vector
1330419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256 &&
1330519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      N->getOpcode() == ISD::INSERT_SUBVECTOR) {
1330619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V1 = N->getOperand(0);
1330719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue V2 = N->getOperand(1);
1330819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1330919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
1331019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        V1.getOperand(0).getOpcode() == ISD::UNDEF &&
1331119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
1331219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ISD::isBuildVectorAllOnes(V2.getNode()))
1331319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return true;
1331419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1331519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1331619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
1331719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1331819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1331919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
1332019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 TargetLowering::DAGCombinerInfo &DCI,
1332119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 const X86Subtarget *Subtarget) {
1332219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (DCI.isBeforeLegalizeOps())
1332319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1332419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1332519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
1332619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (R.getNode())
1332719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return R;
1332819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1332919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
1333019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1333119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Create ANDN instructions
1333219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
1333319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue N0 = N->getOperand(0);
1333419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue N1 = N->getOperand(1);
1333519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc DL = N->getDebugLoc();
1333619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1333719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Check LHS for not
1333819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
1333919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
1334019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Check RHS for not
1334119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
1334219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
1334319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1334419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1334519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1334619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1334719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Want to form ANDNP nodes:
1334819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 1) In the hopes of then easily combining them with OR and AND nodes
1334919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //    to form PBLEND/PSIGN.
1335019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 2) To match ANDN packed intrinsics
1335119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT != MVT::v2i64 && VT != MVT::v4i64)
1335219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1335319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1335419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue N0 = N->getOperand(0);
1335519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue N1 = N->getOperand(1);
1335619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = N->getDebugLoc();
1335719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1335819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check LHS for vnot
1335919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N0.getOpcode() == ISD::XOR &&
1336019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
1336119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
1336219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
1336319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1336419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check RHS for vnot
1336519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (N1.getOpcode() == ISD::XOR &&
1336619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
1336719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
1336819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
1336919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1337019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1337119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1337219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
13374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                TargetLowering::DAGCombinerInfo &DCI,
13375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                const X86Subtarget *Subtarget) {
13376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DCI.isBeforeLegalizeOps())
13377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1337919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
1338019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (R.getNode())
1338119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return R;
1338219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
1338419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
13385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N0 = N->getOperand(0);
13388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N1 = N->getOperand(1);
1338919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1339019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // look for psign/blend
1339119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget->hasSSSE3() || Subtarget->hasAVX()) {
1339219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VT == MVT::v2i64) {
1339319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Canonicalize pandn to RHS
1339419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (N0.getOpcode() == X86ISD::ANDNP)
1339519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        std::swap(N0, N1);
1339619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // or (and (m, x), (pandn m, y))
1339719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
1339819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue Mask = N1.getOperand(0);
1339919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue X    = N1.getOperand(1);
1340019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue Y;
1340119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (N0.getOperand(0) == Mask)
1340219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Y = N0.getOperand(1);
1340319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (N0.getOperand(1) == Mask)
1340419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Y = N0.getOperand(0);
1340519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1340619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Check to see if the mask appeared in both the AND and ANDNP and
1340719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!Y.getNode())
1340819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return SDValue();
1340919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1341019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
1341119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Mask.getOpcode() != ISD::BITCAST ||
1341219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            X.getOpcode() != ISD::BITCAST ||
1341319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            Y.getOpcode() != ISD::BITCAST)
1341419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return SDValue();
1341519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1341619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Look through mask bitcast.
1341719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Mask = Mask.getOperand(0);
1341819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        EVT MaskVT = Mask.getValueType();
1341919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1342019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Validate that the Mask operand is a vector sra node.  The sra node
1342119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // will be an intrinsic.
1342219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
1342319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return SDValue();
1342419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1342519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
1342619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // there is no psrai.b
1342719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
1342819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_sse2_psrai_w:
1342919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Intrinsic::x86_sse2_psrai_d:
1343019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          break;
1343119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        default: return SDValue();
1343219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1343319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1343419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Check that the SRA is all signbits.
1343519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue SraC = Mask.getOperand(2);
1343619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned SraAmt  = cast<ConstantSDNode>(SraC)->getZExtValue();
1343719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
1343819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if ((SraAmt + 1) != EltBits)
1343919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return SDValue();
1344019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1344119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        DebugLoc DL = N->getDebugLoc();
1344219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1344319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Now we know we at least have a plendvb with the mask val.  See if
1344419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // we can form a psignb/w/d.
1344519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // psign = x.type == y.type == mask.type && y = sub(0, x);
1344619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        X = X.getOperand(0);
1344719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Y = Y.getOperand(0);
1344819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
1344919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
1345019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
1345119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          unsigned Opc = 0;
1345219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          switch (EltBits) {
1345319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case 8: Opc = X86ISD::PSIGNB; break;
1345419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case 16: Opc = X86ISD::PSIGNW; break;
1345519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case 32: Opc = X86ISD::PSIGND; break;
1345619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          default: break;
1345719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
1345819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (Opc) {
1345919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
1346019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
1346119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
1346219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1346319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // PBLENDVB only available on SSE 4.1
1346419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!(Subtarget->hasSSE41() || Subtarget->hasAVX()))
1346519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return SDValue();
1346619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1346719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
1346819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
1346919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
1347019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Mask = DAG.getNode(ISD::VSELECT, DL, MVT::v16i8, Mask, X, Y);
1347119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
1347219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1347319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1347419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1347519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1347619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
13477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
13478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(N0, N1);
13479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
13480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!N0.hasOneUse() || !N1.hasOneUse())
13482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShAmt0 = N0.getOperand(1);
13485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmt0.getValueType() != MVT::i8)
13486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue ShAmt1 = N1.getOperand(1);
13488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmt1.getValueType() != MVT::i8)
13489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmt0.getOpcode() == ISD::TRUNCATE)
13491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShAmt0 = ShAmt0.getOperand(0);
13492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmt1.getOpcode() == ISD::TRUNCATE)
13493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ShAmt1 = ShAmt1.getOperand(0);
13494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc DL = N->getDebugLoc();
13496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc = X86ISD::SHLD;
13497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op0 = N0.getOperand(0);
13498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op1 = N1.getOperand(0);
13499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmt0.getOpcode() == ISD::SUB) {
13500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = X86ISD::SHRD;
13501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(Op0, Op1);
13502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::swap(ShAmt0, ShAmt1);
13503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Bits = VT.getSizeInBits();
13506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ShAmt1.getOpcode() == ISD::SUB) {
13507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Sum = ShAmt1.getOperand(0);
13508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
13509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
13510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
13511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ShAmt1Op1 = ShAmt1Op1.getOperand(0);
13512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
13513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return DAG.getNode(Opc, DL, VT,
13514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           Op0, Op1,
13515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           DAG.getNode(ISD::TRUNCATE, DL,
13516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       MVT::i8, ShAmt0));
13517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
13519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
13520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ShAmt0C &&
13521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
13522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getNode(Opc, DL, VT,
13523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         N0.getOperand(0), N1.getOperand(0),
13524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getNode(ISD::TRUNCATE, DL,
13525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       MVT::i8, ShAmt0));
13526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
13529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1353119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
1353219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
1353319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   const X86Subtarget *Subtarget) {
1353419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LoadSDNode *Ld = cast<LoadSDNode>(N);
1353519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT RegVT = Ld->getValueType(0);
1353619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT MemVT = Ld->getMemoryVT();
1353719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Ld->getDebugLoc();
1353819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1353919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1354019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ISD::LoadExtType Ext = Ld->getExtensionType();
1354119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1354219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is a vector EXT Load then attempt to optimize it using a
1354319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // shuffle. We need SSE4 for the shuffles.
1354419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // TODO: It is possible to support ZExt by zeroing the undef values
1354519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // during the shuffle phase or after the shuffle.
1354619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (RegVT.isVector() && Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
1354719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(MemVT != RegVT && "Cannot extend to the same type");
1354819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(MemVT.isVector() && "Must load a vector from memory");
1354919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1355019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumElems = RegVT.getVectorNumElements();
1355119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned RegSz = RegVT.getSizeInBits();
1355219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned MemSz = MemVT.getSizeInBits();
1355319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(RegSz > MemSz && "Register size must be greater than the mem size");
1355419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // All sizes must be a power of two
1355519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isPowerOf2_32(RegSz * MemSz * NumElems)) return SDValue();
1355619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1355719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Attempt to load the original value using a single load op.
1355819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Find a scalar type which is equal to the loaded word size.
1355919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MVT SclrLoadTy = MVT::i8;
1356019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
1356119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
1356219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MVT Tp = (MVT::SimpleValueType)tp;
1356319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (TLI.isTypeLegal(Tp) &&  Tp.getSizeInBits() == MemSz) {
1356419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SclrLoadTy = Tp;
1356519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        break;
1356619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1356719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1356819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1356919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Proceed if a load word is found.
1357019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SclrLoadTy.getSizeInBits() != MemSz) return SDValue();
1357119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1357219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
1357319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      RegSz/SclrLoadTy.getSizeInBits());
1357419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1357519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
1357619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  RegSz/MemVT.getScalarType().getSizeInBits());
1357719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Can't shuffle using an illegal type.
1357819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
1357919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1358019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Perform a single load.
1358119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
1358219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  Ld->getBasePtr(),
1358319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  Ld->getPointerInfo(), Ld->isVolatile(),
1358419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  Ld->isNonTemporal(), Ld->getAlignment());
1358519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1358619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Insert the word loaded into a vector.
1358719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ScalarInVector = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
1358819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      LoadUnitVecVT, ScalarLoad);
1358919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1359019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Bitcast the loaded value to a vector of the original element type, in
1359119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // the size of the target vector type.
1359219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, ScalarInVector);
1359319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned SizeRatio = RegSz/MemSz;
1359419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1359519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Redistribute the loaded elements into the different locations.
1359619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
1359719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i < NumElems; i++) ShuffleVec[i*SizeRatio] = i;
1359819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1359919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
1360019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG.getUNDEF(SlicedVec.getValueType()),
1360119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                ShuffleVec.data());
1360219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1360319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Bitcast to the requested type.
1360419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
1360519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Replace the original load with the new sequence
1360619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // and return the new chain.
1360719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Shuff);
1360819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue(ScalarLoad.getNode(), 1);
1360919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1361019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1361119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1361219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1361319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
13615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
13616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const X86Subtarget *Subtarget) {
1361719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  StoreSDNode *St = cast<StoreSDNode>(N);
1361819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = St->getValue().getValueType();
1361919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT StVT = St->getMemoryVT();
1362019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = St->getDebugLoc();
1362119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue StoredVal = St->getOperand(1);
1362219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1362319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1362419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we are saving a concatination of two XMM registers, perform two stores.
1362519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // This is better in Sandy Bridge cause one 256-bit mem op is done via two
1362619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 128-bit ones. If in the future the cost becomes only one memory access the
1362719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // first version would be better.
1362819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VT.getSizeInBits() == 256 &&
1362919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
1363019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StoredVal.getNumOperands() == 2) {
1363119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1363219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Value0 = StoredVal.getOperand(0);
1363319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Value1 = StoredVal.getOperand(1);
1363419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1363519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
1363619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ptr0 = St->getBasePtr();
1363719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
1363819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1363919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
1364019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->getPointerInfo(), St->isVolatile(),
1364119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->isNonTemporal(), St->getAlignment());
1364219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
1364319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->getPointerInfo(), St->isVolatile(),
1364419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->isNonTemporal(), St->getAlignment());
1364519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
1364619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1364719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1364819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Optimize trunc store (of multiple scalars) to shuffle and store.
1364919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // First, pack all of the elements in one place. Next, store to memory
1365019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // in fewer chunks.
1365119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (St->isTruncatingStore() && VT.isVector()) {
1365219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1365319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumElems = VT.getVectorNumElements();
1365419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(StVT != VT && "Cannot truncate to the same type");
1365519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned FromSz = VT.getVectorElementType().getSizeInBits();
1365619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
1365719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1365819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // From, To sizes and ElemCount must be pow of two
1365919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
1366019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // We are going to use the original vector elt for storing.
1366119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Accumulated smaller vector elements must be a multiple of the store size.
1366219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (0 != (NumElems * FromSz) % ToSz) return SDValue();
1366319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1366419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned SizeRatio  = FromSz / ToSz;
1366519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1366619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
1366719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1366819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Create a type on which we perform the shuffle
1366919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
1367019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            StVT.getScalarType(), NumElems*SizeRatio);
1367119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1367219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
1367319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1367419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
1367519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
1367619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i < NumElems; i++ ) ShuffleVec[i] = i * SizeRatio;
1367719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1367819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Can't shuffle using an illegal type
1367919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
1368019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1368119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
1368219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG.getUNDEF(WideVec.getValueType()),
1368319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                ShuffleVec.data());
1368419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // At this point all of the data is stored at the bottom of the
1368519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // register. We now need to save it to mem.
1368619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1368719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Find the largest store unit
1368819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MVT StoreType = MVT::i8;
1368919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
1369019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
1369119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MVT Tp = (MVT::SimpleValueType)tp;
1369219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (TLI.isTypeLegal(Tp) && StoreType.getSizeInBits() < NumElems * ToSz)
1369319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        StoreType = Tp;
1369419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1369519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1369619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Bitcast the original vector into a vector of store-size units
1369719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
1369819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
1369919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
1370019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
1370119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallVector<SDValue, 8> Chains;
1370219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
1370319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        TLI.getPointerTy());
1370419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Ptr = St->getBasePtr();
1370519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1370619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Perform one or more big stores into memory.
1370719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i < (ToSz*NumElems)/StoreType.getSizeInBits() ; i++) {
1370819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1370919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   StoreType, ShuffWide,
1371019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   DAG.getIntPtrConstant(i));
1371119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
1371219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->getPointerInfo(), St->isVolatile(),
1371319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->isNonTemporal(), St->getAlignment());
1371419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
1371519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Chains.push_back(Ch);
1371619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1371719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1371819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
1371919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Chains.size());
1372019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1372119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1372219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Turn load->store of MMX types into GPR load/stores.  This avoids clobbering
13724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the FP state in cases where an emms may be missing.
13725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // A preferable solution to the general problem is to figure out the right
13726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // places to insert EMMS.  This qualifies as a quick hack.
13727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
13729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT.getSizeInBits() != 64)
13730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SDValue();
13731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function *F = DAG.getMachineFunction().getFunction();
13733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
13734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
1373519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     && Subtarget->hasXMMInt();
13736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((VT.isVector() ||
13737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
13738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isa<LoadSDNode>(St->getValue()) &&
13739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !cast<LoadSDNode>(St->getValue())->isVolatile() &&
13740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      St->getChain().hasOneUse() && !St->isVolatile()) {
13741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDNode* LdVal = St->getValue().getNode();
13742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LoadSDNode *Ld = 0;
13743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int TokenFactorIndex = -1;
13744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<SDValue, 8> Ops;
13745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDNode* ChainVal = St->getChain().getNode();
13746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Must be a store of a load.  We currently handle two cases:  the load
13747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // is a direct child, and it's under an intervening TokenFactor.  It is
13748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // possible to dig deeper under nested TokenFactors.
13749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ChainVal == LdVal)
13750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ld = cast<LoadSDNode>(St->getChain());
13751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (St->getValue().hasOneUse() &&
13752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             ChainVal->getOpcode() == ISD::TokenFactor) {
13753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
13754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ChainVal->getOperand(i).getNode() == LdVal) {
13755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          TokenFactorIndex = i;
13756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Ld = cast<LoadSDNode>(St->getValue());
13757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        } else
13758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Ops.push_back(ChainVal->getOperand(i));
13759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
13760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Ld || !ISD::isNormalLoad(Ld))
13763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
13764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is not the MMX case, i.e. we are just turning i64 load/store
13766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // into f64 load/store, avoid the transformation if there are multiple
13767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // uses of the loaded value.
13768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
13769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
13770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DebugLoc LdDL = Ld->getDebugLoc();
13772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DebugLoc StDL = N->getDebugLoc();
13773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we are a 64-bit capable x86, lower to a single movq load/store pair.
13774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
13775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // pair instead.
13776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget->is64Bit() || F64IsLegal) {
13777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
1377819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
1377919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  Ld->getPointerInfo(), Ld->isVolatile(),
13780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  Ld->isNonTemporal(), Ld->getAlignment());
13781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue NewChain = NewLd.getValue(1);
13782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (TokenFactorIndex != -1) {
13783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Ops.push_back(NewChain);
13784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
13785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Ops.size());
13786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
13787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
1378819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          St->getPointerInfo(),
13789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          St->isVolatile(), St->isNonTemporal(),
13790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          St->getAlignment());
13791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, lower to two pairs of 32-bit loads / stores.
13794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LoAddr = Ld->getBasePtr();
13795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
13796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 DAG.getConstant(4, MVT::i32));
13797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
1379919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Ld->getPointerInfo(),
13800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Ld->isVolatile(), Ld->isNonTemporal(),
13801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Ld->getAlignment());
13802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
1380319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Ld->getPointerInfo().getWithOffset(4),
13804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Ld->isVolatile(), Ld->isNonTemporal(),
13805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               MinAlign(Ld->getAlignment(), 4));
13806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue NewChain = LoLd.getValue(1);
13808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TokenFactorIndex != -1) {
13809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ops.push_back(LoLd);
13810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ops.push_back(HiLd);
13811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
13812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Ops.size());
13813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
13814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LoAddr = St->getBasePtr();
13816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
13817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         DAG.getConstant(4, MVT::i32));
13818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
1382019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->getPointerInfo(),
13821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                St->isVolatile(), St->isNonTemporal(),
13822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                St->getAlignment());
13823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
1382419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                St->getPointerInfo().getWithOffset(4),
13825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                St->isVolatile(),
13826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                St->isNonTemporal(),
13827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                MinAlign(St->getAlignment(), 4));
13828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
13829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
13830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
13831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1383319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
1383419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and return the operands for the horizontal operation in LHS and RHS.  A
1383519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// horizontal operation performs the binary operation on successive elements
1383619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// of its first operand, then on successive elements of its second operand,
1383719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// returning the resulting values in a vector.  For example, if
1383819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///   A = < float a0, float a1, float a2, float a3 >
1383919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and
1384019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///   B = < float b0, float b1, float b2, float b3 >
1384119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// then the result of doing a horizontal operation on A and B is
1384219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///   A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
1384319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
1384419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// A horizontal-op B, for some already available A and B, and if so then LHS is
1384519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// set to A, RHS to B, and the routine returns 'true'.
1384619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Note that the binary operation should have the property that if one of the
1384719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// operands is UNDEF then the result is UNDEF.
1384819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool isCommutative) {
1384919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look for the following pattern: if
1385019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   A = < float a0, float a1, float a2, float a3 >
1385119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   B = < float b0, float b1, float b2, float b3 >
1385219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // and
1385319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
1385419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
1385519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
1385619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // which is A horizontal-op B.
1385719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1385819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // At least one of the operands should be a vector shuffle.
1385919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
1386019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
1386119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
1386219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1386319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = LHS.getValueType();
1386419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned N = VT.getVectorNumElements();
1386519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1386619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // View LHS in the form
1386719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   LHS = VECTOR_SHUFFLE A, B, LMask
1386819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If LHS is not a shuffle then pretend it is the shuffle
1386919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
1387019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // NOTE: in what follows a default initialized SDValue represents an UNDEF of
1387119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // type VT.
1387219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue A, B;
1387319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<int, 8> LMask(N);
1387419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
1387519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
1387619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      A = LHS.getOperand(0);
1387719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
1387819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      B = LHS.getOperand(1);
1387919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(LMask);
1388019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
1388119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LHS.getOpcode() != ISD::UNDEF)
1388219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      A = LHS;
1388319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i != N; ++i)
1388419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      LMask[i] = i;
1388519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1388619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1388719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Likewise, view RHS in the form
1388819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   RHS = VECTOR_SHUFFLE C, D, RMask
1388919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue C, D;
1389019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<int, 8> RMask(N);
1389119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
1389219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
1389319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      C = RHS.getOperand(0);
1389419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
1389519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      D = RHS.getOperand(1);
1389619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(RMask);
1389719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
1389819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (RHS.getOpcode() != ISD::UNDEF)
1389919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      C = RHS;
1390019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i != N; ++i)
1390119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      RMask[i] = i;
1390219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1390319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1390419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check that the shuffles are both shuffling the same vectors.
1390519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!(A == C && B == D) && !(A == D && B == C))
1390619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
1390719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1390819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
1390919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!A.getNode() && !B.getNode())
1391019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return false;
1391119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1391219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If A and B occur in reverse order in RHS, then "swap" them (which means
1391319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // rewriting the mask).
1391419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (A != C)
1391519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i != N; ++i) {
1391619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned Idx = RMask[i];
1391719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Idx < N)
1391819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        RMask[i] += N;
1391919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else if (Idx < 2*N)
1392019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        RMask[i] -= N;
1392119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1392219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1392319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // At this point LHS and RHS are equivalent to
1392419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   LHS = VECTOR_SHUFFLE A, B, LMask
1392519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   RHS = VECTOR_SHUFFLE A, B, RMask
1392619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check that the masks correspond to performing a horizontal operation.
1392719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0; i != N; ++i) {
1392819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned LIdx = LMask[i], RIdx = RMask[i];
1392919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1393019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Ignore any UNDEF components.
1393119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LIdx >= 2*N || RIdx >= 2*N || (!A.getNode() && (LIdx < N || RIdx < N))
1393219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        || (!B.getNode() && (LIdx >= N || RIdx >= N)))
1393319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
1393419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1393519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Check that successive elements are being operated on.  If not, this is
1393619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // not a horizontal operation.
1393719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!(LIdx == 2*i && RIdx == 2*i + 1) &&
1393819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        !(isCommutative && LIdx == 2*i + 1 && RIdx == 2*i))
1393919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
1394019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1394119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1394219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
1394319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
1394419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
1394519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1394619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1394719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
1394819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
1394919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  const X86Subtarget *Subtarget) {
1395019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
1395119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS = N->getOperand(0);
1395219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS = N->getOperand(1);
1395319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1395419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Try to synthesize horizontal adds from adds of shuffles.
1395519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
1395619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (VT == MVT::v4f32 || VT == MVT::v2f64) &&
1395719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      isHorizontalBinOp(LHS, RHS, true))
1395819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
1395919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1396019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1396119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1396219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
1396319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
1396419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  const X86Subtarget *Subtarget) {
1396519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = N->getValueType(0);
1396619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue LHS = N->getOperand(0);
1396719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RHS = N->getOperand(1);
1396819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1396919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Try to synthesize horizontal subs from subs of shuffles.
1397019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((Subtarget->hasSSE3() || Subtarget->hasAVX()) &&
1397119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (VT == MVT::v4f32 || VT == MVT::v2f64) &&
1397219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      isHorizontalBinOp(LHS, RHS, false))
1397319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
1397419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1397519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1397619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
13978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// X86ISD::FXOR nodes.
13979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
13980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
13981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // F[X]OR(0.0, x) -> x
13982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // F[X]OR(x, 0.0) -> x
13983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C->getValueAPF().isPosZero())
13985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return N->getOperand(1);
13986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
13987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C->getValueAPF().isPosZero())
13988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return N->getOperand(0);
13989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
13990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
13991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
13993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
13994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FAND(0.0, x) -> 0.0
13995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FAND(x, 0.0) -> 0.0
13996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
13997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C->getValueAPF().isPosZero())
13998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return N->getOperand(0);
13999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
14000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C->getValueAPF().isPosZero())
14001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return N->getOperand(1);
14002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
14003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformBTCombine(SDNode *N,
14006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                SelectionDAG &DAG,
14007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                TargetLowering::DAGCombinerInfo &DCI) {
14008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // BT ignores high bits in the bit index operand.
14009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op1 = N->getOperand(1);
14010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op1.hasOneUse()) {
14011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned BitWidth = Op1.getValueSizeInBits();
14012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
14013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt KnownZero, KnownOne;
14014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
14015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          !DCI.isBeforeLegalizeOps());
14016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
14018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
14019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DCI.CommitTargetLoweringOpt(TLO);
14020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
14022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
14025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Op = N->getOperand(0);
1402619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op.getOpcode() == ISD::BITCAST)
14027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op = Op.getOperand(0);
14028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0), OpVT = Op.getValueType();
14029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
14030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      VT.getVectorElementType().getSizeInBits() ==
14031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OpVT.getVectorElementType().getSizeInBits()) {
1403219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
14033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
14035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
14038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // (i32 zext (and (i8  x86isd::setcc_carry), 1)) ->
14039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //           (and (i32 x86isd::setcc_carry), 1)
14040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This eliminates the zext. This transformation is necessary because
14041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ISD::SETCC is always legalized to i8.
14042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = N->getDebugLoc();
14043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue N0 = N->getOperand(0);
14044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = N->getValueType(0);
14045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (N0.getOpcode() == ISD::AND &&
14046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0.hasOneUse() &&
14047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      N0.getOperand(0).hasOneUse()) {
14048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N00 = N0.getOperand(0);
14049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N00.getOpcode() != X86ISD::SETCC_CARRY)
14050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
14051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
14052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!C || C->getZExtValue() != 1)
14053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return SDValue();
14054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::AND, dl, VT,
14055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
14056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   N00.getOperand(0), N00.getOperand(1)),
14057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       DAG.getConstant(1, VT));
14058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
14061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1406319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Optimize  RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
1406419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
1406519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned X86CC = N->getConstantOperandVal(0);
1406619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue EFLAG = N->getOperand(1);
1406719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = N->getDebugLoc();
1406819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1406919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
1407019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // a zext and produces an all-ones bit which is more useful than 0/1 in some
1407119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // cases.
1407219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86CC == X86::COND_B)
1407319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(ISD::AND, DL, MVT::i8,
1407419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
1407519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   DAG.getConstant(X86CC, MVT::i8), EFLAG),
1407619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getConstant(1, MVT::i8));
1407719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1407819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1407919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1408019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1408119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
1408219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        const X86TargetLowering *XTLI) {
1408319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Op0 = N->getOperand(0);
1408419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
1408519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // a 32-bit target where SSE doesn't support i64->FP operations.
1408619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Op0.getOpcode() == ISD::LOAD) {
1408719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
1408819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT VT = Ld->getValueType(0);
1408919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
1409019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
1409119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        !XTLI->getSubtarget()->is64Bit() &&
1409219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
1409319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
1409419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          Ld->getChain(), Op0, DAG);
1409519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
1409619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return FILDChain;
1409719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1409819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1409919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1410019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1410119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1410219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
1410319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
1410419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 X86TargetLowering::DAGCombinerInfo &DCI) {
1410519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the LHS and RHS of the ADC node are zero, then it can't overflow and
1410619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the result is either zero or one (depending on the input carry bit).
1410719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
1410819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (X86::isZeroNode(N->getOperand(0)) &&
1410919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      X86::isZeroNode(N->getOperand(1)) &&
1411019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // We don't have a good way to replace an EFLAGS use, so only do this when
1411119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // dead right now.
1411219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue(N, 1).use_empty()) {
1411319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc DL = N->getDebugLoc();
1411419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EVT VT = N->getValueType(0);
1411519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
1411619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
1411719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
1411819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           DAG.getConstant(X86::COND_B,MVT::i8),
1411919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           N->getOperand(2)),
1412019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               DAG.getConstant(1, VT));
1412119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DCI.CombineTo(N, Res1, CarryOut);
1412219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1412319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1412419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return SDValue();
1412519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1412619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1412719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// fold (add Y, (sete  X, 0)) -> adc  0, Y
1412819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//      (add Y, (setne X, 0)) -> sbb -1, Y
1412919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//      (sub (sete  X, 0), Y) -> sbb  0, Y
1413019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//      (sub (setne X, 0), Y) -> adc -1, Y
1413119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
1413219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc DL = N->getDebugLoc();
1413319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1413419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look through ZExts.
1413519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
1413619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
1413719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1413819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1413919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue SetCC = Ext.getOperand(0);
1414019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
1414119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1414219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1414319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
1414419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CC != X86::COND_E && CC != X86::COND_NE)
1414519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1414619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1414719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Cmp = SetCC.getOperand(1);
1414819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
1414919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !X86::isZeroNode(Cmp.getOperand(1)) ||
1415019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !Cmp.getOperand(0).getValueType().isInteger())
1415119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SDValue();
1415219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1415319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue CmpOp0 = Cmp.getOperand(0);
1415419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
1415519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               DAG.getConstant(1, CmpOp0.getValueType()));
1415619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1415719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
1415819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CC == X86::COND_NE)
1415919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
1416019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DL, OtherVal.getValueType(), OtherVal,
1416119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
1416219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
1416319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DL, OtherVal.getValueType(), OtherVal,
1416419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
1416519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1416619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1416719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG) {
1416819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Op0 = N->getOperand(0);
1416919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Op1 = N->getOperand(1);
1417019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1417119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // X86 can't encode an immediate LHS of a sub. See if we can push the
1417219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // negation into a preceding instruction.
1417319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
1417419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If the RHS of the sub is a XOR with one use and a constant, invert the
1417519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // immediate. Then add one to the LHS of the sub so we can turn
1417619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // X-Y -> X+~Y+1, saving one register.
1417719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
1417819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        isa<ConstantSDNode>(Op1.getOperand(1))) {
1417919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
1418019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EVT VT = Op0.getValueType();
1418119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
1418219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   Op1.getOperand(0),
1418319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   DAG.getConstant(~XorC, VT));
1418419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
1418519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         DAG.getConstant(C->getAPIntValue()+1, VT));
1418619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1418719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1418819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1418919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return OptimizeConditionalInDecrement(N, DAG);
1419019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1419119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
14193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                             DAGCombinerInfo &DCI) const {
14194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SelectionDAG &DAG = DCI.DAG;
14195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (N->getOpcode()) {
14196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
14197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::EXTRACT_VECTOR_ELT:
1419819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
1419919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::VSELECT:
14200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SELECT:         return PerformSELECTCombine(N, DAG, Subtarget);
1420124809dcfa1985b053638ef7cae2a968d2cd82388Nicolas Capens  case ISD::BITCAST:        return PerformBITCASTCombine(N, DAG);
14202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::CMOV:        return PerformCMOVCombine(N, DAG, DCI);
1420319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::ADD:            return OptimizeConditionalInDecrement(N, DAG);
1420419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SUB:            return PerformSubCombine(N, DAG);
1420519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::ADC:         return PerformADCCombine(N, DAG, DCI);
14206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::MUL:            return PerformMulCombine(N, DAG, DCI);
14207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SHL:
14208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRA:
14209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRL:            return PerformShiftCombine(N, DAG, Subtarget);
1421019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::AND:            return PerformAndCombine(N, DAG, DCI, Subtarget);
14211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::OR:             return PerformOrCombine(N, DAG, DCI, Subtarget);
1421219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::LOAD:           return PerformLOADCombine(N, DAG, Subtarget);
14213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::STORE:          return PerformSTORECombine(N, DAG, Subtarget);
1421419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::SINT_TO_FP:     return PerformSINT_TO_FPCombine(N, DAG, this);
1421519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::FADD:           return PerformFADDCombine(N, DAG, Subtarget);
1421619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::FSUB:           return PerformFSUBCombine(N, DAG, Subtarget);
14217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FXOR:
14218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FOR:         return PerformFORCombine(N, DAG);
14219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::FAND:        return PerformFANDCombine(N, DAG);
14220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::BT:          return PerformBTCombine(N, DAG, DCI);
14221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case X86ISD::VZEXT_MOVL:  return PerformVZEXT_MOVLCombine(N, DAG);
14222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ZERO_EXTEND:    return PerformZExtCombine(N, DAG);
1422319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SETCC:       return PerformSETCCCombine(N, DAG);
1422419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPS:      // Handle all target specific shuffles
1422519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::SHUFPD:
1422619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PALIGN:
1422719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHBW:
1422819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHWD:
1422919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHDQ:
1423019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKHQDQ:
1423119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPS:
1423219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKHPD:
1423319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKHPSY:
1423419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKHPDY:
1423519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLBW:
1423619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLWD:
1423719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLDQ:
1423819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PUNPCKLQDQ:
1423919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPS:
1424019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::UNPCKLPD:
1424119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPSY:
1424219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VUNPCKLPDY:
1424319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVHLPS:
1424419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVLHPS:
1424519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFD:
1424619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFHW:
1424719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::PSHUFLW:
1424819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSS:
1424919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::MOVSD:
1425019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPS:
1425119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPSY:
1425219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPD:
1425319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERMILPDY:
1425419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case X86ISD::VPERM2F128:
1425519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
14256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return SDValue();
14259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isTypeDesirableForOp - Return true if the target has native support for
14262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the specified value type and it is 'desirable' to use the type for the
14263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
14264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instruction encodings are longer and some i16 instructions are slow.
14265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
14266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isTypeLegal(VT))
14267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
14268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::i16)
14269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
14270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Opc) {
14272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
14273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
14274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::LOAD:
14275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SIGN_EXTEND:
14276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ZERO_EXTEND:
14277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ANY_EXTEND:
14278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SHL:
14279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRL:
14280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SUB:
14281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ADD:
14282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::MUL:
14283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::AND:
14284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::OR:
14285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::XOR:
14286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
14287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// IsDesirableToPromoteOp - This method query the target whether it is
14291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// beneficial for dag combiner to promote the specified node. If true, it
14292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// should return the desired promotion type by reference.
14293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
14294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Op.getValueType();
14295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::i16)
14296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
14297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Promote = false;
14299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Commute = false;
14300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
14301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
14302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::LOAD: {
14303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LoadSDNode *LD = cast<LoadSDNode>(Op);
14304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the non-extending load has a single use and it's not live out, then it
14305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // might be folded.
14306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
14307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     Op.hasOneUse()*/) {
14308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             UE = Op.getNode()->use_end(); UI != UE; ++UI) {
14310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // The only case where we'd want to promote LOAD (rather then it being
14311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // promoted as an operand is when it's only use is liveout.
14312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (UI->getOpcode() != ISD::CopyToReg)
14313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return false;
14314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Promote = true;
14317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
14318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SIGN_EXTEND:
14320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ZERO_EXTEND:
14321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ANY_EXTEND:
14322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Promote = true;
14323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
14324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SHL:
14325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SRL: {
14326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N0 = Op.getOperand(0);
14327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Look out for (store (shl (load), x)).
14328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
14329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
14330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Promote = true;
14331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
14332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ADD:
14334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::MUL:
14335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::AND:
14336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::OR:
14337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::XOR:
14338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Commute = true;
14339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // fallthrough
14340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SUB: {
14341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N0 = Op.getOperand(0);
14342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue N1 = Op.getOperand(1);
14343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Commute && MayFoldLoad(N1))
14344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
14345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Avoid disabling potential load folding opportunities.
14346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
14347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
14348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
14349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
14350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Promote = true;
14351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PVT = MVT::i32;
14355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Promote;
14356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                           X86 Inline Assembly Support
14360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
14363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
14364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string AsmStr = IA->getAsmString();
14366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
14368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<StringRef, 4> AsmPieces;
1436919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SplitString(AsmStr, AsmPieces, ";\n");
14370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (AsmPieces.size()) {
14372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: return false;
14373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 1:
14374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AsmStr = AsmPieces[0];
14375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AsmPieces.clear();
14376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SplitString(AsmStr, AsmPieces, " \t");  // Split with whitespace.
14377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1437819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // FIXME: this should verify that we are targeting a 486 or better.  If not,
1437919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // we will turn this bswap into something that will be lowered to logical ops
1438019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // instead of emitting the bswap asm.  For now, we don't support 486 or lower
1438119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // so don't worry about this.
14382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // bswap $0
14383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AsmPieces.size() == 2 &&
14384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (AsmPieces[0] == "bswap" ||
14385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         AsmPieces[0] == "bswapq" ||
14386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         AsmPieces[0] == "bswapl") &&
14387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (AsmPieces[1] == "$0" ||
14388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         AsmPieces[1] == "${0:q}")) {
14389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // No need to check constraints, nothing other than the equivalent of
14390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // "=r,0" would be valid here.
1439119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
1439219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!Ty || Ty->getBitWidth() % 16 != 0)
1439319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return false;
1439419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return IntrinsicLowering::LowerToByteSwap(CI);
14395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // rorw $$8, ${0:w}  -->  llvm.bswap.i16
14397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CI->getType()->isIntegerTy(16) &&
14398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        AsmPieces.size() == 3 &&
14399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
14400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        AsmPieces[1] == "$$8," &&
14401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        AsmPieces[2] == "${0:w}" &&
14402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AsmPieces.clear();
1440419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      const std::string &ConstraintsStr = IA->getConstraintString();
1440519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
14406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::sort(AsmPieces.begin(), AsmPieces.end());
14407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AsmPieces.size() == 4 &&
14408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          AsmPieces[0] == "~{cc}" &&
14409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          AsmPieces[1] == "~{dirflag}" &&
14410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          AsmPieces[2] == "~{flags}" &&
14411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          AsmPieces[3] == "~{fpsr}") {
1441219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
1441319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (!Ty || Ty->getBitWidth() % 16 != 0)
1441419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          return false;
1441519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        return IntrinsicLowering::LowerToByteSwap(CI);
14416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
14419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 3:
1442019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (CI->getType()->isIntegerTy(32) &&
1442119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
14422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SmallVector<StringRef, 4> Words;
1442319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SplitString(AsmPieces[0], Words, " \t,");
1442419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
1442519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Words[2] == "${0:w}") {
14426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Words.clear();
1442719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SplitString(AsmPieces[1], Words, " \t,");
1442819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
1442919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            Words[2] == "$0") {
14430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Words.clear();
14431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SplitString(AsmPieces[2], Words, " \t,");
1443219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
1443319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              Words[2] == "${0:w}") {
1443419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            AsmPieces.clear();
1443519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            const std::string &ConstraintsStr = IA->getConstraintString();
1443619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
1443719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            std::sort(AsmPieces.begin(), AsmPieces.end());
1443819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if (AsmPieces.size() == 4 &&
1443919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                AsmPieces[0] == "~{cc}" &&
1444019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                AsmPieces[1] == "~{dirflag}" &&
1444119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                AsmPieces[2] == "~{flags}" &&
1444219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                AsmPieces[3] == "~{fpsr}") {
1444319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
1444419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              if (!Ty || Ty->getBitWidth() % 16 != 0)
1444519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                return false;
1444619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              return IntrinsicLowering::LowerToByteSwap(CI);
1444719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            }
1444819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
1444919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1445019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1445119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1445219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1445319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (CI->getType()->isIntegerTy(64)) {
1445419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
1445519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Constraints.size() >= 2 &&
1445619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
1445719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
1445819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
1445919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SmallVector<StringRef, 4> Words;
1446019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SplitString(AsmPieces[0], Words, " \t");
1446119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
1446219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Words.clear();
1446319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SplitString(AsmPieces[1], Words, " \t");
1446419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
1446519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            Words.clear();
1446619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            SplitString(AsmPieces[2], Words, " \t,");
1446719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
1446819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                Words[2] == "%edx") {
1446919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
1447019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              if (!Ty || Ty->getBitWidth() % 16 != 0)
1447119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                return false;
1447219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              return IntrinsicLowering::LowerToByteSwap(CI);
1447319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            }
14474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
14475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
14476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
14479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
14481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getConstraintType - Given a constraint letter, return the type of
14486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// constraint it is for this target.
14487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::ConstraintType
14488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::getConstraintType(const std::string &Constraint) const {
14489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constraint.size() == 1) {
14490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Constraint[0]) {
14491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'R':
14492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'q':
14493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'Q':
1449419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'f':
1449519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 't':
1449619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'u':
14497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'y':
1449819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'x':
14499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'Y':
1450019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'l':
14501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return C_RegisterClass;
1450219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'a':
1450319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'b':
1450419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'c':
1450519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'd':
1450619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'S':
1450719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'D':
1450819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'A':
1450919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return C_Register;
1451019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'I':
1451119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'J':
1451219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'K':
1451319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'L':
1451419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'M':
1451519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'N':
1451619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'G':
1451719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'C':
14518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'e':
14519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'Z':
14520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return C_Other;
14521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default:
14522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
14523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::getConstraintType(Constraint);
14526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1452819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Examine constraint type and operand type and determine a weight value.
1452919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// This object must already have been set up with the operand type
1453019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// and the current alternative constraint selected.
1453119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanTargetLowering::ConstraintWeight
1453219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  X86TargetLowering::getSingleConstraintMatchWeight(
1453319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AsmOperandInfo &info, const char *constraint) const {
1453419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstraintWeight weight = CW_Invalid;
1453519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Value *CallOperandVal = info.CallOperandVal;
1453619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If we don't have a value, we can't do a match,
1453719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // but allow it at the lowest weight.
1453819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CallOperandVal == NULL)
1453919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CW_Default;
1454019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *type = CallOperandVal->getType();
1454119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look at the constraint type.
1454219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (*constraint) {
1454319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
1454419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1454519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'R':
1454619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'q':
1454719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'Q':
1454819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'a':
1454919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'b':
1455019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'c':
1455119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'd':
1455219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'S':
1455319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'D':
1455419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'A':
1455519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (CallOperandVal->getType()->isIntegerTy())
1455619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      weight = CW_SpecificReg;
1455719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1455819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'f':
1455919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 't':
1456019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'u':
1456119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (type->isFloatingPointTy())
1456219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_SpecificReg;
1456319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
1456419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'y':
1456519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (type->isX86_MMXTy() && Subtarget->hasMMX())
1456619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_SpecificReg;
1456719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
1456819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'x':
1456919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'Y':
1457019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
1457119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      weight = CW_Register;
1457219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1457319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'I':
1457419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
1457519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->getZExtValue() <= 31)
1457619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1457719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1457819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1457919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'J':
1458019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1458119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->getZExtValue() <= 63)
1458219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1458319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1458419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1458519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'K':
1458619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1458719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
1458819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1458919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1459019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1459119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'L':
1459219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1459319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
1459419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1459519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1459619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1459719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'M':
1459819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1459919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->getZExtValue() <= 3)
1460019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1460119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1460219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1460319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'N':
1460419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1460519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->getZExtValue() <= 0xff)
1460619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1460719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1460819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1460919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'G':
1461019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'C':
1461119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (dyn_cast<ConstantFP>(CallOperandVal)) {
1461219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      weight = CW_Constant;
1461319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1461419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1461519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'e':
1461619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1461719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if ((C->getSExtValue() >= -0x80000000LL) &&
1461819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          (C->getSExtValue() <= 0x7fffffffLL))
1461919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1462019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1462119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1462219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case 'Z':
1462319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
1462419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (C->getZExtValue() <= 0xffffffff)
1462519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        weight = CW_Constant;
1462619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1462719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1462819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1462919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return weight;
1463019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1463119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerXConstraint - try to replace an X constraint, which matches anything,
14633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// with another that has more specific requirements based on the type of the
14634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// corresponding operand.
14635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char *X86TargetLowering::
14636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanLowerXConstraint(EVT ConstraintVT) const {
14637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FP X constraints get lowered to SSE1/2 registers if available, otherwise
14638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 'f' like normal targets.
14639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ConstraintVT.isFloatingPoint()) {
1464019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->hasXMMInt())
14641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return "Y";
1464219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Subtarget->hasXMM())
14643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return "x";
14644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::LowerXConstraint(ConstraintVT);
14647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// vector.  If it is invalid, don't add anything to Ops.
14651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
1465219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                     std::string &Constraint,
14653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     std::vector<SDValue>&Ops,
14654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                     SelectionDAG &DAG) const {
14655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Result(0, 0);
14656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1465719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Only support length 1 constraints for now.
1465819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constraint.length() > 1) return;
1465919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1466019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  char ConstraintLetter = Constraint[0];
1466119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (ConstraintLetter) {
14662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
14663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'I':
14664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
14665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getZExtValue() <= 31) {
14666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'J':
14672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
14673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getZExtValue() <= 63) {
14674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'K':
14680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
14681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
14682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'N':
14688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
14689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (C->getZExtValue() <= 255) {
14690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'e': {
14696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // 32-bit signed value
14697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
14698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           C->getSExtValue())) {
14700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Widen to 64 bits here to get it sign extended.
14701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
14702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME gcc accepts some relocatable values here too, but only in certain
14705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // memory models; it's complicated.
14706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'Z': {
14710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // 32-bit unsigned value
14711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
14712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
14713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           C->getZExtValue())) {
14714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
14715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME gcc accepts some relocatable values here too, but only in certain
14719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // memory models; it's complicated.
14720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case 'i': {
14723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Literal immediates are always ok.
14724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
14725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Widen to 64 bits here to get it sign extended.
14726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
14727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
14728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // In any sort of PIC mode addresses need to be computed at runtime by
14731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // adding in a register or some sort of table lookup.  These can't
14732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // be used as immediates.
14733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
14734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
14735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we are in non-pic codegen mode, we allow the address of a global (with
14737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // an optional displacement) to be used with 'i'.
14738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GlobalAddressSDNode *GA = 0;
14739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int64_t Offset = 0;
14740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Match either (GA), (GA+C), (GA+C1+C2), etc.
14742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    while (1) {
14743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
14744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Offset += GA->getOffset();
14745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
14746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (Op.getOpcode() == ISD::ADD) {
14747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Offset += C->getZExtValue();
14749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op = Op.getOperand(0);
14750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
14751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
14752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else if (Op.getOpcode() == ISD::SUB) {
14753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Offset += -C->getZExtValue();
14755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Op = Op.getOperand(0);
14756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
14757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
14758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Otherwise, this isn't something we can handle, reject it.
14761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
14762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const GlobalValue *GV = GA->getGlobal();
14765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If we require an extra load to get this address, as in PIC mode, we
14766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // can't accept it.
14767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
14768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                        getTargetMachine())))
14769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
14770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
14772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        GA->getValueType(0), Offset);
14773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
14774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Result.getNode()) {
14778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.push_back(Result);
14779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
14780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
14782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<unsigned, const TargetRegisterClass*>
14785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanX86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
14786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                EVT VT) const {
14787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First, see if this is a constraint that directly corresponds to an LLVM
14788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // register class.
14789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constraint.size() == 1) {
14790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // GCC Constraint Letters
14791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Constraint[0]) {
14792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: break;
1479319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // TODO: Slight differences here in allocation order and leaving
1479419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // RIP in the class. Do they matter any more here than they do
1479519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // in the normal allocation?
1479619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'q':   // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
1479719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Subtarget->is64Bit()) {
1479819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	if (VT == MVT::i32 || VT == MVT::f32)
1479919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	  return std::make_pair(0U, X86::GR32RegisterClass);
1480019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	else if (VT == MVT::i16)
1480119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	  return std::make_pair(0U, X86::GR16RegisterClass);
1480219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	else if (VT == MVT::i8 || VT == MVT::i1)
1480319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	  return std::make_pair(0U, X86::GR8RegisterClass);
1480419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	else if (VT == MVT::i64 || VT == MVT::f64)
1480519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	  return std::make_pair(0U, X86::GR64RegisterClass);
1480619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	break;
1480719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
1480819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // 32-bit fallthrough
1480919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case 'Q':   // Q_REGS
1481019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::i32 || VT == MVT::f32)
1481119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	return std::make_pair(0U, X86::GR32_ABCDRegisterClass);
1481219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else if (VT == MVT::i16)
1481319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	return std::make_pair(0U, X86::GR16_ABCDRegisterClass);
1481419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else if (VT == MVT::i8 || VT == MVT::i1)
1481519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	return std::make_pair(0U, X86::GR8_ABCD_LRegisterClass);
1481619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else if (VT == MVT::i64)
1481719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman	return std::make_pair(0U, X86::GR64_ABCDRegisterClass);
1481819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
14819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'r':   // GENERAL_REGS
14820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'l':   // INDEX_REGS
1482119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::i8 || VT == MVT::i1)
14822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::GR8RegisterClass);
14823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VT == MVT::i16)
14824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::GR16RegisterClass);
1482519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
14826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::GR32RegisterClass);
14827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return std::make_pair(0U, X86::GR64RegisterClass);
14828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'R':   // LEGACY_REGS
1482919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VT == MVT::i8 || VT == MVT::i1)
14830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
14831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VT == MVT::i16)
14832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
14833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VT == MVT::i32 || !Subtarget->is64Bit())
14834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
14835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
14836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'f':  // FP Stack registers.
14837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If SSE is enabled for this VT, use f80 to ensure the isel moves the
14838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // value to the correct fpstack register class.
14839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
14840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::RFP32RegisterClass);
14841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
14842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::RFP64RegisterClass);
14843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return std::make_pair(0U, X86::RFP80RegisterClass);
14844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'y':   // MMX_REGS if MMX allowed.
14845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!Subtarget->hasMMX()) break;
14846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return std::make_pair(0U, X86::VR64RegisterClass);
14847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'Y':   // SSE_REGS if SSE2 allowed
1484819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!Subtarget->hasXMMInt()) break;
14849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FALL THROUGH.
14850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'x':   // SSE_REGS if SSE1 allowed
1485119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (!Subtarget->hasXMM()) break;
14852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (VT.getSimpleVT().SimpleTy) {
14854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
14855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Scalar SSE types.
14856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::f32:
14857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::i32:
14858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::FR32RegisterClass);
14859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::f64:
14860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::i64:
14861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::FR64RegisterClass);
14862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Vector types.
14863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::v16i8:
14864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::v8i16:
14865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::v4i32:
14866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::v2i64:
14867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::v4f32:
14868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case MVT::v2f64:
14869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return std::make_pair(0U, X86::VR128RegisterClass);
14870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
14872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Use the default implementation in TargetLowering to convert the register
14876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // constraint into a member of a register class.
14877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::pair<unsigned, const TargetRegisterClass*> Res;
14878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
14879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Not found as a standard register?
14881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Res.second == 0) {
14882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Map st(0) -> st(7) -> ST0
14883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Constraint.size() == 7 && Constraint[0] == '{' &&
14884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        tolower(Constraint[1]) == 's' &&
14885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        tolower(Constraint[2]) == 't' &&
14886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Constraint[3] == '(' &&
14887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (Constraint[4] >= '0' && Constraint[4] <= '7') &&
14888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Constraint[5] == ')' &&
14889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Constraint[6] == '}') {
14890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.first = X86::ST0+Constraint[4]-'0';
14892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::RFP80RegisterClass;
14893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Res;
14894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // GCC allows "st(0)" to be called just plain "st".
14897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (StringRef("{st}").equals_lower(Constraint)) {
14898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.first = X86::ST0;
14899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::RFP80RegisterClass;
14900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Res;
14901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // flags -> EFLAGS
14904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (StringRef("{flags}").equals_lower(Constraint)) {
14905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.first = X86::EFLAGS;
14906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::CCRRegisterClass;
14907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Res;
14908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // 'A' means EAX + EDX.
14911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Constraint == "A") {
14912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.first = X86::EAX;
14913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::GR32_ADRegisterClass;
14914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Res;
14915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Res;
14917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, check to see if this is a register class of the wrong value
14920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // type.  For example, we want to map "{ax},i32" -> {eax}, we don't want it to
14921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // turn into {ax},{dx}.
14922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Res.second->hasType(VT))
14923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Res;   // Correct type already, nothing to do.
14924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // All of the single-register GCC register classes map their values onto
14926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp".  If we
14927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // really want an 8-bit or 32-bit register, map to the appropriate register
14928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // class and return the appropriate register.
14929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Res.second == X86::GR16RegisterClass) {
14930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::i8) {
14931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned DestReg = 0;
14932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (Res.first) {
14933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
14934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::AX: DestReg = X86::AL; break;
14935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::DX: DestReg = X86::DL; break;
14936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::CX: DestReg = X86::CL; break;
14937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::BX: DestReg = X86::BL; break;
14938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (DestReg) {
14940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Res.first = DestReg;
14941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Res.second = X86::GR8RegisterClass;
14942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (VT == MVT::i32) {
14944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned DestReg = 0;
14945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (Res.first) {
14946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
14947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::AX: DestReg = X86::EAX; break;
14948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::DX: DestReg = X86::EDX; break;
14949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::CX: DestReg = X86::ECX; break;
14950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::BX: DestReg = X86::EBX; break;
14951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::SI: DestReg = X86::ESI; break;
14952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::DI: DestReg = X86::EDI; break;
14953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::BP: DestReg = X86::EBP; break;
14954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::SP: DestReg = X86::ESP; break;
14955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (DestReg) {
14957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Res.first = DestReg;
14958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Res.second = X86::GR32RegisterClass;
14959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (VT == MVT::i64) {
14961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned DestReg = 0;
14962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (Res.first) {
14963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: break;
14964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::AX: DestReg = X86::RAX; break;
14965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::DX: DestReg = X86::RDX; break;
14966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::CX: DestReg = X86::RCX; break;
14967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::BX: DestReg = X86::RBX; break;
14968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::SI: DestReg = X86::RSI; break;
14969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::DI: DestReg = X86::RDI; break;
14970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::BP: DestReg = X86::RBP; break;
14971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case X86::SP: DestReg = X86::RSP; break;
14972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (DestReg) {
14974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Res.first = DestReg;
14975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Res.second = X86::GR64RegisterClass;
14976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
14977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
14978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (Res.second == X86::FR32RegisterClass ||
14979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             Res.second == X86::FR64RegisterClass ||
14980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             Res.second == X86::VR128RegisterClass) {
14981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Handle references to XMM physical registers that got mapped into the
14982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // wrong class.  This can happen with constraints like {xmm0} where the
14983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // target independent register mapper will just pick the first match it can
14984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // find, ignoring the required type.
14985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (VT == MVT::f32)
14986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::FR32RegisterClass;
14987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (VT == MVT::f64)
14988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::FR64RegisterClass;
14989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (X86::VR128RegisterClass->hasType(VT))
14990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Res.second = X86::VR128RegisterClass;
14991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
14992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Res;
14994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
14995