Skip to content
X86ISelLowering.cpp 357 KiB
Newer Older
//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the interfaces that X86 uses to lower LLVM code into a
// selection DAG.
//
//===----------------------------------------------------------------------===//

#include "X86.h"
#include "X86InstrBuilder.h"
#include "X86ISelLowering.h"
#include "X86TargetMachine.h"
#include "llvm/CallingConv.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
Evan Cheng's avatar
Evan Cheng committed
#include "llvm/ADT/VectorExtras.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng's avatar
Evan Cheng committed
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/PseudoSourceValue.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetOptions.h"
Chris Lattner's avatar
Chris Lattner committed
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
static cl::opt<bool>
DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
  switch (TM.getSubtarget<X86Subtarget>().TargetType) {
  default: llvm_unreachable("unknown subtarget type");
  case X86Subtarget::isDarwin:
    return new TargetLoweringObjectFileMachO();
  case X86Subtarget::isELF:
    return new TargetLoweringObjectFileELF();
  case X86Subtarget::isMingw:
  case X86Subtarget::isCygwin:
  case X86Subtarget::isWindows:
    return new TargetLoweringObjectFileCOFF();
  }
  
}

X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
  : TargetLowering(TM, createTLOF(TM)) {
  Subtarget = &TM.getSubtarget<X86Subtarget>();
  X86ScalarSSEf64 = Subtarget->hasSSE2();
  X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng's avatar
Evan Cheng committed
  X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
  RegInfo = TM.getRegisterInfo();
  // Set up the TargetLowering object.

  // X86 is weird, it always uses i8 for shift amounts and setcc results.
  setBooleanContents(ZeroOrOneBooleanContent);
  setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng's avatar
Evan Cheng committed
  setStackPointerRegisterToSaveRestore(X86StackPtr);
    // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
    setUseUnderscoreSetJmp(false);
    setUseUnderscoreLongJmp(false);
Anton Korobeynikov's avatar
Anton Korobeynikov committed
  } else if (Subtarget->isTargetMingw()) {
    // MS runtime is weird: it exports _setjmp, but longjmp!
    setUseUnderscoreSetJmp(true);
    setUseUnderscoreLongJmp(false);
  } else {
    setUseUnderscoreSetJmp(true);
    setUseUnderscoreLongJmp(true);
  }
  addRegisterClass(EVT::i8, X86::GR8RegisterClass);
  addRegisterClass(EVT::i16, X86::GR16RegisterClass);
  addRegisterClass(EVT::i32, X86::GR32RegisterClass);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit())
    addRegisterClass(EVT::i64, X86::GR64RegisterClass);
  setLoadExtAction(ISD::SEXTLOAD, EVT::i1, Promote);
  // We don't accept any truncstore of integer registers.
  setTruncStoreAction(EVT::i64, EVT::i32, Expand);
  setTruncStoreAction(EVT::i64, EVT::i16, Expand);
  setTruncStoreAction(EVT::i64, EVT::i8 , Expand);
  setTruncStoreAction(EVT::i32, EVT::i16, Expand);
  setTruncStoreAction(EVT::i32, EVT::i8 , Expand);
  setTruncStoreAction(EVT::i16, EVT::i8,  Expand);

  // SETOEQ and SETUNE require checking two conditions.
  setCondCodeAction(ISD::SETOEQ, EVT::f32, Expand);
  setCondCodeAction(ISD::SETOEQ, EVT::f64, Expand);
  setCondCodeAction(ISD::SETOEQ, EVT::f80, Expand);
  setCondCodeAction(ISD::SETUNE, EVT::f32, Expand);
  setCondCodeAction(ISD::SETUNE, EVT::f64, Expand);
  setCondCodeAction(ISD::SETUNE, EVT::f80, Expand);
  // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
  // operation.
  setOperationAction(ISD::UINT_TO_FP       , EVT::i1   , Promote);
  setOperationAction(ISD::UINT_TO_FP       , EVT::i8   , Promote);
  setOperationAction(ISD::UINT_TO_FP       , EVT::i16  , Promote);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit()) {
    setOperationAction(ISD::UINT_TO_FP     , EVT::i32  , Promote);
    setOperationAction(ISD::UINT_TO_FP     , EVT::i64  , Expand);
  } else if (!UseSoftFloat) {
    if (X86ScalarSSEf64) {
      // We have an impenetrably clever algorithm for ui64->double only.
      setOperationAction(ISD::UINT_TO_FP   , EVT::i64  , Custom);
    // We have an algorithm for SSE2, and we turn this into a 64-bit
    // FILD for other targets.
    setOperationAction(ISD::UINT_TO_FP   , EVT::i32  , Custom);

  // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
  // this operation.
  setOperationAction(ISD::SINT_TO_FP       , EVT::i1   , Promote);
  setOperationAction(ISD::SINT_TO_FP       , EVT::i8   , Promote);
    // SSE has no i16 to fp conversion, only i32
    if (X86ScalarSSEf32) {
      setOperationAction(ISD::SINT_TO_FP     , EVT::i16  , Promote);
      // f32 and f64 cases are Legal, f80 case is not
      setOperationAction(ISD::SINT_TO_FP     , EVT::i32  , Custom);
      setOperationAction(ISD::SINT_TO_FP     , EVT::i16  , Custom);
      setOperationAction(ISD::SINT_TO_FP     , EVT::i32  , Custom);
    setOperationAction(ISD::SINT_TO_FP     , EVT::i16  , Promote);
    setOperationAction(ISD::SINT_TO_FP     , EVT::i32  , Promote);
Evan Cheng's avatar
Evan Cheng committed
  }
  // In 32-bit mode these are custom lowered.  In 64-bit mode F32 and F64
  // are Legal, f80 is custom lowered.
  setOperationAction(ISD::FP_TO_SINT     , EVT::i64  , Custom);
  setOperationAction(ISD::SINT_TO_FP     , EVT::i64  , Custom);
  // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
  // this operation.
  setOperationAction(ISD::FP_TO_SINT       , EVT::i1   , Promote);
  setOperationAction(ISD::FP_TO_SINT       , EVT::i8   , Promote);
  if (X86ScalarSSEf32) {
    setOperationAction(ISD::FP_TO_SINT     , EVT::i16  , Promote);
    // f32 and f64 cases are Legal, f80 case is not
    setOperationAction(ISD::FP_TO_SINT     , EVT::i32  , Custom);
    setOperationAction(ISD::FP_TO_SINT     , EVT::i16  , Custom);
    setOperationAction(ISD::FP_TO_SINT     , EVT::i32  , Custom);
  }

  // Handle FP_TO_UINT by promoting the destination to a larger signed
  // conversion.
  setOperationAction(ISD::FP_TO_UINT       , EVT::i1   , Promote);
  setOperationAction(ISD::FP_TO_UINT       , EVT::i8   , Promote);
  setOperationAction(ISD::FP_TO_UINT       , EVT::i16  , Promote);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit()) {
    setOperationAction(ISD::FP_TO_UINT     , EVT::i64  , Expand);
    setOperationAction(ISD::FP_TO_UINT     , EVT::i32  , Promote);
    if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng's avatar
Evan Cheng committed
      // Expand FP_TO_UINT into a select.
      // FIXME: We would like to use a Custom expander here eventually to do
      // the optimal thing for SSE vs. the default expansion in the legalizer.
      setOperationAction(ISD::FP_TO_UINT   , EVT::i32  , Expand);
Evan Cheng's avatar
Evan Cheng committed
    else
      // With SSE3 we can use fisttpll to convert to a signed i64; without
      // SSE, we're stuck with a fistpll.
      setOperationAction(ISD::FP_TO_UINT   , EVT::i32  , Custom);
  // TODO: when we have SSE, these could be more efficient, by using movd/movq.
  if (!X86ScalarSSEf64) {
    setOperationAction(ISD::BIT_CONVERT      , EVT::f32  , Expand);
    setOperationAction(ISD::BIT_CONVERT      , EVT::i32  , Expand);
  // Scalar integer divide and remainder are lowered to use operations that
  // produce two results, to match the available instructions. This exposes
  // the two-result form to trivial CSE, which is able to combine x/y and x%y
  // into a single instruction.
  //
  // Scalar integer multiply-high is also lowered to use two-result
  // operations, to match the available instructions. However, plain multiply
  // (low) operations are left as Legal, as there are single-result
  // instructions for this in x86. Using the two-result multiply instructions
  // when both high and low results are needed must be arranged by dagcombine.
  setOperationAction(ISD::MULHS           , EVT::i8    , Expand);
  setOperationAction(ISD::MULHU           , EVT::i8    , Expand);
  setOperationAction(ISD::SDIV            , EVT::i8    , Expand);
  setOperationAction(ISD::UDIV            , EVT::i8    , Expand);
  setOperationAction(ISD::SREM            , EVT::i8    , Expand);
  setOperationAction(ISD::UREM            , EVT::i8    , Expand);
  setOperationAction(ISD::MULHS           , EVT::i16   , Expand);
  setOperationAction(ISD::MULHU           , EVT::i16   , Expand);
  setOperationAction(ISD::SDIV            , EVT::i16   , Expand);
  setOperationAction(ISD::UDIV            , EVT::i16   , Expand);
  setOperationAction(ISD::SREM            , EVT::i16   , Expand);
  setOperationAction(ISD::UREM            , EVT::i16   , Expand);
  setOperationAction(ISD::MULHS           , EVT::i32   , Expand);
  setOperationAction(ISD::MULHU           , EVT::i32   , Expand);
  setOperationAction(ISD::SDIV            , EVT::i32   , Expand);
  setOperationAction(ISD::UDIV            , EVT::i32   , Expand);
  setOperationAction(ISD::SREM            , EVT::i32   , Expand);
  setOperationAction(ISD::UREM            , EVT::i32   , Expand);
  setOperationAction(ISD::MULHS           , EVT::i64   , Expand);
  setOperationAction(ISD::MULHU           , EVT::i64   , Expand);
  setOperationAction(ISD::SDIV            , EVT::i64   , Expand);
  setOperationAction(ISD::UDIV            , EVT::i64   , Expand);
  setOperationAction(ISD::SREM            , EVT::i64   , Expand);
  setOperationAction(ISD::UREM            , EVT::i64   , Expand);

  setOperationAction(ISD::BR_JT            , EVT::Other, Expand);
  setOperationAction(ISD::BRCOND           , EVT::Other, Custom);
  setOperationAction(ISD::BR_CC            , EVT::Other, Expand);
  setOperationAction(ISD::SELECT_CC        , EVT::Other, Expand);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit())
    setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i32, Legal);
  setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i16  , Legal);
  setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i8   , Legal);
  setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i1   , Expand);
  setOperationAction(ISD::FP_ROUND_INREG   , EVT::f32  , Expand);
  setOperationAction(ISD::FREM             , EVT::f32  , Expand);
  setOperationAction(ISD::FREM             , EVT::f64  , Expand);
  setOperationAction(ISD::FREM             , EVT::f80  , Expand);
  setOperationAction(ISD::FLT_ROUNDS_      , EVT::i32  , Custom);

  setOperationAction(ISD::CTPOP            , EVT::i8   , Expand);
  setOperationAction(ISD::CTTZ             , EVT::i8   , Custom);
  setOperationAction(ISD::CTLZ             , EVT::i8   , Custom);
  setOperationAction(ISD::CTPOP            , EVT::i16  , Expand);
  setOperationAction(ISD::CTTZ             , EVT::i16  , Custom);
  setOperationAction(ISD::CTLZ             , EVT::i16  , Custom);
  setOperationAction(ISD::CTPOP            , EVT::i32  , Expand);
  setOperationAction(ISD::CTTZ             , EVT::i32  , Custom);
  setOperationAction(ISD::CTLZ             , EVT::i32  , Custom);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit()) {
    setOperationAction(ISD::CTPOP          , EVT::i64  , Expand);
    setOperationAction(ISD::CTTZ           , EVT::i64  , Custom);
    setOperationAction(ISD::CTLZ           , EVT::i64  , Custom);
  setOperationAction(ISD::READCYCLECOUNTER , EVT::i64  , Custom);
  setOperationAction(ISD::BSWAP            , EVT::i16  , Expand);
  // These should be promoted to a larger select which is supported.
  setOperationAction(ISD::SELECT           , EVT::i1   , Promote);
  setOperationAction(ISD::SELECT           , EVT::i8   , Promote);
  // X86 wants to expand cmov itself.
  setOperationAction(ISD::SELECT          , EVT::i16  , Custom);
  setOperationAction(ISD::SELECT          , EVT::i32  , Custom);
  setOperationAction(ISD::SELECT          , EVT::f32  , Custom);
  setOperationAction(ISD::SELECT          , EVT::f64  , Custom);
  setOperationAction(ISD::SELECT          , EVT::f80  , Custom);
  setOperationAction(ISD::SETCC           , EVT::i8   , Custom);
  setOperationAction(ISD::SETCC           , EVT::i16  , Custom);
  setOperationAction(ISD::SETCC           , EVT::i32  , Custom);
  setOperationAction(ISD::SETCC           , EVT::f32  , Custom);
  setOperationAction(ISD::SETCC           , EVT::f64  , Custom);
  setOperationAction(ISD::SETCC           , EVT::f80  , Custom);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit()) {
    setOperationAction(ISD::SELECT        , EVT::i64  , Custom);
    setOperationAction(ISD::SETCC         , EVT::i64  , Custom);
  setOperationAction(ISD::EH_RETURN       , EVT::Other, Custom);
  // Darwin ABI issue.
  setOperationAction(ISD::ConstantPool    , EVT::i32  , Custom);
  setOperationAction(ISD::JumpTable       , EVT::i32  , Custom);
  setOperationAction(ISD::GlobalAddress   , EVT::i32  , Custom);
  setOperationAction(ISD::GlobalTLSAddress, EVT::i32  , Custom);
    setOperationAction(ISD::GlobalTLSAddress, EVT::i64, Custom);
  setOperationAction(ISD::ExternalSymbol  , EVT::i32  , Custom);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit()) {
    setOperationAction(ISD::ConstantPool  , EVT::i64  , Custom);
    setOperationAction(ISD::JumpTable     , EVT::i64  , Custom);
    setOperationAction(ISD::GlobalAddress , EVT::i64  , Custom);
    setOperationAction(ISD::ExternalSymbol, EVT::i64  , Custom);
  // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
  setOperationAction(ISD::SHL_PARTS       , EVT::i32  , Custom);
  setOperationAction(ISD::SRA_PARTS       , EVT::i32  , Custom);
  setOperationAction(ISD::SRL_PARTS       , EVT::i32  , Custom);
  if (Subtarget->is64Bit()) {
    setOperationAction(ISD::SHL_PARTS     , EVT::i64  , Custom);
    setOperationAction(ISD::SRA_PARTS     , EVT::i64  , Custom);
    setOperationAction(ISD::SRL_PARTS     , EVT::i64  , Custom);
  if (Subtarget->hasSSE1())
    setOperationAction(ISD::PREFETCH      , EVT::Other, Legal);
    setOperationAction(ISD::MEMBARRIER    , EVT::Other, Expand);
  // Expand certain atomics
  setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i8, Custom);
  setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i16, Custom);
  setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i32, Custom);
  setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i64, Custom);
  setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i8, Custom);
  setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i16, Custom);
  setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i32, Custom);
  setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i64, Custom);
  if (!Subtarget->is64Bit()) {
    setOperationAction(ISD::ATOMIC_LOAD_ADD, EVT::i64, Custom);
    setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i64, Custom);
    setOperationAction(ISD::ATOMIC_LOAD_AND, EVT::i64, Custom);
    setOperationAction(ISD::ATOMIC_LOAD_OR, EVT::i64, Custom);
    setOperationAction(ISD::ATOMIC_LOAD_XOR, EVT::i64, Custom);
    setOperationAction(ISD::ATOMIC_LOAD_NAND, EVT::i64, Custom);
    setOperationAction(ISD::ATOMIC_SWAP, EVT::i64, Custom);
  // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
  setOperationAction(ISD::DBG_STOPPOINT, EVT::Other, Expand);
Evan Cheng's avatar
Evan Cheng committed
  // FIXME - use subtarget debug flags
  if (!Subtarget->isTargetDarwin() &&
      !Subtarget->isTargetELF() &&
      !Subtarget->isTargetCygMing()) {
    setOperationAction(ISD::DBG_LABEL, EVT::Other, Expand);
    setOperationAction(ISD::EH_LABEL, EVT::Other, Expand);
  setOperationAction(ISD::EXCEPTIONADDR, EVT::i64, Expand);
  setOperationAction(ISD::EHSELECTION,   EVT::i64, Expand);
  setOperationAction(ISD::EXCEPTIONADDR, EVT::i32, Expand);
  setOperationAction(ISD::EHSELECTION,   EVT::i32, Expand);
  if (Subtarget->is64Bit()) {
    setExceptionPointerRegister(X86::RAX);
    setExceptionSelectorRegister(X86::RDX);
  } else {
    setExceptionPointerRegister(X86::EAX);
    setExceptionSelectorRegister(X86::EDX);
  }
  setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, EVT::i32, Custom);
  setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, EVT::i64, Custom);
  setOperationAction(ISD::TRAMPOLINE, EVT::Other, Custom);
  setOperationAction(ISD::TRAP, EVT::Other, Legal);
Nate Begeman's avatar
Nate Begeman committed
  // VASTART needs to be custom lowered to use the VarArgsFrameIndex
  setOperationAction(ISD::VASTART           , EVT::Other, Custom);
  setOperationAction(ISD::VAEND             , EVT::Other, Expand);
    setOperationAction(ISD::VAARG           , EVT::Other, Custom);
    setOperationAction(ISD::VACOPY          , EVT::Other, Custom);
    setOperationAction(ISD::VAARG           , EVT::Other, Expand);
    setOperationAction(ISD::VACOPY          , EVT::Other, Expand);
  setOperationAction(ISD::STACKSAVE,          EVT::Other, Expand);
  setOperationAction(ISD::STACKRESTORE,       EVT::Other, Expand);
Evan Cheng's avatar
Evan Cheng committed
  if (Subtarget->is64Bit())
    setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i64, Expand);
    setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i32, Custom);
    setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i32, Expand);
  if (!UseSoftFloat && X86ScalarSSEf64) {
    // f32 and f64 use SSE.
    addRegisterClass(EVT::f32, X86::FR32RegisterClass);
    addRegisterClass(EVT::f64, X86::FR64RegisterClass);
    // Use ANDPD to simulate FABS.
    setOperationAction(ISD::FABS , EVT::f64, Custom);
    setOperationAction(ISD::FABS , EVT::f32, Custom);
    setOperationAction(ISD::FNEG , EVT::f64, Custom);
    setOperationAction(ISD::FNEG , EVT::f32, Custom);
    // Use ANDPD and ORPD to simulate FCOPYSIGN.
    setOperationAction(ISD::FCOPYSIGN, EVT::f64, Custom);
    setOperationAction(ISD::FCOPYSIGN, EVT::f32, Custom);
Evan Cheng's avatar
Evan Cheng committed
    // We don't support sin/cos/fmod
    setOperationAction(ISD::FSIN , EVT::f64, Expand);
    setOperationAction(ISD::FCOS , EVT::f64, Expand);
    setOperationAction(ISD::FSIN , EVT::f32, Expand);
    setOperationAction(ISD::FCOS , EVT::f32, Expand);
    // Expand FP immediates into loads from the stack, except for the special
    // cases we handle.
    addLegalFPImmediate(APFloat(+0.0)); // xorpd
    addLegalFPImmediate(APFloat(+0.0f)); // xorps
  } else if (!UseSoftFloat && X86ScalarSSEf32) {
    // Use SSE for f32, x87 for f64.
    // Set up the FP register classes.
    addRegisterClass(EVT::f32, X86::FR32RegisterClass);
    addRegisterClass(EVT::f64, X86::RFP64RegisterClass);

    // Use ANDPS to simulate FABS.
    setOperationAction(ISD::FABS , EVT::f32, Custom);

    // Use XORP to simulate FNEG.
    setOperationAction(ISD::FNEG , EVT::f32, Custom);
    setOperationAction(ISD::UNDEF,     EVT::f64, Expand);

    // Use ANDPS and ORPS to simulate FCOPYSIGN.
    setOperationAction(ISD::FCOPYSIGN, EVT::f64, Expand);
    setOperationAction(ISD::FCOPYSIGN, EVT::f32, Custom);

    // We don't support sin/cos/fmod
    setOperationAction(ISD::FSIN , EVT::f32, Expand);
    setOperationAction(ISD::FCOS , EVT::f32, Expand);
    // Special cases we handle for FP constants.
    addLegalFPImmediate(APFloat(+0.0f)); // xorps
    addLegalFPImmediate(APFloat(+0.0)); // FLD0
    addLegalFPImmediate(APFloat(+1.0)); // FLD1
    addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
    addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS

    if (!UnsafeFPMath) {
      setOperationAction(ISD::FSIN           , EVT::f64  , Expand);
      setOperationAction(ISD::FCOS           , EVT::f64  , Expand);
  } else if (!UseSoftFloat) {
    // f32 and f64 in x87.
    addRegisterClass(EVT::f64, X86::RFP64RegisterClass);
    addRegisterClass(EVT::f32, X86::RFP32RegisterClass);
    setOperationAction(ISD::UNDEF,     EVT::f64, Expand);
    setOperationAction(ISD::UNDEF,     EVT::f32, Expand);
    setOperationAction(ISD::FCOPYSIGN, EVT::f64, Expand);
    setOperationAction(ISD::FCOPYSIGN, EVT::f32, Expand);
      setOperationAction(ISD::FSIN           , EVT::f64  , Expand);
      setOperationAction(ISD::FCOS           , EVT::f64  , Expand);
    addLegalFPImmediate(APFloat(+0.0)); // FLD0
    addLegalFPImmediate(APFloat(+1.0)); // FLD1
    addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
    addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
    addLegalFPImmediate(APFloat(+0.0f)); // FLD0
    addLegalFPImmediate(APFloat(+1.0f)); // FLD1
    addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
    addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
  // Long double always uses X87.
    addRegisterClass(EVT::f80, X86::RFP80RegisterClass);
    setOperationAction(ISD::UNDEF,     EVT::f80, Expand);
    setOperationAction(ISD::FCOPYSIGN, EVT::f80, Expand);
    {
      bool ignored;
      APFloat TmpFlt(+0.0);
      TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
                     &ignored);
      addLegalFPImmediate(TmpFlt);  // FLD0
      TmpFlt.changeSign();
      addLegalFPImmediate(TmpFlt);  // FLD0/FCHS
      APFloat TmpFlt2(+1.0);
      TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
                      &ignored);
      addLegalFPImmediate(TmpFlt2);  // FLD1
      TmpFlt2.changeSign();
      addLegalFPImmediate(TmpFlt2);  // FLD1/FCHS
    }
    if (!UnsafeFPMath) {
      setOperationAction(ISD::FSIN           , EVT::f80  , Expand);
      setOperationAction(ISD::FCOS           , EVT::f80  , Expand);
Dan Gohman's avatar
Dan Gohman committed
  // Always use a library call for pow.
  setOperationAction(ISD::FPOW             , EVT::f32  , Expand);
  setOperationAction(ISD::FPOW             , EVT::f64  , Expand);
  setOperationAction(ISD::FPOW             , EVT::f80  , Expand);
  setOperationAction(ISD::FLOG, EVT::f80, Expand);
  setOperationAction(ISD::FLOG2, EVT::f80, Expand);
  setOperationAction(ISD::FLOG10, EVT::f80, Expand);
  setOperationAction(ISD::FEXP, EVT::f80, Expand);
  setOperationAction(ISD::FEXP2, EVT::f80, Expand);
Mon P Wang's avatar
Mon P Wang committed
  // First set operation action for all vector types to either promote
  // (for widening) or expand (for scalarization). Then we will selectively
  // turn on ones that can be effectively codegen'd.
  for (unsigned VT = (unsigned)EVT::FIRST_VECTOR_VALUETYPE;
       VT <= (unsigned)EVT::LAST_VECTOR_VALUETYPE; ++VT) {
    setOperationAction(ISD::ADD , (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SUB , (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FADD, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FNEG, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FSUB, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::MUL , (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FMUL, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SDIV, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::UDIV, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FDIV, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SREM, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::UREM, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::LOAD, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::VECTOR_SHUFFLE, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT,(EVT::SimpleValueType)VT,Expand);
    setOperationAction(ISD::EXTRACT_SUBVECTOR,(EVT::SimpleValueType)VT,Expand);
    setOperationAction(ISD::INSERT_VECTOR_ELT,(EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FABS, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FSIN, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FCOS, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FREM, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FPOWI, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FSQRT, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FCOPYSIGN, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SMUL_LOHI, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::UMUL_LOHI, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SDIVREM, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::UDIVREM, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FPOW, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::CTPOP, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::CTTZ, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::CTLZ, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SHL, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SRA, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SRL, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::ROTL, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::ROTR, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::BSWAP, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::VSETCC, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FLOG, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FLOG2, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FLOG10, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FEXP, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FEXP2, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FP_TO_UINT, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::FP_TO_SINT, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::UINT_TO_FP, (EVT::SimpleValueType)VT, Expand);
    setOperationAction(ISD::SINT_TO_FP, (EVT::SimpleValueType)VT, Expand);
  // FIXME: In order to prevent SSE instructions being expanded to MMX ones
  // with -msoft-float, disable use of MMX as well.
  if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
    addRegisterClass(EVT::v8i8,  X86::VR64RegisterClass);
    addRegisterClass(EVT::v4i16, X86::VR64RegisterClass);
    addRegisterClass(EVT::v2i32, X86::VR64RegisterClass);
    addRegisterClass(EVT::v2f32, X86::VR64RegisterClass);
    addRegisterClass(EVT::v1i64, X86::VR64RegisterClass);

    setOperationAction(ISD::ADD,                EVT::v8i8,  Legal);
    setOperationAction(ISD::ADD,                EVT::v4i16, Legal);
    setOperationAction(ISD::ADD,                EVT::v2i32, Legal);
    setOperationAction(ISD::ADD,                EVT::v1i64, Legal);

    setOperationAction(ISD::SUB,                EVT::v8i8,  Legal);
    setOperationAction(ISD::SUB,                EVT::v4i16, Legal);
    setOperationAction(ISD::SUB,                EVT::v2i32, Legal);
    setOperationAction(ISD::SUB,                EVT::v1i64, Legal);

    setOperationAction(ISD::MULHS,              EVT::v4i16, Legal);
    setOperationAction(ISD::MUL,                EVT::v4i16, Legal);

    setOperationAction(ISD::AND,                EVT::v8i8,  Promote);
    AddPromotedToType (ISD::AND,                EVT::v8i8,  EVT::v1i64);
    setOperationAction(ISD::AND,                EVT::v4i16, Promote);
    AddPromotedToType (ISD::AND,                EVT::v4i16, EVT::v1i64);
    setOperationAction(ISD::AND,                EVT::v2i32, Promote);
    AddPromotedToType (ISD::AND,                EVT::v2i32, EVT::v1i64);
    setOperationAction(ISD::AND,                EVT::v1i64, Legal);

    setOperationAction(ISD::OR,                 EVT::v8i8,  Promote);
    AddPromotedToType (ISD::OR,                 EVT::v8i8,  EVT::v1i64);
    setOperationAction(ISD::OR,                 EVT::v4i16, Promote);
    AddPromotedToType (ISD::OR,                 EVT::v4i16, EVT::v1i64);
    setOperationAction(ISD::OR,                 EVT::v2i32, Promote);
    AddPromotedToType (ISD::OR,                 EVT::v2i32, EVT::v1i64);
    setOperationAction(ISD::OR,                 EVT::v1i64, Legal);

    setOperationAction(ISD::XOR,                EVT::v8i8,  Promote);
    AddPromotedToType (ISD::XOR,                EVT::v8i8,  EVT::v1i64);
    setOperationAction(ISD::XOR,                EVT::v4i16, Promote);
    AddPromotedToType (ISD::XOR,                EVT::v4i16, EVT::v1i64);
    setOperationAction(ISD::XOR,                EVT::v2i32, Promote);
    AddPromotedToType (ISD::XOR,                EVT::v2i32, EVT::v1i64);
    setOperationAction(ISD::XOR,                EVT::v1i64, Legal);

    setOperationAction(ISD::LOAD,               EVT::v8i8,  Promote);
    AddPromotedToType (ISD::LOAD,               EVT::v8i8,  EVT::v1i64);
    setOperationAction(ISD::LOAD,               EVT::v4i16, Promote);
    AddPromotedToType (ISD::LOAD,               EVT::v4i16, EVT::v1i64);
    setOperationAction(ISD::LOAD,               EVT::v2i32, Promote);
    AddPromotedToType (ISD::LOAD,               EVT::v2i32, EVT::v1i64);
    setOperationAction(ISD::LOAD,               EVT::v2f32, Promote);
    AddPromotedToType (ISD::LOAD,               EVT::v2f32, EVT::v1i64);
    setOperationAction(ISD::LOAD,               EVT::v1i64, Legal);

    setOperationAction(ISD::BUILD_VECTOR,       EVT::v8i8,  Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v4i16, Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v2i32, Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v2f32, Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v1i64, Custom);

    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v8i8,  Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v4i16, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v2i32, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v1i64, Custom);

    setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v2f32, Custom);
    setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v8i8,  Custom);
    setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v4i16, Custom);
    setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v1i64, Custom);

    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4i16, Custom);

    setTruncStoreAction(EVT::v8i16,             EVT::v8i8, Expand);
    setOperationAction(ISD::TRUNCATE,           EVT::v8i8, Expand);
    setOperationAction(ISD::SELECT,             EVT::v8i8, Promote);
    setOperationAction(ISD::SELECT,             EVT::v4i16, Promote);
    setOperationAction(ISD::SELECT,             EVT::v2i32, Promote);
    setOperationAction(ISD::SELECT,             EVT::v1i64, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v8i8, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v4i16, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v2i32, Custom);
  if (!UseSoftFloat && Subtarget->hasSSE1()) {
    addRegisterClass(EVT::v4f32, X86::VR128RegisterClass);

    setOperationAction(ISD::FADD,               EVT::v4f32, Legal);
    setOperationAction(ISD::FSUB,               EVT::v4f32, Legal);
    setOperationAction(ISD::FMUL,               EVT::v4f32, Legal);
    setOperationAction(ISD::FDIV,               EVT::v4f32, Legal);
    setOperationAction(ISD::FSQRT,              EVT::v4f32, Legal);
    setOperationAction(ISD::FNEG,               EVT::v4f32, Custom);
    setOperationAction(ISD::LOAD,               EVT::v4f32, Legal);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v4f32, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v4f32, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4f32, Custom);
    setOperationAction(ISD::SELECT,             EVT::v4f32, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v4f32, Custom);
  if (!UseSoftFloat && Subtarget->hasSSE2()) {
    addRegisterClass(EVT::v2f64, X86::VR128RegisterClass);
    // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
    // registers cannot be used even for integer operations.
    addRegisterClass(EVT::v16i8, X86::VR128RegisterClass);
    addRegisterClass(EVT::v8i16, X86::VR128RegisterClass);
    addRegisterClass(EVT::v4i32, X86::VR128RegisterClass);
    addRegisterClass(EVT::v2i64, X86::VR128RegisterClass);

    setOperationAction(ISD::ADD,                EVT::v16i8, Legal);
    setOperationAction(ISD::ADD,                EVT::v8i16, Legal);
    setOperationAction(ISD::ADD,                EVT::v4i32, Legal);
    setOperationAction(ISD::ADD,                EVT::v2i64, Legal);
    setOperationAction(ISD::MUL,                EVT::v2i64, Custom);
    setOperationAction(ISD::SUB,                EVT::v16i8, Legal);
    setOperationAction(ISD::SUB,                EVT::v8i16, Legal);
    setOperationAction(ISD::SUB,                EVT::v4i32, Legal);
    setOperationAction(ISD::SUB,                EVT::v2i64, Legal);
    setOperationAction(ISD::MUL,                EVT::v8i16, Legal);
    setOperationAction(ISD::FADD,               EVT::v2f64, Legal);
    setOperationAction(ISD::FSUB,               EVT::v2f64, Legal);
    setOperationAction(ISD::FMUL,               EVT::v2f64, Legal);
    setOperationAction(ISD::FDIV,               EVT::v2f64, Legal);
    setOperationAction(ISD::FSQRT,              EVT::v2f64, Legal);
    setOperationAction(ISD::FNEG,               EVT::v2f64, Custom);

    setOperationAction(ISD::VSETCC,             EVT::v2f64, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v16i8, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v8i16, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v4i32, Custom);

    setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v16i8, Custom);
    setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v8i16, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v8i16, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4i32, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4f32, Custom);

    // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
    for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v2i64; ++i) {
      EVT VT = (EVT::SimpleValueType)i;
      // Do not attempt to custom lower non-power-of-2 vectors
      if (!isPowerOf2_32(VT.getVectorNumElements()))
David Greene's avatar
 
David Greene committed
      // Do not attempt to custom lower non-128-bit vectors
      if (!VT.is128BitVector())
        continue;
      setOperationAction(ISD::BUILD_VECTOR,       VT.getSimpleVT(), Custom);
      setOperationAction(ISD::VECTOR_SHUFFLE,     VT.getSimpleVT(), Custom);
      setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v2f64, Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v2i64, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v2f64, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v2i64, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v2f64, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v2f64, Custom);
    if (Subtarget->is64Bit()) {
      setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v2i64, Custom);
      setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v2i64, Custom);
    // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
    for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v2i64; i++) {
      EVT::SimpleValueType SVT = (EVT::SimpleValueType)i;
      EVT VT = SVT;
David Greene's avatar
 
David Greene committed

      // Do not attempt to promote non-128-bit vectors
      if (!VT.is128BitVector()) {
        continue;
      }
      setOperationAction(ISD::AND,    SVT, Promote);
      setOperationAction(ISD::OR,     SVT, Promote);
      setOperationAction(ISD::XOR,    SVT, Promote);
      setOperationAction(ISD::LOAD,   SVT, Promote);
      setOperationAction(ISD::SELECT, SVT, Promote);
      AddPromotedToType (ISD::SELECT, SVT, EVT::v2i64);
    // Custom lower v2i64 and v2f64 selects.
    setOperationAction(ISD::LOAD,               EVT::v2f64, Legal);
    setOperationAction(ISD::LOAD,               EVT::v2i64, Legal);
    setOperationAction(ISD::SELECT,             EVT::v2f64, Custom);
    setOperationAction(ISD::SELECT,             EVT::v2i64, Custom);
    setOperationAction(ISD::FP_TO_SINT,         EVT::v4i32, Legal);
    setOperationAction(ISD::SINT_TO_FP,         EVT::v4i32, Legal);
    if (!DisableMMX && Subtarget->hasMMX()) {
      setOperationAction(ISD::FP_TO_SINT,         EVT::v2i32, Custom);
      setOperationAction(ISD::SINT_TO_FP,         EVT::v2i32, Custom);
  if (Subtarget->hasSSE41()) {
    // FIXME: Do we need to handle scalar-to-vector here?

    // i8 and i16 vectors are custom , because the source register and source
    // source memory operand types are not the same width.  f32 vectors are
    // custom since the immediate controlling the insert encodes additional
    // information.
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v16i8, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v8i16, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4i32, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4f32, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v16i8, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v8i16, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4i32, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4f32, Custom);

    if (Subtarget->is64Bit()) {
      setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v2i64, Legal);
      setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v2i64, Legal);
  if (Subtarget->hasSSE42()) {
    setOperationAction(ISD::VSETCC,             EVT::v2i64, Custom);
David Greene's avatar
 
David Greene committed
  if (!UseSoftFloat && Subtarget->hasAVX()) {
    addRegisterClass(EVT::v8f32, X86::VR256RegisterClass);
    addRegisterClass(EVT::v4f64, X86::VR256RegisterClass);
    addRegisterClass(EVT::v8i32, X86::VR256RegisterClass);
    addRegisterClass(EVT::v4i64, X86::VR256RegisterClass);

    setOperationAction(ISD::LOAD,               EVT::v8f32, Legal);
    setOperationAction(ISD::LOAD,               EVT::v8i32, Legal);
    setOperationAction(ISD::LOAD,               EVT::v4f64, Legal);
    setOperationAction(ISD::LOAD,               EVT::v4i64, Legal);
    setOperationAction(ISD::FADD,               EVT::v8f32, Legal);
    setOperationAction(ISD::FSUB,               EVT::v8f32, Legal);
    setOperationAction(ISD::FMUL,               EVT::v8f32, Legal);
    setOperationAction(ISD::FDIV,               EVT::v8f32, Legal);
    setOperationAction(ISD::FSQRT,              EVT::v8f32, Legal);
    setOperationAction(ISD::FNEG,               EVT::v8f32, Custom);
    //setOperationAction(ISD::BUILD_VECTOR,       EVT::v8f32, Custom);
    //setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v8f32, Custom);
    //setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v8f32, Custom);
    //setOperationAction(ISD::SELECT,             EVT::v8f32, Custom);
    //setOperationAction(ISD::VSETCC,             EVT::v8f32, Custom);
David Greene's avatar
 
David Greene committed

    // Operations to consider commented out -v16i16 v32i8
    //setOperationAction(ISD::ADD,                EVT::v16i16, Legal);
    setOperationAction(ISD::ADD,                EVT::v8i32, Custom);
    setOperationAction(ISD::ADD,                EVT::v4i64, Custom);
    //setOperationAction(ISD::SUB,                EVT::v32i8, Legal);
    //setOperationAction(ISD::SUB,                EVT::v16i16, Legal);
    setOperationAction(ISD::SUB,                EVT::v8i32, Custom);
    setOperationAction(ISD::SUB,                EVT::v4i64, Custom);
    //setOperationAction(ISD::MUL,                EVT::v16i16, Legal);
    setOperationAction(ISD::FADD,               EVT::v4f64, Legal);
    setOperationAction(ISD::FSUB,               EVT::v4f64, Legal);
    setOperationAction(ISD::FMUL,               EVT::v4f64, Legal);
    setOperationAction(ISD::FDIV,               EVT::v4f64, Legal);
    setOperationAction(ISD::FSQRT,              EVT::v4f64, Legal);
    setOperationAction(ISD::FNEG,               EVT::v4f64, Custom);

    setOperationAction(ISD::VSETCC,             EVT::v4f64, Custom);
    // setOperationAction(ISD::VSETCC,             EVT::v32i8, Custom);
    // setOperationAction(ISD::VSETCC,             EVT::v16i16, Custom);
    setOperationAction(ISD::VSETCC,             EVT::v8i32, Custom);

    // setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v32i8, Custom);
    // setOperationAction(ISD::SCALAR_TO_VECTOR,   EVT::v16i16, Custom);
    // setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v16i16, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v8i32, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v8f32, Custom);

    setOperationAction(ISD::BUILD_VECTOR,       EVT::v4f64, Custom);
    setOperationAction(ISD::BUILD_VECTOR,       EVT::v4i64, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v4f64, Custom);
    setOperationAction(ISD::VECTOR_SHUFFLE,     EVT::v4i64, Custom);
    setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4f64, Custom);
    setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4f64, Custom);
David Greene's avatar
 
David Greene committed

#if 0
    // Not sure we want to do this since there are no 256-bit integer
    // operations in AVX

    // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
    // This includes 256-bit vectors
    for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v4i64; ++i) {
      EVT VT = (EVT::SimpleValueType)i;
David Greene's avatar
 
David Greene committed

      // Do not attempt to custom lower non-power-of-2 vectors
      if (!isPowerOf2_32(VT.getVectorNumElements()))
        continue;

      setOperationAction(ISD::BUILD_VECTOR,       VT, Custom);
      setOperationAction(ISD::VECTOR_SHUFFLE,     VT, Custom);
      setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
    }

    if (Subtarget->is64Bit()) {
      setOperationAction(ISD::INSERT_VECTOR_ELT,  EVT::v4i64, Custom);
      setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4i64, Custom);
David Greene's avatar
 
David Greene committed
    }    
#endif

#if 0
    // Not sure we want to do this since there are no 256-bit integer
    // operations in AVX

    // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
    // Including 256-bit vectors
    for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v4i64; i++) {
      EVT VT = (EVT::SimpleValueType)i;
David Greene's avatar
 
David Greene committed

      if (!VT.is256BitVector()) {
        continue;
      }
      setOperationAction(ISD::AND,    VT, Promote);
David Greene's avatar
 
David Greene committed
      setOperationAction(ISD::OR,     VT, Promote);
David Greene's avatar
 
David Greene committed
      setOperationAction(ISD::XOR,    VT, Promote);
David Greene's avatar
 
David Greene committed
      setOperationAction(ISD::LOAD,   VT, Promote);
David Greene's avatar
 
David Greene committed
      setOperationAction(ISD::SELECT, VT, Promote);
David Greene's avatar
 
David Greene committed
    }

David Greene's avatar
 
David Greene committed
#endif
  }

  // We want to custom lower some of our intrinsics.
  setOperationAction(ISD::INTRINSIC_WO_CHAIN, EVT::Other, Custom);
  // Add/Sub/Mul with overflow operations are custom lowered.
  setOperationAction(ISD::SADDO, EVT::i32, Custom);
  setOperationAction(ISD::SADDO, EVT::i64, Custom);
  setOperationAction(ISD::UADDO, EVT::i32, Custom);
  setOperationAction(ISD::UADDO, EVT::i64, Custom);
  setOperationAction(ISD::SSUBO, EVT::i32, Custom);
  setOperationAction(ISD::SSUBO, EVT::i64, Custom);
  setOperationAction(ISD::USUBO, EVT::i32, Custom);
  setOperationAction(ISD::USUBO, EVT::i64, Custom);
  setOperationAction(ISD::SMULO, EVT::i32, Custom);
  setOperationAction(ISD::SMULO, EVT::i64, Custom);
  if (!Subtarget->is64Bit()) {
    // These libcalls are not available in 32-bit.
    setLibcallName(RTLIB::SHL_I128, 0);
    setLibcallName(RTLIB::SRL_I128, 0);
    setLibcallName(RTLIB::SRA_I128, 0);
  }

  // We have target-specific dag combine patterns for the following nodes:
  setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
  setTargetDAGCombine(ISD::BUILD_VECTOR);
  setTargetDAGCombine(ISD::SELECT);
  setTargetDAGCombine(ISD::SHL);
  setTargetDAGCombine(ISD::SRA);
  setTargetDAGCombine(ISD::SRL);
  setTargetDAGCombine(ISD::STORE);
  setTargetDAGCombine(ISD::MEMBARRIER);
  if (Subtarget->is64Bit())
    setTargetDAGCombine(ISD::MUL);
  // FIXME: These should be based on subtarget info. Plus, the values should
  // be smaller when we are in optimizing for size mode.
  maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
  maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
  maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
  allowUnalignedMemoryAccesses = true; // x86 supports it!
EVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
  return EVT::i8;
/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
/// the desired ByVal argument alignment.
static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
  if (MaxAlign == 16)
    return;
  if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
    if (VTy->getBitWidth() == 128)
      MaxAlign = 16;
  } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
    unsigned EltAlign = 0;
    getMaxByValAlign(ATy->getElementType(), EltAlign);
    if (EltAlign > MaxAlign)
      MaxAlign = EltAlign;
  } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
      unsigned EltAlign = 0;
      getMaxByValAlign(STy->getElementType(i), EltAlign);
      if (EltAlign > MaxAlign)
        MaxAlign = EltAlign;
      if (MaxAlign == 16)
        break;
    }
  }
  return;
}

/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
/// function arguments in the caller parameter area. For X86, aggregates
/// that contain SSE vectors are placed at 16-byte boundaries while the rest
/// are at 4-byte boundaries.
unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
  if (Subtarget->is64Bit()) {
    // Max of 8 and alignment of type.