blob: 831386f408e3f06aae19c52177a64237ce46c6fc [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Craig Topper1bf724b2012-02-19 07:15:48 +000016#include "X86ISelLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000161 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000162
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000163 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000164 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000167 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000170 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000171 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Eric Christopherde5e1012011-03-11 01:05:58 +0000174 // For 64-bit since we have so many registers use the ILP scheduler, for
175 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000176 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000177 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000179 else if (Subtarget->is64Bit())
180 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000181 else
182 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000184
Preston Gurd2e2efd92012-09-04 18:22:17 +0000185 // Bypass i32 with i8 on Atom when compiling with O2
186 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
187 addBypassSlowDivType(Type::getInt32Ty(getGlobalContext()), Type::getInt8Ty(getGlobalContext()));
188
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000189 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000190 // Setup Windows compiler runtime calls.
191 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000192 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000193 setLibcallName(RTLIB::SREM_I64, "_allrem");
194 setLibcallName(RTLIB::UREM_I64, "_aullrem");
195 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
200 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000201
202 // The _ftol2 runtime function has an unusual calling conv, which
203 // is modeled by a special pseudo-instruction.
204 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
206 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
207 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000208 }
209
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000211 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 setUseUnderscoreSetJmp(false);
213 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000214 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 // MS runtime is weird: it exports _setjmp, but longjmp!
216 setUseUnderscoreSetJmp(true);
217 setUseUnderscoreLongJmp(false);
218 } else {
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(true);
221 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i8, &X86::GR8RegClass);
225 addRegisterClass(MVT::i16, &X86::GR16RegClass);
226 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000228 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000231
Scott Michelfdc40a02009-02-17 22:15:04 +0000232 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
238 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000239
240 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
249 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000253
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000257 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 // We have an algorithm for SSE2->double, and we turn this into a
259 // 64-bit FILD followed by conditional FADD for other targets.
260 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 // We have an algorithm for SSE2, and we turn this into a 64-bit
262 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
266 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
267 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
269 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000271 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 // SSE has no i16 to fp conversion, only i32
273 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000281 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285
Dale Johannesen73328d12007-09-19 23:55:34 +0000286 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
287 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
289 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000290
Evan Cheng02568ff2006-01-30 22:13:22 +0000291 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
294 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000296 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000298 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000300 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 }
304
305 // Handle FP_TO_UINT by promoting the destination to a larger signed
306 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
313 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000314 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000315 // Since AVX is a superset of SSE3, only check for SSE here.
316 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 // Expand FP_TO_UINT into a select.
318 // FIXME: We would like to use a Custom expander here eventually to do
319 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // With SSE3 we can use fisttpll to convert to a signed i64; without
323 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000327 if (isTargetFTOL()) {
328 // Use the _ftol2 runtime function, which has a pseudo-instruction
329 // to handle its weird calling convention.
330 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
331 }
332
Chris Lattner399610a2006-12-05 18:22:22 +0000333 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000334 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
336 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000337 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000338 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000339 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000340 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000341 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000342 }
Chris Lattner21f66852005-12-23 05:15:23 +0000343
Dan Gohmanb00ee212008-02-18 19:34:53 +0000344 // Scalar integer divide and remainder are lowered to use operations that
345 // produce two results, to match the available instructions. This exposes
346 // the two-result form to trivial CSE, which is able to combine x/y and x%y
347 // into a single instruction.
348 //
349 // Scalar integer multiply-high is also lowered to use two-result
350 // operations, to match the available instructions. However, plain multiply
351 // (low) operations are left as Legal, as there are single-result
352 // instructions for this in x86. Using the two-result multiply instructions
353 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000354 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000355 MVT VT = IntVTs[i];
356 setOperationAction(ISD::MULHS, VT, Expand);
357 setOperationAction(ISD::MULHU, VT, Expand);
358 setOperationAction(ISD::SDIV, VT, Expand);
359 setOperationAction(ISD::UDIV, VT, Expand);
360 setOperationAction(ISD::SREM, VT, Expand);
361 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000363 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000364 setOperationAction(ISD::ADDC, VT, Custom);
365 setOperationAction(ISD::ADDE, VT, Custom);
366 setOperationAction(ISD::SUBC, VT, Custom);
367 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000368 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000369
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
371 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
372 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
373 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000374 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
379 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f32 , Expand);
381 setOperationAction(ISD::FREM , MVT::f64 , Expand);
382 setOperationAction(ISD::FREM , MVT::f80 , Expand);
383 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000384
Chandler Carruth77821022011-12-24 12:12:34 +0000385 // Promote the i8 variants and force them on up to i32 which has a shorter
386 // encoding.
387 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
388 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
390 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000391 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
393 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
394 if (Subtarget->is64Bit())
395 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000396 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000397 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
398 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
401 }
Craig Topper37f21672011-10-11 06:44:02 +0000402
403 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000404 // When promoting the i8 variants, force them to i32 for a shorter
405 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000406 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000407 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
408 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
409 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000414 } else {
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
421 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
424 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 }
426
Benjamin Kramer1292c222010-12-04 20:32:23 +0000427 if (Subtarget->hasPOPCNT()) {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
429 } else {
430 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
432 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
433 if (Subtarget->is64Bit())
434 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
435 }
436
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
438 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000443 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000444 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
449 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000450 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000457 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000458 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000460
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000461 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
463 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
464 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
465 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000466 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
468 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000469 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000470 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
472 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
473 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
474 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000475 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000476 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000477 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
483 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
484 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000485 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486
Craig Topper1accb7e2012-01-10 06:54:16 +0000487 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000489
Eric Christopher9a9d2752010-07-22 02:48:34 +0000490 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000491 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000492
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000493 // On X86 and X86-64, atomic operations are lowered to locked instructions.
494 // Locked instructions, in turn, have implicit fence semantics (all memory
495 // operations are flushed before issuing the locked instruction, and they
496 // are not buffered), so we can fold away the common pattern of
497 // fence-atomic-fence.
498 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000499
Mon P Wang63307c32008-05-05 19:05:59 +0000500 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000501 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 MVT VT = IntVTs[i];
503 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000505 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000506 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000507
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000509 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
516 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000517 }
518
Eli Friedman43f51ae2011-08-26 21:21:21 +0000519 if (Subtarget->hasCmpxchg16b()) {
520 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
521 }
522
Evan Cheng3c992d22006-03-07 02:02:57 +0000523 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000524 if (!Subtarget->isTargetDarwin() &&
525 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000526 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000528 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000529
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
531 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
532 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
533 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000534 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000535 setExceptionPointerRegister(X86::RAX);
536 setExceptionSelectorRegister(X86::RDX);
537 } else {
538 setExceptionPointerRegister(X86::EAX);
539 setExceptionSelectorRegister(X86::EDX);
540 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
542 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000543
Duncan Sands4a544a72011-09-06 13:37:06 +0000544 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
545 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000546
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000548
Nate Begemanacc398c2006-01-25 18:21:52 +0000549 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::VASTART , MVT::Other, Custom);
551 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000552 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::VAARG , MVT::Other, Custom);
554 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000555 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::VAARG , MVT::Other, Expand);
557 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000558 }
Evan Chengae642192007-03-02 23:16:35 +0000559
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
561 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000562
563 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
564 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
565 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000566 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000567 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
568 MVT::i64 : MVT::i32, Custom);
569 else
570 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
571 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000572
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000573 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000574 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000576 addRegisterClass(MVT::f32, &X86::FR32RegClass);
577 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000578
Evan Cheng223547a2006-01-31 22:28:30 +0000579 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FABS , MVT::f64, Custom);
581 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000582
583 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FNEG , MVT::f64, Custom);
585 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000586
Evan Cheng68c47cb2007-01-05 07:55:56 +0000587 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
589 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000590
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000591 // Lower this to FGETSIGNx86 plus an AND.
592 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
593 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
594
Evan Chengd25e9e82006-02-02 00:28:23 +0000595 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FSIN , MVT::f64, Expand);
597 setOperationAction(ISD::FCOS , MVT::f64, Expand);
598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000600
Chris Lattnera54aa942006-01-29 06:26:08 +0000601 // Expand FP immediates into loads from the stack, except for the special
602 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0)); // xorpd
604 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000605 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606 // Use SSE for f32, x87 for f64.
607 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000608 addRegisterClass(MVT::f32, &X86::FR32RegClass);
609 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610
611 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613
614 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
619 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f32, Expand);
625 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
Nate Begemane1795842008-02-14 08:57:00 +0000627 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628 addLegalFPImmediate(APFloat(+0.0f)); // xorps
629 addLegalFPImmediate(APFloat(+0.0)); // FLD0
630 addLegalFPImmediate(APFloat(+1.0)); // FLD1
631 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
632 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
633
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000634 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
636 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000637 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000640 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000641 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
642 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000643
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
645 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
647 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000648
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000649 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
651 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000653 addLegalFPImmediate(APFloat(+0.0)); // FLD0
654 addLegalFPImmediate(APFloat(+1.0)); // FLD1
655 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
656 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
658 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
659 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
660 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000661 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000662
Cameron Zwarich33390842011-07-08 21:39:21 +0000663 // We don't support FMA.
664 setOperationAction(ISD::FMA, MVT::f64, Expand);
665 setOperationAction(ISD::FMA, MVT::f32, Expand);
666
Dale Johannesen59a58732007-08-05 18:49:15 +0000667 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000669 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
671 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000673 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000674 addLegalFPImmediate(TmpFlt); // FLD0
675 TmpFlt.changeSign();
676 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000677
678 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 APFloat TmpFlt2(+1.0);
680 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
681 &ignored);
682 addLegalFPImmediate(TmpFlt2); // FLD1
683 TmpFlt2.changeSign();
684 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
685 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000687 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
689 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000690 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000691
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000692 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
693 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
694 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
695 setOperationAction(ISD::FRINT, MVT::f80, Expand);
696 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000697 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000698 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000699
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000700 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
702 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
703 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000704
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FLOG, MVT::f80, Expand);
706 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
707 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
708 setOperationAction(ISD::FEXP, MVT::f80, Expand);
709 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000710
Mon P Wangf007a8b2008-11-06 05:31:54 +0000711 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000712 // (for widening) or expand (for scalarization). Then we will selectively
713 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000714 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
715 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000732 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
733 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000738 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000749 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000751 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
753 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000758 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000768 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000769 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000773 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000774 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
775 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000776 setTruncStoreAction((MVT::SimpleValueType)VT,
777 (MVT::SimpleValueType)InnerVT, Expand);
778 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
779 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
780 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000781 }
782
Evan Chengc7ce29b2009-02-13 22:36:38 +0000783 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
784 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000785 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000786 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000787 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000788 }
789
Dale Johannesen0488fb62010-09-30 23:57:10 +0000790 // MMX-sized vectors (other than x86mmx) are expected to be expanded
791 // into smaller operations.
792 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
793 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
794 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
795 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
796 setOperationAction(ISD::AND, MVT::v8i8, Expand);
797 setOperationAction(ISD::AND, MVT::v4i16, Expand);
798 setOperationAction(ISD::AND, MVT::v2i32, Expand);
799 setOperationAction(ISD::AND, MVT::v1i64, Expand);
800 setOperationAction(ISD::OR, MVT::v8i8, Expand);
801 setOperationAction(ISD::OR, MVT::v4i16, Expand);
802 setOperationAction(ISD::OR, MVT::v2i32, Expand);
803 setOperationAction(ISD::OR, MVT::v1i64, Expand);
804 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
805 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
806 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
807 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
808 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
809 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
810 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
812 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
813 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
814 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
815 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
816 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000817 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
818 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
819 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
820 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000821
Craig Topper1accb7e2012-01-10 06:54:16 +0000822 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000823 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
826 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
827 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
828 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
829 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
830 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
831 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
832 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
833 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
834 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
835 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000836 }
837
Craig Topper1accb7e2012-01-10 06:54:16 +0000838 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000839 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000840
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000841 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
842 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
844 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
845 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
846 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000847
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
849 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
850 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
851 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
852 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
853 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
854 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
855 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
856 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
857 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
858 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
859 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
860 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
861 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
862 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
863 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864
Nadav Rotem354efd82011-09-18 14:57:03 +0000865 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000866 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
867 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
868 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000869
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
871 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
872 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
873 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000875
Evan Cheng2c3ae372006-04-12 21:21:57 +0000876 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000877 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000878 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000879 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000881 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000882 // Do not attempt to custom lower non-128-bit vectors
883 if (!VT.is128BitVector())
884 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000885 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
886 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
887 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000888 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
891 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
892 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
893 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
894 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
895 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000896
Nate Begemancdd1eec2008-02-12 22:51:28 +0000897 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
899 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000900 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000901
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000902 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000903 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000904 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000905
906 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000907 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000908 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000909
Craig Topper0d1f1762012-08-12 00:34:56 +0000910 setOperationAction(ISD::AND, VT, Promote);
911 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
912 setOperationAction(ISD::OR, VT, Promote);
913 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
914 setOperationAction(ISD::XOR, VT, Promote);
915 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
916 setOperationAction(ISD::LOAD, VT, Promote);
917 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
918 setOperationAction(ISD::SELECT, VT, Promote);
919 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000920 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000921
Owen Anderson825b72b2009-08-11 20:47:22 +0000922 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000923
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
926 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
927 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
928 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000929
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
931 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000932 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000933
Craig Topperd0a31172012-01-10 06:37:29 +0000934 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000935 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
936 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
937 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
938 setOperationAction(ISD::FRINT, MVT::f32, Legal);
939 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
940 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
941 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
942 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
943 setOperationAction(ISD::FRINT, MVT::f64, Legal);
944 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
945
Nate Begeman14d12ca2008-02-11 04:19:36 +0000946 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000948
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000949 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
950 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
951 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
952 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
953 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000954
Nate Begeman14d12ca2008-02-11 04:19:36 +0000955 // i8 and i16 vectors are custom , because the source register and source
956 // source memory operand types are not the same width. f32 vectors are
957 // custom since the immediate controlling the insert encodes additional
958 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
960 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
961 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000963
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
965 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
966 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
967 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000968
Pete Coopera77214a2011-11-14 19:38:42 +0000969 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000970 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000971 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000972 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
973 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000974 }
975 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000976
Craig Topper1accb7e2012-01-10 06:54:16 +0000977 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000978 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000979 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000980
Nadav Rotem43012222011-05-11 08:12:09 +0000981 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000982 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000983
Nadav Rotem43012222011-05-11 08:12:09 +0000984 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000985 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000986
987 if (Subtarget->hasAVX2()) {
988 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
989 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
990
991 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
992 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
993
994 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
995 } else {
996 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
997 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
998
999 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1000 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1001
1002 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1003 }
Nadav Rotem43012222011-05-11 08:12:09 +00001004 }
1005
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001006 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001007 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1008 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1009 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1010 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1011 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1012 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001013
Owen Anderson825b72b2009-08-11 20:47:22 +00001014 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001015 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1016 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001017
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1019 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1020 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1021 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1022 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1023 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001024
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1026 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1027 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1028 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1029 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1030 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001031
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001032 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1033 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001034 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001035
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001036 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1037 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1038
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001039 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1040 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1041
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001042 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001043 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001044
Duncan Sands28b77e92011-09-06 19:07:46 +00001045 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1046 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1047 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1048 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001049
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001050 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1051 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1052 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1053
Craig Topperaaa643c2011-11-09 07:28:55 +00001054 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1055 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1056 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1057 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001058
Craig Topperbf404372012-08-31 15:40:30 +00001059 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001060 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1061 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1062 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1063 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1064 setOperationAction(ISD::FMA, MVT::f32, Custom);
1065 setOperationAction(ISD::FMA, MVT::f64, Custom);
1066 }
Craig Topper880ef452012-08-11 22:34:26 +00001067
Craig Topperaaa643c2011-11-09 07:28:55 +00001068 if (Subtarget->hasAVX2()) {
1069 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1070 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1071 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1072 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001073
Craig Topperaaa643c2011-11-09 07:28:55 +00001074 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1075 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1076 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1077 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001078
Craig Topperaaa643c2011-11-09 07:28:55 +00001079 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1080 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1081 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001082 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001083
1084 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001085
1086 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1087 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1088
1089 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1090 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1091
1092 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001093 } else {
1094 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1095 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1096 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1097 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1098
1099 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1100 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1101 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1102 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1103
1104 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1105 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1106 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1107 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001108
1109 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1110 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1111
1112 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1113 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1114
1115 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001116 }
Craig Topper13894fa2011-08-24 06:14:18 +00001117
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001118 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001119 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1120 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001121 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001122
1123 // Extract subvector is special because the value type
1124 // (result) is 128-bit but the source is 256-bit wide.
1125 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001126 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 // Do not attempt to custom lower other non-256-bit vectors
1129 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001130 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001131
Craig Topper0d1f1762012-08-12 00:34:56 +00001132 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1133 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1134 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1135 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1136 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1137 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1138 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001139 }
1140
David Greene54d8eba2011-01-27 22:38:56 +00001141 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001142 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001143 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001144
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001145 // Do not attempt to promote non-256-bit vectors
1146 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001147 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001148
Craig Topper0d1f1762012-08-12 00:34:56 +00001149 setOperationAction(ISD::AND, VT, Promote);
1150 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1151 setOperationAction(ISD::OR, VT, Promote);
1152 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1153 setOperationAction(ISD::XOR, VT, Promote);
1154 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1155 setOperationAction(ISD::LOAD, VT, Promote);
1156 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1157 setOperationAction(ISD::SELECT, VT, Promote);
1158 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001159 }
David Greene9b9838d2009-06-29 16:47:10 +00001160 }
1161
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001162 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1163 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001164 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1165 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001166 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1167 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001168 }
1169
Evan Cheng6be2c582006-04-05 23:38:46 +00001170 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001172 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001173
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001174
Eli Friedman962f5492010-06-02 19:35:46 +00001175 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1176 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001177 //
Eli Friedman962f5492010-06-02 19:35:46 +00001178 // FIXME: We really should do custom legalization for addition and
1179 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1180 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001181 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1182 // Add/Sub/Mul with overflow operations are custom lowered.
1183 MVT VT = IntVTs[i];
1184 setOperationAction(ISD::SADDO, VT, Custom);
1185 setOperationAction(ISD::UADDO, VT, Custom);
1186 setOperationAction(ISD::SSUBO, VT, Custom);
1187 setOperationAction(ISD::USUBO, VT, Custom);
1188 setOperationAction(ISD::SMULO, VT, Custom);
1189 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001190 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001191
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001192 // There are no 8-bit 3-address imul/mul instructions
1193 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1194 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001195
Evan Chengd54f2d52009-03-31 19:38:51 +00001196 if (!Subtarget->is64Bit()) {
1197 // These libcalls are not available in 32-bit.
1198 setLibcallName(RTLIB::SHL_I128, 0);
1199 setLibcallName(RTLIB::SRL_I128, 0);
1200 setLibcallName(RTLIB::SRA_I128, 0);
1201 }
1202
Evan Cheng206ee9d2006-07-07 08:33:52 +00001203 // We have target-specific dag combine patterns for the following nodes:
1204 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001205 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001206 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001207 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001208 setTargetDAGCombine(ISD::SHL);
1209 setTargetDAGCombine(ISD::SRA);
1210 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001211 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001212 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001213 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001214 setTargetDAGCombine(ISD::FADD);
1215 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001216 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001217 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001218 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001219 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001220 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001221 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001222 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001223 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001224 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001225 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001226 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001227 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001228 if (Subtarget->is64Bit())
1229 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001230 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001231
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001232 computeRegisterProperties();
1233
Evan Cheng05219282011-01-06 06:52:41 +00001234 // On Darwin, -Os means optimize for size without hurting performance,
1235 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001236 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001237 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001238 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001239 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1240 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1241 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001242 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001243 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001244
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001245 // Predictable cmov don't hurt on atom because it's in-order.
1246 predictableSelectIsExpensive = !Subtarget->isAtom();
1247
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001248 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001249}
1250
Scott Michel5b8f82e2008-03-10 15:42:14 +00001251
Duncan Sands28b77e92011-09-06 19:07:46 +00001252EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1253 if (!VT.isVector()) return MVT::i8;
1254 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001255}
1256
1257
Evan Cheng29286502008-01-23 23:17:41 +00001258/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1259/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001260static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001261 if (MaxAlign == 16)
1262 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001263 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001264 if (VTy->getBitWidth() == 128)
1265 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001266 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001267 unsigned EltAlign = 0;
1268 getMaxByValAlign(ATy->getElementType(), EltAlign);
1269 if (EltAlign > MaxAlign)
1270 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001271 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001272 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1273 unsigned EltAlign = 0;
1274 getMaxByValAlign(STy->getElementType(i), EltAlign);
1275 if (EltAlign > MaxAlign)
1276 MaxAlign = EltAlign;
1277 if (MaxAlign == 16)
1278 break;
1279 }
1280 }
Evan Cheng29286502008-01-23 23:17:41 +00001281}
1282
1283/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1284/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001285/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1286/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001287unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001288 if (Subtarget->is64Bit()) {
1289 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001290 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001291 if (TyAlign > 8)
1292 return TyAlign;
1293 return 8;
1294 }
1295
Evan Cheng29286502008-01-23 23:17:41 +00001296 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001297 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001298 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001299 return Align;
1300}
Chris Lattner2b02a442007-02-25 08:29:00 +00001301
Evan Chengf0df0312008-05-15 08:39:06 +00001302/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001303/// and store operations as a result of memset, memcpy, and memmove
1304/// lowering. If DstAlign is zero that means it's safe to destination
1305/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1306/// means there isn't a need to check it against alignment requirement,
1307/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001308/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001309/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1310/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1311/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001312/// It returns EVT::Other if the type should be determined using generic
1313/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001314EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001315X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1316 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001317 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001318 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001319 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001320 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1321 // linux. This is because the stack realignment code can't handle certain
1322 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001323 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001324 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001325 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001326 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001327 (Subtarget->isUnalignedMemAccessFast() ||
1328 ((DstAlign == 0 || DstAlign >= 16) &&
1329 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001330 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001331 if (Subtarget->getStackAlignment() >= 32) {
1332 if (Subtarget->hasAVX2())
1333 return MVT::v8i32;
1334 if (Subtarget->hasAVX())
1335 return MVT::v8f32;
1336 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001337 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001338 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001339 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001340 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001341 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001342 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001343 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001344 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001345 // Do not use f64 to lower memcpy if source is string constant. It's
1346 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001347 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001348 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001349 }
Evan Chengf0df0312008-05-15 08:39:06 +00001350 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001351 return MVT::i64;
1352 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001353}
1354
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001355/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1356/// current function. The returned value is a member of the
1357/// MachineJumpTableInfo::JTEntryKind enum.
1358unsigned X86TargetLowering::getJumpTableEncoding() const {
1359 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1360 // symbol.
1361 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1362 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001363 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001364
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001365 // Otherwise, use the normal jump table encoding heuristics.
1366 return TargetLowering::getJumpTableEncoding();
1367}
1368
Chris Lattnerc64daab2010-01-26 05:02:42 +00001369const MCExpr *
1370X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1371 const MachineBasicBlock *MBB,
1372 unsigned uid,MCContext &Ctx) const{
1373 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1374 Subtarget->isPICStyleGOT());
1375 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1376 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001377 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1378 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001379}
1380
Evan Chengcc415862007-11-09 01:32:10 +00001381/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1382/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001383SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001384 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001385 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001386 // This doesn't have DebugLoc associated with it, but is not really the
1387 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001388 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001389 return Table;
1390}
1391
Chris Lattner589c6f62010-01-26 06:28:43 +00001392/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1393/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1394/// MCExpr.
1395const MCExpr *X86TargetLowering::
1396getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1397 MCContext &Ctx) const {
1398 // X86-64 uses RIP relative addressing based on the jump table label.
1399 if (Subtarget->isPICStyleRIPRel())
1400 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1401
1402 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001403 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001404}
1405
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001406// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001407std::pair<const TargetRegisterClass*, uint8_t>
1408X86TargetLowering::findRepresentativeClass(EVT VT) const{
1409 const TargetRegisterClass *RRC = 0;
1410 uint8_t Cost = 1;
1411 switch (VT.getSimpleVT().SimpleTy) {
1412 default:
1413 return TargetLowering::findRepresentativeClass(VT);
1414 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001415 RRC = Subtarget->is64Bit() ?
1416 (const TargetRegisterClass*)&X86::GR64RegClass :
1417 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001418 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001419 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001420 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001421 break;
1422 case MVT::f32: case MVT::f64:
1423 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1424 case MVT::v4f32: case MVT::v2f64:
1425 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1426 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001427 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001428 break;
1429 }
1430 return std::make_pair(RRC, Cost);
1431}
1432
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001433bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1434 unsigned &Offset) const {
1435 if (!Subtarget->isTargetLinux())
1436 return false;
1437
1438 if (Subtarget->is64Bit()) {
1439 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1440 Offset = 0x28;
1441 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1442 AddressSpace = 256;
1443 else
1444 AddressSpace = 257;
1445 } else {
1446 // %gs:0x14 on i386
1447 Offset = 0x14;
1448 AddressSpace = 256;
1449 }
1450 return true;
1451}
1452
1453
Chris Lattner2b02a442007-02-25 08:29:00 +00001454//===----------------------------------------------------------------------===//
1455// Return Value Calling Convention Implementation
1456//===----------------------------------------------------------------------===//
1457
Chris Lattner59ed56b2007-02-28 04:55:35 +00001458#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001459
Michael J. Spencerec38de22010-10-10 22:04:20 +00001460bool
Eric Christopher471e4222011-06-08 23:55:35 +00001461X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001462 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001463 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001464 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001465 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001466 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001467 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001468 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001469}
1470
Dan Gohman98ca4f22009-08-05 01:29:28 +00001471SDValue
1472X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001473 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001475 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001476 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001477 MachineFunction &MF = DAG.getMachineFunction();
1478 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Chris Lattner9774c912007-02-27 05:28:59 +00001480 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001481 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 RVLocs, *DAG.getContext());
1483 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Evan Chengdcea1632010-02-04 02:40:39 +00001485 // Add the regs to the liveout set for the function.
1486 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1487 for (unsigned i = 0; i != RVLocs.size(); ++i)
1488 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1489 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001492
Dan Gohman475871a2008-07-27 21:46:04 +00001493 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001494 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1495 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001496 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1497 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001498
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001499 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001500 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1501 CCValAssign &VA = RVLocs[i];
1502 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001503 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001504 EVT ValVT = ValToCopy.getValueType();
1505
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001506 // Promote values to the appropriate types
1507 if (VA.getLocInfo() == CCValAssign::SExt)
1508 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1509 else if (VA.getLocInfo() == CCValAssign::ZExt)
1510 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1511 else if (VA.getLocInfo() == CCValAssign::AExt)
1512 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1513 else if (VA.getLocInfo() == CCValAssign::BCvt)
1514 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1515
Dale Johannesenc4510512010-09-24 19:05:48 +00001516 // If this is x86-64, and we disabled SSE, we can't return FP values,
1517 // or SSE or MMX vectors.
1518 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1519 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001520 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001521 report_fatal_error("SSE register return with SSE disabled");
1522 }
1523 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1524 // llvm-gcc has never done it right and no one has noticed, so this
1525 // should be OK for now.
1526 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001527 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001528 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Chris Lattner447ff682008-03-11 03:23:40 +00001530 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1531 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001532 if (VA.getLocReg() == X86::ST0 ||
1533 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001534 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1535 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001536 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001537 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001538 RetOps.push_back(ValToCopy);
1539 // Don't emit a copytoreg.
1540 continue;
1541 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001542
Evan Cheng242b38b2009-02-23 09:03:22 +00001543 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1544 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001545 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001546 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001547 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001548 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001549 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1550 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001551 // If we don't have SSE2 available, convert to v4f32 so the generated
1552 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001553 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001554 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001555 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001556 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001557 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001558
Dale Johannesendd64c412009-02-04 00:33:20 +00001559 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001560 Flag = Chain.getValue(1);
1561 }
Dan Gohman61a92132008-04-21 23:59:07 +00001562
1563 // The x86-64 ABI for returning structs by value requires that we copy
1564 // the sret argument into %rax for the return. We saved the argument into
1565 // a virtual register in the entry block, so now we copy the value out
1566 // and into %rax.
1567 if (Subtarget->is64Bit() &&
1568 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1569 MachineFunction &MF = DAG.getMachineFunction();
1570 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1571 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001572 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001573 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001574 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001575
Dale Johannesendd64c412009-02-04 00:33:20 +00001576 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001577 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001578
1579 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001580 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001582
Chris Lattner447ff682008-03-11 03:23:40 +00001583 RetOps[0] = Chain; // Update chain.
1584
1585 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001586 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001587 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001588
1589 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001590 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001591}
1592
Evan Chengbf010eb2012-04-10 01:51:00 +00001593bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001594 if (N->getNumValues() != 1)
1595 return false;
1596 if (!N->hasNUsesOfValue(1, 0))
1597 return false;
1598
Evan Chengbf010eb2012-04-10 01:51:00 +00001599 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001600 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001601 if (Copy->getOpcode() == ISD::CopyToReg) {
1602 // If the copy has a glue operand, we conservatively assume it isn't safe to
1603 // perform a tail call.
1604 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1605 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001606 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001607 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001608 return false;
1609
Evan Cheng1bf891a2010-12-01 22:59:46 +00001610 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001611 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001612 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001613 if (UI->getOpcode() != X86ISD::RET_FLAG)
1614 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001615 HasRet = true;
1616 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001617
Evan Chengbf010eb2012-04-10 01:51:00 +00001618 if (!HasRet)
1619 return false;
1620
1621 Chain = TCChain;
1622 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001623}
1624
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001625EVT
1626X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001627 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001628 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001629 // TODO: Is this also valid on 32-bit?
1630 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001631 ReturnMVT = MVT::i8;
1632 else
1633 ReturnMVT = MVT::i32;
1634
1635 EVT MinVT = getRegisterType(Context, ReturnMVT);
1636 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001637}
1638
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639/// LowerCallResult - Lower the result values of a call into the
1640/// appropriate copies out of appropriate physical registers.
1641///
1642SDValue
1643X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001644 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001645 const SmallVectorImpl<ISD::InputArg> &Ins,
1646 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001647 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001648
Chris Lattnere32bbf62007-02-28 07:09:55 +00001649 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001650 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001651 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001652 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001653 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
Chris Lattner3085e152007-02-25 08:59:22 +00001656 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001657 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001658 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001659 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Torok Edwin3f142c32009-02-01 18:15:56 +00001661 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001663 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001664 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001665 }
1666
Evan Cheng79fb3b42009-02-20 20:43:02 +00001667 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001668
1669 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001670 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001671 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001672 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001673 // instead.
1674 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1675 // If we prefer to use the value in xmm registers, copy it out as f80 and
1676 // use a truncate to move it from fp stack reg to xmm reg.
1677 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001678 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001679 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1680 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001681 Val = Chain.getValue(0);
1682
1683 // Round the f80 to the right size, which also moves it to the appropriate
1684 // xmm register.
1685 if (CopyVT != VA.getValVT())
1686 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1687 // This truncation won't change the value.
1688 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001689 } else {
1690 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1691 CopyVT, InFlag).getValue(1);
1692 Val = Chain.getValue(0);
1693 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001694 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001695 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001696 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001697
Dan Gohman98ca4f22009-08-05 01:29:28 +00001698 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001699}
1700
1701
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001702//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001703// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001704//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001705// StdCall calling convention seems to be standard for many Windows' API
1706// routines and around. It differs from C calling convention just a little:
1707// callee should clean up the stack, not caller. Symbols should be also
1708// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001709// For info on fast calling convention see Fast Calling Convention (tail call)
1710// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001711
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001713/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001714enum StructReturnType {
1715 NotStructReturn,
1716 RegStructReturn,
1717 StackStructReturn
1718};
1719static StructReturnType
1720callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001722 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001723
Rafael Espindola1cee7102012-07-25 13:41:10 +00001724 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1725 if (!Flags.isSRet())
1726 return NotStructReturn;
1727 if (Flags.isInReg())
1728 return RegStructReturn;
1729 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001730}
1731
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001732/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001733/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001734static StructReturnType
1735argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001737 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001738
Rafael Espindola1cee7102012-07-25 13:41:10 +00001739 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1740 if (!Flags.isSRet())
1741 return NotStructReturn;
1742 if (Flags.isInReg())
1743 return RegStructReturn;
1744 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001745}
1746
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001747/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1748/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001749/// the specific parameter attribute. The copy will be passed as a byval
1750/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001751static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001752CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001753 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1754 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001755 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001756
Dale Johannesendd64c412009-02-04 00:33:20 +00001757 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001758 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001759 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001760}
1761
Chris Lattner29689432010-03-11 00:22:57 +00001762/// IsTailCallConvention - Return true if the calling convention is one that
1763/// supports tail call optimization.
1764static bool IsTailCallConvention(CallingConv::ID CC) {
1765 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1766}
1767
Evan Cheng485fafc2011-03-21 01:19:09 +00001768bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001769 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001770 return false;
1771
1772 CallSite CS(CI);
1773 CallingConv::ID CalleeCC = CS.getCallingConv();
1774 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1775 return false;
1776
1777 return true;
1778}
1779
Evan Cheng0c439eb2010-01-27 00:07:07 +00001780/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1781/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001782static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1783 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001784 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001785}
1786
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787SDValue
1788X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001789 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 const SmallVectorImpl<ISD::InputArg> &Ins,
1791 DebugLoc dl, SelectionDAG &DAG,
1792 const CCValAssign &VA,
1793 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001794 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001795 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001797 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1798 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001799 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001800 EVT ValVT;
1801
1802 // If value is passed by pointer we have address passed instead of the value
1803 // itself.
1804 if (VA.getLocInfo() == CCValAssign::Indirect)
1805 ValVT = VA.getLocVT();
1806 else
1807 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001808
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001809 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001810 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001811 // In case of tail call optimization mark all arguments mutable. Since they
1812 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001813 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001814 unsigned Bytes = Flags.getByValSize();
1815 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1816 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001817 return DAG.getFrameIndex(FI, getPointerTy());
1818 } else {
1819 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001820 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001821 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1822 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001823 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001824 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001825 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001826}
1827
Dan Gohman475871a2008-07-27 21:46:04 +00001828SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001829X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001830 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001831 bool isVarArg,
1832 const SmallVectorImpl<ISD::InputArg> &Ins,
1833 DebugLoc dl,
1834 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001835 SmallVectorImpl<SDValue> &InVals)
1836 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001837 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001839
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 const Function* Fn = MF.getFunction();
1841 if (Fn->hasExternalLinkage() &&
1842 Subtarget->isTargetCygMing() &&
1843 Fn->getName() == "main")
1844 FuncInfo->setForceFramePointer(true);
1845
Evan Cheng1bc78042006-04-26 01:20:17 +00001846 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001848 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001849 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001850
Chris Lattner29689432010-03-11 00:22:57 +00001851 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1852 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001853
Chris Lattner638402b2007-02-28 07:00:42 +00001854 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001855 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001856 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001858
1859 // Allocate shadow area for Win64
1860 if (IsWin64) {
1861 CCInfo.AllocateStack(32, 8);
1862 }
1863
Duncan Sands45907662010-10-31 13:21:44 +00001864 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001865
Chris Lattnerf39f7712007-02-28 05:46:49 +00001866 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001867 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001868 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1869 CCValAssign &VA = ArgLocs[i];
1870 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1871 // places.
1872 assert(VA.getValNo() != LastVal &&
1873 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001874 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001875 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001876
Chris Lattnerf39f7712007-02-28 05:46:49 +00001877 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001878 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001879 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001881 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001883 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001885 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001886 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001887 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001888 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001889 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001890 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001891 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001892 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001893 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001894 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001895 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001896
Devang Patel68e6bee2011-02-21 23:21:26 +00001897 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Chris Lattnerf39f7712007-02-28 05:46:49 +00001900 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1901 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1902 // right size.
1903 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001904 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001905 DAG.getValueType(VA.getValVT()));
1906 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001907 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001908 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001909 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001910 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001912 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001913 // Handle MMX values passed in XMM regs.
1914 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001915 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1916 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001917 } else
1918 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001919 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001920 } else {
1921 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001922 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001923 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001924
1925 // If value is passed via pointer - do a load.
1926 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001927 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001928 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001929
Dan Gohman98ca4f22009-08-05 01:29:28 +00001930 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001931 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001932
Dan Gohman61a92132008-04-21 23:59:07 +00001933 // The x86-64 ABI for returning structs by value requires that we copy
1934 // the sret argument into %rax for the return. Save the argument into
1935 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001936 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001937 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1938 unsigned Reg = FuncInfo->getSRetReturnReg();
1939 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001941 FuncInfo->setSRetReturnReg(Reg);
1942 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001945 }
1946
Chris Lattnerf39f7712007-02-28 05:46:49 +00001947 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001948 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001949 if (FuncIsMadeTailCallSafe(CallConv,
1950 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001951 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001952
Evan Cheng1bc78042006-04-26 01:20:17 +00001953 // If the function takes variable number of arguments, make a frame index for
1954 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001955 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001956 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1957 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001958 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001959 }
1960 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001961 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1962
1963 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001964 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001965 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001967 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001968 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1969 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001970 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1972 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1973 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001974 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001975 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001976
1977 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001978 // The XMM registers which might contain var arg parameters are shadowed
1979 // in their paired GPR. So we only need to save the GPR to their home
1980 // slots.
1981 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001982 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001983 } else {
1984 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1985 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001986
Chad Rosier30450e82011-12-22 22:35:21 +00001987 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1988 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001989 }
1990 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1991 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992
Devang Patel578efa92009-06-05 21:57:13 +00001993 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001994 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001995 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001996 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1997 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001998 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001999 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002000 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002001 // Kernel mode asks for SSE to be disabled, so don't push them
2002 // on the stack.
2003 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002004
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002005 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002006 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002007 // Get to the caller-allocated home save location. Add 8 to account
2008 // for the return address.
2009 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002010 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002011 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002012 // Fixup to set vararg frame on shadow area (4 x i64).
2013 if (NumIntRegs < 4)
2014 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002015 } else {
2016 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002017 // registers, then we must store them to their spots on the stack so
2018 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002019 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2020 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2021 FuncInfo->setRegSaveFrameIndex(
2022 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002023 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002024 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002025
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002028 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2029 getPointerTy());
2030 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002031 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002032 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2033 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002034 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002035 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002038 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002039 MachinePointerInfo::getFixedStack(
2040 FuncInfo->getRegSaveFrameIndex(), Offset),
2041 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002042 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002043 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002044 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002045
Dan Gohmanface41a2009-08-16 21:24:25 +00002046 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2047 // Now store the XMM (fp + vector) parameter registers.
2048 SmallVector<SDValue, 11> SaveXMMOps;
2049 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002050
Craig Topperc9099502012-04-20 06:31:50 +00002051 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002052 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2053 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002054
Dan Gohman1e93df62010-04-17 14:41:14 +00002055 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2056 FuncInfo->getRegSaveFrameIndex()));
2057 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2058 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002059
Dan Gohmanface41a2009-08-16 21:24:25 +00002060 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002061 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002062 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002063 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2064 SaveXMMOps.push_back(Val);
2065 }
2066 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2067 MVT::Other,
2068 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002069 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002070
2071 if (!MemOps.empty())
2072 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2073 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002074 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002078 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2079 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002080 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002081 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002082 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002083 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002084 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002085 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002086 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002087 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002088
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002090 // RegSaveFrameIndex is X86-64 only.
2091 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002092 if (CallConv == CallingConv::X86_FastCall ||
2093 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002094 // fastcc functions can't have varargs.
2095 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002096 }
Evan Cheng25caf632006-05-23 21:06:34 +00002097
Rafael Espindola76927d752011-08-30 19:39:58 +00002098 FuncInfo->setArgumentStackSize(StackSize);
2099
Dan Gohman98ca4f22009-08-05 01:29:28 +00002100 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002101}
2102
Dan Gohman475871a2008-07-27 21:46:04 +00002103SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002104X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2105 SDValue StackPtr, SDValue Arg,
2106 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002107 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002108 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002109 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002111 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002112 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002113 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002114
2115 return DAG.getStore(Chain, dl, Arg, PtrOff,
2116 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002117 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002118}
2119
Bill Wendling64e87322009-01-16 19:25:27 +00002120/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002121/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002122SDValue
2123X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002124 SDValue &OutRetAddr, SDValue Chain,
2125 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002126 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002127 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002129 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002130
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002131 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002132 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002133 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002134 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002135}
2136
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002137/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002138/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002139static SDValue
2140EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002141 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002142 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 // Store the return address to the appropriate stack slot.
2144 if (!FPDiff) return Chain;
2145 // Calculate the new stack slot for the return address.
2146 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002147 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002148 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002150 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002151 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002152 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002153 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002154 return Chain;
2155}
2156
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002158X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002159 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002160 SelectionDAG &DAG = CLI.DAG;
2161 DebugLoc &dl = CLI.DL;
2162 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2163 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2164 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2165 SDValue Chain = CLI.Chain;
2166 SDValue Callee = CLI.Callee;
2167 CallingConv::ID CallConv = CLI.CallConv;
2168 bool &isTailCall = CLI.IsTailCall;
2169 bool isVarArg = CLI.IsVarArg;
2170
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 MachineFunction &MF = DAG.getMachineFunction();
2172 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002173 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002174 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002175 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002176 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177
Nick Lewycky22de16d2012-01-19 00:34:10 +00002178 if (MF.getTarget().Options.DisableTailCalls)
2179 isTailCall = false;
2180
Evan Cheng5f941932010-02-05 02:21:12 +00002181 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002182 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002183 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002184 isVarArg, SR != NotStructReturn,
2185 MF.getFunction()->hasStructRetAttr(),
2186 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002187
2188 // Sibcalls are automatically detected tailcalls which do not require
2189 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002190 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002191 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002192
2193 if (isTailCall)
2194 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002195 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002196
Chris Lattner29689432010-03-11 00:22:57 +00002197 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2198 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002199
Chris Lattner638402b2007-02-28 07:00:42 +00002200 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002201 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002202 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002204
2205 // Allocate shadow area for Win64
2206 if (IsWin64) {
2207 CCInfo.AllocateStack(32, 8);
2208 }
2209
Duncan Sands45907662010-10-31 13:21:44 +00002210 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002211
Chris Lattner423c5f42007-02-28 05:31:48 +00002212 // Get a count of how many bytes are to be pushed on the stack.
2213 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002214 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002215 // This is a sibcall. The memory operands are available in caller's
2216 // own caller's stack.
2217 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002218 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2219 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002220 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002221
Gordon Henriksen86737662008-01-05 16:56:59 +00002222 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002223 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002224 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002225 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002226 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2227 FPDiff = NumBytesCallerPushed - NumBytes;
2228
2229 // Set the delta of movement of the returnaddr stackslot.
2230 // But only set if delta is greater than previous delta.
2231 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2232 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2233 }
2234
Evan Chengf22f9b32010-02-06 03:28:46 +00002235 if (!IsSibcall)
2236 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002237
Dan Gohman475871a2008-07-27 21:46:04 +00002238 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002239 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002240 if (isTailCall && FPDiff)
2241 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2242 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002243
Dan Gohman475871a2008-07-27 21:46:04 +00002244 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2245 SmallVector<SDValue, 8> MemOpChains;
2246 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002247
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002248 // Walk the register/memloc assignments, inserting copies/loads. In the case
2249 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002250 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2251 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002252 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002253 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002254 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002255 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002256
Chris Lattner423c5f42007-02-28 05:31:48 +00002257 // Promote the value if needed.
2258 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002259 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002260 case CCValAssign::Full: break;
2261 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002262 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002263 break;
2264 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002265 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002266 break;
2267 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002268 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002269 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002270 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2272 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002273 } else
2274 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2275 break;
2276 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002277 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002278 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002279 case CCValAssign::Indirect: {
2280 // Store the argument.
2281 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002282 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002283 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002284 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002285 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002286 Arg = SpillSlot;
2287 break;
2288 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Chris Lattner423c5f42007-02-28 05:31:48 +00002291 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002292 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2293 if (isVarArg && IsWin64) {
2294 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2295 // shadow reg if callee is a varargs function.
2296 unsigned ShadowReg = 0;
2297 switch (VA.getLocReg()) {
2298 case X86::XMM0: ShadowReg = X86::RCX; break;
2299 case X86::XMM1: ShadowReg = X86::RDX; break;
2300 case X86::XMM2: ShadowReg = X86::R8; break;
2301 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002302 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002303 if (ShadowReg)
2304 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002305 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002306 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002307 assert(VA.isMemLoc());
2308 if (StackPtr.getNode() == 0)
2309 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2310 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2311 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002312 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Evan Cheng32fe1032006-05-25 00:59:30 +00002315 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002317 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002318
Chris Lattner88e1fd52009-07-09 04:24:46 +00002319 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002320 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2321 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002323 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2324 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002325 } else {
2326 // If we are tail calling and generating PIC/GOT style code load the
2327 // address of the callee into ECX. The value in ecx is used as target of
2328 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2329 // for tail calls on PIC/GOT architectures. Normally we would just put the
2330 // address of GOT into ebx and then call target@PLT. But for tail calls
2331 // ebx would be restored (since ebx is callee saved) before jumping to the
2332 // target@PLT.
2333
2334 // Note: The actual moving to ECX is done further down.
2335 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2336 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2337 !G->getGlobal()->hasProtectedVisibility())
2338 Callee = LowerGlobalAddress(Callee, DAG);
2339 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002340 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002341 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002342 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002343
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002344 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 // From AMD64 ABI document:
2346 // For calls that may call functions that use varargs or stdargs
2347 // (prototype-less calls or calls to functions containing ellipsis (...) in
2348 // the declaration) %al is used as hidden argument to specify the number
2349 // of SSE registers used. The contents of %al do not need to match exactly
2350 // the number of registers, but must be an ubound on the number of SSE
2351 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002352
Gordon Henriksen86737662008-01-05 16:56:59 +00002353 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002354 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2356 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2357 };
2358 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002359 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002360 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002362 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2363 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002364 }
2365
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002366 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002367 if (isTailCall) {
2368 // Force all the incoming stack arguments to be loaded from the stack
2369 // before any new outgoing arguments are stored to the stack, because the
2370 // outgoing stack slots may alias the incoming argument stack slots, and
2371 // the alias isn't otherwise explicit. This is slightly more conservative
2372 // than necessary, because it means that each store effectively depends
2373 // on every argument instead of just those arguments it would clobber.
2374 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2375
Dan Gohman475871a2008-07-27 21:46:04 +00002376 SmallVector<SDValue, 8> MemOpChains2;
2377 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002378 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002379 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002380 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2381 CCValAssign &VA = ArgLocs[i];
2382 if (VA.isRegLoc())
2383 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002384 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002385 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002386 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002387 // Create frame index.
2388 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002389 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002390 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002391 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002392
Duncan Sands276dcbd2008-03-21 09:14:45 +00002393 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002394 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002395 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002396 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002397 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002398 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002399 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002400
Dan Gohman98ca4f22009-08-05 01:29:28 +00002401 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2402 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002403 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002404 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002405 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002406 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002408 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002409 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002410 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002411 }
2412 }
2413
2414 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002415 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002416 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002417
2418 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002419 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002420 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002421 }
2422
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002423 // Build a sequence of copy-to-reg nodes chained together with token chain
2424 // and flag operands which copy the outgoing args into registers.
2425 SDValue InFlag;
2426 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2427 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2428 RegsToPass[i].second, InFlag);
2429 InFlag = Chain.getValue(1);
2430 }
2431
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002432 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2433 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2434 // In the 64-bit large code model, we have to make all calls
2435 // through a register, since the call instruction's 32-bit
2436 // pc-relative offset may not be large enough to hold the whole
2437 // address.
2438 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002439 // If the callee is a GlobalAddress node (quite common, every direct call
2440 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2441 // it.
2442
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002443 // We should use extra load for direct calls to dllimported functions in
2444 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002445 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002446 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002447 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002448 bool ExtraLoad = false;
2449 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002450
Chris Lattner48a7d022009-07-09 05:02:21 +00002451 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2452 // external symbols most go through the PLT in PIC mode. If the symbol
2453 // has hidden or protected visibility, or if it is static or local, then
2454 // we don't need to use the PLT - we can directly call it.
2455 if (Subtarget->isTargetELF() &&
2456 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002457 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002458 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002459 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002460 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002461 (!Subtarget->getTargetTriple().isMacOSX() ||
2462 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002463 // PC-relative references to external symbols should go through $stub,
2464 // unless we're building with the leopard linker or later, which
2465 // automatically synthesizes these stubs.
2466 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002467 } else if (Subtarget->isPICStyleRIPRel() &&
2468 isa<Function>(GV) &&
2469 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2470 // If the function is marked as non-lazy, generate an indirect call
2471 // which loads from the GOT directly. This avoids runtime overhead
2472 // at the cost of eager binding (and one extra byte of encoding).
2473 OpFlags = X86II::MO_GOTPCREL;
2474 WrapperKind = X86ISD::WrapperRIP;
2475 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002476 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002477
Devang Patel0d881da2010-07-06 22:08:15 +00002478 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002479 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002480
2481 // Add a wrapper if needed.
2482 if (WrapperKind != ISD::DELETED_NODE)
2483 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2484 // Add extra indirection if needed.
2485 if (ExtraLoad)
2486 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2487 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002488 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002489 }
Bill Wendling056292f2008-09-16 21:48:12 +00002490 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002491 unsigned char OpFlags = 0;
2492
Evan Cheng1bf891a2010-12-01 22:59:46 +00002493 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2494 // external symbols should go through the PLT.
2495 if (Subtarget->isTargetELF() &&
2496 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2497 OpFlags = X86II::MO_PLT;
2498 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002499 (!Subtarget->getTargetTriple().isMacOSX() ||
2500 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002501 // PC-relative references to external symbols should go through $stub,
2502 // unless we're building with the leopard linker or later, which
2503 // automatically synthesizes these stubs.
2504 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002505 }
Eric Christopherfd179292009-08-27 18:07:15 +00002506
Chris Lattner48a7d022009-07-09 05:02:21 +00002507 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2508 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002509 }
2510
Chris Lattnerd96d0722007-02-25 06:40:16 +00002511 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002512 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002513 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002514
Evan Chengf22f9b32010-02-06 03:28:46 +00002515 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002516 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2517 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002518 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002519 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002520
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002521 Ops.push_back(Chain);
2522 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002523
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002526
Gordon Henriksen86737662008-01-05 16:56:59 +00002527 // Add argument registers to the end of the list so that they are known live
2528 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002529 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2530 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2531 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002532
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002533 // Add a register mask operand representing the call-preserved registers.
2534 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2535 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2536 assert(Mask && "Missing call preserved mask for calling convention");
2537 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002538
Gabor Greifba36cb52008-08-28 21:40:38 +00002539 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002540 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002541
Dan Gohman98ca4f22009-08-05 01:29:28 +00002542 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002543 // We used to do:
2544 //// If this is the first return lowered for this function, add the regs
2545 //// to the liveout set for the function.
2546 // This isn't right, although it's probably harmless on x86; liveouts
2547 // should be computed from returns not tail calls. Consider a void
2548 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002549 return DAG.getNode(X86ISD::TC_RETURN, dl,
2550 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002551 }
2552
Dale Johannesenace16102009-02-03 19:33:06 +00002553 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002554 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002555
Chris Lattner2d297092006-05-23 18:50:38 +00002556 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002557 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002558 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2559 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002560 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002561 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002562 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002563 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002564 // pops the hidden struct pointer, so we have to push it back.
2565 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002566 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002567 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002568 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002569 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002570
Gordon Henriksenae636f82008-01-03 16:47:34 +00002571 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002572 if (!IsSibcall) {
2573 Chain = DAG.getCALLSEQ_END(Chain,
2574 DAG.getIntPtrConstant(NumBytes, true),
2575 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2576 true),
2577 InFlag);
2578 InFlag = Chain.getValue(1);
2579 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002580
Chris Lattner3085e152007-02-25 08:59:22 +00002581 // Handle result values, copying them out of physregs into vregs that we
2582 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002583 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2584 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002585}
2586
Evan Cheng25ab6902006-09-08 06:48:29 +00002587
2588//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002589// Fast Calling Convention (tail call) implementation
2590//===----------------------------------------------------------------------===//
2591
2592// Like std call, callee cleans arguments, convention except that ECX is
2593// reserved for storing the tail called function address. Only 2 registers are
2594// free for argument passing (inreg). Tail call optimization is performed
2595// provided:
2596// * tailcallopt is enabled
2597// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002598// On X86_64 architecture with GOT-style position independent code only local
2599// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002600// To keep the stack aligned according to platform abi the function
2601// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2602// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002603// If a tail called function callee has more arguments than the caller the
2604// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002605// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002606// original REtADDR, but before the saved framepointer or the spilled registers
2607// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2608// stack layout:
2609// arg1
2610// arg2
2611// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002612// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002613// move area ]
2614// (possible EBP)
2615// ESI
2616// EDI
2617// local1 ..
2618
2619/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2620/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002621unsigned
2622X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2623 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002624 MachineFunction &MF = DAG.getMachineFunction();
2625 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002626 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002627 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002628 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002629 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002630 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002631 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2632 // Number smaller than 12 so just add the difference.
2633 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2634 } else {
2635 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002636 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002637 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002638 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002639 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002640}
2641
Evan Cheng5f941932010-02-05 02:21:12 +00002642/// MatchingStackOffset - Return true if the given stack call argument is
2643/// already available in the same position (relatively) of the caller's
2644/// incoming argument stack.
2645static
2646bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2647 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2648 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002649 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2650 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002651 if (Arg.getOpcode() == ISD::CopyFromReg) {
2652 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002653 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002654 return false;
2655 MachineInstr *Def = MRI->getVRegDef(VR);
2656 if (!Def)
2657 return false;
2658 if (!Flags.isByVal()) {
2659 if (!TII->isLoadFromStackSlot(Def, FI))
2660 return false;
2661 } else {
2662 unsigned Opcode = Def->getOpcode();
2663 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2664 Def->getOperand(1).isFI()) {
2665 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002666 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002667 } else
2668 return false;
2669 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002670 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2671 if (Flags.isByVal())
2672 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002673 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002674 // define @foo(%struct.X* %A) {
2675 // tail call @bar(%struct.X* byval %A)
2676 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002677 return false;
2678 SDValue Ptr = Ld->getBasePtr();
2679 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2680 if (!FINode)
2681 return false;
2682 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002683 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002684 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002685 FI = FINode->getIndex();
2686 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002687 } else
2688 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002689
Evan Cheng4cae1332010-03-05 08:38:04 +00002690 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002691 if (!MFI->isFixedObjectIndex(FI))
2692 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002693 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002694}
2695
Dan Gohman98ca4f22009-08-05 01:29:28 +00002696/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2697/// for tail call optimization. Targets which want to do tail call
2698/// optimization should implement this function.
2699bool
2700X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002701 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002702 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002703 bool isCalleeStructRet,
2704 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002705 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002706 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002707 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002708 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002709 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002710 CalleeCC != CallingConv::C)
2711 return false;
2712
Evan Cheng7096ae42010-01-29 06:45:59 +00002713 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002714 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002715 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002716 CallingConv::ID CallerCC = CallerF->getCallingConv();
2717 bool CCMatch = CallerCC == CalleeCC;
2718
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002719 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002720 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002721 return true;
2722 return false;
2723 }
2724
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002725 // Look for obvious safe cases to perform tail call optimization that do not
2726 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002727
Evan Cheng2c12cb42010-03-26 16:26:03 +00002728 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2729 // emit a special epilogue.
2730 if (RegInfo->needsStackRealignment(MF))
2731 return false;
2732
Evan Chenga375d472010-03-15 18:54:48 +00002733 // Also avoid sibcall optimization if either caller or callee uses struct
2734 // return semantics.
2735 if (isCalleeStructRet || isCallerStructRet)
2736 return false;
2737
Chad Rosier2416da32011-06-24 21:15:36 +00002738 // An stdcall caller is expected to clean up its arguments; the callee
2739 // isn't going to do that.
2740 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2741 return false;
2742
Chad Rosier871f6642011-05-18 19:59:50 +00002743 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002744 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002745 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002746
2747 // Optimizing for varargs on Win64 is unlikely to be safe without
2748 // additional testing.
2749 if (Subtarget->isTargetWin64())
2750 return false;
2751
Chad Rosier871f6642011-05-18 19:59:50 +00002752 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002753 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002754 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002755
Chad Rosier871f6642011-05-18 19:59:50 +00002756 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2757 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2758 if (!ArgLocs[i].isRegLoc())
2759 return false;
2760 }
2761
Chad Rosier30450e82011-12-22 22:35:21 +00002762 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2763 // stack. Therefore, if it's not used by the call it is not safe to optimize
2764 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002765 bool Unused = false;
2766 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2767 if (!Ins[i].Used) {
2768 Unused = true;
2769 break;
2770 }
2771 }
2772 if (Unused) {
2773 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002774 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002775 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002776 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002777 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002778 CCValAssign &VA = RVLocs[i];
2779 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2780 return false;
2781 }
2782 }
2783
Evan Cheng13617962010-04-30 01:12:32 +00002784 // If the calling conventions do not match, then we'd better make sure the
2785 // results are returned in the same way as what the caller expects.
2786 if (!CCMatch) {
2787 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002788 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002789 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002790 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2791
2792 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002793 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002794 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002795 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2796
2797 if (RVLocs1.size() != RVLocs2.size())
2798 return false;
2799 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2800 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2801 return false;
2802 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2803 return false;
2804 if (RVLocs1[i].isRegLoc()) {
2805 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2806 return false;
2807 } else {
2808 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2809 return false;
2810 }
2811 }
2812 }
2813
Evan Chenga6bff982010-01-30 01:22:00 +00002814 // If the callee takes no arguments then go on to check the results of the
2815 // call.
2816 if (!Outs.empty()) {
2817 // Check if stack adjustment is needed. For now, do not do this if any
2818 // argument is passed on the stack.
2819 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002820 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002821 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002822
2823 // Allocate shadow area for Win64
2824 if (Subtarget->isTargetWin64()) {
2825 CCInfo.AllocateStack(32, 8);
2826 }
2827
Duncan Sands45907662010-10-31 13:21:44 +00002828 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002829 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002830 MachineFunction &MF = DAG.getMachineFunction();
2831 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2832 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002833
2834 // Check if the arguments are already laid out in the right way as
2835 // the caller's fixed stack objects.
2836 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002837 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2838 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002839 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002840 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2841 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002842 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002843 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002844 if (VA.getLocInfo() == CCValAssign::Indirect)
2845 return false;
2846 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002847 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2848 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002849 return false;
2850 }
2851 }
2852 }
Evan Cheng9c044672010-05-29 01:35:22 +00002853
2854 // If the tailcall address may be in a register, then make sure it's
2855 // possible to register allocate for it. In 32-bit, the call address can
2856 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002857 // callee-saved registers are restored. These happen to be the same
2858 // registers used to pass 'inreg' arguments so watch out for those.
2859 if (!Subtarget->is64Bit() &&
2860 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002861 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002862 unsigned NumInRegs = 0;
2863 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2864 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002865 if (!VA.isRegLoc())
2866 continue;
2867 unsigned Reg = VA.getLocReg();
2868 switch (Reg) {
2869 default: break;
2870 case X86::EAX: case X86::EDX: case X86::ECX:
2871 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002872 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002873 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002874 }
2875 }
2876 }
Evan Chenga6bff982010-01-30 01:22:00 +00002877 }
Evan Chengb1712452010-01-27 06:25:16 +00002878
Evan Cheng86809cc2010-02-03 03:28:02 +00002879 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002880}
2881
Dan Gohman3df24e62008-09-03 23:12:08 +00002882FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002883X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2884 const TargetLibraryInfo *libInfo) const {
2885 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002886}
2887
2888
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002889//===----------------------------------------------------------------------===//
2890// Other Lowering Hooks
2891//===----------------------------------------------------------------------===//
2892
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002893static bool MayFoldLoad(SDValue Op) {
2894 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2895}
2896
2897static bool MayFoldIntoStore(SDValue Op) {
2898 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2899}
2900
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002901static bool isTargetShuffle(unsigned Opcode) {
2902 switch(Opcode) {
2903 default: return false;
2904 case X86ISD::PSHUFD:
2905 case X86ISD::PSHUFHW:
2906 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002907 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002908 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002909 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002910 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002911 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002912 case X86ISD::MOVLPS:
2913 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002914 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002915 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002916 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002917 case X86ISD::MOVSS:
2918 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002919 case X86ISD::UNPCKL:
2920 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002921 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002922 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002923 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002924 return true;
2925 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002926}
2927
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002928static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002929 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002930 switch(Opc) {
2931 default: llvm_unreachable("Unknown x86 shuffle node");
2932 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002933 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002934 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002935 return DAG.getNode(Opc, dl, VT, V1);
2936 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002937}
2938
2939static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002940 SDValue V1, unsigned TargetMask,
2941 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002942 switch(Opc) {
2943 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002944 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002945 case X86ISD::PSHUFHW:
2946 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002947 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002948 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002949 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2950 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002951}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002952
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002953static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002954 SDValue V1, SDValue V2, unsigned TargetMask,
2955 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002956 switch(Opc) {
2957 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002958 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002959 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002960 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002961 return DAG.getNode(Opc, dl, VT, V1, V2,
2962 DAG.getConstant(TargetMask, MVT::i8));
2963 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002964}
2965
2966static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2967 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2968 switch(Opc) {
2969 default: llvm_unreachable("Unknown x86 shuffle node");
2970 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002971 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002972 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002973 case X86ISD::MOVLPS:
2974 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002975 case X86ISD::MOVSS:
2976 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002977 case X86ISD::UNPCKL:
2978 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002979 return DAG.getNode(Opc, dl, VT, V1, V2);
2980 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002981}
2982
Dan Gohmand858e902010-04-17 15:26:15 +00002983SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002984 MachineFunction &MF = DAG.getMachineFunction();
2985 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2986 int ReturnAddrIndex = FuncInfo->getRAIndex();
2987
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002988 if (ReturnAddrIndex == 0) {
2989 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002990 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002991 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002992 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002993 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002994 }
2995
Evan Cheng25ab6902006-09-08 06:48:29 +00002996 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002997}
2998
2999
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003000bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3001 bool hasSymbolicDisplacement) {
3002 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003003 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003004 return false;
3005
3006 // If we don't have a symbolic displacement - we don't have any extra
3007 // restrictions.
3008 if (!hasSymbolicDisplacement)
3009 return true;
3010
3011 // FIXME: Some tweaks might be needed for medium code model.
3012 if (M != CodeModel::Small && M != CodeModel::Kernel)
3013 return false;
3014
3015 // For small code model we assume that latest object is 16MB before end of 31
3016 // bits boundary. We may also accept pretty large negative constants knowing
3017 // that all objects are in the positive half of address space.
3018 if (M == CodeModel::Small && Offset < 16*1024*1024)
3019 return true;
3020
3021 // For kernel code model we know that all object resist in the negative half
3022 // of 32bits address space. We may not accept negative offsets, since they may
3023 // be just off and we may accept pretty large positive ones.
3024 if (M == CodeModel::Kernel && Offset > 0)
3025 return true;
3026
3027 return false;
3028}
3029
Evan Chengef41ff62011-06-23 17:54:54 +00003030/// isCalleePop - Determines whether the callee is required to pop its
3031/// own arguments. Callee pop is necessary to support tail calls.
3032bool X86::isCalleePop(CallingConv::ID CallingConv,
3033 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3034 if (IsVarArg)
3035 return false;
3036
3037 switch (CallingConv) {
3038 default:
3039 return false;
3040 case CallingConv::X86_StdCall:
3041 return !is64Bit;
3042 case CallingConv::X86_FastCall:
3043 return !is64Bit;
3044 case CallingConv::X86_ThisCall:
3045 return !is64Bit;
3046 case CallingConv::Fast:
3047 return TailCallOpt;
3048 case CallingConv::GHC:
3049 return TailCallOpt;
3050 }
3051}
3052
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003053/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3054/// specific condition code, returning the condition code and the LHS/RHS of the
3055/// comparison to make.
3056static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3057 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003058 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003059 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3060 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3061 // X > -1 -> X == 0, jump !sign.
3062 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003064 }
3065 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003066 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003067 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003068 }
3069 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003070 // X < 1 -> X <= 0
3071 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003072 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003073 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003074 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003075
Evan Chengd9558e02006-01-06 00:43:03 +00003076 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003077 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003078 case ISD::SETEQ: return X86::COND_E;
3079 case ISD::SETGT: return X86::COND_G;
3080 case ISD::SETGE: return X86::COND_GE;
3081 case ISD::SETLT: return X86::COND_L;
3082 case ISD::SETLE: return X86::COND_LE;
3083 case ISD::SETNE: return X86::COND_NE;
3084 case ISD::SETULT: return X86::COND_B;
3085 case ISD::SETUGT: return X86::COND_A;
3086 case ISD::SETULE: return X86::COND_BE;
3087 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003088 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003089 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003090
Chris Lattner4c78e022008-12-23 23:42:27 +00003091 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003092
Chris Lattner4c78e022008-12-23 23:42:27 +00003093 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003094 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3095 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003096 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3097 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003098 }
3099
Chris Lattner4c78e022008-12-23 23:42:27 +00003100 switch (SetCCOpcode) {
3101 default: break;
3102 case ISD::SETOLT:
3103 case ISD::SETOLE:
3104 case ISD::SETUGT:
3105 case ISD::SETUGE:
3106 std::swap(LHS, RHS);
3107 break;
3108 }
3109
3110 // On a floating point condition, the flags are set as follows:
3111 // ZF PF CF op
3112 // 0 | 0 | 0 | X > Y
3113 // 0 | 0 | 1 | X < Y
3114 // 1 | 0 | 0 | X == Y
3115 // 1 | 1 | 1 | unordered
3116 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003117 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003118 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003119 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003120 case ISD::SETOLT: // flipped
3121 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003122 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003123 case ISD::SETOLE: // flipped
3124 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003125 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003126 case ISD::SETUGT: // flipped
3127 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003128 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003129 case ISD::SETUGE: // flipped
3130 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003131 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003132 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003133 case ISD::SETNE: return X86::COND_NE;
3134 case ISD::SETUO: return X86::COND_P;
3135 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003136 case ISD::SETOEQ:
3137 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003138 }
Evan Chengd9558e02006-01-06 00:43:03 +00003139}
3140
Evan Cheng4a460802006-01-11 00:33:36 +00003141/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3142/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003143/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003144static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003145 switch (X86CC) {
3146 default:
3147 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003148 case X86::COND_B:
3149 case X86::COND_BE:
3150 case X86::COND_E:
3151 case X86::COND_P:
3152 case X86::COND_A:
3153 case X86::COND_AE:
3154 case X86::COND_NE:
3155 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003156 return true;
3157 }
3158}
3159
Evan Chengeb2f9692009-10-27 19:56:55 +00003160/// isFPImmLegal - Returns true if the target can instruction select the
3161/// specified FP immediate natively. If false, the legalizer will
3162/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003163bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003164 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3165 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3166 return true;
3167 }
3168 return false;
3169}
3170
Nate Begeman9008ca62009-04-27 18:41:29 +00003171/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3172/// the specified range (L, H].
3173static bool isUndefOrInRange(int Val, int Low, int Hi) {
3174 return (Val < 0) || (Val >= Low && Val < Hi);
3175}
3176
3177/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3178/// specified value.
3179static bool isUndefOrEqual(int Val, int CmpVal) {
3180 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003181 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003183}
3184
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003185/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003186/// from position Pos and ending in Pos+Size, falls within the specified
3187/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003188static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003189 unsigned Pos, unsigned Size, int Low) {
3190 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003191 if (!isUndefOrEqual(Mask[i], Low))
3192 return false;
3193 return true;
3194}
3195
Nate Begeman9008ca62009-04-27 18:41:29 +00003196/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3197/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3198/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003199static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003200 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 return (Mask[0] < 2 && Mask[1] < 2);
3204 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205}
3206
Nate Begeman9008ca62009-04-27 18:41:29 +00003207/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3208/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003209static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3210 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003211 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003212
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003214 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3215 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003216
Evan Cheng506d3df2006-03-29 23:07:14 +00003217 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003218 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003219 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003220 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003221
Craig Toppera9a568a2012-05-02 08:03:44 +00003222 if (VT == MVT::v16i16) {
3223 // Lower quadword copied in order or undef.
3224 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3225 return false;
3226
3227 // Upper quadword shuffled.
3228 for (unsigned i = 12; i != 16; ++i)
3229 if (!isUndefOrInRange(Mask[i], 12, 16))
3230 return false;
3231 }
3232
Evan Cheng506d3df2006-03-29 23:07:14 +00003233 return true;
3234}
3235
Nate Begeman9008ca62009-04-27 18:41:29 +00003236/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3237/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003238static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3239 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003240 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003241
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003243 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3244 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003245
Rafael Espindola15684b22009-04-24 12:40:33 +00003246 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003247 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003248 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003249 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003250
Craig Toppera9a568a2012-05-02 08:03:44 +00003251 if (VT == MVT::v16i16) {
3252 // Upper quadword copied in order.
3253 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3254 return false;
3255
3256 // Lower quadword shuffled.
3257 for (unsigned i = 8; i != 12; ++i)
3258 if (!isUndefOrInRange(Mask[i], 8, 12))
3259 return false;
3260 }
3261
Rafael Espindola15684b22009-04-24 12:40:33 +00003262 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003263}
3264
Nate Begemana09008b2009-10-19 02:17:23 +00003265/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3266/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003267static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3268 const X86Subtarget *Subtarget) {
3269 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3270 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003271 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003272
Craig Topper0e2037b2012-01-20 05:53:00 +00003273 unsigned NumElts = VT.getVectorNumElements();
3274 unsigned NumLanes = VT.getSizeInBits()/128;
3275 unsigned NumLaneElts = NumElts/NumLanes;
3276
3277 // Do not handle 64-bit element shuffles with palignr.
3278 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003279 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003280
Craig Topper0e2037b2012-01-20 05:53:00 +00003281 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3282 unsigned i;
3283 for (i = 0; i != NumLaneElts; ++i) {
3284 if (Mask[i+l] >= 0)
3285 break;
3286 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003287
Craig Topper0e2037b2012-01-20 05:53:00 +00003288 // Lane is all undef, go to next lane
3289 if (i == NumLaneElts)
3290 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003291
Craig Topper0e2037b2012-01-20 05:53:00 +00003292 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003293
Craig Topper0e2037b2012-01-20 05:53:00 +00003294 // Make sure its in this lane in one of the sources
3295 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3296 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003297 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003298
3299 // If not lane 0, then we must match lane 0
3300 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3301 return false;
3302
3303 // Correct second source to be contiguous with first source
3304 if (Start >= (int)NumElts)
3305 Start -= NumElts - NumLaneElts;
3306
3307 // Make sure we're shifting in the right direction.
3308 if (Start <= (int)(i+l))
3309 return false;
3310
3311 Start -= i;
3312
3313 // Check the rest of the elements to see if they are consecutive.
3314 for (++i; i != NumLaneElts; ++i) {
3315 int Idx = Mask[i+l];
3316
3317 // Make sure its in this lane
3318 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3319 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3320 return false;
3321
3322 // If not lane 0, then we must match lane 0
3323 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3324 return false;
3325
3326 if (Idx >= (int)NumElts)
3327 Idx -= NumElts - NumLaneElts;
3328
3329 if (!isUndefOrEqual(Idx, Start+i))
3330 return false;
3331
3332 }
Nate Begemana09008b2009-10-19 02:17:23 +00003333 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003334
Nate Begemana09008b2009-10-19 02:17:23 +00003335 return true;
3336}
3337
Craig Topper1a7700a2012-01-19 08:19:12 +00003338/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3339/// the two vector operands have swapped position.
3340static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3341 unsigned NumElems) {
3342 for (unsigned i = 0; i != NumElems; ++i) {
3343 int idx = Mask[i];
3344 if (idx < 0)
3345 continue;
3346 else if (idx < (int)NumElems)
3347 Mask[i] = idx + NumElems;
3348 else
3349 Mask[i] = idx - NumElems;
3350 }
3351}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003352
Craig Topper1a7700a2012-01-19 08:19:12 +00003353/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3354/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3355/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3356/// reverse of what x86 shuffles want.
3357static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3358 bool Commuted = false) {
3359 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003360 return false;
3361
Craig Topper1a7700a2012-01-19 08:19:12 +00003362 unsigned NumElems = VT.getVectorNumElements();
3363 unsigned NumLanes = VT.getSizeInBits()/128;
3364 unsigned NumLaneElems = NumElems/NumLanes;
3365
3366 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003367 return false;
3368
3369 // VSHUFPSY divides the resulting vector into 4 chunks.
3370 // The sources are also splitted into 4 chunks, and each destination
3371 // chunk must come from a different source chunk.
3372 //
3373 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3374 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3375 //
3376 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3377 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3378 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003379 // VSHUFPDY divides the resulting vector into 4 chunks.
3380 // The sources are also splitted into 4 chunks, and each destination
3381 // chunk must come from a different source chunk.
3382 //
3383 // SRC1 => X3 X2 X1 X0
3384 // SRC2 => Y3 Y2 Y1 Y0
3385 //
3386 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3387 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003388 unsigned HalfLaneElems = NumLaneElems/2;
3389 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3390 for (unsigned i = 0; i != NumLaneElems; ++i) {
3391 int Idx = Mask[i+l];
3392 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3393 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3394 return false;
3395 // For VSHUFPSY, the mask of the second half must be the same as the
3396 // first but with the appropriate offsets. This works in the same way as
3397 // VPERMILPS works with masks.
3398 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3399 continue;
3400 if (!isUndefOrEqual(Idx, Mask[i]+l))
3401 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003402 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003403 }
3404
3405 return true;
3406}
3407
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003408/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3409/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003410static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003411 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003412 return false;
3413
Craig Topper7a9a28b2012-08-12 02:23:29 +00003414 unsigned NumElems = VT.getVectorNumElements();
3415
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003416 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003417 return false;
3418
Evan Cheng2064a2b2006-03-28 06:50:32 +00003419 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003420 return isUndefOrEqual(Mask[0], 6) &&
3421 isUndefOrEqual(Mask[1], 7) &&
3422 isUndefOrEqual(Mask[2], 2) &&
3423 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003424}
3425
Nate Begeman0b10b912009-11-07 23:17:15 +00003426/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3427/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3428/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003429static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003430 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003431 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003432
Craig Topper7a9a28b2012-08-12 02:23:29 +00003433 unsigned NumElems = VT.getVectorNumElements();
3434
Nate Begeman0b10b912009-11-07 23:17:15 +00003435 if (NumElems != 4)
3436 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003437
Craig Topperdd637ae2012-02-19 05:41:45 +00003438 return isUndefOrEqual(Mask[0], 2) &&
3439 isUndefOrEqual(Mask[1], 3) &&
3440 isUndefOrEqual(Mask[2], 2) &&
3441 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003442}
3443
Evan Cheng5ced1d82006-04-06 23:23:56 +00003444/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3445/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003446static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003447 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003448 return false;
3449
Craig Topperdd637ae2012-02-19 05:41:45 +00003450 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003451
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452 if (NumElems != 2 && NumElems != 4)
3453 return false;
3454
Chad Rosier238ae312012-04-30 17:47:15 +00003455 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003456 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
Chad Rosier238ae312012-04-30 17:47:15 +00003459 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003460 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003461 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003462
3463 return true;
3464}
3465
Nate Begeman0b10b912009-11-07 23:17:15 +00003466/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3467/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003468static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003469 if (!VT.is128BitVector())
3470 return false;
3471
Craig Topperdd637ae2012-02-19 05:41:45 +00003472 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003473
Craig Topper7a9a28b2012-08-12 02:23:29 +00003474 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003475 return false;
3476
Chad Rosier238ae312012-04-30 17:47:15 +00003477 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003478 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003479 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480
Chad Rosier238ae312012-04-30 17:47:15 +00003481 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3482 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003483 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003484
3485 return true;
3486}
3487
Elena Demikhovsky15963732012-06-26 08:04:10 +00003488//
3489// Some special combinations that can be optimized.
3490//
3491static
3492SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3493 SelectionDAG &DAG) {
3494 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003495 DebugLoc dl = SVOp->getDebugLoc();
3496
3497 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3498 return SDValue();
3499
3500 ArrayRef<int> Mask = SVOp->getMask();
3501
3502 // These are the special masks that may be optimized.
3503 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3504 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3505 bool MatchEvenMask = true;
3506 bool MatchOddMask = true;
3507 for (int i=0; i<8; ++i) {
3508 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3509 MatchEvenMask = false;
3510 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3511 MatchOddMask = false;
3512 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003513
Elena Demikhovsky32510202012-09-04 12:49:02 +00003514 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003515 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003516
Elena Demikhovsky15963732012-06-26 08:04:10 +00003517 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3518
Elena Demikhovsky32510202012-09-04 12:49:02 +00003519 SDValue Op0 = SVOp->getOperand(0);
3520 SDValue Op1 = SVOp->getOperand(1);
3521
3522 if (MatchEvenMask) {
3523 // Shift the second operand right to 32 bits.
3524 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3525 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3526 } else {
3527 // Shift the first operand left to 32 bits.
3528 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3529 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3530 }
3531 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3532 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003533}
3534
Evan Cheng0038e592006-03-28 00:39:58 +00003535/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3536/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003537static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003538 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003539 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003540
3541 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3542 "Unsupported vector type for unpckh");
3543
Craig Topper6347e862011-11-21 06:57:39 +00003544 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003545 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003546 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003547
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003548 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3549 // independently on 128-bit lanes.
3550 unsigned NumLanes = VT.getSizeInBits()/128;
3551 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003552
Craig Topper94438ba2011-12-16 08:06:31 +00003553 for (unsigned l = 0; l != NumLanes; ++l) {
3554 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3555 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003556 i += 2, ++j) {
3557 int BitI = Mask[i];
3558 int BitI1 = Mask[i+1];
3559 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003560 return false;
David Greenea20244d2011-03-02 17:23:43 +00003561 if (V2IsSplat) {
3562 if (!isUndefOrEqual(BitI1, NumElts))
3563 return false;
3564 } else {
3565 if (!isUndefOrEqual(BitI1, j + NumElts))
3566 return false;
3567 }
Evan Cheng39623da2006-04-20 08:58:49 +00003568 }
Evan Cheng0038e592006-03-28 00:39:58 +00003569 }
David Greenea20244d2011-03-02 17:23:43 +00003570
Evan Cheng0038e592006-03-28 00:39:58 +00003571 return true;
3572}
3573
Evan Cheng4fcb9222006-03-28 02:43:26 +00003574/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3575/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003576static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003577 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003578 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003579
3580 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3581 "Unsupported vector type for unpckh");
3582
Craig Topper6347e862011-11-21 06:57:39 +00003583 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003584 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003585 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003586
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003587 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3588 // independently on 128-bit lanes.
3589 unsigned NumLanes = VT.getSizeInBits()/128;
3590 unsigned NumLaneElts = NumElts/NumLanes;
3591
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003592 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003593 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3594 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003595 int BitI = Mask[i];
3596 int BitI1 = Mask[i+1];
3597 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003598 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003599 if (V2IsSplat) {
3600 if (isUndefOrEqual(BitI1, NumElts))
3601 return false;
3602 } else {
3603 if (!isUndefOrEqual(BitI1, j+NumElts))
3604 return false;
3605 }
Evan Cheng39623da2006-04-20 08:58:49 +00003606 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003607 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003608 return true;
3609}
3610
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003611/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3612/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3613/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003614static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003615 bool HasAVX2) {
3616 unsigned NumElts = VT.getVectorNumElements();
3617
3618 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3619 "Unsupported vector type for unpckh");
3620
3621 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3622 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003623 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003624
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003625 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3626 // FIXME: Need a better way to get rid of this, there's no latency difference
3627 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3628 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003629 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003630 return false;
3631
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003632 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3633 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003634 unsigned NumLanes = VT.getSizeInBits()/128;
3635 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003636
Craig Topper94438ba2011-12-16 08:06:31 +00003637 for (unsigned l = 0; l != NumLanes; ++l) {
3638 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3639 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003640 i += 2, ++j) {
3641 int BitI = Mask[i];
3642 int BitI1 = Mask[i+1];
3643
3644 if (!isUndefOrEqual(BitI, j))
3645 return false;
3646 if (!isUndefOrEqual(BitI1, j))
3647 return false;
3648 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003649 }
David Greenea20244d2011-03-02 17:23:43 +00003650
Rafael Espindola15684b22009-04-24 12:40:33 +00003651 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003652}
3653
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003654/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3655/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3656/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003657static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003658 unsigned NumElts = VT.getVectorNumElements();
3659
3660 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3661 "Unsupported vector type for unpckh");
3662
3663 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3664 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003665 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003666
Craig Topper94438ba2011-12-16 08:06:31 +00003667 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3668 // independently on 128-bit lanes.
3669 unsigned NumLanes = VT.getSizeInBits()/128;
3670 unsigned NumLaneElts = NumElts/NumLanes;
3671
3672 for (unsigned l = 0; l != NumLanes; ++l) {
3673 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3674 i != (l+1)*NumLaneElts; i += 2, ++j) {
3675 int BitI = Mask[i];
3676 int BitI1 = Mask[i+1];
3677 if (!isUndefOrEqual(BitI, j))
3678 return false;
3679 if (!isUndefOrEqual(BitI1, j))
3680 return false;
3681 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003682 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003683 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003684}
3685
Evan Cheng017dcc62006-04-21 01:05:10 +00003686/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3687/// specifies a shuffle of elements that is suitable for input to MOVSS,
3688/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003689static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003690 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003691 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003692 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003693 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003694
Craig Topperc612d792012-01-02 09:17:37 +00003695 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003696
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003698 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003699
Craig Topperc612d792012-01-02 09:17:37 +00003700 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003702 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003703
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003704 return true;
3705}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003706
Craig Topper70b883b2011-11-28 10:14:51 +00003707/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003708/// as permutations between 128-bit chunks or halves. As an example: this
3709/// shuffle bellow:
3710/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3711/// The first half comes from the second half of V1 and the second half from the
3712/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003713static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003714 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003715 return false;
3716
3717 // The shuffle result is divided into half A and half B. In total the two
3718 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3719 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003720 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003721 bool MatchA = false, MatchB = false;
3722
3723 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003724 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003725 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3726 MatchA = true;
3727 break;
3728 }
3729 }
3730
3731 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003732 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003733 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3734 MatchB = true;
3735 break;
3736 }
3737 }
3738
3739 return MatchA && MatchB;
3740}
3741
Craig Topper70b883b2011-11-28 10:14:51 +00003742/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3743/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003744static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003745 EVT VT = SVOp->getValueType(0);
3746
Craig Topperc612d792012-01-02 09:17:37 +00003747 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003748
Craig Topperc612d792012-01-02 09:17:37 +00003749 unsigned FstHalf = 0, SndHalf = 0;
3750 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003751 if (SVOp->getMaskElt(i) > 0) {
3752 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3753 break;
3754 }
3755 }
Craig Topperc612d792012-01-02 09:17:37 +00003756 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757 if (SVOp->getMaskElt(i) > 0) {
3758 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3759 break;
3760 }
3761 }
3762
3763 return (FstHalf | (SndHalf << 4));
3764}
3765
Craig Topper70b883b2011-11-28 10:14:51 +00003766/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003767/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3768/// Note that VPERMIL mask matching is different depending whether theunderlying
3769/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3770/// to the same elements of the low, but to the higher half of the source.
3771/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003772/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003773static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003774 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003775 return false;
3776
Craig Topperc612d792012-01-02 09:17:37 +00003777 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003778 // Only match 256-bit with 32/64-bit types
3779 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003780 return false;
3781
Craig Topperc612d792012-01-02 09:17:37 +00003782 unsigned NumLanes = VT.getSizeInBits()/128;
3783 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003784 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003785 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003786 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003787 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003788 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003789 continue;
3790 // VPERMILPS handling
3791 if (Mask[i] < 0)
3792 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003793 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003794 return false;
3795 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003796 }
3797
3798 return true;
3799}
3800
Craig Topper5aaffa82012-02-19 02:53:47 +00003801/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003802/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003803/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003804static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003805 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003806 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003807 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003808
3809 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003810 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003811 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003812
Nate Begeman9008ca62009-04-27 18:41:29 +00003813 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003814 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003815
Craig Topperc612d792012-01-02 09:17:37 +00003816 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3818 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3819 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003820 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003821
Evan Cheng39623da2006-04-20 08:58:49 +00003822 return true;
3823}
3824
Evan Chengd9539472006-04-14 21:59:03 +00003825/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3826/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003827/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003828static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003829 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003830 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003831 return false;
3832
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003833 unsigned NumElems = VT.getVectorNumElements();
3834
3835 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3836 (VT.getSizeInBits() == 256 && NumElems != 8))
3837 return false;
3838
3839 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003840 for (unsigned i = 0; i != NumElems; i += 2)
3841 if (!isUndefOrEqual(Mask[i], i+1) ||
3842 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003844
3845 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003846}
3847
3848/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3849/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003850/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003851static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003852 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003853 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003854 return false;
3855
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003856 unsigned NumElems = VT.getVectorNumElements();
3857
3858 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3859 (VT.getSizeInBits() == 256 && NumElems != 8))
3860 return false;
3861
3862 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003863 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003864 if (!isUndefOrEqual(Mask[i], i) ||
3865 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003867
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003868 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003869}
3870
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003871/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3872/// specifies a shuffle of elements that is suitable for input to 256-bit
3873/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003874static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003875 if (!HasAVX || !VT.is256BitVector())
3876 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003877
Craig Topper7a9a28b2012-08-12 02:23:29 +00003878 unsigned NumElts = VT.getVectorNumElements();
3879 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003880 return false;
3881
Craig Topperc612d792012-01-02 09:17:37 +00003882 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003883 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003884 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003885 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003886 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003887 return false;
3888 return true;
3889}
3890
Evan Cheng0b457f02008-09-25 20:50:48 +00003891/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003892/// specifies a shuffle of elements that is suitable for input to 128-bit
3893/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003894static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003895 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003896 return false;
3897
Craig Topperc612d792012-01-02 09:17:37 +00003898 unsigned e = VT.getVectorNumElements() / 2;
3899 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003900 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003901 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003902 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003903 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003904 return false;
3905 return true;
3906}
3907
David Greenec38a03e2011-02-03 15:50:00 +00003908/// isVEXTRACTF128Index - Return true if the specified
3909/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3910/// suitable for input to VEXTRACTF128.
3911bool X86::isVEXTRACTF128Index(SDNode *N) {
3912 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3913 return false;
3914
3915 // The index should be aligned on a 128-bit boundary.
3916 uint64_t Index =
3917 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3918
3919 unsigned VL = N->getValueType(0).getVectorNumElements();
3920 unsigned VBits = N->getValueType(0).getSizeInBits();
3921 unsigned ElSize = VBits / VL;
3922 bool Result = (Index * ElSize) % 128 == 0;
3923
3924 return Result;
3925}
3926
David Greeneccacdc12011-02-04 16:08:29 +00003927/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3928/// operand specifies a subvector insert that is suitable for input to
3929/// VINSERTF128.
3930bool X86::isVINSERTF128Index(SDNode *N) {
3931 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3932 return false;
3933
3934 // The index should be aligned on a 128-bit boundary.
3935 uint64_t Index =
3936 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3937
3938 unsigned VL = N->getValueType(0).getVectorNumElements();
3939 unsigned VBits = N->getValueType(0).getSizeInBits();
3940 unsigned ElSize = VBits / VL;
3941 bool Result = (Index * ElSize) % 128 == 0;
3942
3943 return Result;
3944}
3945
Evan Cheng63d33002006-03-22 08:01:21 +00003946/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003947/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003948/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003949static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003950 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003951
Craig Topper1a7700a2012-01-19 08:19:12 +00003952 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3953 "Unsupported vector type for PSHUF/SHUFP");
3954
3955 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3956 // independently on 128-bit lanes.
3957 unsigned NumElts = VT.getVectorNumElements();
3958 unsigned NumLanes = VT.getSizeInBits()/128;
3959 unsigned NumLaneElts = NumElts/NumLanes;
3960
3961 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3962 "Only supports 2 or 4 elements per lane");
3963
3964 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003965 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003966 for (unsigned i = 0; i != NumElts; ++i) {
3967 int Elt = N->getMaskElt(i);
3968 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003969 Elt &= NumLaneElts - 1;
3970 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003971 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003972 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003973
Evan Cheng63d33002006-03-22 08:01:21 +00003974 return Mask;
3975}
3976
Evan Cheng506d3df2006-03-29 23:07:14 +00003977/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003978/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003979static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003980 EVT VT = N->getValueType(0);
3981
3982 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3983 "Unsupported vector type for PSHUFHW");
3984
3985 unsigned NumElts = VT.getVectorNumElements();
3986
Evan Cheng506d3df2006-03-29 23:07:14 +00003987 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003988 for (unsigned l = 0; l != NumElts; l += 8) {
3989 // 8 nodes per lane, but we only care about the last 4.
3990 for (unsigned i = 0; i < 4; ++i) {
3991 int Elt = N->getMaskElt(l+i+4);
3992 if (Elt < 0) continue;
3993 Elt &= 0x3; // only 2-bits.
3994 Mask |= Elt << (i * 2);
3995 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003996 }
Craig Topper6b28d352012-05-03 07:12:59 +00003997
Evan Cheng506d3df2006-03-29 23:07:14 +00003998 return Mask;
3999}
4000
4001/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004002/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004003static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004004 EVT VT = N->getValueType(0);
4005
4006 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4007 "Unsupported vector type for PSHUFHW");
4008
4009 unsigned NumElts = VT.getVectorNumElements();
4010
Evan Cheng506d3df2006-03-29 23:07:14 +00004011 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004012 for (unsigned l = 0; l != NumElts; l += 8) {
4013 // 8 nodes per lane, but we only care about the first 4.
4014 for (unsigned i = 0; i < 4; ++i) {
4015 int Elt = N->getMaskElt(l+i);
4016 if (Elt < 0) continue;
4017 Elt &= 0x3; // only 2-bits
4018 Mask |= Elt << (i * 2);
4019 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004020 }
Craig Topper6b28d352012-05-03 07:12:59 +00004021
Evan Cheng506d3df2006-03-29 23:07:14 +00004022 return Mask;
4023}
4024
Nate Begemana09008b2009-10-19 02:17:23 +00004025/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4026/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004027static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4028 EVT VT = SVOp->getValueType(0);
4029 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004030
Craig Topper0e2037b2012-01-20 05:53:00 +00004031 unsigned NumElts = VT.getVectorNumElements();
4032 unsigned NumLanes = VT.getSizeInBits()/128;
4033 unsigned NumLaneElts = NumElts/NumLanes;
4034
4035 int Val = 0;
4036 unsigned i;
4037 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004038 Val = SVOp->getMaskElt(i);
4039 if (Val >= 0)
4040 break;
4041 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004042 if (Val >= (int)NumElts)
4043 Val -= NumElts - NumLaneElts;
4044
Eli Friedman63f8dde2011-07-25 21:36:45 +00004045 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004046 return (Val - i) * EltSize;
4047}
4048
David Greenec38a03e2011-02-03 15:50:00 +00004049/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4050/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4051/// instructions.
4052unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4053 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4054 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4055
4056 uint64_t Index =
4057 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4058
4059 EVT VecVT = N->getOperand(0).getValueType();
4060 EVT ElVT = VecVT.getVectorElementType();
4061
4062 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004063 return Index / NumElemsPerChunk;
4064}
4065
David Greeneccacdc12011-02-04 16:08:29 +00004066/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4067/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4068/// instructions.
4069unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4070 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4071 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4072
4073 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004074 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004075
4076 EVT VecVT = N->getValueType(0);
4077 EVT ElVT = VecVT.getVectorElementType();
4078
4079 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004080 return Index / NumElemsPerChunk;
4081}
4082
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004083/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4084/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4085/// Handles 256-bit.
4086static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4087 EVT VT = N->getValueType(0);
4088
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004089 unsigned NumElts = VT.getVectorNumElements();
4090
Craig Topper095c5282012-04-15 23:48:57 +00004091 assert((VT.is256BitVector() && NumElts == 4) &&
4092 "Unsupported vector type for VPERMQ/VPERMPD");
4093
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004094 unsigned Mask = 0;
4095 for (unsigned i = 0; i != NumElts; ++i) {
4096 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004097 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004098 continue;
4099 Mask |= Elt << (i*2);
4100 }
4101
4102 return Mask;
4103}
Evan Cheng37b73872009-07-30 08:33:02 +00004104/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4105/// constant +0.0.
4106bool X86::isZeroNode(SDValue Elt) {
4107 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004108 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004109 (isa<ConstantFPSDNode>(Elt) &&
4110 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4111}
4112
Nate Begeman9008ca62009-04-27 18:41:29 +00004113/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4114/// their permute mask.
4115static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4116 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004117 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004118 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004119 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004120
Nate Begeman5a5ca152009-04-29 05:20:52 +00004121 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004122 int Idx = SVOp->getMaskElt(i);
4123 if (Idx >= 0) {
4124 if (Idx < (int)NumElems)
4125 Idx += NumElems;
4126 else
4127 Idx -= NumElems;
4128 }
4129 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004130 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004131 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4132 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004133}
4134
Evan Cheng533a0aa2006-04-19 20:35:22 +00004135/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4136/// match movhlps. The lower half elements should come from upper half of
4137/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004138/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004139static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004140 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004141 return false;
4142 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004143 return false;
4144 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004145 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004146 return false;
4147 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004148 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004149 return false;
4150 return true;
4151}
4152
Evan Cheng5ced1d82006-04-06 23:23:56 +00004153/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004154/// is promoted to a vector. It also returns the LoadSDNode by reference if
4155/// required.
4156static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004157 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4158 return false;
4159 N = N->getOperand(0).getNode();
4160 if (!ISD::isNON_EXTLoad(N))
4161 return false;
4162 if (LD)
4163 *LD = cast<LoadSDNode>(N);
4164 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004165}
4166
Dan Gohman65fd6562011-11-03 21:49:52 +00004167// Test whether the given value is a vector value which will be legalized
4168// into a load.
4169static bool WillBeConstantPoolLoad(SDNode *N) {
4170 if (N->getOpcode() != ISD::BUILD_VECTOR)
4171 return false;
4172
4173 // Check for any non-constant elements.
4174 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4175 switch (N->getOperand(i).getNode()->getOpcode()) {
4176 case ISD::UNDEF:
4177 case ISD::ConstantFP:
4178 case ISD::Constant:
4179 break;
4180 default:
4181 return false;
4182 }
4183
4184 // Vectors of all-zeros and all-ones are materialized with special
4185 // instructions rather than being loaded.
4186 return !ISD::isBuildVectorAllZeros(N) &&
4187 !ISD::isBuildVectorAllOnes(N);
4188}
4189
Evan Cheng533a0aa2006-04-19 20:35:22 +00004190/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4191/// match movlp{s|d}. The lower half elements should come from lower half of
4192/// V1 (and in order), and the upper half elements should come from the upper
4193/// half of V2 (and in order). And since V1 will become the source of the
4194/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004195static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004196 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004197 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004198 return false;
4199
Evan Cheng466685d2006-10-09 20:57:25 +00004200 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004201 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004202 // Is V2 is a vector load, don't do this transformation. We will try to use
4203 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004204 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004205 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004206
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004207 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004208
Evan Cheng533a0aa2006-04-19 20:35:22 +00004209 if (NumElems != 2 && NumElems != 4)
4210 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004211 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004212 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004213 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004214 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004215 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004216 return false;
4217 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004218}
4219
Evan Cheng39623da2006-04-20 08:58:49 +00004220/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4221/// all the same.
4222static bool isSplatVector(SDNode *N) {
4223 if (N->getOpcode() != ISD::BUILD_VECTOR)
4224 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004225
Dan Gohman475871a2008-07-27 21:46:04 +00004226 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004227 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4228 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004229 return false;
4230 return true;
4231}
4232
Evan Cheng213d2cf2007-05-17 18:45:50 +00004233/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004234/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004235/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004236static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004237 SDValue V1 = N->getOperand(0);
4238 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004239 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4240 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004242 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004244 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4245 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004246 if (Opc != ISD::BUILD_VECTOR ||
4247 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 return false;
4249 } else if (Idx >= 0) {
4250 unsigned Opc = V1.getOpcode();
4251 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4252 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004253 if (Opc != ISD::BUILD_VECTOR ||
4254 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004255 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004256 }
4257 }
4258 return true;
4259}
4260
4261/// getZeroVector - Returns a vector of specified type with all zero elements.
4262///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004263static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004264 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004265 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004266 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004267
Dale Johannesen0488fb62010-09-30 23:57:10 +00004268 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004269 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004270 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004271 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004272 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004273 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4274 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4275 } else { // SSE1
4276 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4277 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4278 }
Craig Topper9d352402012-04-23 07:24:41 +00004279 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004280 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004281 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4282 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4283 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4284 } else {
4285 // 256-bit logic and arithmetic instructions in AVX are all
4286 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4287 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4288 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4289 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4290 }
Craig Topper9d352402012-04-23 07:24:41 +00004291 } else
4292 llvm_unreachable("Unexpected vector type");
4293
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004294 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004295}
4296
Chris Lattner8a594482007-11-25 00:24:49 +00004297/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004298/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4299/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4300/// Then bitcast to their original type, ensuring they get CSE'd.
4301static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4302 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004303 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004304 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004305
Owen Anderson825b72b2009-08-11 20:47:22 +00004306 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004307 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004308 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004309 if (HasAVX2) { // AVX2
4310 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4311 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4312 } else { // AVX
4313 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004314 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004315 }
Craig Topper9d352402012-04-23 07:24:41 +00004316 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004317 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004318 } else
4319 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004320
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004321 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004322}
4323
Evan Cheng39623da2006-04-20 08:58:49 +00004324/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4325/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004326static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004327 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004328 if (Mask[i] > (int)NumElems) {
4329 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004330 }
Evan Cheng39623da2006-04-20 08:58:49 +00004331 }
Evan Cheng39623da2006-04-20 08:58:49 +00004332}
4333
Evan Cheng017dcc62006-04-21 01:05:10 +00004334/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4335/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004336static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 SDValue V2) {
4338 unsigned NumElems = VT.getVectorNumElements();
4339 SmallVector<int, 8> Mask;
4340 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004341 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 Mask.push_back(i);
4343 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004344}
4345
Nate Begeman9008ca62009-04-27 18:41:29 +00004346/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004347static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 SDValue V2) {
4349 unsigned NumElems = VT.getVectorNumElements();
4350 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004351 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 Mask.push_back(i);
4353 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004354 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004356}
4357
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004358/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004359static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 SDValue V2) {
4361 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004363 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 Mask.push_back(i + Half);
4365 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004366 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004368}
4369
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004370// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004371// a generic shuffle instruction because the target has no such instructions.
4372// Generate shuffles which repeat i16 and i8 several times until they can be
4373// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004374static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004375 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004377 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004378
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 while (NumElems > 4) {
4380 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004381 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004383 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 EltNo -= NumElems/2;
4385 }
4386 NumElems >>= 1;
4387 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004388 return V;
4389}
Eric Christopherfd179292009-08-27 18:07:15 +00004390
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4392static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4393 EVT VT = V.getValueType();
4394 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004395 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004396
Craig Topper9d352402012-04-23 07:24:41 +00004397 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004398 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004400 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4401 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004402 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004403 // To use VPERMILPS to splat scalars, the second half of indicies must
4404 // refer to the higher part, which is a duplication of the lower one,
4405 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4407 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004408
4409 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4410 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4411 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004412 } else
4413 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004414
4415 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4416}
4417
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004418/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004419static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4420 EVT SrcVT = SV->getValueType(0);
4421 SDValue V1 = SV->getOperand(0);
4422 DebugLoc dl = SV->getDebugLoc();
4423
4424 int EltNo = SV->getSplatIndex();
4425 int NumElems = SrcVT.getVectorNumElements();
4426 unsigned Size = SrcVT.getSizeInBits();
4427
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004428 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4429 "Unknown how to promote splat for type");
4430
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004431 // Extract the 128-bit part containing the splat element and update
4432 // the splat element index when it refers to the higher register.
4433 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004434 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4435 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 EltNo -= NumElems/2;
4437 }
4438
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004439 // All i16 and i8 vector types can't be used directly by a generic shuffle
4440 // instruction because the target has no such instruction. Generate shuffles
4441 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004442 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004443 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004444 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004445 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446
4447 // Recreate the 256-bit vector and place the same 128-bit vector
4448 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004449 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004450 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004451 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452 }
4453
4454 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004455}
4456
Evan Chengba05f722006-04-21 23:03:30 +00004457/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004458/// vector of zero or undef vector. This produces a shuffle where the low
4459/// element of V2 is swizzled into the zero/undef vector, landing at element
4460/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00004461static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004462 bool IsZero,
4463 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004464 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004465 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004466 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004467 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 unsigned NumElems = VT.getVectorNumElements();
4469 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004470 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 // If this is the insertion idx, put the low elt of V2 here.
4472 MaskVec.push_back(i == Idx ? NumElems : i);
4473 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004474}
4475
Craig Toppera1ffc682012-03-20 06:42:26 +00004476/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4477/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004478/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004479static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004480 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004481 unsigned NumElems = VT.getVectorNumElements();
4482 SDValue ImmN;
4483
Craig Topper89f4e662012-03-20 07:17:59 +00004484 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004485 switch(N->getOpcode()) {
4486 case X86ISD::SHUFP:
4487 ImmN = N->getOperand(N->getNumOperands()-1);
4488 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4489 break;
4490 case X86ISD::UNPCKH:
4491 DecodeUNPCKHMask(VT, Mask);
4492 break;
4493 case X86ISD::UNPCKL:
4494 DecodeUNPCKLMask(VT, Mask);
4495 break;
4496 case X86ISD::MOVHLPS:
4497 DecodeMOVHLPSMask(NumElems, Mask);
4498 break;
4499 case X86ISD::MOVLHPS:
4500 DecodeMOVLHPSMask(NumElems, Mask);
4501 break;
4502 case X86ISD::PSHUFD:
4503 case X86ISD::VPERMILP:
4504 ImmN = N->getOperand(N->getNumOperands()-1);
4505 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004506 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004507 break;
4508 case X86ISD::PSHUFHW:
4509 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004510 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004511 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004512 break;
4513 case X86ISD::PSHUFLW:
4514 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004515 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004516 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004517 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004518 case X86ISD::VPERMI:
4519 ImmN = N->getOperand(N->getNumOperands()-1);
4520 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4521 IsUnary = true;
4522 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004523 case X86ISD::MOVSS:
4524 case X86ISD::MOVSD: {
4525 // The index 0 always comes from the first element of the second source,
4526 // this is why MOVSS and MOVSD are used in the first place. The other
4527 // elements come from the other positions of the first source vector
4528 Mask.push_back(NumElems);
4529 for (unsigned i = 1; i != NumElems; ++i) {
4530 Mask.push_back(i);
4531 }
4532 break;
4533 }
4534 case X86ISD::VPERM2X128:
4535 ImmN = N->getOperand(N->getNumOperands()-1);
4536 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004537 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004538 break;
4539 case X86ISD::MOVDDUP:
4540 case X86ISD::MOVLHPD:
4541 case X86ISD::MOVLPD:
4542 case X86ISD::MOVLPS:
4543 case X86ISD::MOVSHDUP:
4544 case X86ISD::MOVSLDUP:
4545 case X86ISD::PALIGN:
4546 // Not yet implemented
4547 return false;
4548 default: llvm_unreachable("unknown target shuffle node");
4549 }
4550
4551 return true;
4552}
4553
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004554/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4555/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004556static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004557 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004558 if (Depth == 6)
4559 return SDValue(); // Limit search depth.
4560
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004561 SDValue V = SDValue(N, 0);
4562 EVT VT = V.getValueType();
4563 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004564
4565 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4566 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004567 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004568
Craig Topper3d092db2012-03-21 02:14:01 +00004569 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004570 return DAG.getUNDEF(VT.getVectorElementType());
4571
Craig Topperd156dc12012-02-06 07:17:51 +00004572 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004573 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4574 : SV->getOperand(1);
4575 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004576 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004577
4578 // Recurse into target specific vector shuffles to find scalars.
4579 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004580 MVT ShufVT = V.getValueType().getSimpleVT();
4581 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004582 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004583 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004584 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004585
Craig Topperd978c542012-05-06 19:46:21 +00004586 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004587 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004588
Craig Topper3d092db2012-03-21 02:14:01 +00004589 int Elt = ShuffleMask[Index];
4590 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004591 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004592
Craig Topper3d092db2012-03-21 02:14:01 +00004593 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004594 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004595 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004596 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004597 }
4598
4599 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004600 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601 V = V.getOperand(0);
4602 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004603 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004604
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004605 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004606 return SDValue();
4607 }
4608
4609 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4610 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004611 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004612
4613 if (V.getOpcode() == ISD::BUILD_VECTOR)
4614 return V.getOperand(Index);
4615
4616 return SDValue();
4617}
4618
4619/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4620/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004621/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622static
Craig Topper3d092db2012-03-21 02:14:01 +00004623unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004624 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004625 unsigned i;
4626 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004628 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 if (!(Elt.getNode() &&
4630 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4631 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632 }
4633
4634 return i;
4635}
4636
Craig Topper3d092db2012-03-21 02:14:01 +00004637/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4638/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4640static
Craig Topper3d092db2012-03-21 02:14:01 +00004641bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4642 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4643 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004644 bool SeenV1 = false;
4645 bool SeenV2 = false;
4646
Craig Topper3d092db2012-03-21 02:14:01 +00004647 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 int Idx = SVOp->getMaskElt(i);
4649 // Ignore undef indicies
4650 if (Idx < 0)
4651 continue;
4652
Craig Topper3d092db2012-03-21 02:14:01 +00004653 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 SeenV1 = true;
4655 else
4656 SeenV2 = true;
4657
4658 // Only accept consecutive elements from the same vector
4659 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4660 return false;
4661 }
4662
4663 OpNum = SeenV1 ? 0 : 1;
4664 return true;
4665}
4666
4667/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4668/// logical left shift of a vector.
4669static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4670 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4671 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4672 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4673 false /* check zeros from right */, DAG);
4674 unsigned OpSrc;
4675
4676 if (!NumZeros)
4677 return false;
4678
4679 // Considering the elements in the mask that are not consecutive zeros,
4680 // check if they consecutively come from only one of the source vectors.
4681 //
4682 // V1 = {X, A, B, C} 0
4683 // \ \ \ /
4684 // vector_shuffle V1, V2 <1, 2, 3, X>
4685 //
4686 if (!isShuffleMaskConsecutive(SVOp,
4687 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004688 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689 NumZeros, // Where to start looking in the src vector
4690 NumElems, // Number of elements in vector
4691 OpSrc)) // Which source operand ?
4692 return false;
4693
4694 isLeft = false;
4695 ShAmt = NumZeros;
4696 ShVal = SVOp->getOperand(OpSrc);
4697 return true;
4698}
4699
4700/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4701/// logical left shift of a vector.
4702static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4703 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4704 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4705 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4706 true /* check zeros from left */, DAG);
4707 unsigned OpSrc;
4708
4709 if (!NumZeros)
4710 return false;
4711
4712 // Considering the elements in the mask that are not consecutive zeros,
4713 // check if they consecutively come from only one of the source vectors.
4714 //
4715 // 0 { A, B, X, X } = V2
4716 // / \ / /
4717 // vector_shuffle V1, V2 <X, X, 4, 5>
4718 //
4719 if (!isShuffleMaskConsecutive(SVOp,
4720 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004721 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004722 0, // Where to start looking in the src vector
4723 NumElems, // Number of elements in vector
4724 OpSrc)) // Which source operand ?
4725 return false;
4726
4727 isLeft = true;
4728 ShAmt = NumZeros;
4729 ShVal = SVOp->getOperand(OpSrc);
4730 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004731}
4732
4733/// isVectorShift - Returns true if the shuffle can be implemented as a
4734/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004735static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004736 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004737 // Although the logic below support any bitwidth size, there are no
4738 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004739 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004740 return false;
4741
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004742 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4743 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4744 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004745
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004747}
4748
Evan Chengc78d3b42006-04-24 18:01:45 +00004749/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4750///
Dan Gohman475871a2008-07-27 21:46:04 +00004751static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004752 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004753 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004754 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004755 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004756 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004757 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004758
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004759 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004760 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004761 bool First = true;
4762 for (unsigned i = 0; i < 16; ++i) {
4763 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4764 if (ThisIsNonZero && First) {
4765 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004766 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004767 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004768 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004769 First = false;
4770 }
4771
4772 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004773 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004774 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4775 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004776 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004777 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004778 }
4779 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004780 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4781 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4782 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004783 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004785 } else
4786 ThisElt = LastElt;
4787
Gabor Greifba36cb52008-08-28 21:40:38 +00004788 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004790 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004791 }
4792 }
4793
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004794 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004795}
4796
Bill Wendlinga348c562007-03-22 18:42:45 +00004797/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004798///
Dan Gohman475871a2008-07-27 21:46:04 +00004799static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004800 unsigned NumNonZero, unsigned NumZero,
4801 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004802 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004803 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004804 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004805 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004806
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004807 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004808 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 bool First = true;
4810 for (unsigned i = 0; i < 8; ++i) {
4811 bool isNonZero = (NonZeros & (1 << i)) != 0;
4812 if (isNonZero) {
4813 if (First) {
4814 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004815 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004816 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004818 First = false;
4819 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004820 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004822 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 }
4824 }
4825
4826 return V;
4827}
4828
Evan Chengf26ffe92008-05-29 08:22:04 +00004829/// getVShift - Return a vector logical shift node.
4830///
Owen Andersone50ed302009-08-10 22:56:29 +00004831static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004832 unsigned NumBits, SelectionDAG &DAG,
4833 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004834 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004835 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004836 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004837 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4838 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004839 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004840 DAG.getConstant(NumBits,
4841 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004842}
4843
Dan Gohman475871a2008-07-27 21:46:04 +00004844SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004845X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004846 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004847
Evan Chengc3630942009-12-09 21:00:30 +00004848 // Check if the scalar load can be widened into a vector load. And if
4849 // the address is "base + cst" see if the cst can be "absorbed" into
4850 // the shuffle mask.
4851 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4852 SDValue Ptr = LD->getBasePtr();
4853 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4854 return SDValue();
4855 EVT PVT = LD->getValueType(0);
4856 if (PVT != MVT::i32 && PVT != MVT::f32)
4857 return SDValue();
4858
4859 int FI = -1;
4860 int64_t Offset = 0;
4861 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4862 FI = FINode->getIndex();
4863 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004864 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004865 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4866 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4867 Offset = Ptr.getConstantOperandVal(1);
4868 Ptr = Ptr.getOperand(0);
4869 } else {
4870 return SDValue();
4871 }
4872
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004873 // FIXME: 256-bit vector instructions don't require a strict alignment,
4874 // improve this code to support it better.
4875 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004876 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004877 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004878 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004879 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004880 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004881 // Can't change the alignment. FIXME: It's possible to compute
4882 // the exact stack offset and reference FI + adjust offset instead.
4883 // If someone *really* cares about this. That's the way to implement it.
4884 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004885 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004886 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004887 }
4888 }
4889
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004890 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004891 // Ptr + (Offset & ~15).
4892 if (Offset < 0)
4893 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004894 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004895 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004896 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004897 if (StartOffset)
4898 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4899 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4900
4901 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004902 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004903
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004904 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4905 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004906 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004907 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004909 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004910 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004911 Mask.push_back(EltNo);
4912
Craig Toppercc3000632012-01-30 07:50:31 +00004913 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004914 }
4915
4916 return SDValue();
4917}
4918
Michael J. Spencerec38de22010-10-10 22:04:20 +00004919/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4920/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004921/// load which has the same value as a build_vector whose operands are 'elts'.
4922///
4923/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004924///
Nate Begeman1449f292010-03-24 22:19:06 +00004925/// FIXME: we'd also like to handle the case where the last elements are zero
4926/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4927/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004928static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004929 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004930 EVT EltVT = VT.getVectorElementType();
4931 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004932
Nate Begemanfdea31a2010-03-24 20:49:50 +00004933 LoadSDNode *LDBase = NULL;
4934 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004935
Nate Begeman1449f292010-03-24 22:19:06 +00004936 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004937 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004938 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004939 for (unsigned i = 0; i < NumElems; ++i) {
4940 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004941
Nate Begemanfdea31a2010-03-24 20:49:50 +00004942 if (!Elt.getNode() ||
4943 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4944 return SDValue();
4945 if (!LDBase) {
4946 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4947 return SDValue();
4948 LDBase = cast<LoadSDNode>(Elt.getNode());
4949 LastLoadedElt = i;
4950 continue;
4951 }
4952 if (Elt.getOpcode() == ISD::UNDEF)
4953 continue;
4954
4955 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4956 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4957 return SDValue();
4958 LastLoadedElt = i;
4959 }
Nate Begeman1449f292010-03-24 22:19:06 +00004960
4961 // If we have found an entire vector of loads and undefs, then return a large
4962 // load of the entire vector width starting at the base pointer. If we found
4963 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004964 if (LastLoadedElt == NumElems - 1) {
4965 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004966 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004967 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004968 LDBase->isVolatile(), LDBase->isNonTemporal(),
4969 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004970 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004971 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004972 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004973 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004974 }
4975 if (NumElems == 4 && LastLoadedElt == 1 &&
4976 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004977 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4978 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004979 SDValue ResNode =
4980 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4981 LDBase->getPointerInfo(),
4982 LDBase->getAlignment(),
4983 false/*isVolatile*/, true/*ReadMem*/,
4984 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00004985
4986 // Make sure the newly-created LOAD is in the same position as LDBase in
4987 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
4988 // update uses of LDBase's output chain to use the TokenFactor.
4989 if (LDBase->hasAnyUseOfValue(1)) {
4990 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
4991 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
4992 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
4993 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
4994 SDValue(ResNode.getNode(), 1));
4995 }
4996
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004997 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004998 }
4999 return SDValue();
5000}
5001
Nadav Rotem9d68b062012-04-08 12:54:54 +00005002/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5003/// to generate a splat value for the following cases:
5004/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005005/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005006/// a scalar load, or a constant.
5007/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005008/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005009SDValue
5010X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005011 if (!Subtarget->hasAVX())
5012 return SDValue();
5013
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005014 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005015 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005016
Craig Topper5da8a802012-05-04 05:49:51 +00005017 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5018 "Unsupported vector type for broadcast.");
5019
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005020 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005021 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005022
Nadav Rotem9d68b062012-04-08 12:54:54 +00005023 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005024 default:
5025 // Unknown pattern found.
5026 return SDValue();
5027
5028 case ISD::BUILD_VECTOR: {
5029 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005030 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005031 return SDValue();
5032
Nadav Rotem9d68b062012-04-08 12:54:54 +00005033 Ld = Op.getOperand(0);
5034 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5035 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005036
5037 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005039 // Constants may have multiple users.
5040 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005041 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005042 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005043 }
5044
5045 case ISD::VECTOR_SHUFFLE: {
5046 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5047
5048 // Shuffles must have a splat mask where the first element is
5049 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005050 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005051 return SDValue();
5052
5053 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005054 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005055 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5056
5057 if (!Subtarget->hasAVX2())
5058 return SDValue();
5059
5060 // Use the register form of the broadcast instruction available on AVX2.
5061 if (VT.is256BitVector())
5062 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5063 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5064 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005065
5066 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005067 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005068 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005069
5070 // The scalar_to_vector node and the suspected
5071 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005072 // Constants may have multiple users.
5073 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005074 return SDValue();
5075 break;
5076 }
5077 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005078
Craig Topper7a9a28b2012-08-12 02:23:29 +00005079 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005080
5081 // Handle the broadcasting a single constant scalar from the constant pool
5082 // into a vector. On Sandybridge it is still better to load a constant vector
5083 // from the constant pool and not to broadcast it from a scalar.
5084 if (ConstSplatVal && Subtarget->hasAVX2()) {
5085 EVT CVT = Ld.getValueType();
5086 assert(!CVT.isVector() && "Must not broadcast a vector type");
5087 unsigned ScalarSize = CVT.getSizeInBits();
5088
Craig Topper5da8a802012-05-04 05:49:51 +00005089 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005090 const Constant *C = 0;
5091 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5092 C = CI->getConstantIntValue();
5093 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5094 C = CF->getConstantFPValue();
5095
5096 assert(C && "Invalid constant type");
5097
Nadav Rotem154819d2012-04-09 07:45:58 +00005098 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005099 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005100 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005101 MachinePointerInfo::getConstantPool(),
5102 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005103
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5105 }
5106 }
5107
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005108 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005109 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5110
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005111 // Handle AVX2 in-register broadcasts.
5112 if (!IsLoad && Subtarget->hasAVX2() &&
5113 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5114 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5115
5116 // The scalar source must be a normal load.
5117 if (!IsLoad)
5118 return SDValue();
5119
Craig Topper5da8a802012-05-04 05:49:51 +00005120 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005121 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005122
Craig Toppera9376332012-01-10 08:23:59 +00005123 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005124 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005125 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005126 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005128 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005129
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005130 // Unsupported broadcast.
5131 return SDValue();
5132}
5133
Michael Liao7091b242012-08-14 21:24:47 +00005134// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5135// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5136// constraint of matching input/output vector elements.
5137SDValue
5138X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5139 DebugLoc DL = Op.getDebugLoc();
5140 SDNode *N = Op.getNode();
5141 EVT VT = Op.getValueType();
5142 unsigned NumElts = Op.getNumOperands();
5143
5144 // Check supported types and sub-targets.
5145 //
5146 // Only v2f32 -> v2f64 needs special handling.
5147 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5148 return SDValue();
5149
5150 SDValue VecIn;
5151 EVT VecInVT;
5152 SmallVector<int, 8> Mask;
5153 EVT SrcVT = MVT::Other;
5154
5155 // Check the patterns could be translated into X86vfpext.
5156 for (unsigned i = 0; i < NumElts; ++i) {
5157 SDValue In = N->getOperand(i);
5158 unsigned Opcode = In.getOpcode();
5159
5160 // Skip if the element is undefined.
5161 if (Opcode == ISD::UNDEF) {
5162 Mask.push_back(-1);
5163 continue;
5164 }
5165
5166 // Quit if one of the elements is not defined from 'fpext'.
5167 if (Opcode != ISD::FP_EXTEND)
5168 return SDValue();
5169
5170 // Check how the source of 'fpext' is defined.
5171 SDValue L2In = In.getOperand(0);
5172 EVT L2InVT = L2In.getValueType();
5173
5174 // Check the original type
5175 if (SrcVT == MVT::Other)
5176 SrcVT = L2InVT;
5177 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5178 return SDValue();
5179
5180 // Check whether the value being 'fpext'ed is extracted from the same
5181 // source.
5182 Opcode = L2In.getOpcode();
5183
5184 // Quit if it's not extracted with a constant index.
5185 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5186 !isa<ConstantSDNode>(L2In.getOperand(1)))
5187 return SDValue();
5188
5189 SDValue ExtractedFromVec = L2In.getOperand(0);
5190
5191 if (VecIn.getNode() == 0) {
5192 VecIn = ExtractedFromVec;
5193 VecInVT = ExtractedFromVec.getValueType();
5194 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5195 return SDValue();
5196
5197 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5198 }
5199
Michael Liao24438b82012-08-20 17:59:18 +00005200 // Quit if all operands of BUILD_VECTOR are undefined.
5201 if (!VecIn.getNode())
5202 return SDValue();
5203
Michael Liao7091b242012-08-14 21:24:47 +00005204 // Fill the remaining mask as undef.
5205 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5206 Mask.push_back(-1);
5207
5208 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5209 DAG.getVectorShuffle(VecInVT, DL,
5210 VecIn, DAG.getUNDEF(VecInVT),
5211 &Mask[0]));
5212}
5213
Evan Chengc3630942009-12-09 21:00:30 +00005214SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005215X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005216 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005217
David Greenef125a292011-02-08 19:04:41 +00005218 EVT VT = Op.getValueType();
5219 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005220 unsigned NumElems = Op.getNumOperands();
5221
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005222 // Vectors containing all zeros can be matched by pxor and xorps later
5223 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5224 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5225 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005226 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005227 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005229 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005230 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005232 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005233 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5234 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005235 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005236 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005237 return Op;
5238
Craig Topper07a27622012-01-22 03:07:48 +00005239 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005240 }
5241
Nadav Rotem154819d2012-04-09 07:45:58 +00005242 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005243 if (Broadcast.getNode())
5244 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005245
Michael Liao7091b242012-08-14 21:24:47 +00005246 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5247 if (FpExt.getNode())
5248 return FpExt;
5249
Owen Andersone50ed302009-08-10 22:56:29 +00005250 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005251
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252 unsigned NumZero = 0;
5253 unsigned NumNonZero = 0;
5254 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005255 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005256 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005258 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005259 if (Elt.getOpcode() == ISD::UNDEF)
5260 continue;
5261 Values.insert(Elt);
5262 if (Elt.getOpcode() != ISD::Constant &&
5263 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005264 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005265 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005266 NumZero++;
5267 else {
5268 NonZeros |= (1 << i);
5269 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 }
5271 }
5272
Chris Lattner97a2a562010-08-26 05:24:29 +00005273 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5274 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005275 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276
Chris Lattner67f453a2008-03-09 05:42:06 +00005277 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005278 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005279 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005280 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005281
Chris Lattner62098042008-03-09 01:05:04 +00005282 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5283 // the value are obviously zero, truncate the value to i32 and do the
5284 // insertion that way. Only do this if the value is non-constant or if the
5285 // value is a constant being inserted into element 0. It is cheaper to do
5286 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005288 (!IsAllConstants || Idx == 0)) {
5289 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005290 // Handle SSE only.
5291 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5292 EVT VecVT = MVT::v4i32;
5293 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005294
Chris Lattner62098042008-03-09 01:05:04 +00005295 // Truncate the value (which may itself be a constant) to i32, and
5296 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005297 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005298 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005299 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005300
Chris Lattner62098042008-03-09 01:05:04 +00005301 // Now we have our 32-bit value zero extended in the low element of
5302 // a vector. If Idx != 0, swizzle it into place.
5303 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005304 SmallVector<int, 4> Mask;
5305 Mask.push_back(Idx);
5306 for (unsigned i = 1; i != VecElts; ++i)
5307 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005308 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005310 }
Craig Topper07a27622012-01-22 03:07:48 +00005311 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005312 }
5313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005314
Chris Lattner19f79692008-03-08 22:59:52 +00005315 // If we have a constant or non-constant insertion into the low element of
5316 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5317 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005318 // depending on what the source datatype is.
5319 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005320 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005321 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005322
5323 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005324 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005325 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005326 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005327 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5328 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005329 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005330 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005331 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5332 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005333 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005334 }
5335
5336 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005337 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005338 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005339 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005340 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005341 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005342 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005343 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005344 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005345 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005346 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005347 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005348 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005349
5350 // Is it a vector logical left shift?
5351 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005352 X86::isZeroNode(Op.getOperand(0)) &&
5353 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005354 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005355 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005356 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005357 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005358 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005360
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005361 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005362 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363
Chris Lattner19f79692008-03-08 22:59:52 +00005364 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5365 // is a non-constant being inserted into an element other than the low one,
5366 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5367 // movd/movss) to move this into the low element, then shuffle it into
5368 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005369 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005370 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005371
Evan Cheng0db9fe62006-04-25 20:13:52 +00005372 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005373 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005374 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005375 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005376 MaskVec.push_back(i == Idx ? 0 : 1);
5377 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378 }
5379 }
5380
Chris Lattner67f453a2008-03-09 05:42:06 +00005381 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005382 if (Values.size() == 1) {
5383 if (EVTBits == 32) {
5384 // Instead of a shuffle like this:
5385 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5386 // Check if it's possible to issue this instead.
5387 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5388 unsigned Idx = CountTrailingZeros_32(NonZeros);
5389 SDValue Item = Op.getOperand(Idx);
5390 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5391 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5392 }
Dan Gohman475871a2008-07-27 21:46:04 +00005393 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005394 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005395
Dan Gohmana3941172007-07-24 22:55:08 +00005396 // A vector full of immediates; various special cases are already
5397 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005398 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005399 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005400
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005401 // For AVX-length vectors, build the individual 128-bit pieces and use
5402 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005403 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005404 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005405 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005406 V.push_back(Op.getOperand(i));
5407
5408 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5409
5410 // Build both the lower and upper subvector.
5411 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5412 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5413 NumElems/2);
5414
5415 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005416 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005417 }
5418
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005419 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005420 if (EVTBits == 64) {
5421 if (NumNonZero == 1) {
5422 // One half is zero or undef.
5423 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005424 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005425 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005426 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005427 }
Dan Gohman475871a2008-07-27 21:46:04 +00005428 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005429 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005430
5431 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005432 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005433 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005434 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005435 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005436 }
5437
Bill Wendling826f36f2007-03-28 00:57:11 +00005438 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005439 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005440 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005441 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 }
5443
5444 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005445 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005446 if (NumElems == 4 && NumZero > 0) {
5447 for (unsigned i = 0; i < 4; ++i) {
5448 bool isZero = !(NonZeros & (1 << i));
5449 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005450 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 else
Dale Johannesenace16102009-02-03 19:33:06 +00005452 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005453 }
5454
5455 for (unsigned i = 0; i < 2; ++i) {
5456 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5457 default: break;
5458 case 0:
5459 V[i] = V[i*2]; // Must be a zero vector.
5460 break;
5461 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005462 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463 break;
5464 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005466 break;
5467 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005468 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 break;
5470 }
5471 }
5472
Benjamin Kramer9c683542012-01-30 15:16:21 +00005473 bool Reverse1 = (NonZeros & 0x3) == 2;
5474 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5475 int MaskVec[] = {
5476 Reverse1 ? 1 : 0,
5477 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005478 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5479 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005480 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482 }
5483
Craig Topper7a9a28b2012-08-12 02:23:29 +00005484 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005485 // Check for a build vector of consecutive loads.
5486 for (unsigned i = 0; i < NumElems; ++i)
5487 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005488
Nate Begemanfdea31a2010-03-24 20:49:50 +00005489 // Check for elements which are consecutive loads.
5490 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5491 if (LD.getNode())
5492 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005493
5494 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005495 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005496 SDValue Result;
5497 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5498 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5499 else
5500 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005501
Chris Lattner24faf612010-08-28 17:59:08 +00005502 for (unsigned i = 1; i < NumElems; ++i) {
5503 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5504 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005505 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005506 }
5507 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005508 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005509
Chris Lattner6e80e442010-08-28 17:15:43 +00005510 // Otherwise, expand into a number of unpckl*, start by extending each of
5511 // our (non-undef) elements to the full vector width with the element in the
5512 // bottom slot of the vector (which generates no code for SSE).
5513 for (unsigned i = 0; i < NumElems; ++i) {
5514 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5515 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5516 else
5517 V[i] = DAG.getUNDEF(VT);
5518 }
5519
5520 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5522 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5523 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005524 unsigned EltStride = NumElems >> 1;
5525 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005526 for (unsigned i = 0; i < EltStride; ++i) {
5527 // If V[i+EltStride] is undef and this is the first round of mixing,
5528 // then it is safe to just drop this shuffle: V[i] is already in the
5529 // right place, the one element (since it's the first round) being
5530 // inserted as undef can be dropped. This isn't safe for successive
5531 // rounds because they will permute elements within both vectors.
5532 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5533 EltStride == NumElems/2)
5534 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005535
Chris Lattner6e80e442010-08-28 17:15:43 +00005536 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005537 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005538 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005539 }
5540 return V[0];
5541 }
Dan Gohman475871a2008-07-27 21:46:04 +00005542 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543}
5544
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005545// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5546// to create 256-bit vectors from two other 128-bit ones.
5547static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5548 DebugLoc dl = Op.getDebugLoc();
5549 EVT ResVT = Op.getValueType();
5550
Craig Topper7a9a28b2012-08-12 02:23:29 +00005551 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005552
5553 SDValue V1 = Op.getOperand(0);
5554 SDValue V2 = Op.getOperand(1);
5555 unsigned NumElems = ResVT.getVectorNumElements();
5556
Craig Topper4c7972d2012-04-22 18:15:59 +00005557 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005558}
5559
5560SDValue
5561X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005562 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005563
5564 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5565 // from two other 128-bit ones.
5566 return LowerAVXCONCAT_VECTORS(Op, DAG);
5567}
5568
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005569// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005570static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005571 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005572 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005573 SDValue V1 = SVOp->getOperand(0);
5574 SDValue V2 = SVOp->getOperand(1);
5575 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005576 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005577 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005578
Nadav Roteme6113782012-04-11 06:40:27 +00005579 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005580 return SDValue();
5581
Craig Topper1842ba02012-04-23 06:38:28 +00005582 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005583 MVT OpTy;
5584
Craig Topper708e44f2012-04-23 07:36:33 +00005585 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005586 default: return SDValue();
5587 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005588 ISDNo = X86ISD::BLENDPW;
5589 OpTy = MVT::v8i16;
5590 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005591 case MVT::v4i32:
5592 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005593 ISDNo = X86ISD::BLENDPS;
5594 OpTy = MVT::v4f32;
5595 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005596 case MVT::v2i64:
5597 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005598 ISDNo = X86ISD::BLENDPD;
5599 OpTy = MVT::v2f64;
5600 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005601 case MVT::v8i32:
5602 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005603 if (!Subtarget->hasAVX())
5604 return SDValue();
5605 ISDNo = X86ISD::BLENDPS;
5606 OpTy = MVT::v8f32;
5607 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005608 case MVT::v4i64:
5609 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005610 if (!Subtarget->hasAVX())
5611 return SDValue();
5612 ISDNo = X86ISD::BLENDPD;
5613 OpTy = MVT::v4f64;
5614 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005615 }
5616 assert(ISDNo && "Invalid Op Number");
5617
5618 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005619
Craig Topper1842ba02012-04-23 06:38:28 +00005620 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005621 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005622 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005623 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005624 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005625 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005626 else
5627 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005628 }
5629
Nadav Roteme6113782012-04-11 06:40:27 +00005630 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5631 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5632 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5633 DAG.getConstant(MaskVals, MVT::i32));
5634 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005635}
5636
Nate Begemanb9a47b82009-02-23 08:49:38 +00005637// v8i16 shuffles - Prefer shuffles in the following order:
5638// 1. [all] pshuflw, pshufhw, optional move
5639// 2. [ssse3] 1 x pshufb
5640// 3. [ssse3] 2 x pshufb + 1 x por
5641// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005642SDValue
5643X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5644 SelectionDAG &DAG) const {
5645 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005646 SDValue V1 = SVOp->getOperand(0);
5647 SDValue V2 = SVOp->getOperand(1);
5648 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005649 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005650
Nate Begemanb9a47b82009-02-23 08:49:38 +00005651 // Determine if more than 1 of the words in each of the low and high quadwords
5652 // of the result come from the same quadword of one of the two inputs. Undef
5653 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005654 unsigned LoQuad[] = { 0, 0, 0, 0 };
5655 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005656 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005657 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005658 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005659 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660 MaskVals.push_back(EltIdx);
5661 if (EltIdx < 0) {
5662 ++Quad[0];
5663 ++Quad[1];
5664 ++Quad[2];
5665 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005666 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005667 }
5668 ++Quad[EltIdx / 4];
5669 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005670 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005671
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005673 unsigned MaxQuad = 1;
5674 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 if (LoQuad[i] > MaxQuad) {
5676 BestLoQuad = i;
5677 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005678 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005679 }
5680
Nate Begemanb9a47b82009-02-23 08:49:38 +00005681 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005682 MaxQuad = 1;
5683 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005684 if (HiQuad[i] > MaxQuad) {
5685 BestHiQuad = i;
5686 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005687 }
5688 }
5689
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005691 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005692 // single pshufb instruction is necessary. If There are more than 2 input
5693 // quads, disable the next transformation since it does not help SSSE3.
5694 bool V1Used = InputQuads[0] || InputQuads[1];
5695 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005696 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005698 BestLoQuad = InputQuads[0] ? 0 : 1;
5699 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 }
5701 if (InputQuads.count() > 2) {
5702 BestLoQuad = -1;
5703 BestHiQuad = -1;
5704 }
5705 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005706
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5708 // the shuffle mask. If a quad is scored as -1, that means that it contains
5709 // words from all 4 input quadwords.
5710 SDValue NewV;
5711 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005712 int MaskV[] = {
5713 BestLoQuad < 0 ? 0 : BestLoQuad,
5714 BestHiQuad < 0 ? 1 : BestHiQuad
5715 };
Eric Christopherfd179292009-08-27 18:07:15 +00005716 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005717 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5718 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5719 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005720
Nate Begemanb9a47b82009-02-23 08:49:38 +00005721 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5722 // source words for the shuffle, to aid later transformations.
5723 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005724 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005725 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005727 if (idx != (int)i)
5728 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005729 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005730 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 AllWordsInNewV = false;
5732 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005733 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005734
Nate Begemanb9a47b82009-02-23 08:49:38 +00005735 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5736 if (AllWordsInNewV) {
5737 for (int i = 0; i != 8; ++i) {
5738 int idx = MaskVals[i];
5739 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005740 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005741 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742 if ((idx != i) && idx < 4)
5743 pshufhw = false;
5744 if ((idx != i) && idx > 3)
5745 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005746 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 V1 = NewV;
5748 V2Used = false;
5749 BestLoQuad = 0;
5750 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005751 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005752
Nate Begemanb9a47b82009-02-23 08:49:38 +00005753 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5754 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005755 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005756 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5757 unsigned TargetMask = 0;
5758 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005759 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005760 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5761 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5762 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005763 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005764 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005765 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005766 }
Eric Christopherfd179292009-08-27 18:07:15 +00005767
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 // If we have SSSE3, and all words of the result are from 1 input vector,
5769 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5770 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005771 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005773
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005775 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 // mask, and elements that come from V1 in the V2 mask, so that the two
5777 // results can be OR'd together.
5778 bool TwoInputs = V1Used && V2Used;
5779 for (unsigned i = 0; i != 8; ++i) {
5780 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005781 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5782 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5783 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5784 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005786 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005787 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005788 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005791 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005792
Nate Begemanb9a47b82009-02-23 08:49:38 +00005793 // Calculate the shuffle mask for the second input, shuffle it, and
5794 // OR it with the first shuffled input.
5795 pshufbMask.clear();
5796 for (unsigned i = 0; i != 8; ++i) {
5797 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005798 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5799 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5800 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5801 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005803 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005804 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005805 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 MVT::v16i8, &pshufbMask[0], 16));
5807 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005808 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005809 }
5810
5811 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5812 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005813 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005815 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 for (int i = 0; i != 4; ++i) {
5817 int idx = MaskVals[i];
5818 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 InOrder.set(i);
5820 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005821 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005822 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005823 }
5824 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005826 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005827
Craig Topperdd637ae2012-02-19 05:41:45 +00005828 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5829 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005830 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005831 NewV.getOperand(0),
5832 getShufflePSHUFLWImmediate(SVOp), DAG);
5833 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 }
Eric Christopherfd179292009-08-27 18:07:15 +00005835
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5837 // and update MaskVals with the new element order.
5838 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005839 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 for (unsigned i = 4; i != 8; ++i) {
5841 int idx = MaskVals[i];
5842 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 InOrder.set(i);
5844 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005845 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 }
5848 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005850 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005851
Craig Topperdd637ae2012-02-19 05:41:45 +00005852 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5853 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005854 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005855 NewV.getOperand(0),
5856 getShufflePSHUFHWImmediate(SVOp), DAG);
5857 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 }
Eric Christopherfd179292009-08-27 18:07:15 +00005859
Nate Begemanb9a47b82009-02-23 08:49:38 +00005860 // In case BestHi & BestLo were both -1, which means each quadword has a word
5861 // from each of the four input quadwords, calculate the InOrder bitvector now
5862 // before falling through to the insert/extract cleanup.
5863 if (BestLoQuad == -1 && BestHiQuad == -1) {
5864 NewV = V1;
5865 for (int i = 0; i != 8; ++i)
5866 if (MaskVals[i] < 0 || MaskVals[i] == i)
5867 InOrder.set(i);
5868 }
Eric Christopherfd179292009-08-27 18:07:15 +00005869
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 // The other elements are put in the right place using pextrw and pinsrw.
5871 for (unsigned i = 0; i != 8; ++i) {
5872 if (InOrder[i])
5873 continue;
5874 int EltIdx = MaskVals[i];
5875 if (EltIdx < 0)
5876 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005877 SDValue ExtOp = (EltIdx < 8) ?
5878 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5879 DAG.getIntPtrConstant(EltIdx)) :
5880 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 DAG.getIntPtrConstant(i));
5884 }
5885 return NewV;
5886}
5887
5888// v16i8 shuffles - Prefer shuffles in the following order:
5889// 1. [ssse3] 1 x pshufb
5890// 2. [ssse3] 2 x pshufb + 1 x por
5891// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5892static
Nate Begeman9008ca62009-04-27 18:41:29 +00005893SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005894 SelectionDAG &DAG,
5895 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005896 SDValue V1 = SVOp->getOperand(0);
5897 SDValue V2 = SVOp->getOperand(1);
5898 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005899 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005900
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005902 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005904
Nate Begemanb9a47b82009-02-23 08:49:38 +00005905 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005906 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005907 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005908
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005910 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 //
5912 // Otherwise, we have elements from both input vectors, and must zero out
5913 // elements that come from V2 in the first mask, and V1 in the second mask
5914 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 for (unsigned i = 0; i != 16; ++i) {
5916 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005917 if (EltIdx < 0 || EltIdx >= 16)
5918 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005922 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005923 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005924
5925 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5926 // the 2nd operand if it's undefined or zero.
5927 if (V2.getOpcode() == ISD::UNDEF ||
5928 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005930
Nate Begemanb9a47b82009-02-23 08:49:38 +00005931 // Calculate the shuffle mask for the second input, shuffle it, and
5932 // OR it with the first shuffled input.
5933 pshufbMask.clear();
5934 for (unsigned i = 0; i != 16; ++i) {
5935 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005936 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005937 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005940 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005941 MVT::v16i8, &pshufbMask[0], 16));
5942 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 }
Eric Christopherfd179292009-08-27 18:07:15 +00005944
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 // No SSSE3 - Calculate in place words and then fix all out of place words
5946 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5947 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005948 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5949 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005950 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 for (int i = 0; i != 8; ++i) {
5952 int Elt0 = MaskVals[i*2];
5953 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 // This word of the result is all undef, skip it.
5956 if (Elt0 < 0 && Elt1 < 0)
5957 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005958
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005960 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005961 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005962
Nate Begemanb9a47b82009-02-23 08:49:38 +00005963 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5964 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5965 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005966
5967 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5968 // using a single extract together, load it and store it.
5969 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005970 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005971 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005972 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005973 DAG.getIntPtrConstant(i));
5974 continue;
5975 }
5976
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005978 // source byte is not also odd, shift the extracted word left 8 bits
5979 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005981 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 DAG.getIntPtrConstant(Elt1 / 2));
5983 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005984 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005985 DAG.getConstant(8,
5986 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005987 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005988 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5989 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005990 }
5991 // If Elt0 is defined, extract it from the appropriate source. If the
5992 // source byte is not also even, shift the extracted word right 8 bits. If
5993 // Elt1 was also defined, OR the extracted values together before
5994 // inserting them in the result.
5995 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005996 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005997 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5998 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006000 DAG.getConstant(8,
6001 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006002 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6004 DAG.getConstant(0x00FF, MVT::i16));
6005 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006006 : InsElt0;
6007 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006009 DAG.getIntPtrConstant(i));
6010 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006011 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006012}
6013
Elena Demikhovsky41789462012-09-06 12:42:01 +00006014// v32i8 shuffles - Translate to VPSHUFB if possible.
6015static
6016SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
6017 SelectionDAG &DAG,
6018 const X86TargetLowering &TLI) {
6019 EVT VT = SVOp->getValueType(0);
6020 SDValue V1 = SVOp->getOperand(0);
6021 SDValue V2 = SVOp->getOperand(1);
6022 DebugLoc dl = SVOp->getDebugLoc();
6023 ArrayRef<int> MaskVals = SVOp->getMask();
6024
6025 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
6026
6027 if (VT != MVT::v32i8 || !TLI.getSubtarget()->hasAVX2() || !V2IsUndef)
6028 return SDValue();
6029
6030 SmallVector<SDValue,32> pshufbMask;
6031 for (unsigned i = 0; i != 32; i++) {
6032 int EltIdx = MaskVals[i];
6033 if (EltIdx < 0 || EltIdx >= 32)
6034 EltIdx = 0x80;
6035 else {
6036 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6037 // Cross lane is not allowed.
6038 return SDValue();
6039 EltIdx &= 0xf;
6040 }
6041 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6042 }
6043 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6044 DAG.getNode(ISD::BUILD_VECTOR, dl,
6045 MVT::v32i8, &pshufbMask[0], 32));
6046}
6047
Evan Cheng7a831ce2007-12-15 03:00:47 +00006048/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006049/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006050/// done when every pair / quad of shuffle mask elements point to elements in
6051/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006052/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006053static
Nate Begeman9008ca62009-04-27 18:41:29 +00006054SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006055 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006056 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006057 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006058 MVT NewVT;
6059 unsigned Scale;
6060 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006061 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006062 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6063 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6064 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6065 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6066 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6067 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006068 }
6069
Nate Begeman9008ca62009-04-27 18:41:29 +00006070 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006071 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006072 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006073 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006074 int EltIdx = SVOp->getMaskElt(i+j);
6075 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006076 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006077 if (StartIdx < 0)
6078 StartIdx = (EltIdx / Scale);
6079 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006080 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006081 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006082 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006083 }
6084
Craig Topper11ac1f82012-05-04 04:08:44 +00006085 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6086 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006087 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006088}
6089
Evan Chengd880b972008-05-09 21:53:03 +00006090/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006091///
Owen Andersone50ed302009-08-10 22:56:29 +00006092static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 SDValue SrcOp, SelectionDAG &DAG,
6094 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006095 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006096 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006097 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006098 LD = dyn_cast<LoadSDNode>(SrcOp);
6099 if (!LD) {
6100 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6101 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006102 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006103 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006104 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006105 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006106 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006107 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006108 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006109 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006110 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6111 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6112 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006113 SrcOp.getOperand(0)
6114 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006115 }
6116 }
6117 }
6118
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006119 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006120 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006121 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006122 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006123}
6124
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006125/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6126/// which could not be matched by any known target speficic shuffle
6127static SDValue
6128LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006129
6130 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6131 if (NewOp.getNode())
6132 return NewOp;
6133
Craig Topper8f35c132012-01-20 09:29:03 +00006134 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006135
Craig Topper8f35c132012-01-20 09:29:03 +00006136 unsigned NumElems = VT.getVectorNumElements();
6137 unsigned NumLaneElems = NumElems / 2;
6138
Craig Topper8f35c132012-01-20 09:29:03 +00006139 DebugLoc dl = SVOp->getDebugLoc();
6140 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006141 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006142 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006143
Craig Topper9a2b6e12012-04-06 07:45:23 +00006144 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006145 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006146 // Build a shuffle mask for the output, discovering on the fly which
6147 // input vectors to use as shuffle operands (recorded in InputUsed).
6148 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006149 // out with UseBuildVector set.
6150 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006151 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006152 unsigned LaneStart = l * NumLaneElems;
6153 for (unsigned i = 0; i != NumLaneElems; ++i) {
6154 // The mask element. This indexes into the input.
6155 int Idx = SVOp->getMaskElt(i+LaneStart);
6156 if (Idx < 0) {
6157 // the mask element does not index into any input vector.
6158 Mask.push_back(-1);
6159 continue;
6160 }
Craig Topper8f35c132012-01-20 09:29:03 +00006161
Craig Topper9a2b6e12012-04-06 07:45:23 +00006162 // The input vector this mask element indexes into.
6163 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006164
Craig Topper9a2b6e12012-04-06 07:45:23 +00006165 // Turn the index into an offset from the start of the input vector.
6166 Idx -= Input * NumLaneElems;
6167
6168 // Find or create a shuffle vector operand to hold this input.
6169 unsigned OpNo;
6170 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6171 if (InputUsed[OpNo] == Input)
6172 // This input vector is already an operand.
6173 break;
6174 if (InputUsed[OpNo] < 0) {
6175 // Create a new operand for this input vector.
6176 InputUsed[OpNo] = Input;
6177 break;
6178 }
6179 }
6180
6181 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006182 // More than two input vectors used! Give up on trying to create a
6183 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6184 UseBuildVector = true;
6185 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006186 }
6187
6188 // Add the mask index for the new shuffle vector.
6189 Mask.push_back(Idx + OpNo * NumLaneElems);
6190 }
6191
Craig Topper8ae97ba2012-05-21 06:40:16 +00006192 if (UseBuildVector) {
6193 SmallVector<SDValue, 16> SVOps;
6194 for (unsigned i = 0; i != NumLaneElems; ++i) {
6195 // The mask element. This indexes into the input.
6196 int Idx = SVOp->getMaskElt(i+LaneStart);
6197 if (Idx < 0) {
6198 SVOps.push_back(DAG.getUNDEF(EltVT));
6199 continue;
6200 }
6201
6202 // The input vector this mask element indexes into.
6203 int Input = Idx / NumElems;
6204
6205 // Turn the index into an offset from the start of the input vector.
6206 Idx -= Input * NumElems;
6207
6208 // Extract the vector element by hand.
6209 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6210 SVOp->getOperand(Input),
6211 DAG.getIntPtrConstant(Idx)));
6212 }
6213
6214 // Construct the output using a BUILD_VECTOR.
6215 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6216 SVOps.size());
6217 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006218 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006219 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006220 } else {
6221 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006222 (InputUsed[0] % 2) * NumLaneElems,
6223 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006224 // If only one input was used, use an undefined vector for the other.
6225 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6226 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006227 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006228 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006229 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006230 }
6231
6232 Mask.clear();
6233 }
Craig Topper8f35c132012-01-20 09:29:03 +00006234
6235 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006236 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006237}
6238
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006239/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6240/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006241static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006242LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006243 SDValue V1 = SVOp->getOperand(0);
6244 SDValue V2 = SVOp->getOperand(1);
6245 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006246 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006247
Craig Topper7a9a28b2012-08-12 02:23:29 +00006248 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006249
Benjamin Kramer9c683542012-01-30 15:16:21 +00006250 std::pair<int, int> Locs[4];
6251 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006252 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006253
Evan Chengace3c172008-07-22 21:13:36 +00006254 unsigned NumHi = 0;
6255 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006256 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006257 int Idx = PermMask[i];
6258 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006259 Locs[i] = std::make_pair(-1, -1);
6260 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006261 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6262 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006263 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006264 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006265 NumLo++;
6266 } else {
6267 Locs[i] = std::make_pair(1, NumHi);
6268 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006269 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006270 NumHi++;
6271 }
6272 }
6273 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006274
Evan Chengace3c172008-07-22 21:13:36 +00006275 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006276 // If no more than two elements come from either vector. This can be
6277 // implemented with two shuffles. First shuffle gather the elements.
6278 // The second shuffle, which takes the first shuffle as both of its
6279 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006280 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006281
Benjamin Kramer9c683542012-01-30 15:16:21 +00006282 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006283
Benjamin Kramer9c683542012-01-30 15:16:21 +00006284 for (unsigned i = 0; i != 4; ++i)
6285 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006286 unsigned Idx = (i < 2) ? 0 : 4;
6287 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006288 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006289 }
Evan Chengace3c172008-07-22 21:13:36 +00006290
Nate Begeman9008ca62009-04-27 18:41:29 +00006291 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006292 }
6293
6294 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006295 // Otherwise, we must have three elements from one vector, call it X, and
6296 // one element from the other, call it Y. First, use a shufps to build an
6297 // intermediate vector with the one element from Y and the element from X
6298 // that will be in the same half in the final destination (the indexes don't
6299 // matter). Then, use a shufps to build the final vector, taking the half
6300 // containing the element from Y from the intermediate, and the other half
6301 // from X.
6302 if (NumHi == 3) {
6303 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006304 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006305 std::swap(V1, V2);
6306 }
6307
6308 // Find the element from V2.
6309 unsigned HiIndex;
6310 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006311 int Val = PermMask[HiIndex];
6312 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006313 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006314 if (Val >= 4)
6315 break;
6316 }
6317
Nate Begeman9008ca62009-04-27 18:41:29 +00006318 Mask1[0] = PermMask[HiIndex];
6319 Mask1[1] = -1;
6320 Mask1[2] = PermMask[HiIndex^1];
6321 Mask1[3] = -1;
6322 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006323
6324 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006325 Mask1[0] = PermMask[0];
6326 Mask1[1] = PermMask[1];
6327 Mask1[2] = HiIndex & 1 ? 6 : 4;
6328 Mask1[3] = HiIndex & 1 ? 4 : 6;
6329 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006330 }
Craig Topper69947b92012-04-23 06:57:04 +00006331
6332 Mask1[0] = HiIndex & 1 ? 2 : 0;
6333 Mask1[1] = HiIndex & 1 ? 0 : 2;
6334 Mask1[2] = PermMask[2];
6335 Mask1[3] = PermMask[3];
6336 if (Mask1[2] >= 0)
6337 Mask1[2] += 4;
6338 if (Mask1[3] >= 0)
6339 Mask1[3] += 4;
6340 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006341 }
6342
6343 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006344 int LoMask[] = { -1, -1, -1, -1 };
6345 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006346
Benjamin Kramer9c683542012-01-30 15:16:21 +00006347 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006348 unsigned MaskIdx = 0;
6349 unsigned LoIdx = 0;
6350 unsigned HiIdx = 2;
6351 for (unsigned i = 0; i != 4; ++i) {
6352 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006353 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006354 MaskIdx = 1;
6355 LoIdx = 0;
6356 HiIdx = 2;
6357 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006358 int Idx = PermMask[i];
6359 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006360 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006361 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006362 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006363 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006364 LoIdx++;
6365 } else {
6366 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006367 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006368 HiIdx++;
6369 }
6370 }
6371
Nate Begeman9008ca62009-04-27 18:41:29 +00006372 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6373 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006374 int MaskOps[] = { -1, -1, -1, -1 };
6375 for (unsigned i = 0; i != 4; ++i)
6376 if (Locs[i].first != -1)
6377 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006378 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006379}
6380
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006381static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006382 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006383 V = V.getOperand(0);
6384 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6385 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006386 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6387 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6388 // BUILD_VECTOR (load), undef
6389 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006390 if (MayFoldLoad(V))
6391 return true;
6392 return false;
6393}
6394
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006395// FIXME: the version above should always be used. Since there's
6396// a bug where several vector shuffles can't be folded because the
6397// DAG is not updated during lowering and a node claims to have two
6398// uses while it only has one, use this version, and let isel match
6399// another instruction if the load really happens to have more than
6400// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006401// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006402static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006403 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006404 V = V.getOperand(0);
6405 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6406 V = V.getOperand(0);
6407 if (ISD::isNormalLoad(V.getNode()))
6408 return true;
6409 return false;
6410}
6411
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006412static
Evan Cheng835580f2010-10-07 20:50:20 +00006413SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6414 EVT VT = Op.getValueType();
6415
6416 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006417 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6418 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006419 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6420 V1, DAG));
6421}
6422
6423static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006424SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006425 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006426 SDValue V1 = Op.getOperand(0);
6427 SDValue V2 = Op.getOperand(1);
6428 EVT VT = Op.getValueType();
6429
6430 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6431
Craig Topper1accb7e2012-01-10 06:54:16 +00006432 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006433 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6434
Evan Cheng0899f5c2011-08-31 02:05:24 +00006435 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6436 return DAG.getNode(ISD::BITCAST, dl, VT,
6437 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6438 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6439 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006440}
6441
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006442static
6443SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6444 SDValue V1 = Op.getOperand(0);
6445 SDValue V2 = Op.getOperand(1);
6446 EVT VT = Op.getValueType();
6447
6448 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6449 "unsupported shuffle type");
6450
6451 if (V2.getOpcode() == ISD::UNDEF)
6452 V2 = V1;
6453
6454 // v4i32 or v4f32
6455 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6456}
6457
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006458static
Craig Topper1accb7e2012-01-10 06:54:16 +00006459SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006460 SDValue V1 = Op.getOperand(0);
6461 SDValue V2 = Op.getOperand(1);
6462 EVT VT = Op.getValueType();
6463 unsigned NumElems = VT.getVectorNumElements();
6464
6465 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6466 // operand of these instructions is only memory, so check if there's a
6467 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6468 // same masks.
6469 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006470
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006471 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006472 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006473 CanFoldLoad = true;
6474
6475 // When V1 is a load, it can be folded later into a store in isel, example:
6476 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6477 // turns into:
6478 // (MOVLPSmr addr:$src1, VR128:$src2)
6479 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006480 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006481 CanFoldLoad = true;
6482
Dan Gohman65fd6562011-11-03 21:49:52 +00006483 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006484 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006485 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006486 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6487
6488 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006489 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006490 if (SVOp->getMaskElt(1) != -1)
6491 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006492 }
6493
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006494 // movl and movlp will both match v2i64, but v2i64 is never matched by
6495 // movl earlier because we make it strict to avoid messing with the movlp load
6496 // folding logic (see the code above getMOVLP call). Match it here then,
6497 // this is horrible, but will stay like this until we move all shuffle
6498 // matching to x86 specific nodes. Note that for the 1st condition all
6499 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006500 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006501 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6502 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006503 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006504 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006505 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006506 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006507
6508 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6509
6510 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006511 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006512 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006513}
6514
Nadav Rotem154819d2012-04-09 07:45:58 +00006515SDValue
6516X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006517 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6518 EVT VT = Op.getValueType();
6519 DebugLoc dl = Op.getDebugLoc();
6520 SDValue V1 = Op.getOperand(0);
6521 SDValue V2 = Op.getOperand(1);
6522
6523 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006524 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006525
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006526 // Handle splat operations
6527 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006528 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006529 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006530
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006531 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006532 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006533 if (Broadcast.getNode())
6534 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006535
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006536 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006537 if ((Size == 128 && NumElem <= 4) ||
6538 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006539 return SDValue();
6540
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006541 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006542 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006543 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006544
6545 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6546 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006547 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6548 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006549 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6550 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006551 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006552 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006553 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006554 // FIXME: Figure out a cleaner way to do this.
6555 // Try to make use of movq to zero out the top part.
6556 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6557 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6558 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006559 EVT NewVT = NewOp.getValueType();
6560 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6561 NewVT, true, false))
6562 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006563 DAG, Subtarget, dl);
6564 }
6565 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6566 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006567 if (NewOp.getNode()) {
6568 EVT NewVT = NewOp.getValueType();
6569 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6570 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6571 DAG, Subtarget, dl);
6572 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006573 }
6574 }
6575 return SDValue();
6576}
6577
Dan Gohman475871a2008-07-27 21:46:04 +00006578SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006579X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006580 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006581 SDValue V1 = Op.getOperand(0);
6582 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006583 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006584 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006585 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006586 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006587 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006588 bool V1IsSplat = false;
6589 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006590 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006591 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006592 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006593 MachineFunction &MF = DAG.getMachineFunction();
6594 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006595
Craig Topper3426a3e2011-11-14 06:46:21 +00006596 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006597
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006598 if (V1IsUndef && V2IsUndef)
6599 return DAG.getUNDEF(VT);
6600
6601 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006602
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006603 // Vector shuffle lowering takes 3 steps:
6604 //
6605 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6606 // narrowing and commutation of operands should be handled.
6607 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6608 // shuffle nodes.
6609 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6610 // so the shuffle can be broken into other shuffles and the legalizer can
6611 // try the lowering again.
6612 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006613 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006614 // be matched during isel, all of them must be converted to a target specific
6615 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006616
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006617 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6618 // narrowing and commutation of operands should be handled. The actual code
6619 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006620 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006621 if (NewOp.getNode())
6622 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006623
Craig Topper5aaffa82012-02-19 02:53:47 +00006624 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6625
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006626 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6627 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006628 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006629 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006630 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006631 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006632
Craig Topperdd637ae2012-02-19 05:41:45 +00006633 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006634 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006635 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006636
Craig Topperdd637ae2012-02-19 05:41:45 +00006637 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006638 return getMOVHighToLow(Op, dl, DAG);
6639
6640 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006641 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006642 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006643 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006644
Craig Topper5aaffa82012-02-19 02:53:47 +00006645 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006646 // The actual implementation will match the mask in the if above and then
6647 // during isel it can match several different instructions, not only pshufd
6648 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006649 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6650 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006651
Craig Topper5aaffa82012-02-19 02:53:47 +00006652 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006653
Craig Topperdbd98a42012-02-07 06:28:42 +00006654 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6655 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6656
Craig Topper1accb7e2012-01-10 06:54:16 +00006657 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006658 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6659
Craig Topperb3982da2011-12-31 23:50:21 +00006660 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006661 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006662 }
Eric Christopherfd179292009-08-27 18:07:15 +00006663
Evan Chengf26ffe92008-05-29 08:22:04 +00006664 // Check if this can be converted into a logical shift.
6665 bool isLeft = false;
6666 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006667 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006668 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006669 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006670 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006671 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006672 EVT EltVT = VT.getVectorElementType();
6673 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006674 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006675 }
Eric Christopherfd179292009-08-27 18:07:15 +00006676
Craig Topper5aaffa82012-02-19 02:53:47 +00006677 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006678 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006679 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006680 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006681 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006682 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6683
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006684 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006685 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6686 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006687 }
Eric Christopherfd179292009-08-27 18:07:15 +00006688
Nate Begeman9008ca62009-04-27 18:41:29 +00006689 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006690 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006691 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006692
Craig Topperdd637ae2012-02-19 05:41:45 +00006693 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006694 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006695
Craig Topperdd637ae2012-02-19 05:41:45 +00006696 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006697 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006698
Craig Topperdd637ae2012-02-19 05:41:45 +00006699 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006700 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006701
Craig Topperdd637ae2012-02-19 05:41:45 +00006702 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006703 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006704
Craig Topperdd637ae2012-02-19 05:41:45 +00006705 if (ShouldXformToMOVHLPS(M, VT) ||
6706 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006707 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006708
Evan Chengf26ffe92008-05-29 08:22:04 +00006709 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006710 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006711 EVT EltVT = VT.getVectorElementType();
6712 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006713 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006714 }
Eric Christopherfd179292009-08-27 18:07:15 +00006715
Evan Cheng9eca5e82006-10-25 21:49:50 +00006716 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006717 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6718 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006719 V1IsSplat = isSplatVector(V1.getNode());
6720 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006721
Chris Lattner8a594482007-11-25 00:24:49 +00006722 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006723 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6724 CommuteVectorShuffleMask(M, NumElems);
6725 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006726 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006727 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006728 }
6729
Craig Topperbeabc6c2011-12-05 06:56:46 +00006730 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006731 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006732 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006733 return V1;
6734 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6735 // the instruction selector will not match, so get a canonical MOVL with
6736 // swapped operands to undo the commute.
6737 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006738 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006739
Craig Topperbeabc6c2011-12-05 06:56:46 +00006740 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006741 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006742
Craig Topperbeabc6c2011-12-05 06:56:46 +00006743 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006744 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006745
Evan Cheng9bbbb982006-10-25 20:48:19 +00006746 if (V2IsSplat) {
6747 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006748 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006749 // new vector_shuffle with the corrected mask.p
6750 SmallVector<int, 8> NewMask(M.begin(), M.end());
6751 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006752 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006753 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006754 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006755 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006756 }
6757
Evan Cheng9eca5e82006-10-25 21:49:50 +00006758 if (Commuted) {
6759 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006760 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006761 CommuteVectorShuffleMask(M, NumElems);
6762 std::swap(V1, V2);
6763 std::swap(V1IsSplat, V2IsSplat);
6764 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006765
Craig Topper39a9e482012-02-11 06:24:48 +00006766 if (isUNPCKLMask(M, VT, HasAVX2))
6767 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006768
Craig Topper39a9e482012-02-11 06:24:48 +00006769 if (isUNPCKHMask(M, VT, HasAVX2))
6770 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006771 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006772
Nate Begeman9008ca62009-04-27 18:41:29 +00006773 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006774 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006775 return CommuteVectorShuffle(SVOp, DAG);
6776
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006777 // The checks below are all present in isShuffleMaskLegal, but they are
6778 // inlined here right now to enable us to directly emit target specific
6779 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006780
Craig Topper0e2037b2012-01-20 05:53:00 +00006781 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006782 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006783 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006784 DAG);
6785
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006786 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6787 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006788 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006789 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006790 }
6791
Craig Toppera9a568a2012-05-02 08:03:44 +00006792 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006793 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006794 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006795 DAG);
6796
Craig Toppera9a568a2012-05-02 08:03:44 +00006797 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006798 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006799 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006800 DAG);
6801
Craig Topper1a7700a2012-01-19 08:19:12 +00006802 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006803 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006804 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006805
Craig Topper94438ba2011-12-16 08:06:31 +00006806 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006807 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006808 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006809 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006810
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006811 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006812 // Generate target specific nodes for 128 or 256-bit shuffles only
6813 // supported in the AVX instruction set.
6814 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006815
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006816 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006817 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006818 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6819
Craig Topper70b883b2011-11-28 10:14:51 +00006820 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006821 if (isVPERMILPMask(M, VT, HasAVX)) {
6822 if (HasAVX2 && VT == MVT::v8i32)
6823 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006824 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006825 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006826 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006827 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006828
Craig Topper70b883b2011-11-28 10:14:51 +00006829 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006830 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006831 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006832 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006833
Craig Topper1842ba02012-04-23 06:38:28 +00006834 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006835 if (BlendOp.getNode())
6836 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006837
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006838 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006839 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006840 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006841 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006842 }
Craig Topper92040742012-04-16 06:43:40 +00006843 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6844 &permclMask[0], 8);
6845 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006846 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006847 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006848 }
Craig Topper095c5282012-04-15 23:48:57 +00006849
Craig Topper8325c112012-04-16 00:41:45 +00006850 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6851 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006852 getShuffleCLImmediate(SVOp), DAG);
6853
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006854
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006855 //===--------------------------------------------------------------------===//
6856 // Since no target specific shuffle was selected for this generic one,
6857 // lower it into other known shuffles. FIXME: this isn't true yet, but
6858 // this is the plan.
6859 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006860
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006861 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6862 if (VT == MVT::v8i16) {
6863 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6864 if (NewOp.getNode())
6865 return NewOp;
6866 }
6867
6868 if (VT == MVT::v16i8) {
6869 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6870 if (NewOp.getNode())
6871 return NewOp;
6872 }
6873
Elena Demikhovsky41789462012-09-06 12:42:01 +00006874 if (VT == MVT::v32i8) {
6875 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, DAG, *this);
6876 if (NewOp.getNode())
6877 return NewOp;
6878 }
6879
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006880 // Handle all 128-bit wide vectors with 4 elements, and match them with
6881 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006882 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006883 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6884
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006885 // Handle general 256-bit shuffles
6886 if (VT.is256BitVector())
6887 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6888
Dan Gohman475871a2008-07-27 21:46:04 +00006889 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006890}
6891
Dan Gohman475871a2008-07-27 21:46:04 +00006892SDValue
6893X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006894 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006895 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006896 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006897
Craig Topper7a9a28b2012-08-12 02:23:29 +00006898 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006899 return SDValue();
6900
Duncan Sands83ec4b62008-06-06 12:08:01 +00006901 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006903 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006905 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006906 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006907 }
6908
6909 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006910 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6911 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6912 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6914 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006915 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006916 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006917 Op.getOperand(0)),
6918 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006919 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006920 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006922 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006923 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006924 }
6925
6926 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006927 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6928 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006929 // result has a single use which is a store or a bitcast to i32. And in
6930 // the case of a store, it's not worth it if the index is a constant 0,
6931 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006932 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006933 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006934 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006935 if ((User->getOpcode() != ISD::STORE ||
6936 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6937 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006938 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006939 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006940 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006941 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006942 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006943 Op.getOperand(0)),
6944 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006945 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006946 }
6947
6948 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006949 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006950 if (isa<ConstantSDNode>(Op.getOperand(1)))
6951 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006952 }
Dan Gohman475871a2008-07-27 21:46:04 +00006953 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006954}
6955
6956
Dan Gohman475871a2008-07-27 21:46:04 +00006957SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006958X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6959 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006960 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006961 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006962
David Greene74a579d2011-02-10 16:57:36 +00006963 SDValue Vec = Op.getOperand(0);
6964 EVT VecVT = Vec.getValueType();
6965
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006966 // If this is a 256-bit vector result, first extract the 128-bit vector and
6967 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006968 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006969 DebugLoc dl = Op.getNode()->getDebugLoc();
6970 unsigned NumElems = VecVT.getVectorNumElements();
6971 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006972 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6973
6974 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006975 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006976
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006977 if (IdxVal >= NumElems/2)
6978 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006979 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006980 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006981 }
6982
Craig Topper7a9a28b2012-08-12 02:23:29 +00006983 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006984
Craig Topperd0a31172012-01-10 06:37:29 +00006985 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006986 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006987 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006988 return Res;
6989 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006990
Owen Andersone50ed302009-08-10 22:56:29 +00006991 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006992 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006993 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006994 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006995 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006996 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006997 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6999 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007000 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007001 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007002 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007003 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007004 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007005 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007006 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007007 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007008 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007009 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007010 }
7011
7012 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007013 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 if (Idx == 0)
7015 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007016
Evan Cheng0db9fe62006-04-25 20:13:52 +00007017 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007018 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007019 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007020 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007021 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007022 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007023 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007024 }
7025
7026 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007027 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7028 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7029 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007030 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007031 if (Idx == 0)
7032 return Op;
7033
7034 // UNPCKHPD the element to the lowest double word, then movsd.
7035 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7036 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007037 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007038 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007039 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007040 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007041 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007042 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007043 }
7044
Dan Gohman475871a2008-07-27 21:46:04 +00007045 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007046}
7047
Dan Gohman475871a2008-07-27 21:46:04 +00007048SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007049X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7050 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007051 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007052 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007053 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007054
Dan Gohman475871a2008-07-27 21:46:04 +00007055 SDValue N0 = Op.getOperand(0);
7056 SDValue N1 = Op.getOperand(1);
7057 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007058
Craig Topper7a9a28b2012-08-12 02:23:29 +00007059 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007060 return SDValue();
7061
Dan Gohman8a55ce42009-09-23 21:02:20 +00007062 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007063 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007064 unsigned Opc;
7065 if (VT == MVT::v8i16)
7066 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007067 else if (VT == MVT::v16i8)
7068 Opc = X86ISD::PINSRB;
7069 else
7070 Opc = X86ISD::PINSRB;
7071
Nate Begeman14d12ca2008-02-11 04:19:36 +00007072 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7073 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007074 if (N1.getValueType() != MVT::i32)
7075 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7076 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007077 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007078 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007079 }
7080
7081 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007082 // Bits [7:6] of the constant are the source select. This will always be
7083 // zero here. The DAG Combiner may combine an extract_elt index into these
7084 // bits. For example (insert (extract, 3), 2) could be matched by putting
7085 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007086 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007087 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007088 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007089 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007090 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007091 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007093 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007094 }
7095
7096 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007097 // PINSR* works with constant index.
7098 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007099 }
Dan Gohman475871a2008-07-27 21:46:04 +00007100 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007101}
7102
Dan Gohman475871a2008-07-27 21:46:04 +00007103SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007104X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007105 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007106 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007107
David Greene6b381262011-02-09 15:32:06 +00007108 DebugLoc dl = Op.getDebugLoc();
7109 SDValue N0 = Op.getOperand(0);
7110 SDValue N1 = Op.getOperand(1);
7111 SDValue N2 = Op.getOperand(2);
7112
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007113 // If this is a 256-bit vector result, first extract the 128-bit vector,
7114 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007115 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007116 if (!isa<ConstantSDNode>(N2))
7117 return SDValue();
7118
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007119 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007120 unsigned NumElems = VT.getVectorNumElements();
7121 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007122 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007123
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007124 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007125 bool Upper = IdxVal >= NumElems/2;
7126 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7127 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007128
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007129 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007130 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007131 }
7132
Craig Topperd0a31172012-01-10 06:37:29 +00007133 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007134 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7135
Dan Gohman8a55ce42009-09-23 21:02:20 +00007136 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007137 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007138
Dan Gohman8a55ce42009-09-23 21:02:20 +00007139 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007140 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7141 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007142 if (N1.getValueType() != MVT::i32)
7143 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7144 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007145 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007146 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007147 }
Dan Gohman475871a2008-07-27 21:46:04 +00007148 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007149}
7150
Dan Gohman475871a2008-07-27 21:46:04 +00007151SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007152X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007153 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007154 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007155 EVT OpVT = Op.getValueType();
7156
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007157 // If this is a 256-bit vector result, first insert into a 128-bit
7158 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007159 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007160 // Insert into a 128-bit vector.
7161 EVT VT128 = EVT::getVectorVT(*Context,
7162 OpVT.getVectorElementType(),
7163 OpVT.getVectorNumElements() / 2);
7164
7165 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7166
7167 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007168 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007169 }
7170
Craig Topperd77d2fe2012-04-29 20:22:05 +00007171 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007172 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007173 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007174
Owen Anderson825b72b2009-08-11 20:47:22 +00007175 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007176 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007177 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007178 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179}
7180
David Greene91585092011-01-26 15:38:49 +00007181// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7182// a simple subregister reference or explicit instructions to grab
7183// upper bits of a vector.
7184SDValue
7185X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7186 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007187 DebugLoc dl = Op.getNode()->getDebugLoc();
7188 SDValue Vec = Op.getNode()->getOperand(0);
7189 SDValue Idx = Op.getNode()->getOperand(1);
7190
Craig Topper7a9a28b2012-08-12 02:23:29 +00007191 if (Op.getNode()->getValueType(0).is128BitVector() &&
7192 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007193 isa<ConstantSDNode>(Idx)) {
7194 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7195 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007196 }
David Greene91585092011-01-26 15:38:49 +00007197 }
7198 return SDValue();
7199}
7200
David Greenecfe33c42011-01-26 19:13:22 +00007201// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7202// simple superregister reference or explicit instructions to insert
7203// the upper bits of a vector.
7204SDValue
7205X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7206 if (Subtarget->hasAVX()) {
7207 DebugLoc dl = Op.getNode()->getDebugLoc();
7208 SDValue Vec = Op.getNode()->getOperand(0);
7209 SDValue SubVec = Op.getNode()->getOperand(1);
7210 SDValue Idx = Op.getNode()->getOperand(2);
7211
Craig Topper7a9a28b2012-08-12 02:23:29 +00007212 if (Op.getNode()->getValueType(0).is256BitVector() &&
7213 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007214 isa<ConstantSDNode>(Idx)) {
7215 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7216 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007217 }
7218 }
7219 return SDValue();
7220}
7221
Bill Wendling056292f2008-09-16 21:48:12 +00007222// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7223// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7224// one of the above mentioned nodes. It has to be wrapped because otherwise
7225// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7226// be used to form addressing mode. These wrapped nodes will be selected
7227// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007228SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007229X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007230 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007231
Chris Lattner41621a22009-06-26 19:22:52 +00007232 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7233 // global base reg.
7234 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007235 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007236 CodeModel::Model M = getTargetMachine().getCodeModel();
7237
Chris Lattner4f066492009-07-11 20:29:19 +00007238 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007239 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007240 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007241 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007242 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007243 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007244 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007245
Evan Cheng1606e8e2009-03-13 07:51:59 +00007246 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007247 CP->getAlignment(),
7248 CP->getOffset(), OpFlag);
7249 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007250 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007251 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007252 if (OpFlag) {
7253 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007254 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007255 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007256 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007257 }
7258
7259 return Result;
7260}
7261
Dan Gohmand858e902010-04-17 15:26:15 +00007262SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007263 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007264
Chris Lattner18c59872009-06-27 04:16:01 +00007265 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7266 // global base reg.
7267 unsigned char OpFlag = 0;
7268 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007269 CodeModel::Model M = getTargetMachine().getCodeModel();
7270
Chris Lattner4f066492009-07-11 20:29:19 +00007271 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007272 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007273 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007274 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007275 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007276 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007277 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007278
Chris Lattner18c59872009-06-27 04:16:01 +00007279 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7280 OpFlag);
7281 DebugLoc DL = JT->getDebugLoc();
7282 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007283
Chris Lattner18c59872009-06-27 04:16:01 +00007284 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007285 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007286 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7287 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007288 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007289 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007290
Chris Lattner18c59872009-06-27 04:16:01 +00007291 return Result;
7292}
7293
7294SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007295X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007296 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007297
Chris Lattner18c59872009-06-27 04:16:01 +00007298 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7299 // global base reg.
7300 unsigned char OpFlag = 0;
7301 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007302 CodeModel::Model M = getTargetMachine().getCodeModel();
7303
Chris Lattner4f066492009-07-11 20:29:19 +00007304 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007305 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7306 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7307 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007308 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007309 } else if (Subtarget->isPICStyleGOT()) {
7310 OpFlag = X86II::MO_GOT;
7311 } else if (Subtarget->isPICStyleStubPIC()) {
7312 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7313 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7314 OpFlag = X86II::MO_DARWIN_NONLAZY;
7315 }
Eric Christopherfd179292009-08-27 18:07:15 +00007316
Chris Lattner18c59872009-06-27 04:16:01 +00007317 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007318
Chris Lattner18c59872009-06-27 04:16:01 +00007319 DebugLoc DL = Op.getDebugLoc();
7320 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007321
7322
Chris Lattner18c59872009-06-27 04:16:01 +00007323 // With PIC, the address is actually $g + Offset.
7324 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007325 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007326 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7327 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007328 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007329 Result);
7330 }
Eric Christopherfd179292009-08-27 18:07:15 +00007331
Eli Friedman586272d2011-08-11 01:48:05 +00007332 // For symbols that require a load from a stub to get the address, emit the
7333 // load.
7334 if (isGlobalStubReference(OpFlag))
7335 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007336 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007337
Chris Lattner18c59872009-06-27 04:16:01 +00007338 return Result;
7339}
7340
Dan Gohman475871a2008-07-27 21:46:04 +00007341SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007342X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007343 // Create the TargetBlockAddressAddress node.
7344 unsigned char OpFlags =
7345 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007346 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007347 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007348 DebugLoc dl = Op.getDebugLoc();
7349 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7350 /*isTarget=*/true, OpFlags);
7351
Dan Gohmanf705adb2009-10-30 01:28:02 +00007352 if (Subtarget->isPICStyleRIPRel() &&
7353 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007354 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7355 else
7356 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007357
Dan Gohman29cbade2009-11-20 23:18:13 +00007358 // With PIC, the address is actually $g + Offset.
7359 if (isGlobalRelativeToPICBase(OpFlags)) {
7360 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7361 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7362 Result);
7363 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007364
7365 return Result;
7366}
7367
7368SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007369X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007370 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007371 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007372 // Create the TargetGlobalAddress node, folding in the constant
7373 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007374 unsigned char OpFlags =
7375 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007376 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007377 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007378 if (OpFlags == X86II::MO_NO_FLAG &&
7379 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007380 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007381 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007382 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007383 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007384 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007385 }
Eric Christopherfd179292009-08-27 18:07:15 +00007386
Chris Lattner4f066492009-07-11 20:29:19 +00007387 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007388 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007389 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7390 else
7391 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007392
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007393 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007394 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007395 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7396 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007397 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007398 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007399
Chris Lattner36c25012009-07-10 07:34:39 +00007400 // For globals that require a load from a stub to get the address, emit the
7401 // load.
7402 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007403 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007404 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007405
Dan Gohman6520e202008-10-18 02:06:02 +00007406 // If there was a non-zero offset that we didn't fold, create an explicit
7407 // addition for it.
7408 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007409 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007410 DAG.getConstant(Offset, getPointerTy()));
7411
Evan Cheng0db9fe62006-04-25 20:13:52 +00007412 return Result;
7413}
7414
Evan Chengda43bcf2008-09-24 00:05:32 +00007415SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007416X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007417 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007418 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007419 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007420}
7421
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007422static SDValue
7423GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007424 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007425 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007426 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007427 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007428 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007429 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007430 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007431 GA->getOffset(),
7432 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007433
7434 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7435 : X86ISD::TLSADDR;
7436
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007437 if (InFlag) {
7438 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007439 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007440 } else {
7441 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007442 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007443 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007444
7445 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007446 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007447
Rafael Espindola15f1b662009-04-24 12:59:40 +00007448 SDValue Flag = Chain.getValue(1);
7449 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007450}
7451
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007452// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007453static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007454LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007455 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007456 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007457 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7458 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007459 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007460 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007461 InFlag = Chain.getValue(1);
7462
Chris Lattnerb903bed2009-06-26 21:20:29 +00007463 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007464}
7465
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007466// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007467static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007468LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007469 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007470 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7471 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007472}
7473
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007474static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7475 SelectionDAG &DAG,
7476 const EVT PtrVT,
7477 bool is64Bit) {
7478 DebugLoc dl = GA->getDebugLoc();
7479
7480 // Get the start address of the TLS block for this module.
7481 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7482 .getInfo<X86MachineFunctionInfo>();
7483 MFI->incNumLocalDynamicTLSAccesses();
7484
7485 SDValue Base;
7486 if (is64Bit) {
7487 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7488 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7489 } else {
7490 SDValue InFlag;
7491 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7492 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7493 InFlag = Chain.getValue(1);
7494 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7495 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7496 }
7497
7498 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7499 // of Base.
7500
7501 // Build x@dtpoff.
7502 unsigned char OperandFlags = X86II::MO_DTPOFF;
7503 unsigned WrapperKind = X86ISD::Wrapper;
7504 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7505 GA->getValueType(0),
7506 GA->getOffset(), OperandFlags);
7507 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7508
7509 // Add x@dtpoff with the base.
7510 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7511}
7512
Hans Wennborg228756c2012-05-11 10:11:01 +00007513// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007514static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007515 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007516 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007517 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007518
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007519 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7520 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7521 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007522
Michael J. Spencerec38de22010-10-10 22:04:20 +00007523 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007524 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007525 MachinePointerInfo(Ptr),
7526 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007527
Chris Lattnerb903bed2009-06-26 21:20:29 +00007528 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007529 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7530 // initialexec.
7531 unsigned WrapperKind = X86ISD::Wrapper;
7532 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007533 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007534 } else if (model == TLSModel::InitialExec) {
7535 if (is64Bit) {
7536 OperandFlags = X86II::MO_GOTTPOFF;
7537 WrapperKind = X86ISD::WrapperRIP;
7538 } else {
7539 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7540 }
Chris Lattner18c59872009-06-27 04:16:01 +00007541 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007542 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007543 }
Eric Christopherfd179292009-08-27 18:07:15 +00007544
Hans Wennborg228756c2012-05-11 10:11:01 +00007545 // emit "addl x@ntpoff,%eax" (local exec)
7546 // or "addl x@indntpoff,%eax" (initial exec)
7547 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007548 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007549 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007550 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007551 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007552
Hans Wennborg228756c2012-05-11 10:11:01 +00007553 if (model == TLSModel::InitialExec) {
7554 if (isPIC && !is64Bit) {
7555 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7556 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7557 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007558 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007559
7560 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7561 MachinePointerInfo::getGOT(), false, false, false,
7562 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007563 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007564
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007565 // The address of the thread local variable is the add of the thread
7566 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007567 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007568}
7569
Dan Gohman475871a2008-07-27 21:46:04 +00007570SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007571X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007572
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007573 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007574 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007575
Eric Christopher30ef0e52010-06-03 04:07:48 +00007576 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007577 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007578
Eric Christopher30ef0e52010-06-03 04:07:48 +00007579 switch (model) {
7580 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007581 if (Subtarget->is64Bit())
7582 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7583 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007584 case TLSModel::LocalDynamic:
7585 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7586 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007587 case TLSModel::InitialExec:
7588 case TLSModel::LocalExec:
7589 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007590 Subtarget->is64Bit(),
7591 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007592 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007593 llvm_unreachable("Unknown TLS model.");
7594 }
7595
7596 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007597 // Darwin only has one model of TLS. Lower to that.
7598 unsigned char OpFlag = 0;
7599 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7600 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007601
Eric Christopher30ef0e52010-06-03 04:07:48 +00007602 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7603 // global base reg.
7604 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7605 !Subtarget->is64Bit();
7606 if (PIC32)
7607 OpFlag = X86II::MO_TLVP_PIC_BASE;
7608 else
7609 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007610 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007611 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007612 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007613 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007614 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007615
Eric Christopher30ef0e52010-06-03 04:07:48 +00007616 // With PIC32, the address is actually $g + Offset.
7617 if (PIC32)
7618 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7619 DAG.getNode(X86ISD::GlobalBaseReg,
7620 DebugLoc(), getPointerTy()),
7621 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007622
Eric Christopher30ef0e52010-06-03 04:07:48 +00007623 // Lowering the machine isd will make sure everything is in the right
7624 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007625 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007626 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007627 SDValue Args[] = { Chain, Offset };
7628 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007629
Eric Christopher30ef0e52010-06-03 04:07:48 +00007630 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7631 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7632 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007633
Eric Christopher30ef0e52010-06-03 04:07:48 +00007634 // And our return value (tls address) is in the standard call return value
7635 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007636 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007637 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7638 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007639 }
7640
7641 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007642 // Just use the implicit TLS architecture
7643 // Need to generate someting similar to:
7644 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7645 // ; from TEB
7646 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7647 // mov rcx, qword [rdx+rcx*8]
7648 // mov eax, .tls$:tlsvar
7649 // [rax+rcx] contains the address
7650 // Windows 64bit: gs:0x58
7651 // Windows 32bit: fs:__tls_array
7652
7653 // If GV is an alias then use the aliasee for determining
7654 // thread-localness.
7655 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7656 GV = GA->resolveAliasedGlobal(false);
7657 DebugLoc dl = GA->getDebugLoc();
7658 SDValue Chain = DAG.getEntryNode();
7659
7660 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7661 // %gs:0x58 (64-bit).
7662 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7663 ? Type::getInt8PtrTy(*DAG.getContext(),
7664 256)
7665 : Type::getInt32PtrTy(*DAG.getContext(),
7666 257));
7667
7668 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7669 Subtarget->is64Bit()
7670 ? DAG.getIntPtrConstant(0x58)
7671 : DAG.getExternalSymbol("_tls_array",
7672 getPointerTy()),
7673 MachinePointerInfo(Ptr),
7674 false, false, false, 0);
7675
7676 // Load the _tls_index variable
7677 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7678 if (Subtarget->is64Bit())
7679 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7680 IDX, MachinePointerInfo(), MVT::i32,
7681 false, false, 0);
7682 else
7683 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7684 false, false, false, 0);
7685
7686 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007687 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007688 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7689
7690 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7691 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7692 false, false, false, 0);
7693
7694 // Get the offset of start of .tls section
7695 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7696 GA->getValueType(0),
7697 GA->getOffset(), X86II::MO_SECREL);
7698 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7699
7700 // The address of the thread local variable is the add of the thread
7701 // pointer with the offset of the variable.
7702 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007703 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007704
David Blaikie4d6ccb52012-01-20 21:51:11 +00007705 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007706}
7707
Evan Cheng0db9fe62006-04-25 20:13:52 +00007708
Chad Rosierb90d2a92012-01-03 23:19:12 +00007709/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7710/// and take a 2 x i32 value to shift plus a shift amount.
7711SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007712 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007713 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007714 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007715 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007716 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007717 SDValue ShOpLo = Op.getOperand(0);
7718 SDValue ShOpHi = Op.getOperand(1);
7719 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007720 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007722 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007723
Dan Gohman475871a2008-07-27 21:46:04 +00007724 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007725 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007726 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7727 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007728 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007729 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7730 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007731 }
Evan Chenge3413162006-01-09 18:33:28 +00007732
Owen Anderson825b72b2009-08-11 20:47:22 +00007733 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7734 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007735 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007737
Dan Gohman475871a2008-07-27 21:46:04 +00007738 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007739 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007740 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7741 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007742
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007743 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007744 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7745 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007746 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007747 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7748 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007749 }
7750
Dan Gohman475871a2008-07-27 21:46:04 +00007751 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007752 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007753}
Evan Chenga3195e82006-01-12 22:54:21 +00007754
Dan Gohmand858e902010-04-17 15:26:15 +00007755SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7756 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007757 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007758
Dale Johannesen0488fb62010-09-30 23:57:10 +00007759 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007760 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007761
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007763 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007764
Eli Friedman36df4992009-05-27 00:47:34 +00007765 // These are really Legal; return the operand so the caller accepts it as
7766 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007768 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007769 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007770 Subtarget->is64Bit()) {
7771 return Op;
7772 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007773
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007774 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007775 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007776 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007777 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007778 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007779 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007780 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007781 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007782 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007783 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7784}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007785
Owen Andersone50ed302009-08-10 22:56:29 +00007786SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007787 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007788 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007789 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007790 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007791 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007792 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007793 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007794 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007795 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007797
Chris Lattner492a43e2010-09-22 01:28:21 +00007798 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007799
Stuart Hastings84be9582011-06-02 15:57:11 +00007800 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7801 MachineMemOperand *MMO;
7802 if (FI) {
7803 int SSFI = FI->getIndex();
7804 MMO =
7805 DAG.getMachineFunction()
7806 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7807 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7808 } else {
7809 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7810 StackSlot = StackSlot.getOperand(1);
7811 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007812 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007813 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7814 X86ISD::FILD, DL,
7815 Tys, Ops, array_lengthof(Ops),
7816 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007817
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007818 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007819 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007820 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007821
7822 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7823 // shouldn't be necessary except that RFP cannot be live across
7824 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007825 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007826 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7827 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007828 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007829 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007830 SDValue Ops[] = {
7831 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7832 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007833 MachineMemOperand *MMO =
7834 DAG.getMachineFunction()
7835 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007836 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007837
Chris Lattner492a43e2010-09-22 01:28:21 +00007838 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7839 Ops, array_lengthof(Ops),
7840 Op.getValueType(), MMO);
7841 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007842 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007843 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007844 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007845
Evan Cheng0db9fe62006-04-25 20:13:52 +00007846 return Result;
7847}
7848
Bill Wendling8b8a6362009-01-17 03:56:04 +00007849// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007850SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7851 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007852 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007853 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007854 movq %rax, %xmm0
7855 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7856 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7857 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007858 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007859 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007860 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007861 addpd %xmm1, %xmm0
7862 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007863 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007864
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007865 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007866 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007867
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007868 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007869 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7870 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007871 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007872
Chris Lattner97484792012-01-25 09:56:22 +00007873 SmallVector<Constant*,2> CV1;
7874 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007875 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007876 CV1.push_back(
7877 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7878 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007879 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007880
Bill Wendling397ae212012-01-05 02:13:20 +00007881 // Load the 64-bit value into an XMM register.
7882 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7883 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007884 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007885 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007886 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007887 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7888 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7889 CLod0);
7890
Owen Anderson825b72b2009-08-11 20:47:22 +00007891 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007892 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007893 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007894 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007895 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007896 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007897
Craig Topperd0a31172012-01-10 06:37:29 +00007898 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007899 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7900 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7901 } else {
7902 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7903 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7904 S2F, 0x4E, DAG);
7905 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7906 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7907 Sub);
7908 }
7909
7910 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007911 DAG.getIntPtrConstant(0));
7912}
7913
Bill Wendling8b8a6362009-01-17 03:56:04 +00007914// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007915SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7916 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007917 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007918 // FP constant to bias correct the final result.
7919 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007920 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007921
7922 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007924 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007925
Eli Friedmanf3704762011-08-29 21:15:46 +00007926 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007927 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007928
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007930 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007931 DAG.getIntPtrConstant(0));
7932
7933 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007934 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007935 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007936 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007938 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007939 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007940 MVT::v2f64, Bias)));
7941 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007942 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007943 DAG.getIntPtrConstant(0));
7944
7945 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007947
7948 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007949 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007950
Craig Topper69947b92012-04-23 06:57:04 +00007951 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007952 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007953 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007954 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007955 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007956
7957 // Handle final rounding.
7958 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007959}
7960
Dan Gohmand858e902010-04-17 15:26:15 +00007961SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7962 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007963 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007964 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007965
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007966 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007967 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7968 // the optimization here.
7969 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007970 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007971
Owen Andersone50ed302009-08-10 22:56:29 +00007972 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007973 EVT DstVT = Op.getValueType();
7974 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007975 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007976 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007977 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007978 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007979 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007980
7981 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007982 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007983 if (SrcVT == MVT::i32) {
7984 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7985 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7986 getPointerTy(), StackSlot, WordOff);
7987 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007988 StackSlot, MachinePointerInfo(),
7989 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007990 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007991 OffsetSlot, MachinePointerInfo(),
7992 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007993 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7994 return Fild;
7995 }
7996
7997 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7998 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00007999 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008000 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008001 // For i64 source, we need to add the appropriate power of 2 if the input
8002 // was negative. This is the same as the optimization in
8003 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8004 // we must be careful to do the computation in x87 extended precision, not
8005 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008006 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8007 MachineMemOperand *MMO =
8008 DAG.getMachineFunction()
8009 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8010 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008011
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008012 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8013 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008014 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8015 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008016
8017 APInt FF(32, 0x5F800000ULL);
8018
8019 // Check whether the sign bit is set.
8020 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8021 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8022 ISD::SETLT);
8023
8024 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8025 SDValue FudgePtr = DAG.getConstantPool(
8026 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8027 getPointerTy());
8028
8029 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8030 SDValue Zero = DAG.getIntPtrConstant(0);
8031 SDValue Four = DAG.getIntPtrConstant(4);
8032 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8033 Zero, Four);
8034 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8035
8036 // Load the value out, extending it from f32 to f80.
8037 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008038 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008039 FudgePtr, MachinePointerInfo::getConstantPool(),
8040 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008041 // Extend everything to 80 bits to force it to be done on x87.
8042 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8043 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008044}
8045
Dan Gohman475871a2008-07-27 21:46:04 +00008046std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008047FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008048 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008049
Owen Andersone50ed302009-08-10 22:56:29 +00008050 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008051
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008052 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008053 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8054 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008055 }
8056
Owen Anderson825b72b2009-08-11 20:47:22 +00008057 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8058 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008059 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008060
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008061 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008063 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008064 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008065 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008066 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008067 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008068 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008069
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008070 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8071 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008072 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008073 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008074 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008075 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008076
Evan Cheng0db9fe62006-04-25 20:13:52 +00008077 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008078 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8079 Opc = X86ISD::WIN_FTOL;
8080 else
8081 switch (DstTy.getSimpleVT().SimpleTy) {
8082 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8083 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8084 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8085 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8086 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008087
Dan Gohman475871a2008-07-27 21:46:04 +00008088 SDValue Chain = DAG.getEntryNode();
8089 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008090 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008091 // FIXME This causes a redundant load/store if the SSE-class value is already
8092 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008093 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008094 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008095 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008096 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008097 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008098 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008099 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008100 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008101 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008102
Chris Lattner492a43e2010-09-22 01:28:21 +00008103 MachineMemOperand *MMO =
8104 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8105 MachineMemOperand::MOLoad, MemSize, MemSize);
8106 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8107 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008108 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008109 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008110 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8111 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008112
Chris Lattner07290932010-09-22 01:05:16 +00008113 MachineMemOperand *MMO =
8114 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8115 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008116
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008117 if (Opc != X86ISD::WIN_FTOL) {
8118 // Build the FP_TO_INT*_IN_MEM
8119 SDValue Ops[] = { Chain, Value, StackSlot };
8120 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8121 Ops, 3, DstTy, MMO);
8122 return std::make_pair(FIST, StackSlot);
8123 } else {
8124 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8125 DAG.getVTList(MVT::Other, MVT::Glue),
8126 Chain, Value);
8127 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8128 MVT::i32, ftol.getValue(1));
8129 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8130 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008131 SDValue Ops[] = { eax, edx };
8132 SDValue pair = IsReplace
8133 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8134 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008135 return std::make_pair(pair, SDValue());
8136 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008137}
8138
Dan Gohmand858e902010-04-17 15:26:15 +00008139SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8140 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008141 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008142 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008143
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008144 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8145 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008146 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008147 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8148 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008149
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008150 if (StackSlot.getNode())
8151 // Load the result.
8152 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8153 FIST, StackSlot, MachinePointerInfo(),
8154 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008155
8156 // The node is the result.
8157 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008158}
8159
Dan Gohmand858e902010-04-17 15:26:15 +00008160SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8161 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008162 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8163 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008164 SDValue FIST = Vals.first, StackSlot = Vals.second;
8165 assert(FIST.getNode() && "Unexpected failure");
8166
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008167 if (StackSlot.getNode())
8168 // Load the result.
8169 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8170 FIST, StackSlot, MachinePointerInfo(),
8171 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008172
8173 // The node is the result.
8174 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008175}
8176
Dan Gohmand858e902010-04-17 15:26:15 +00008177SDValue X86TargetLowering::LowerFABS(SDValue Op,
8178 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008179 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008180 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008181 EVT VT = Op.getValueType();
8182 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008183 if (VT.isVector())
8184 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00008185 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00008186 if (EltVT == MVT::f64) {
Chad Rosiera20e1e72012-08-01 18:39:17 +00008187 C = ConstantVector::getSplat(2,
Chris Lattner4ca829e2012-01-25 06:02:56 +00008188 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008189 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00008190 C = ConstantVector::getSplat(4,
8191 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008192 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00008193 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008194 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008195 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008196 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008197 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008198}
8199
Dan Gohmand858e902010-04-17 15:26:15 +00008200SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008201 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008202 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008203 EVT VT = Op.getValueType();
8204 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008205 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8206 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008207 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008208 NumElts = VT.getVectorNumElements();
8209 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008210 Constant *C;
8211 if (EltVT == MVT::f64)
8212 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8213 else
8214 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8215 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008216 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008217 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008218 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008219 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008220 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008221 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008222 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008223 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008224 DAG.getNode(ISD::BITCAST, dl, XORVT,
8225 Op.getOperand(0)),
8226 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008227 }
Craig Topper69947b92012-04-23 06:57:04 +00008228
8229 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008230}
8231
Dan Gohmand858e902010-04-17 15:26:15 +00008232SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008233 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008234 SDValue Op0 = Op.getOperand(0);
8235 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008236 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008237 EVT VT = Op.getValueType();
8238 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008239
8240 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008241 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008242 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008243 SrcVT = VT;
8244 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008245 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008246 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008247 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008248 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008249 }
8250
8251 // At this point the operands and the result should have the same
8252 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008253
Evan Cheng68c47cb2007-01-05 07:55:56 +00008254 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008255 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008256 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008257 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8258 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008259 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008260 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8261 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8262 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8263 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008264 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008265 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008266 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008267 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008268 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008269 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008270 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008271
8272 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008273 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008274 // Op0 is MVT::f32, Op1 is MVT::f64.
8275 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8276 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8277 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008278 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008279 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008280 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008281 }
8282
Evan Cheng73d6cf12007-01-05 21:37:56 +00008283 // Clear first operand sign bit.
8284 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008285 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008286 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8287 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008288 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008289 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8290 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8291 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8292 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008293 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008294 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008295 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008296 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008297 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008298 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008299 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008300
8301 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008302 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008303}
8304
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008305SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8306 SDValue N0 = Op.getOperand(0);
8307 DebugLoc dl = Op.getDebugLoc();
8308 EVT VT = Op.getValueType();
8309
8310 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8311 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8312 DAG.getConstant(1, VT));
8313 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8314}
8315
Dan Gohman076aee32009-03-04 19:44:21 +00008316/// Emit nodes that will be selected as "test Op0,Op0", or something
8317/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008318SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008319 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008320 DebugLoc dl = Op.getDebugLoc();
8321
Dan Gohman31125812009-03-07 01:58:32 +00008322 // CF and OF aren't always set the way we want. Determine which
8323 // of these we need.
8324 bool NeedCF = false;
8325 bool NeedOF = false;
8326 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008327 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008328 case X86::COND_A: case X86::COND_AE:
8329 case X86::COND_B: case X86::COND_BE:
8330 NeedCF = true;
8331 break;
8332 case X86::COND_G: case X86::COND_GE:
8333 case X86::COND_L: case X86::COND_LE:
8334 case X86::COND_O: case X86::COND_NO:
8335 NeedOF = true;
8336 break;
Dan Gohman31125812009-03-07 01:58:32 +00008337 }
8338
Dan Gohman076aee32009-03-04 19:44:21 +00008339 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008340 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8341 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008342 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8343 // Emit a CMP with 0, which is the TEST pattern.
8344 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8345 DAG.getConstant(0, Op.getValueType()));
8346
8347 unsigned Opcode = 0;
8348 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008349
8350 // Truncate operations may prevent the merge of the SETCC instruction
8351 // and the arithmetic intruction before it. Attempt to truncate the operands
8352 // of the arithmetic instruction and use a reduced bit-width instruction.
8353 bool NeedTruncation = false;
8354 SDValue ArithOp = Op;
8355 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8356 SDValue Arith = Op->getOperand(0);
8357 // Both the trunc and the arithmetic op need to have one user each.
8358 if (Arith->hasOneUse())
8359 switch (Arith.getOpcode()) {
8360 default: break;
8361 case ISD::ADD:
8362 case ISD::SUB:
8363 case ISD::AND:
8364 case ISD::OR:
8365 case ISD::XOR: {
8366 NeedTruncation = true;
8367 ArithOp = Arith;
8368 }
8369 }
8370 }
8371
8372 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8373 // which may be the result of a CAST. We use the variable 'Op', which is the
8374 // non-casted variable when we check for possible users.
8375 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008376 case ISD::ADD:
8377 // Due to an isel shortcoming, be conservative if this add is likely to be
8378 // selected as part of a load-modify-store instruction. When the root node
8379 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8380 // uses of other nodes in the match, such as the ADD in this case. This
8381 // leads to the ADD being left around and reselected, with the result being
8382 // two adds in the output. Alas, even if none our users are stores, that
8383 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8384 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8385 // climbing the DAG back to the root, and it doesn't seem to be worth the
8386 // effort.
8387 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008388 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8389 if (UI->getOpcode() != ISD::CopyToReg &&
8390 UI->getOpcode() != ISD::SETCC &&
8391 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008392 goto default_case;
8393
8394 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008395 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008396 // An add of one will be selected as an INC.
8397 if (C->getAPIntValue() == 1) {
8398 Opcode = X86ISD::INC;
8399 NumOperands = 1;
8400 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008401 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008402
8403 // An add of negative one (subtract of one) will be selected as a DEC.
8404 if (C->getAPIntValue().isAllOnesValue()) {
8405 Opcode = X86ISD::DEC;
8406 NumOperands = 1;
8407 break;
8408 }
Dan Gohman076aee32009-03-04 19:44:21 +00008409 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008410
8411 // Otherwise use a regular EFLAGS-setting add.
8412 Opcode = X86ISD::ADD;
8413 NumOperands = 2;
8414 break;
8415 case ISD::AND: {
8416 // If the primary and result isn't used, don't bother using X86ISD::AND,
8417 // because a TEST instruction will be better.
8418 bool NonFlagUse = false;
8419 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8420 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8421 SDNode *User = *UI;
8422 unsigned UOpNo = UI.getOperandNo();
8423 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8424 // Look pass truncate.
8425 UOpNo = User->use_begin().getOperandNo();
8426 User = *User->use_begin();
8427 }
8428
8429 if (User->getOpcode() != ISD::BRCOND &&
8430 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008431 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008432 NonFlagUse = true;
8433 break;
8434 }
Dan Gohman076aee32009-03-04 19:44:21 +00008435 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008436
8437 if (!NonFlagUse)
8438 break;
8439 }
8440 // FALL THROUGH
8441 case ISD::SUB:
8442 case ISD::OR:
8443 case ISD::XOR:
8444 // Due to the ISEL shortcoming noted above, be conservative if this op is
8445 // likely to be selected as part of a load-modify-store instruction.
8446 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8447 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8448 if (UI->getOpcode() == ISD::STORE)
8449 goto default_case;
8450
8451 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008452 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008453 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008454 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008455 case ISD::OR: Opcode = X86ISD::OR; break;
8456 case ISD::XOR: Opcode = X86ISD::XOR; break;
8457 case ISD::AND: Opcode = X86ISD::AND; break;
8458 }
8459
8460 NumOperands = 2;
8461 break;
8462 case X86ISD::ADD:
8463 case X86ISD::SUB:
8464 case X86ISD::INC:
8465 case X86ISD::DEC:
8466 case X86ISD::OR:
8467 case X86ISD::XOR:
8468 case X86ISD::AND:
8469 return SDValue(Op.getNode(), 1);
8470 default:
8471 default_case:
8472 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008473 }
8474
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008475 // If we found that truncation is beneficial, perform the truncation and
8476 // update 'Op'.
8477 if (NeedTruncation) {
8478 EVT VT = Op.getValueType();
8479 SDValue WideVal = Op->getOperand(0);
8480 EVT WideVT = WideVal.getValueType();
8481 unsigned ConvertedOp = 0;
8482 // Use a target machine opcode to prevent further DAGCombine
8483 // optimizations that may separate the arithmetic operations
8484 // from the setcc node.
8485 switch (WideVal.getOpcode()) {
8486 default: break;
8487 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8488 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8489 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8490 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8491 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8492 }
8493
8494 if (ConvertedOp) {
8495 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8496 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8497 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8498 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8499 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8500 }
8501 }
8502 }
8503
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008504 if (Opcode == 0)
8505 // Emit a CMP with 0, which is the TEST pattern.
8506 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8507 DAG.getConstant(0, Op.getValueType()));
8508
8509 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8510 SmallVector<SDValue, 4> Ops;
8511 for (unsigned i = 0; i != NumOperands; ++i)
8512 Ops.push_back(Op.getOperand(i));
8513
8514 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8515 DAG.ReplaceAllUsesWith(Op, New);
8516 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008517}
8518
8519/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8520/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008521SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008522 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8524 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008525 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008526
8527 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008528 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8529 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8530 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8531 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8532 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8533 Op0, Op1);
8534 return SDValue(Sub.getNode(), 1);
8535 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008536 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008537}
8538
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008539/// Convert a comparison if required by the subtarget.
8540SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8541 SelectionDAG &DAG) const {
8542 // If the subtarget does not support the FUCOMI instruction, floating-point
8543 // comparisons have to be converted.
8544 if (Subtarget->hasCMov() ||
8545 Cmp.getOpcode() != X86ISD::CMP ||
8546 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8547 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8548 return Cmp;
8549
8550 // The instruction selector will select an FUCOM instruction instead of
8551 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8552 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8553 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8554 DebugLoc dl = Cmp.getDebugLoc();
8555 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8556 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8557 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8558 DAG.getConstant(8, MVT::i8));
8559 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8560 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8561}
8562
Evan Chengd40d03e2010-01-06 19:38:29 +00008563/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8564/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008565SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8566 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008567 SDValue Op0 = And.getOperand(0);
8568 SDValue Op1 = And.getOperand(1);
8569 if (Op0.getOpcode() == ISD::TRUNCATE)
8570 Op0 = Op0.getOperand(0);
8571 if (Op1.getOpcode() == ISD::TRUNCATE)
8572 Op1 = Op1.getOperand(0);
8573
Evan Chengd40d03e2010-01-06 19:38:29 +00008574 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008575 if (Op1.getOpcode() == ISD::SHL)
8576 std::swap(Op0, Op1);
8577 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008578 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8579 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008580 // If we looked past a truncate, check that it's only truncating away
8581 // known zeros.
8582 unsigned BitWidth = Op0.getValueSizeInBits();
8583 unsigned AndBitWidth = And.getValueSizeInBits();
8584 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008585 APInt Zeros, Ones;
8586 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008587 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8588 return SDValue();
8589 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008590 LHS = Op1;
8591 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008592 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008593 } else if (Op1.getOpcode() == ISD::Constant) {
8594 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008595 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008596 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008597
8598 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008599 LHS = AndLHS.getOperand(0);
8600 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008601 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008602
8603 // Use BT if the immediate can't be encoded in a TEST instruction.
8604 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8605 LHS = AndLHS;
8606 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8607 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008608 }
Evan Cheng0488db92007-09-25 01:57:46 +00008609
Evan Chengd40d03e2010-01-06 19:38:29 +00008610 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008611 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008612 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008613 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008614 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008615 // Also promote i16 to i32 for performance / code size reason.
8616 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008617 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008618 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008619
Evan Chengd40d03e2010-01-06 19:38:29 +00008620 // If the operand types disagree, extend the shift amount to match. Since
8621 // BT ignores high bits (like shifts) we can use anyextend.
8622 if (LHS.getValueType() != RHS.getValueType())
8623 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008624
Evan Chengd40d03e2010-01-06 19:38:29 +00008625 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8626 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8627 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8628 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008629 }
8630
Evan Cheng54de3ea2010-01-05 06:52:31 +00008631 return SDValue();
8632}
8633
Dan Gohmand858e902010-04-17 15:26:15 +00008634SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008635
8636 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8637
Evan Cheng54de3ea2010-01-05 06:52:31 +00008638 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8639 SDValue Op0 = Op.getOperand(0);
8640 SDValue Op1 = Op.getOperand(1);
8641 DebugLoc dl = Op.getDebugLoc();
8642 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8643
8644 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008645 // Lower (X & (1 << N)) == 0 to BT(X, N).
8646 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8647 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008648 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008649 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008650 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008651 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8652 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8653 if (NewSetCC.getNode())
8654 return NewSetCC;
8655 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008656
Chris Lattner481eebc2010-12-19 21:23:48 +00008657 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8658 // these.
8659 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008660 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008661 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8662 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008663
Chris Lattner481eebc2010-12-19 21:23:48 +00008664 // If the input is a setcc, then reuse the input setcc or use a new one with
8665 // the inverted condition.
8666 if (Op0.getOpcode() == X86ISD::SETCC) {
8667 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8668 bool Invert = (CC == ISD::SETNE) ^
8669 cast<ConstantSDNode>(Op1)->isNullValue();
8670 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008671
Evan Cheng2c755ba2010-02-27 07:36:59 +00008672 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008673 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8674 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8675 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008676 }
8677
Evan Chenge5b51ac2010-04-17 06:13:15 +00008678 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008679 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008680 if (X86CC == X86::COND_INVALID)
8681 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008682
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008683 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008684 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008685 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008686 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008687}
8688
Craig Topper89af15e2011-09-18 08:03:58 +00008689// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008690// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008691static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008692 EVT VT = Op.getValueType();
8693
Craig Topper7a9a28b2012-08-12 02:23:29 +00008694 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008695 "Unsupported value type for operation");
8696
Craig Topper66ddd152012-04-27 22:54:43 +00008697 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008698 DebugLoc dl = Op.getDebugLoc();
8699 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008700
8701 // Extract the LHS vectors
8702 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008703 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8704 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008705
8706 // Extract the RHS vectors
8707 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008708 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8709 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008710
8711 // Issue the operation on the smaller types and concatenate the result back
8712 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8713 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8714 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8715 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8716 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8717}
8718
8719
Dan Gohmand858e902010-04-17 15:26:15 +00008720SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008721 SDValue Cond;
8722 SDValue Op0 = Op.getOperand(0);
8723 SDValue Op1 = Op.getOperand(1);
8724 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008725 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008726 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8727 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008728 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008729
8730 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008731#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008732 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008733 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8734#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008735
Craig Topper523908d2012-08-13 02:34:03 +00008736 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008737 bool Swap = false;
8738
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008739 // SSE Condition code mapping:
8740 // 0 - EQ
8741 // 1 - LT
8742 // 2 - LE
8743 // 3 - UNORD
8744 // 4 - NEQ
8745 // 5 - NLT
8746 // 6 - NLE
8747 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008748 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008749 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008750 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008751 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008752 case ISD::SETOGT:
8753 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008754 case ISD::SETLT:
8755 case ISD::SETOLT: SSECC = 1; break;
8756 case ISD::SETOGE:
8757 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008758 case ISD::SETLE:
8759 case ISD::SETOLE: SSECC = 2; break;
8760 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008761 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008762 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008763 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008764 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008765 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008766 case ISD::SETUGT: SSECC = 6; break;
8767 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008768 case ISD::SETUEQ:
8769 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008770 }
8771 if (Swap)
8772 std::swap(Op0, Op1);
8773
Nate Begemanfb8ead02008-07-25 19:05:58 +00008774 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008775 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008776 unsigned CC0, CC1;
8777 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008778 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008779 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8780 } else {
8781 assert(SetCCOpcode == ISD::SETONE);
8782 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008783 }
Craig Topper523908d2012-08-13 02:34:03 +00008784
8785 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8786 DAG.getConstant(CC0, MVT::i8));
8787 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8788 DAG.getConstant(CC1, MVT::i8));
8789 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008790 }
8791 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008792 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8793 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008794 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008795
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008796 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008797 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008798 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008799
Nate Begeman30a0de92008-07-17 16:51:19 +00008800 // We are handling one of the integer comparisons here. Since SSE only has
8801 // GT and EQ comparisons for integer, swapping operands and multiple
8802 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008803 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008804 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008805
Nate Begeman30a0de92008-07-17 16:51:19 +00008806 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008807 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008808 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008809 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008810 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008811 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008812 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008813 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008814 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008815 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008816 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008817 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008818 }
8819 if (Swap)
8820 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008821
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008822 // Check that the operation in question is available (most are plain SSE2,
8823 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008824 if (VT == MVT::v2i64) {
8825 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8826 return SDValue();
8827 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8828 return SDValue();
8829 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008830
Nate Begeman30a0de92008-07-17 16:51:19 +00008831 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8832 // bits of the inputs before performing those operations.
8833 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008834 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008835 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8836 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008837 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008838 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8839 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008840 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8841 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008843
Dale Johannesenace16102009-02-03 19:33:06 +00008844 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008845
8846 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008847 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008848 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008849
Nate Begeman30a0de92008-07-17 16:51:19 +00008850 return Result;
8851}
Evan Cheng0488db92007-09-25 01:57:46 +00008852
Evan Cheng370e5342008-12-03 08:38:43 +00008853// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008854static bool isX86LogicalCmp(SDValue Op) {
8855 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008856 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8857 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008858 return true;
8859 if (Op.getResNo() == 1 &&
8860 (Opc == X86ISD::ADD ||
8861 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008862 Opc == X86ISD::ADC ||
8863 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008864 Opc == X86ISD::SMUL ||
8865 Opc == X86ISD::UMUL ||
8866 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008867 Opc == X86ISD::DEC ||
8868 Opc == X86ISD::OR ||
8869 Opc == X86ISD::XOR ||
8870 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008871 return true;
8872
Chris Lattner9637d5b2010-12-05 07:49:54 +00008873 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8874 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008875
Dan Gohman076aee32009-03-04 19:44:21 +00008876 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008877}
8878
Chris Lattnera2b56002010-12-05 01:23:24 +00008879static bool isZero(SDValue V) {
8880 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8881 return C && C->isNullValue();
8882}
8883
Chris Lattner96908b12010-12-05 02:00:51 +00008884static bool isAllOnes(SDValue V) {
8885 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8886 return C && C->isAllOnesValue();
8887}
8888
Evan Chengb64dd5f2012-08-07 22:21:00 +00008889static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8890 if (V.getOpcode() != ISD::TRUNCATE)
8891 return false;
8892
8893 SDValue VOp0 = V.getOperand(0);
8894 unsigned InBits = VOp0.getValueSizeInBits();
8895 unsigned Bits = V.getValueSizeInBits();
8896 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8897}
8898
Dan Gohmand858e902010-04-17 15:26:15 +00008899SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008900 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008901 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008902 SDValue Op1 = Op.getOperand(1);
8903 SDValue Op2 = Op.getOperand(2);
8904 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008905 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008906
Dan Gohman1a492952009-10-20 16:22:37 +00008907 if (Cond.getOpcode() == ISD::SETCC) {
8908 SDValue NewCond = LowerSETCC(Cond, DAG);
8909 if (NewCond.getNode())
8910 Cond = NewCond;
8911 }
Evan Cheng734503b2006-09-11 02:19:56 +00008912
Chris Lattnera2b56002010-12-05 01:23:24 +00008913 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008914 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008915 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008916 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008917 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008918 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8919 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008920 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008921
Chris Lattnera2b56002010-12-05 01:23:24 +00008922 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008923
8924 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008925 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8926 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008927
8928 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008929 // Apply further optimizations for special cases
8930 // (select (x != 0), -1, 0) -> neg & sbb
8931 // (select (x == 0), 0, -1) -> neg & sbb
8932 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008933 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008934 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8935 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008936 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8937 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008938 CmpOp0);
8939 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8940 DAG.getConstant(X86::COND_B, MVT::i8),
8941 SDValue(Neg.getNode(), 1));
8942 return Res;
8943 }
8944
Chris Lattnera2b56002010-12-05 01:23:24 +00008945 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8946 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008947 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008948
Chris Lattner96908b12010-12-05 02:00:51 +00008949 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008950 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8951 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008952
Chris Lattner96908b12010-12-05 02:00:51 +00008953 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8954 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008955
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008956 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008957 if (N2C == 0 || !N2C->isNullValue())
8958 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8959 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008960 }
8961 }
8962
Chris Lattnera2b56002010-12-05 01:23:24 +00008963 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008964 if (Cond.getOpcode() == ISD::AND &&
8965 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8966 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008967 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008968 Cond = Cond.getOperand(0);
8969 }
8970
Evan Cheng3f41d662007-10-08 22:16:29 +00008971 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8972 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008973 unsigned CondOpcode = Cond.getOpcode();
8974 if (CondOpcode == X86ISD::SETCC ||
8975 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008976 CC = Cond.getOperand(0);
8977
Dan Gohman475871a2008-07-27 21:46:04 +00008978 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008979 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008980 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008981
Evan Cheng3f41d662007-10-08 22:16:29 +00008982 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008983 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008984 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008985 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008986
Chris Lattnerd1980a52009-03-12 06:52:53 +00008987 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8988 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008989 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008990 addTest = false;
8991 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008992 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8993 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8994 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8995 Cond.getOperand(0).getValueType() != MVT::i8)) {
8996 SDValue LHS = Cond.getOperand(0);
8997 SDValue RHS = Cond.getOperand(1);
8998 unsigned X86Opcode;
8999 unsigned X86Cond;
9000 SDVTList VTs;
9001 switch (CondOpcode) {
9002 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9003 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9004 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9005 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9006 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9007 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9008 default: llvm_unreachable("unexpected overflowing operator");
9009 }
9010 if (CondOpcode == ISD::UMULO)
9011 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9012 MVT::i32);
9013 else
9014 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9015
9016 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9017
9018 if (CondOpcode == ISD::UMULO)
9019 Cond = X86Op.getValue(2);
9020 else
9021 Cond = X86Op.getValue(1);
9022
9023 CC = DAG.getConstant(X86Cond, MVT::i8);
9024 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009025 }
9026
9027 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009028 // Look pass the truncate if the high bits are known zero.
9029 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9030 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009031
9032 // We know the result of AND is compared against zero. Try to match
9033 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009034 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009035 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009036 if (NewSetCC.getNode()) {
9037 CC = NewSetCC.getOperand(0);
9038 Cond = NewSetCC.getOperand(1);
9039 addTest = false;
9040 }
9041 }
9042 }
9043
9044 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009045 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009046 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009047 }
9048
Benjamin Kramere915ff32010-12-22 23:09:28 +00009049 // a < b ? -1 : 0 -> RES = ~setcc_carry
9050 // a < b ? 0 : -1 -> RES = setcc_carry
9051 // a >= b ? -1 : 0 -> RES = setcc_carry
9052 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009053 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009054 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009055 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9056
9057 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9058 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9059 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9060 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9061 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9062 return DAG.getNOT(DL, Res, Res.getValueType());
9063 return Res;
9064 }
9065 }
9066
Evan Cheng0488db92007-09-25 01:57:46 +00009067 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9068 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009069 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009070 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009071 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009072}
9073
Evan Cheng370e5342008-12-03 08:38:43 +00009074// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9075// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9076// from the AND / OR.
9077static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9078 Opc = Op.getOpcode();
9079 if (Opc != ISD::OR && Opc != ISD::AND)
9080 return false;
9081 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9082 Op.getOperand(0).hasOneUse() &&
9083 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9084 Op.getOperand(1).hasOneUse());
9085}
9086
Evan Cheng961d6d42009-02-02 08:19:07 +00009087// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9088// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009089static bool isXor1OfSetCC(SDValue Op) {
9090 if (Op.getOpcode() != ISD::XOR)
9091 return false;
9092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9093 if (N1C && N1C->getAPIntValue() == 1) {
9094 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9095 Op.getOperand(0).hasOneUse();
9096 }
9097 return false;
9098}
9099
Dan Gohmand858e902010-04-17 15:26:15 +00009100SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009101 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009102 SDValue Chain = Op.getOperand(0);
9103 SDValue Cond = Op.getOperand(1);
9104 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009105 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009106 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009107 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009108
Dan Gohman1a492952009-10-20 16:22:37 +00009109 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009110 // Check for setcc([su]{add,sub,mul}o == 0).
9111 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9112 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9113 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9114 Cond.getOperand(0).getResNo() == 1 &&
9115 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9116 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9117 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9118 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9119 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9120 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9121 Inverted = true;
9122 Cond = Cond.getOperand(0);
9123 } else {
9124 SDValue NewCond = LowerSETCC(Cond, DAG);
9125 if (NewCond.getNode())
9126 Cond = NewCond;
9127 }
Dan Gohman1a492952009-10-20 16:22:37 +00009128 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009129#if 0
9130 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009131 else if (Cond.getOpcode() == X86ISD::ADD ||
9132 Cond.getOpcode() == X86ISD::SUB ||
9133 Cond.getOpcode() == X86ISD::SMUL ||
9134 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009135 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009136#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009137
Evan Chengad9c0a32009-12-15 00:53:42 +00009138 // Look pass (and (setcc_carry (cmp ...)), 1).
9139 if (Cond.getOpcode() == ISD::AND &&
9140 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9141 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009142 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009143 Cond = Cond.getOperand(0);
9144 }
9145
Evan Cheng3f41d662007-10-08 22:16:29 +00009146 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9147 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009148 unsigned CondOpcode = Cond.getOpcode();
9149 if (CondOpcode == X86ISD::SETCC ||
9150 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009151 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009152
Dan Gohman475871a2008-07-27 21:46:04 +00009153 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009154 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009155 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009156 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009157 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009158 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009159 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009160 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009161 default: break;
9162 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009163 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009164 // These can only come from an arithmetic instruction with overflow,
9165 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009166 Cond = Cond.getNode()->getOperand(1);
9167 addTest = false;
9168 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009169 }
Evan Cheng0488db92007-09-25 01:57:46 +00009170 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009171 }
9172 CondOpcode = Cond.getOpcode();
9173 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9174 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9175 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9176 Cond.getOperand(0).getValueType() != MVT::i8)) {
9177 SDValue LHS = Cond.getOperand(0);
9178 SDValue RHS = Cond.getOperand(1);
9179 unsigned X86Opcode;
9180 unsigned X86Cond;
9181 SDVTList VTs;
9182 switch (CondOpcode) {
9183 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9184 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9185 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9186 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9187 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9188 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9189 default: llvm_unreachable("unexpected overflowing operator");
9190 }
9191 if (Inverted)
9192 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9193 if (CondOpcode == ISD::UMULO)
9194 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9195 MVT::i32);
9196 else
9197 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9198
9199 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9200
9201 if (CondOpcode == ISD::UMULO)
9202 Cond = X86Op.getValue(2);
9203 else
9204 Cond = X86Op.getValue(1);
9205
9206 CC = DAG.getConstant(X86Cond, MVT::i8);
9207 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009208 } else {
9209 unsigned CondOpc;
9210 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9211 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009212 if (CondOpc == ISD::OR) {
9213 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9214 // two branches instead of an explicit OR instruction with a
9215 // separate test.
9216 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009217 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009218 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009219 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009220 Chain, Dest, CC, Cmp);
9221 CC = Cond.getOperand(1).getOperand(0);
9222 Cond = Cmp;
9223 addTest = false;
9224 }
9225 } else { // ISD::AND
9226 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9227 // two branches instead of an explicit AND instruction with a
9228 // separate test. However, we only do this if this block doesn't
9229 // have a fall-through edge, because this requires an explicit
9230 // jmp when the condition is false.
9231 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009232 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009233 Op.getNode()->hasOneUse()) {
9234 X86::CondCode CCode =
9235 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9236 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009237 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009238 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009239 // Look for an unconditional branch following this conditional branch.
9240 // We need this because we need to reverse the successors in order
9241 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009242 if (User->getOpcode() == ISD::BR) {
9243 SDValue FalseBB = User->getOperand(1);
9244 SDNode *NewBR =
9245 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009246 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009247 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009248 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009249
Dale Johannesene4d209d2009-02-03 20:21:25 +00009250 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009251 Chain, Dest, CC, Cmp);
9252 X86::CondCode CCode =
9253 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9254 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009255 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009256 Cond = Cmp;
9257 addTest = false;
9258 }
9259 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009260 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009261 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9262 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9263 // It should be transformed during dag combiner except when the condition
9264 // is set by a arithmetics with overflow node.
9265 X86::CondCode CCode =
9266 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9267 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009268 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009269 Cond = Cond.getOperand(0).getOperand(1);
9270 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009271 } else if (Cond.getOpcode() == ISD::SETCC &&
9272 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9273 // For FCMP_OEQ, we can emit
9274 // two branches instead of an explicit AND instruction with a
9275 // separate test. However, we only do this if this block doesn't
9276 // have a fall-through edge, because this requires an explicit
9277 // jmp when the condition is false.
9278 if (Op.getNode()->hasOneUse()) {
9279 SDNode *User = *Op.getNode()->use_begin();
9280 // Look for an unconditional branch following this conditional branch.
9281 // We need this because we need to reverse the successors in order
9282 // to implement FCMP_OEQ.
9283 if (User->getOpcode() == ISD::BR) {
9284 SDValue FalseBB = User->getOperand(1);
9285 SDNode *NewBR =
9286 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9287 assert(NewBR == User);
9288 (void)NewBR;
9289 Dest = FalseBB;
9290
9291 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9292 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009293 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009294 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9295 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9296 Chain, Dest, CC, Cmp);
9297 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9298 Cond = Cmp;
9299 addTest = false;
9300 }
9301 }
9302 } else if (Cond.getOpcode() == ISD::SETCC &&
9303 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9304 // For FCMP_UNE, we can emit
9305 // two branches instead of an explicit AND instruction with a
9306 // separate test. However, we only do this if this block doesn't
9307 // have a fall-through edge, because this requires an explicit
9308 // jmp when the condition is false.
9309 if (Op.getNode()->hasOneUse()) {
9310 SDNode *User = *Op.getNode()->use_begin();
9311 // Look for an unconditional branch following this conditional branch.
9312 // We need this because we need to reverse the successors in order
9313 // to implement FCMP_UNE.
9314 if (User->getOpcode() == ISD::BR) {
9315 SDValue FalseBB = User->getOperand(1);
9316 SDNode *NewBR =
9317 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9318 assert(NewBR == User);
9319 (void)NewBR;
9320
9321 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9322 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009323 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009324 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9325 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9326 Chain, Dest, CC, Cmp);
9327 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9328 Cond = Cmp;
9329 addTest = false;
9330 Dest = FalseBB;
9331 }
9332 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009333 }
Evan Cheng0488db92007-09-25 01:57:46 +00009334 }
9335
9336 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009337 // Look pass the truncate if the high bits are known zero.
9338 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9339 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009340
9341 // We know the result of AND is compared against zero. Try to match
9342 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009343 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009344 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9345 if (NewSetCC.getNode()) {
9346 CC = NewSetCC.getOperand(0);
9347 Cond = NewSetCC.getOperand(1);
9348 addTest = false;
9349 }
9350 }
9351 }
9352
9353 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009354 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009355 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009356 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009357 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009358 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009359 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009360}
9361
Anton Korobeynikove060b532007-04-17 19:34:00 +00009362
9363// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9364// Calls to _alloca is needed to probe the stack when allocating more than 4k
9365// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9366// that the guard pages used by the OS virtual memory manager are allocated in
9367// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009368SDValue
9369X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009370 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009371 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009372 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009373 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009374 "are being used");
9375 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009376 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009377
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009378 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009379 SDValue Chain = Op.getOperand(0);
9380 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009381 // FIXME: Ensure alignment here
9382
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009383 bool Is64Bit = Subtarget->is64Bit();
9384 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009385
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009386 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009387 MachineFunction &MF = DAG.getMachineFunction();
9388 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009389
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009390 if (Is64Bit) {
9391 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009392 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009393 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009394
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009395 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009396 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009397 if (I->hasNestAttr())
9398 report_fatal_error("Cannot use segmented stacks with functions that "
9399 "have nested arguments.");
9400 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009401
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009402 const TargetRegisterClass *AddrRegClass =
9403 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9404 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9405 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9406 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9407 DAG.getRegister(Vreg, SPTy));
9408 SDValue Ops1[2] = { Value, Chain };
9409 return DAG.getMergeValues(Ops1, 2, dl);
9410 } else {
9411 SDValue Flag;
9412 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009413
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009414 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9415 Flag = Chain.getValue(1);
9416 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009417
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009418 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9419 Flag = Chain.getValue(1);
9420
9421 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9422
9423 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9424 return DAG.getMergeValues(Ops1, 2, dl);
9425 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009426}
9427
Dan Gohmand858e902010-04-17 15:26:15 +00009428SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009429 MachineFunction &MF = DAG.getMachineFunction();
9430 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9431
Dan Gohman69de1932008-02-06 22:27:42 +00009432 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009433 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009434
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009435 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009436 // vastart just stores the address of the VarArgsFrameIndex slot into the
9437 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009438 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9439 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009440 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9441 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009442 }
9443
9444 // __va_list_tag:
9445 // gp_offset (0 - 6 * 8)
9446 // fp_offset (48 - 48 + 8 * 16)
9447 // overflow_arg_area (point to parameters coming in memory).
9448 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009449 SmallVector<SDValue, 8> MemOps;
9450 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009451 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009452 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009453 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9454 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009455 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009456 MemOps.push_back(Store);
9457
9458 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009459 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009460 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009461 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009462 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9463 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009464 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009465 MemOps.push_back(Store);
9466
9467 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009468 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009469 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009470 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9471 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009472 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9473 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009474 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009475 MemOps.push_back(Store);
9476
9477 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009478 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009479 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009480 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9481 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009482 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9483 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009484 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009485 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009486 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009487}
9488
Dan Gohmand858e902010-04-17 15:26:15 +00009489SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009490 assert(Subtarget->is64Bit() &&
9491 "LowerVAARG only handles 64-bit va_arg!");
9492 assert((Subtarget->isTargetLinux() ||
9493 Subtarget->isTargetDarwin()) &&
9494 "Unhandled target in LowerVAARG");
9495 assert(Op.getNode()->getNumOperands() == 4);
9496 SDValue Chain = Op.getOperand(0);
9497 SDValue SrcPtr = Op.getOperand(1);
9498 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9499 unsigned Align = Op.getConstantOperandVal(3);
9500 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009501
Dan Gohman320afb82010-10-12 18:00:49 +00009502 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009503 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009504 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9505 uint8_t ArgMode;
9506
9507 // Decide which area this value should be read from.
9508 // TODO: Implement the AMD64 ABI in its entirety. This simple
9509 // selection mechanism works only for the basic types.
9510 if (ArgVT == MVT::f80) {
9511 llvm_unreachable("va_arg for f80 not yet implemented");
9512 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9513 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9514 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9515 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9516 } else {
9517 llvm_unreachable("Unhandled argument type in LowerVAARG");
9518 }
9519
9520 if (ArgMode == 2) {
9521 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009522 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009523 !(DAG.getMachineFunction()
9524 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009525 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009526 }
9527
9528 // Insert VAARG_64 node into the DAG
9529 // VAARG_64 returns two values: Variable Argument Address, Chain
9530 SmallVector<SDValue, 11> InstOps;
9531 InstOps.push_back(Chain);
9532 InstOps.push_back(SrcPtr);
9533 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9534 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9535 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9536 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9537 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9538 VTs, &InstOps[0], InstOps.size(),
9539 MVT::i64,
9540 MachinePointerInfo(SV),
9541 /*Align=*/0,
9542 /*Volatile=*/false,
9543 /*ReadMem=*/true,
9544 /*WriteMem=*/true);
9545 Chain = VAARG.getValue(1);
9546
9547 // Load the next argument and return it
9548 return DAG.getLoad(ArgVT, dl,
9549 Chain,
9550 VAARG,
9551 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009552 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009553}
9554
Dan Gohmand858e902010-04-17 15:26:15 +00009555SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009556 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009557 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009558 SDValue Chain = Op.getOperand(0);
9559 SDValue DstPtr = Op.getOperand(1);
9560 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009561 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9562 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009563 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009564
Chris Lattnere72f2022010-09-21 05:40:29 +00009565 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009566 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009567 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009568 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009569}
9570
Craig Topper80e46362012-01-23 06:16:53 +00009571// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9572// may or may not be a constant. Takes immediate version of shift as input.
9573static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9574 SDValue SrcOp, SDValue ShAmt,
9575 SelectionDAG &DAG) {
9576 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9577
9578 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009579 // Constant may be a TargetConstant. Use a regular constant.
9580 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009581 switch (Opc) {
9582 default: llvm_unreachable("Unknown target vector shift node");
9583 case X86ISD::VSHLI:
9584 case X86ISD::VSRLI:
9585 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009586 return DAG.getNode(Opc, dl, VT, SrcOp,
9587 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009588 }
9589 }
9590
9591 // Change opcode to non-immediate version
9592 switch (Opc) {
9593 default: llvm_unreachable("Unknown target vector shift node");
9594 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9595 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9596 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9597 }
9598
9599 // Need to build a vector containing shift amount
9600 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9601 SDValue ShOps[4];
9602 ShOps[0] = ShAmt;
9603 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009604 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009605 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009606
9607 // The return type has to be a 128-bit type with the same element
9608 // type as the input type.
9609 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9610 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9611
9612 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009613 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9614}
9615
Dan Gohman475871a2008-07-27 21:46:04 +00009616SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009617X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009618 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009619 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009620 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009621 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009622 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009623 case Intrinsic::x86_sse_comieq_ss:
9624 case Intrinsic::x86_sse_comilt_ss:
9625 case Intrinsic::x86_sse_comile_ss:
9626 case Intrinsic::x86_sse_comigt_ss:
9627 case Intrinsic::x86_sse_comige_ss:
9628 case Intrinsic::x86_sse_comineq_ss:
9629 case Intrinsic::x86_sse_ucomieq_ss:
9630 case Intrinsic::x86_sse_ucomilt_ss:
9631 case Intrinsic::x86_sse_ucomile_ss:
9632 case Intrinsic::x86_sse_ucomigt_ss:
9633 case Intrinsic::x86_sse_ucomige_ss:
9634 case Intrinsic::x86_sse_ucomineq_ss:
9635 case Intrinsic::x86_sse2_comieq_sd:
9636 case Intrinsic::x86_sse2_comilt_sd:
9637 case Intrinsic::x86_sse2_comile_sd:
9638 case Intrinsic::x86_sse2_comigt_sd:
9639 case Intrinsic::x86_sse2_comige_sd:
9640 case Intrinsic::x86_sse2_comineq_sd:
9641 case Intrinsic::x86_sse2_ucomieq_sd:
9642 case Intrinsic::x86_sse2_ucomilt_sd:
9643 case Intrinsic::x86_sse2_ucomile_sd:
9644 case Intrinsic::x86_sse2_ucomigt_sd:
9645 case Intrinsic::x86_sse2_ucomige_sd:
9646 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009647 unsigned Opc;
9648 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009649 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009650 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009651 case Intrinsic::x86_sse_comieq_ss:
9652 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009653 Opc = X86ISD::COMI;
9654 CC = ISD::SETEQ;
9655 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009656 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009657 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009658 Opc = X86ISD::COMI;
9659 CC = ISD::SETLT;
9660 break;
9661 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009662 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009663 Opc = X86ISD::COMI;
9664 CC = ISD::SETLE;
9665 break;
9666 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009667 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009668 Opc = X86ISD::COMI;
9669 CC = ISD::SETGT;
9670 break;
9671 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009672 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009673 Opc = X86ISD::COMI;
9674 CC = ISD::SETGE;
9675 break;
9676 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009677 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009678 Opc = X86ISD::COMI;
9679 CC = ISD::SETNE;
9680 break;
9681 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009682 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009683 Opc = X86ISD::UCOMI;
9684 CC = ISD::SETEQ;
9685 break;
9686 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009687 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009688 Opc = X86ISD::UCOMI;
9689 CC = ISD::SETLT;
9690 break;
9691 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009692 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009693 Opc = X86ISD::UCOMI;
9694 CC = ISD::SETLE;
9695 break;
9696 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009697 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009698 Opc = X86ISD::UCOMI;
9699 CC = ISD::SETGT;
9700 break;
9701 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009702 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009703 Opc = X86ISD::UCOMI;
9704 CC = ISD::SETGE;
9705 break;
9706 case Intrinsic::x86_sse_ucomineq_ss:
9707 case Intrinsic::x86_sse2_ucomineq_sd:
9708 Opc = X86ISD::UCOMI;
9709 CC = ISD::SETNE;
9710 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009711 }
Evan Cheng734503b2006-09-11 02:19:56 +00009712
Dan Gohman475871a2008-07-27 21:46:04 +00009713 SDValue LHS = Op.getOperand(1);
9714 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009715 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009716 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009717 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9718 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9719 DAG.getConstant(X86CC, MVT::i8), Cond);
9720 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009721 }
Craig Topper6d688152012-08-14 07:43:25 +00009722
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009723 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009724 case Intrinsic::x86_sse2_pmulu_dq:
9725 case Intrinsic::x86_avx2_pmulu_dq:
9726 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9727 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009728
9729 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009730 case Intrinsic::x86_sse3_hadd_ps:
9731 case Intrinsic::x86_sse3_hadd_pd:
9732 case Intrinsic::x86_avx_hadd_ps_256:
9733 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009734 case Intrinsic::x86_sse3_hsub_ps:
9735 case Intrinsic::x86_sse3_hsub_pd:
9736 case Intrinsic::x86_avx_hsub_ps_256:
9737 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009738 case Intrinsic::x86_ssse3_phadd_w_128:
9739 case Intrinsic::x86_ssse3_phadd_d_128:
9740 case Intrinsic::x86_avx2_phadd_w:
9741 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009742 case Intrinsic::x86_ssse3_phsub_w_128:
9743 case Intrinsic::x86_ssse3_phsub_d_128:
9744 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009745 case Intrinsic::x86_avx2_phsub_d: {
9746 unsigned Opcode;
9747 switch (IntNo) {
9748 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9749 case Intrinsic::x86_sse3_hadd_ps:
9750 case Intrinsic::x86_sse3_hadd_pd:
9751 case Intrinsic::x86_avx_hadd_ps_256:
9752 case Intrinsic::x86_avx_hadd_pd_256:
9753 Opcode = X86ISD::FHADD;
9754 break;
9755 case Intrinsic::x86_sse3_hsub_ps:
9756 case Intrinsic::x86_sse3_hsub_pd:
9757 case Intrinsic::x86_avx_hsub_ps_256:
9758 case Intrinsic::x86_avx_hsub_pd_256:
9759 Opcode = X86ISD::FHSUB;
9760 break;
9761 case Intrinsic::x86_ssse3_phadd_w_128:
9762 case Intrinsic::x86_ssse3_phadd_d_128:
9763 case Intrinsic::x86_avx2_phadd_w:
9764 case Intrinsic::x86_avx2_phadd_d:
9765 Opcode = X86ISD::HADD;
9766 break;
9767 case Intrinsic::x86_ssse3_phsub_w_128:
9768 case Intrinsic::x86_ssse3_phsub_d_128:
9769 case Intrinsic::x86_avx2_phsub_w:
9770 case Intrinsic::x86_avx2_phsub_d:
9771 Opcode = X86ISD::HSUB;
9772 break;
9773 }
9774 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009775 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009776 }
9777
9778 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009779 case Intrinsic::x86_avx2_psllv_d:
9780 case Intrinsic::x86_avx2_psllv_q:
9781 case Intrinsic::x86_avx2_psllv_d_256:
9782 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009783 case Intrinsic::x86_avx2_psrlv_d:
9784 case Intrinsic::x86_avx2_psrlv_q:
9785 case Intrinsic::x86_avx2_psrlv_d_256:
9786 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009787 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009788 case Intrinsic::x86_avx2_psrav_d_256: {
9789 unsigned Opcode;
9790 switch (IntNo) {
9791 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9792 case Intrinsic::x86_avx2_psllv_d:
9793 case Intrinsic::x86_avx2_psllv_q:
9794 case Intrinsic::x86_avx2_psllv_d_256:
9795 case Intrinsic::x86_avx2_psllv_q_256:
9796 Opcode = ISD::SHL;
9797 break;
9798 case Intrinsic::x86_avx2_psrlv_d:
9799 case Intrinsic::x86_avx2_psrlv_q:
9800 case Intrinsic::x86_avx2_psrlv_d_256:
9801 case Intrinsic::x86_avx2_psrlv_q_256:
9802 Opcode = ISD::SRL;
9803 break;
9804 case Intrinsic::x86_avx2_psrav_d:
9805 case Intrinsic::x86_avx2_psrav_d_256:
9806 Opcode = ISD::SRA;
9807 break;
9808 }
9809 return DAG.getNode(Opcode, dl, Op.getValueType(),
9810 Op.getOperand(1), Op.getOperand(2));
9811 }
9812
Craig Topper969ba282012-01-25 06:43:11 +00009813 case Intrinsic::x86_ssse3_pshuf_b_128:
9814 case Intrinsic::x86_avx2_pshuf_b:
9815 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9816 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009817
Craig Topper969ba282012-01-25 06:43:11 +00009818 case Intrinsic::x86_ssse3_psign_b_128:
9819 case Intrinsic::x86_ssse3_psign_w_128:
9820 case Intrinsic::x86_ssse3_psign_d_128:
9821 case Intrinsic::x86_avx2_psign_b:
9822 case Intrinsic::x86_avx2_psign_w:
9823 case Intrinsic::x86_avx2_psign_d:
9824 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9825 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009826
Craig Toppere566cd02012-01-26 07:18:03 +00009827 case Intrinsic::x86_sse41_insertps:
9828 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9829 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009830
Craig Toppere566cd02012-01-26 07:18:03 +00009831 case Intrinsic::x86_avx_vperm2f128_ps_256:
9832 case Intrinsic::x86_avx_vperm2f128_pd_256:
9833 case Intrinsic::x86_avx_vperm2f128_si_256:
9834 case Intrinsic::x86_avx2_vperm2i128:
9835 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9836 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009837
Craig Topperffa6c402012-04-16 07:13:00 +00009838 case Intrinsic::x86_avx2_permd:
9839 case Intrinsic::x86_avx2_permps:
9840 // Operands intentionally swapped. Mask is last operand to intrinsic,
9841 // but second operand for node/intruction.
9842 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9843 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009844
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009845 // ptest and testp intrinsics. The intrinsic these come from are designed to
9846 // return an integer value, not just an instruction so lower it to the ptest
9847 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009848 case Intrinsic::x86_sse41_ptestz:
9849 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009850 case Intrinsic::x86_sse41_ptestnzc:
9851 case Intrinsic::x86_avx_ptestz_256:
9852 case Intrinsic::x86_avx_ptestc_256:
9853 case Intrinsic::x86_avx_ptestnzc_256:
9854 case Intrinsic::x86_avx_vtestz_ps:
9855 case Intrinsic::x86_avx_vtestc_ps:
9856 case Intrinsic::x86_avx_vtestnzc_ps:
9857 case Intrinsic::x86_avx_vtestz_pd:
9858 case Intrinsic::x86_avx_vtestc_pd:
9859 case Intrinsic::x86_avx_vtestnzc_pd:
9860 case Intrinsic::x86_avx_vtestz_ps_256:
9861 case Intrinsic::x86_avx_vtestc_ps_256:
9862 case Intrinsic::x86_avx_vtestnzc_ps_256:
9863 case Intrinsic::x86_avx_vtestz_pd_256:
9864 case Intrinsic::x86_avx_vtestc_pd_256:
9865 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9866 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009867 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009868 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009869 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009870 case Intrinsic::x86_avx_vtestz_ps:
9871 case Intrinsic::x86_avx_vtestz_pd:
9872 case Intrinsic::x86_avx_vtestz_ps_256:
9873 case Intrinsic::x86_avx_vtestz_pd_256:
9874 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009875 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009876 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009877 // ZF = 1
9878 X86CC = X86::COND_E;
9879 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009880 case Intrinsic::x86_avx_vtestc_ps:
9881 case Intrinsic::x86_avx_vtestc_pd:
9882 case Intrinsic::x86_avx_vtestc_ps_256:
9883 case Intrinsic::x86_avx_vtestc_pd_256:
9884 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009885 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009886 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009887 // CF = 1
9888 X86CC = X86::COND_B;
9889 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009890 case Intrinsic::x86_avx_vtestnzc_ps:
9891 case Intrinsic::x86_avx_vtestnzc_pd:
9892 case Intrinsic::x86_avx_vtestnzc_ps_256:
9893 case Intrinsic::x86_avx_vtestnzc_pd_256:
9894 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009895 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009896 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009897 // ZF and CF = 0
9898 X86CC = X86::COND_A;
9899 break;
9900 }
Eric Christopherfd179292009-08-27 18:07:15 +00009901
Eric Christopher71c67532009-07-29 00:28:05 +00009902 SDValue LHS = Op.getOperand(1);
9903 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009904 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9905 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009906 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9907 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9908 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009909 }
Evan Cheng5759f972008-05-04 09:15:50 +00009910
Craig Topper80e46362012-01-23 06:16:53 +00009911 // SSE/AVX shift intrinsics
9912 case Intrinsic::x86_sse2_psll_w:
9913 case Intrinsic::x86_sse2_psll_d:
9914 case Intrinsic::x86_sse2_psll_q:
9915 case Intrinsic::x86_avx2_psll_w:
9916 case Intrinsic::x86_avx2_psll_d:
9917 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009918 case Intrinsic::x86_sse2_psrl_w:
9919 case Intrinsic::x86_sse2_psrl_d:
9920 case Intrinsic::x86_sse2_psrl_q:
9921 case Intrinsic::x86_avx2_psrl_w:
9922 case Intrinsic::x86_avx2_psrl_d:
9923 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009924 case Intrinsic::x86_sse2_psra_w:
9925 case Intrinsic::x86_sse2_psra_d:
9926 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009927 case Intrinsic::x86_avx2_psra_d: {
9928 unsigned Opcode;
9929 switch (IntNo) {
9930 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9931 case Intrinsic::x86_sse2_psll_w:
9932 case Intrinsic::x86_sse2_psll_d:
9933 case Intrinsic::x86_sse2_psll_q:
9934 case Intrinsic::x86_avx2_psll_w:
9935 case Intrinsic::x86_avx2_psll_d:
9936 case Intrinsic::x86_avx2_psll_q:
9937 Opcode = X86ISD::VSHL;
9938 break;
9939 case Intrinsic::x86_sse2_psrl_w:
9940 case Intrinsic::x86_sse2_psrl_d:
9941 case Intrinsic::x86_sse2_psrl_q:
9942 case Intrinsic::x86_avx2_psrl_w:
9943 case Intrinsic::x86_avx2_psrl_d:
9944 case Intrinsic::x86_avx2_psrl_q:
9945 Opcode = X86ISD::VSRL;
9946 break;
9947 case Intrinsic::x86_sse2_psra_w:
9948 case Intrinsic::x86_sse2_psra_d:
9949 case Intrinsic::x86_avx2_psra_w:
9950 case Intrinsic::x86_avx2_psra_d:
9951 Opcode = X86ISD::VSRA;
9952 break;
9953 }
9954 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009955 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009956 }
9957
9958 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009959 case Intrinsic::x86_sse2_pslli_w:
9960 case Intrinsic::x86_sse2_pslli_d:
9961 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009962 case Intrinsic::x86_avx2_pslli_w:
9963 case Intrinsic::x86_avx2_pslli_d:
9964 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009965 case Intrinsic::x86_sse2_psrli_w:
9966 case Intrinsic::x86_sse2_psrli_d:
9967 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009968 case Intrinsic::x86_avx2_psrli_w:
9969 case Intrinsic::x86_avx2_psrli_d:
9970 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009971 case Intrinsic::x86_sse2_psrai_w:
9972 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009973 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +00009974 case Intrinsic::x86_avx2_psrai_d: {
9975 unsigned Opcode;
9976 switch (IntNo) {
9977 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9978 case Intrinsic::x86_sse2_pslli_w:
9979 case Intrinsic::x86_sse2_pslli_d:
9980 case Intrinsic::x86_sse2_pslli_q:
9981 case Intrinsic::x86_avx2_pslli_w:
9982 case Intrinsic::x86_avx2_pslli_d:
9983 case Intrinsic::x86_avx2_pslli_q:
9984 Opcode = X86ISD::VSHLI;
9985 break;
9986 case Intrinsic::x86_sse2_psrli_w:
9987 case Intrinsic::x86_sse2_psrli_d:
9988 case Intrinsic::x86_sse2_psrli_q:
9989 case Intrinsic::x86_avx2_psrli_w:
9990 case Intrinsic::x86_avx2_psrli_d:
9991 case Intrinsic::x86_avx2_psrli_q:
9992 Opcode = X86ISD::VSRLI;
9993 break;
9994 case Intrinsic::x86_sse2_psrai_w:
9995 case Intrinsic::x86_sse2_psrai_d:
9996 case Intrinsic::x86_avx2_psrai_w:
9997 case Intrinsic::x86_avx2_psrai_d:
9998 Opcode = X86ISD::VSRAI;
9999 break;
10000 }
10001 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010002 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010003 }
10004
Craig Topper4feb6472012-08-06 06:22:36 +000010005 case Intrinsic::x86_sse42_pcmpistria128:
10006 case Intrinsic::x86_sse42_pcmpestria128:
10007 case Intrinsic::x86_sse42_pcmpistric128:
10008 case Intrinsic::x86_sse42_pcmpestric128:
10009 case Intrinsic::x86_sse42_pcmpistrio128:
10010 case Intrinsic::x86_sse42_pcmpestrio128:
10011 case Intrinsic::x86_sse42_pcmpistris128:
10012 case Intrinsic::x86_sse42_pcmpestris128:
10013 case Intrinsic::x86_sse42_pcmpistriz128:
10014 case Intrinsic::x86_sse42_pcmpestriz128: {
10015 unsigned Opcode;
10016 unsigned X86CC;
10017 switch (IntNo) {
10018 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10019 case Intrinsic::x86_sse42_pcmpistria128:
10020 Opcode = X86ISD::PCMPISTRI;
10021 X86CC = X86::COND_A;
10022 break;
10023 case Intrinsic::x86_sse42_pcmpestria128:
10024 Opcode = X86ISD::PCMPESTRI;
10025 X86CC = X86::COND_A;
10026 break;
10027 case Intrinsic::x86_sse42_pcmpistric128:
10028 Opcode = X86ISD::PCMPISTRI;
10029 X86CC = X86::COND_B;
10030 break;
10031 case Intrinsic::x86_sse42_pcmpestric128:
10032 Opcode = X86ISD::PCMPESTRI;
10033 X86CC = X86::COND_B;
10034 break;
10035 case Intrinsic::x86_sse42_pcmpistrio128:
10036 Opcode = X86ISD::PCMPISTRI;
10037 X86CC = X86::COND_O;
10038 break;
10039 case Intrinsic::x86_sse42_pcmpestrio128:
10040 Opcode = X86ISD::PCMPESTRI;
10041 X86CC = X86::COND_O;
10042 break;
10043 case Intrinsic::x86_sse42_pcmpistris128:
10044 Opcode = X86ISD::PCMPISTRI;
10045 X86CC = X86::COND_S;
10046 break;
10047 case Intrinsic::x86_sse42_pcmpestris128:
10048 Opcode = X86ISD::PCMPESTRI;
10049 X86CC = X86::COND_S;
10050 break;
10051 case Intrinsic::x86_sse42_pcmpistriz128:
10052 Opcode = X86ISD::PCMPISTRI;
10053 X86CC = X86::COND_E;
10054 break;
10055 case Intrinsic::x86_sse42_pcmpestriz128:
10056 Opcode = X86ISD::PCMPESTRI;
10057 X86CC = X86::COND_E;
10058 break;
10059 }
10060 SmallVector<SDValue, 5> NewOps;
10061 NewOps.append(Op->op_begin()+1, Op->op_end());
10062 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10063 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10064 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10065 DAG.getConstant(X86CC, MVT::i8),
10066 SDValue(PCMP.getNode(), 1));
10067 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10068 }
Craig Topper6d688152012-08-14 07:43:25 +000010069
Craig Topper4feb6472012-08-06 06:22:36 +000010070 case Intrinsic::x86_sse42_pcmpistri128:
10071 case Intrinsic::x86_sse42_pcmpestri128: {
10072 unsigned Opcode;
10073 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10074 Opcode = X86ISD::PCMPISTRI;
10075 else
10076 Opcode = X86ISD::PCMPESTRI;
10077
10078 SmallVector<SDValue, 5> NewOps;
10079 NewOps.append(Op->op_begin()+1, Op->op_end());
10080 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10081 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10082 }
Craig Topper0e292372012-08-24 04:03:22 +000010083 case Intrinsic::x86_fma_vfmadd_ps:
10084 case Intrinsic::x86_fma_vfmadd_pd:
10085 case Intrinsic::x86_fma_vfmsub_ps:
10086 case Intrinsic::x86_fma_vfmsub_pd:
10087 case Intrinsic::x86_fma_vfnmadd_ps:
10088 case Intrinsic::x86_fma_vfnmadd_pd:
10089 case Intrinsic::x86_fma_vfnmsub_ps:
10090 case Intrinsic::x86_fma_vfnmsub_pd:
10091 case Intrinsic::x86_fma_vfmaddsub_ps:
10092 case Intrinsic::x86_fma_vfmaddsub_pd:
10093 case Intrinsic::x86_fma_vfmsubadd_ps:
10094 case Intrinsic::x86_fma_vfmsubadd_pd:
10095 case Intrinsic::x86_fma_vfmadd_ps_256:
10096 case Intrinsic::x86_fma_vfmadd_pd_256:
10097 case Intrinsic::x86_fma_vfmsub_ps_256:
10098 case Intrinsic::x86_fma_vfmsub_pd_256:
10099 case Intrinsic::x86_fma_vfnmadd_ps_256:
10100 case Intrinsic::x86_fma_vfnmadd_pd_256:
10101 case Intrinsic::x86_fma_vfnmsub_ps_256:
10102 case Intrinsic::x86_fma_vfnmsub_pd_256:
10103 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10104 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10105 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10106 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010107 unsigned Opc;
10108 switch (IntNo) {
10109 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10110 case Intrinsic::x86_fma_vfmadd_ps:
10111 case Intrinsic::x86_fma_vfmadd_pd:
10112 case Intrinsic::x86_fma_vfmadd_ps_256:
10113 case Intrinsic::x86_fma_vfmadd_pd_256:
10114 Opc = X86ISD::FMADD;
10115 break;
10116 case Intrinsic::x86_fma_vfmsub_ps:
10117 case Intrinsic::x86_fma_vfmsub_pd:
10118 case Intrinsic::x86_fma_vfmsub_ps_256:
10119 case Intrinsic::x86_fma_vfmsub_pd_256:
10120 Opc = X86ISD::FMSUB;
10121 break;
10122 case Intrinsic::x86_fma_vfnmadd_ps:
10123 case Intrinsic::x86_fma_vfnmadd_pd:
10124 case Intrinsic::x86_fma_vfnmadd_ps_256:
10125 case Intrinsic::x86_fma_vfnmadd_pd_256:
10126 Opc = X86ISD::FNMADD;
10127 break;
10128 case Intrinsic::x86_fma_vfnmsub_ps:
10129 case Intrinsic::x86_fma_vfnmsub_pd:
10130 case Intrinsic::x86_fma_vfnmsub_ps_256:
10131 case Intrinsic::x86_fma_vfnmsub_pd_256:
10132 Opc = X86ISD::FNMSUB;
10133 break;
10134 case Intrinsic::x86_fma_vfmaddsub_ps:
10135 case Intrinsic::x86_fma_vfmaddsub_pd:
10136 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10137 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10138 Opc = X86ISD::FMADDSUB;
10139 break;
10140 case Intrinsic::x86_fma_vfmsubadd_ps:
10141 case Intrinsic::x86_fma_vfmsubadd_pd:
10142 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10143 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10144 Opc = X86ISD::FMSUBADD;
10145 break;
10146 }
10147
10148 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10149 Op.getOperand(2), Op.getOperand(3));
10150 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010151 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010152}
Evan Cheng72261582005-12-20 06:22:03 +000010153
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010154SDValue
10155X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
10156 DebugLoc dl = Op.getDebugLoc();
10157 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10158 switch (IntNo) {
10159 default: return SDValue(); // Don't custom lower most intrinsics.
10160
10161 // RDRAND intrinsics.
10162 case Intrinsic::x86_rdrand_16:
10163 case Intrinsic::x86_rdrand_32:
10164 case Intrinsic::x86_rdrand_64: {
10165 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010166 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10167 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010168
10169 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10170 // return the value from Rand, which is always 0, casted to i32.
10171 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10172 DAG.getConstant(1, Op->getValueType(1)),
10173 DAG.getConstant(X86::COND_B, MVT::i32),
10174 SDValue(Result.getNode(), 1) };
10175 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10176 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10177 Ops, 4);
10178
10179 // Return { result, isValid, chain }.
10180 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010181 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010182 }
10183 }
10184}
10185
Dan Gohmand858e902010-04-17 15:26:15 +000010186SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10187 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010188 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10189 MFI->setReturnAddressIsTaken(true);
10190
Bill Wendling64e87322009-01-16 19:25:27 +000010191 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010192 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010193
10194 if (Depth > 0) {
10195 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10196 SDValue Offset =
10197 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010198 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010199 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010200 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010201 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010202 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010203 }
10204
10205 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010206 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010207 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010208 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010209}
10210
Dan Gohmand858e902010-04-17 15:26:15 +000010211SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010212 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10213 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010214
Owen Andersone50ed302009-08-10 22:56:29 +000010215 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010216 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010217 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10218 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010219 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010220 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010221 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10222 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010223 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010224 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010225}
10226
Dan Gohman475871a2008-07-27 21:46:04 +000010227SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010228 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010229 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010230}
10231
Dan Gohmand858e902010-04-17 15:26:15 +000010232SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010233 SDValue Chain = Op.getOperand(0);
10234 SDValue Offset = Op.getOperand(1);
10235 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010236 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010237
Dan Gohmand8816272010-08-11 18:14:00 +000010238 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10239 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10240 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010241 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010242
Dan Gohmand8816272010-08-11 18:14:00 +000010243 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10244 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010245 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010246 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10247 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010248 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010249
Dale Johannesene4d209d2009-02-03 20:21:25 +000010250 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010251 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010252 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010253}
10254
Duncan Sands4a544a72011-09-06 13:37:06 +000010255SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10256 SelectionDAG &DAG) const {
10257 return Op.getOperand(0);
10258}
10259
10260SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10261 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010262 SDValue Root = Op.getOperand(0);
10263 SDValue Trmp = Op.getOperand(1); // trampoline
10264 SDValue FPtr = Op.getOperand(2); // nested function
10265 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010266 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010267
Dan Gohman69de1932008-02-06 22:27:42 +000010268 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010269
10270 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010271 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010272
10273 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010274 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10275 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010276
Evan Cheng0e6a0522011-07-18 20:57:22 +000010277 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10278 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010279
10280 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10281
10282 // Load the pointer to the nested function into R11.
10283 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010284 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010285 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010286 Addr, MachinePointerInfo(TrmpAddr),
10287 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010288
Owen Anderson825b72b2009-08-11 20:47:22 +000010289 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10290 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010291 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10292 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010293 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010294
10295 // Load the 'nest' parameter value into R10.
10296 // R10 is specified in X86CallingConv.td
10297 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010298 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10299 DAG.getConstant(10, MVT::i64));
10300 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010301 Addr, MachinePointerInfo(TrmpAddr, 10),
10302 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010303
Owen Anderson825b72b2009-08-11 20:47:22 +000010304 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10305 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010306 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10307 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010308 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010309
10310 // Jump to the nested function.
10311 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010312 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10313 DAG.getConstant(20, MVT::i64));
10314 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010315 Addr, MachinePointerInfo(TrmpAddr, 20),
10316 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010317
10318 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010319 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10320 DAG.getConstant(22, MVT::i64));
10321 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010322 MachinePointerInfo(TrmpAddr, 22),
10323 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010324
Duncan Sands4a544a72011-09-06 13:37:06 +000010325 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010326 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010327 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010328 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010329 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010330 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010331
10332 switch (CC) {
10333 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010334 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010335 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010336 case CallingConv::X86_StdCall: {
10337 // Pass 'nest' parameter in ECX.
10338 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010339 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010340
10341 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010342 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010343 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010344
Chris Lattner58d74912008-03-12 17:45:29 +000010345 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010346 unsigned InRegCount = 0;
10347 unsigned Idx = 1;
10348
10349 for (FunctionType::param_iterator I = FTy->param_begin(),
10350 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010351 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010352 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010353 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010354
10355 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010356 report_fatal_error("Nest register in use - reduce number of inreg"
10357 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010358 }
10359 }
10360 break;
10361 }
10362 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010363 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010364 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010365 // Pass 'nest' parameter in EAX.
10366 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010367 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010368 break;
10369 }
10370
Dan Gohman475871a2008-07-27 21:46:04 +000010371 SDValue OutChains[4];
10372 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010373
Owen Anderson825b72b2009-08-11 20:47:22 +000010374 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10375 DAG.getConstant(10, MVT::i32));
10376 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010377
Chris Lattnera62fe662010-02-05 19:20:30 +000010378 // This is storing the opcode for MOV32ri.
10379 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010380 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010381 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010382 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010383 Trmp, MachinePointerInfo(TrmpAddr),
10384 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010385
Owen Anderson825b72b2009-08-11 20:47:22 +000010386 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10387 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010388 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10389 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010390 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010391
Chris Lattnera62fe662010-02-05 19:20:30 +000010392 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010393 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10394 DAG.getConstant(5, MVT::i32));
10395 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010396 MachinePointerInfo(TrmpAddr, 5),
10397 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010398
Owen Anderson825b72b2009-08-11 20:47:22 +000010399 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10400 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010401 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10402 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010403 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010404
Duncan Sands4a544a72011-09-06 13:37:06 +000010405 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010406 }
10407}
10408
Dan Gohmand858e902010-04-17 15:26:15 +000010409SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10410 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010411 /*
10412 The rounding mode is in bits 11:10 of FPSR, and has the following
10413 settings:
10414 00 Round to nearest
10415 01 Round to -inf
10416 10 Round to +inf
10417 11 Round to 0
10418
10419 FLT_ROUNDS, on the other hand, expects the following:
10420 -1 Undefined
10421 0 Round to 0
10422 1 Round to nearest
10423 2 Round to +inf
10424 3 Round to -inf
10425
10426 To perform the conversion, we do:
10427 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10428 */
10429
10430 MachineFunction &MF = DAG.getMachineFunction();
10431 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010432 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010433 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010434 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010435 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010436
10437 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010438 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010439 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010440
Michael J. Spencerec38de22010-10-10 22:04:20 +000010441
Chris Lattner2156b792010-09-22 01:11:26 +000010442 MachineMemOperand *MMO =
10443 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10444 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010445
Chris Lattner2156b792010-09-22 01:11:26 +000010446 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10447 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10448 DAG.getVTList(MVT::Other),
10449 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010450
10451 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010452 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010453 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010454
10455 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010456 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010457 DAG.getNode(ISD::SRL, DL, MVT::i16,
10458 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010459 CWD, DAG.getConstant(0x800, MVT::i16)),
10460 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010461 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010462 DAG.getNode(ISD::SRL, DL, MVT::i16,
10463 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010464 CWD, DAG.getConstant(0x400, MVT::i16)),
10465 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010466
Dan Gohman475871a2008-07-27 21:46:04 +000010467 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010468 DAG.getNode(ISD::AND, DL, MVT::i16,
10469 DAG.getNode(ISD::ADD, DL, MVT::i16,
10470 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010471 DAG.getConstant(1, MVT::i16)),
10472 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010473
10474
Duncan Sands83ec4b62008-06-06 12:08:01 +000010475 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010476 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010477}
10478
Dan Gohmand858e902010-04-17 15:26:15 +000010479SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010480 EVT VT = Op.getValueType();
10481 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010482 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010483 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010484
10485 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010486 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010487 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010488 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010489 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010490 }
Evan Cheng18efe262007-12-14 02:13:44 +000010491
Evan Cheng152804e2007-12-14 08:30:15 +000010492 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010493 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010494 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010495
10496 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010497 SDValue Ops[] = {
10498 Op,
10499 DAG.getConstant(NumBits+NumBits-1, OpVT),
10500 DAG.getConstant(X86::COND_E, MVT::i8),
10501 Op.getValue(1)
10502 };
10503 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010504
10505 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010506 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010507
Owen Anderson825b72b2009-08-11 20:47:22 +000010508 if (VT == MVT::i8)
10509 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010510 return Op;
10511}
10512
Chandler Carruthacc068e2011-12-24 10:55:54 +000010513SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10514 SelectionDAG &DAG) const {
10515 EVT VT = Op.getValueType();
10516 EVT OpVT = VT;
10517 unsigned NumBits = VT.getSizeInBits();
10518 DebugLoc dl = Op.getDebugLoc();
10519
10520 Op = Op.getOperand(0);
10521 if (VT == MVT::i8) {
10522 // Zero extend to i32 since there is not an i8 bsr.
10523 OpVT = MVT::i32;
10524 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10525 }
10526
10527 // Issue a bsr (scan bits in reverse).
10528 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10529 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10530
10531 // And xor with NumBits-1.
10532 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10533
10534 if (VT == MVT::i8)
10535 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10536 return Op;
10537}
10538
Dan Gohmand858e902010-04-17 15:26:15 +000010539SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010540 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010541 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010542 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010543 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010544
10545 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010546 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010547 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010548
10549 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010550 SDValue Ops[] = {
10551 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010552 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010553 DAG.getConstant(X86::COND_E, MVT::i8),
10554 Op.getValue(1)
10555 };
Chandler Carruth77821022011-12-24 12:12:34 +000010556 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010557}
10558
Craig Topper13894fa2011-08-24 06:14:18 +000010559// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10560// ones, and then concatenate the result back.
10561static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010562 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010563
Craig Topper7a9a28b2012-08-12 02:23:29 +000010564 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010565 "Unsupported value type for operation");
10566
Craig Topper66ddd152012-04-27 22:54:43 +000010567 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010568 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010569
10570 // Extract the LHS vectors
10571 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010572 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10573 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010574
10575 // Extract the RHS vectors
10576 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010577 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10578 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010579
10580 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10581 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10582
10583 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10584 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10585 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10586}
10587
10588SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010589 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010590 Op.getValueType().isInteger() &&
10591 "Only handle AVX 256-bit vector integer operation");
10592 return Lower256IntArith(Op, DAG);
10593}
10594
10595SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010596 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010597 Op.getValueType().isInteger() &&
10598 "Only handle AVX 256-bit vector integer operation");
10599 return Lower256IntArith(Op, DAG);
10600}
10601
10602SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10603 EVT VT = Op.getValueType();
10604
10605 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010606 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010607 return Lower256IntArith(Op, DAG);
10608
Craig Topper5b209e82012-02-05 03:14:49 +000010609 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10610 "Only know how to lower V2I64/V4I64 multiply");
10611
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010612 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010613
Craig Topper5b209e82012-02-05 03:14:49 +000010614 // Ahi = psrlqi(a, 32);
10615 // Bhi = psrlqi(b, 32);
10616 //
10617 // AloBlo = pmuludq(a, b);
10618 // AloBhi = pmuludq(a, Bhi);
10619 // AhiBlo = pmuludq(Ahi, b);
10620
10621 // AloBhi = psllqi(AloBhi, 32);
10622 // AhiBlo = psllqi(AhiBlo, 32);
10623 // return AloBlo + AloBhi + AhiBlo;
10624
Craig Topperaaa643c2011-11-09 07:28:55 +000010625 SDValue A = Op.getOperand(0);
10626 SDValue B = Op.getOperand(1);
10627
Craig Topper5b209e82012-02-05 03:14:49 +000010628 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010629
Craig Topper5b209e82012-02-05 03:14:49 +000010630 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10631 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010632
Craig Topper5b209e82012-02-05 03:14:49 +000010633 // Bit cast to 32-bit vectors for MULUDQ
10634 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10635 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10636 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10637 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10638 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010639
Craig Topper5b209e82012-02-05 03:14:49 +000010640 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10641 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10642 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010643
Craig Topper5b209e82012-02-05 03:14:49 +000010644 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10645 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010646
Dale Johannesene4d209d2009-02-03 20:21:25 +000010647 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010648 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010649}
10650
Nadav Rotem43012222011-05-11 08:12:09 +000010651SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10652
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010653 EVT VT = Op.getValueType();
10654 DebugLoc dl = Op.getDebugLoc();
10655 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010656 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010657 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010658
Craig Topper1accb7e2012-01-10 06:54:16 +000010659 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010660 return SDValue();
10661
Nadav Rotem43012222011-05-11 08:12:09 +000010662 // Optimize shl/srl/sra with constant shift amount.
10663 if (isSplatVector(Amt.getNode())) {
10664 SDValue SclrAmt = Amt->getOperand(0);
10665 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10666 uint64_t ShiftAmt = C->getZExtValue();
10667
Craig Toppered2e13d2012-01-22 19:15:14 +000010668 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10669 (Subtarget->hasAVX2() &&
10670 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10671 if (Op.getOpcode() == ISD::SHL)
10672 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10673 DAG.getConstant(ShiftAmt, MVT::i32));
10674 if (Op.getOpcode() == ISD::SRL)
10675 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10676 DAG.getConstant(ShiftAmt, MVT::i32));
10677 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10678 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10679 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010680 }
10681
Craig Toppered2e13d2012-01-22 19:15:14 +000010682 if (VT == MVT::v16i8) {
10683 if (Op.getOpcode() == ISD::SHL) {
10684 // Make a large shift.
10685 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10686 DAG.getConstant(ShiftAmt, MVT::i32));
10687 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10688 // Zero out the rightmost bits.
10689 SmallVector<SDValue, 16> V(16,
10690 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10691 MVT::i8));
10692 return DAG.getNode(ISD::AND, dl, VT, SHL,
10693 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010694 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010695 if (Op.getOpcode() == ISD::SRL) {
10696 // Make a large shift.
10697 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10698 DAG.getConstant(ShiftAmt, MVT::i32));
10699 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10700 // Zero out the leftmost bits.
10701 SmallVector<SDValue, 16> V(16,
10702 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10703 MVT::i8));
10704 return DAG.getNode(ISD::AND, dl, VT, SRL,
10705 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10706 }
10707 if (Op.getOpcode() == ISD::SRA) {
10708 if (ShiftAmt == 7) {
10709 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010710 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010711 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010712 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010713
Craig Toppered2e13d2012-01-22 19:15:14 +000010714 // R s>> a === ((R u>> a) ^ m) - m
10715 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10716 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10717 MVT::i8));
10718 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10719 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10720 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10721 return Res;
10722 }
Craig Topper731dfd02012-04-23 03:42:40 +000010723 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010724 }
Craig Topper46154eb2011-11-11 07:39:23 +000010725
Craig Topper0d86d462011-11-20 00:12:05 +000010726 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10727 if (Op.getOpcode() == ISD::SHL) {
10728 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010729 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10730 DAG.getConstant(ShiftAmt, MVT::i32));
10731 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010732 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010733 SmallVector<SDValue, 32> V(32,
10734 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10735 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010736 return DAG.getNode(ISD::AND, dl, VT, SHL,
10737 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010738 }
Craig Topper0d86d462011-11-20 00:12:05 +000010739 if (Op.getOpcode() == ISD::SRL) {
10740 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010741 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10742 DAG.getConstant(ShiftAmt, MVT::i32));
10743 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010744 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010745 SmallVector<SDValue, 32> V(32,
10746 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10747 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010748 return DAG.getNode(ISD::AND, dl, VT, SRL,
10749 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10750 }
10751 if (Op.getOpcode() == ISD::SRA) {
10752 if (ShiftAmt == 7) {
10753 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010754 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010755 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010756 }
10757
10758 // R s>> a === ((R u>> a) ^ m) - m
10759 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10760 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10761 MVT::i8));
10762 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10763 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10764 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10765 return Res;
10766 }
Craig Topper731dfd02012-04-23 03:42:40 +000010767 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010768 }
Nadav Rotem43012222011-05-11 08:12:09 +000010769 }
10770 }
10771
10772 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010773 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010774 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10775 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010776
Chris Lattner7302d802012-02-06 21:56:39 +000010777 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10778 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010779 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10780 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010781 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010782 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010783
10784 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010785 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010786 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10787 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10788 }
Nadav Rotem43012222011-05-11 08:12:09 +000010789 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010790 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010791
Nate Begeman51409212010-07-28 00:21:48 +000010792 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010793 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10794 DAG.getConstant(5, MVT::i32));
10795 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010796
Lang Hames8b99c1e2011-12-17 01:08:46 +000010797 // Turn 'a' into a mask suitable for VSELECT
10798 SDValue VSelM = DAG.getConstant(0x80, VT);
10799 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010800 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010801
Lang Hames8b99c1e2011-12-17 01:08:46 +000010802 SDValue CM1 = DAG.getConstant(0x0f, VT);
10803 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010804
Lang Hames8b99c1e2011-12-17 01:08:46 +000010805 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10806 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010807 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10808 DAG.getConstant(4, MVT::i32), DAG);
10809 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010810 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10811
Nate Begeman51409212010-07-28 00:21:48 +000010812 // a += a
10813 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010814 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010815 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010816
Lang Hames8b99c1e2011-12-17 01:08:46 +000010817 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10818 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010819 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10820 DAG.getConstant(2, MVT::i32), DAG);
10821 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010822 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10823
Nate Begeman51409212010-07-28 00:21:48 +000010824 // a += a
10825 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010826 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010827 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010828
Lang Hames8b99c1e2011-12-17 01:08:46 +000010829 // return VSELECT(r, r+r, a);
10830 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010831 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010832 return R;
10833 }
Craig Topper46154eb2011-11-11 07:39:23 +000010834
10835 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010836 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010837 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010838 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10839 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10840
10841 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010842 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10843 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010844
10845 // Recreate the shift amount vectors
10846 SDValue Amt1, Amt2;
10847 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10848 // Constant shift amount
10849 SmallVector<SDValue, 4> Amt1Csts;
10850 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010851 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010852 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010853 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010854 Amt2Csts.push_back(Amt->getOperand(i));
10855
10856 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10857 &Amt1Csts[0], NumElems/2);
10858 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10859 &Amt2Csts[0], NumElems/2);
10860 } else {
10861 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010862 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10863 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010864 }
10865
10866 // Issue new vector shifts for the smaller types
10867 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10868 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10869
10870 // Concatenate the result back
10871 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10872 }
10873
Nate Begeman51409212010-07-28 00:21:48 +000010874 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010875}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010876
Dan Gohmand858e902010-04-17 15:26:15 +000010877SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010878 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10879 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010880 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10881 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010882 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010883 SDValue LHS = N->getOperand(0);
10884 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010885 unsigned BaseOp = 0;
10886 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010887 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010888 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010889 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010890 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010891 // A subtract of one will be selected as a INC. Note that INC doesn't
10892 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010893 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10894 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010895 BaseOp = X86ISD::INC;
10896 Cond = X86::COND_O;
10897 break;
10898 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010899 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010900 Cond = X86::COND_O;
10901 break;
10902 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010903 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010904 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010905 break;
10906 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010907 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10908 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010909 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10910 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010911 BaseOp = X86ISD::DEC;
10912 Cond = X86::COND_O;
10913 break;
10914 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010915 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010916 Cond = X86::COND_O;
10917 break;
10918 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010919 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010920 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010921 break;
10922 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010923 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010924 Cond = X86::COND_O;
10925 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010926 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10927 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10928 MVT::i32);
10929 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010930
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010931 SDValue SetCC =
10932 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10933 DAG.getConstant(X86::COND_O, MVT::i32),
10934 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010935
Dan Gohman6e5fda22011-07-22 18:45:15 +000010936 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010937 }
Bill Wendling74c37652008-12-09 22:08:41 +000010938 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010939
Bill Wendling61edeb52008-12-02 01:06:39 +000010940 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010941 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010942 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010943
Bill Wendling61edeb52008-12-02 01:06:39 +000010944 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010945 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10946 DAG.getConstant(Cond, MVT::i32),
10947 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010948
Dan Gohman6e5fda22011-07-22 18:45:15 +000010949 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010950}
10951
Chad Rosier30450e82011-12-22 22:35:21 +000010952SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10953 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010954 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010955 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10956 EVT VT = Op.getValueType();
10957
Craig Toppered2e13d2012-01-22 19:15:14 +000010958 if (!Subtarget->hasSSE2() || !VT.isVector())
10959 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010960
Craig Toppered2e13d2012-01-22 19:15:14 +000010961 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10962 ExtraVT.getScalarType().getSizeInBits();
10963 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10964
10965 switch (VT.getSimpleVT().SimpleTy) {
10966 default: return SDValue();
10967 case MVT::v8i32:
10968 case MVT::v16i16:
10969 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010970 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010971 if (!Subtarget->hasAVX2()) {
10972 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000010973 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000010974
Craig Toppered2e13d2012-01-22 19:15:14 +000010975 // Extract the LHS vectors
10976 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010977 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10978 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010979
Craig Toppered2e13d2012-01-22 19:15:14 +000010980 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10981 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010982
Craig Toppered2e13d2012-01-22 19:15:14 +000010983 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000010984 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000010985 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10986 ExtraNumElems/2);
10987 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010988
Craig Toppered2e13d2012-01-22 19:15:14 +000010989 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10990 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010991
Craig Toppered2e13d2012-01-22 19:15:14 +000010992 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10993 }
10994 // fall through
10995 case MVT::v4i32:
10996 case MVT::v8i16: {
10997 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10998 Op.getOperand(0), ShAmt, DAG);
10999 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011000 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011001 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011002}
11003
11004
Eric Christopher9a9d2752010-07-22 02:48:34 +000011005SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
11006 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011007
Eric Christopher77ed1352011-07-08 00:04:56 +000011008 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11009 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011010 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011011 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011012 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011013 SDValue Ops[] = {
11014 DAG.getRegister(X86::ESP, MVT::i32), // Base
11015 DAG.getTargetConstant(1, MVT::i8), // Scale
11016 DAG.getRegister(0, MVT::i32), // Index
11017 DAG.getTargetConstant(0, MVT::i32), // Disp
11018 DAG.getRegister(0, MVT::i32), // Segment.
11019 Zero,
11020 Chain
11021 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011022 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011023 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11024 array_lengthof(Ops));
11025 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011026 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011027
Eric Christopher9a9d2752010-07-22 02:48:34 +000011028 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011029 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011030 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011031
Chris Lattner132929a2010-08-14 17:26:09 +000011032 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11033 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11034 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11035 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011036
Chris Lattner132929a2010-08-14 17:26:09 +000011037 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11038 if (!Op1 && !Op2 && !Op3 && Op4)
11039 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011040
Chris Lattner132929a2010-08-14 17:26:09 +000011041 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11042 if (Op1 && !Op2 && !Op3 && !Op4)
11043 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011044
11045 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011046 // (MFENCE)>;
11047 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011048}
11049
Eli Friedman14648462011-07-27 22:21:52 +000011050SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
11051 SelectionDAG &DAG) const {
11052 DebugLoc dl = Op.getDebugLoc();
11053 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11054 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11055 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11056 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11057
11058 // The only fence that needs an instruction is a sequentially-consistent
11059 // cross-thread fence.
11060 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11061 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11062 // no-sse2). There isn't any reason to disable it if the target processor
11063 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011064 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011065 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11066
11067 SDValue Chain = Op.getOperand(0);
11068 SDValue Zero = DAG.getConstant(0, MVT::i32);
11069 SDValue Ops[] = {
11070 DAG.getRegister(X86::ESP, MVT::i32), // Base
11071 DAG.getTargetConstant(1, MVT::i8), // Scale
11072 DAG.getRegister(0, MVT::i32), // Index
11073 DAG.getTargetConstant(0, MVT::i32), // Disp
11074 DAG.getRegister(0, MVT::i32), // Segment.
11075 Zero,
11076 Chain
11077 };
11078 SDNode *Res =
11079 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11080 array_lengthof(Ops));
11081 return SDValue(Res, 0);
11082 }
11083
11084 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11085 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11086}
11087
11088
Dan Gohmand858e902010-04-17 15:26:15 +000011089SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000011090 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011091 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011092 unsigned Reg = 0;
11093 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011094 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011095 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011096 case MVT::i8: Reg = X86::AL; size = 1; break;
11097 case MVT::i16: Reg = X86::AX; size = 2; break;
11098 case MVT::i32: Reg = X86::EAX; size = 4; break;
11099 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011100 assert(Subtarget->is64Bit() && "Node not type legal!");
11101 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011102 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011103 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011104 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011105 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011106 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011107 Op.getOperand(1),
11108 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011109 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011110 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011111 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011112 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11113 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11114 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011115 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011116 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011117 return cpOut;
11118}
11119
Duncan Sands1607f052008-12-01 11:39:25 +000011120SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011121 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000011122 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011123 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011124 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011125 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011126 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011127 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11128 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011129 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011130 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11131 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011132 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011133 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011134 rdx.getValue(1)
11135 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011136 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011137}
11138
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011139SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000011140 SelectionDAG &DAG) const {
11141 EVT SrcVT = Op.getOperand(0).getValueType();
11142 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011143 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011144 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011145 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011146 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011147 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011148 // i64 <=> MMX conversions are Legal.
11149 if (SrcVT==MVT::i64 && DstVT.isVector())
11150 return Op;
11151 if (DstVT==MVT::i64 && SrcVT.isVector())
11152 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011153 // MMX <=> MMX conversions are Legal.
11154 if (SrcVT.isVector() && DstVT.isVector())
11155 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011156 // All other conversions need to be expanded.
11157 return SDValue();
11158}
Chris Lattner5b856542010-12-20 00:59:46 +000011159
Dan Gohmand858e902010-04-17 15:26:15 +000011160SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011161 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011162 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011163 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011164 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011165 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011166 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011167 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011168 Node->getOperand(0),
11169 Node->getOperand(1), negOp,
11170 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011171 cast<AtomicSDNode>(Node)->getAlignment(),
11172 cast<AtomicSDNode>(Node)->getOrdering(),
11173 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011174}
11175
Eli Friedman327236c2011-08-24 20:50:09 +000011176static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11177 SDNode *Node = Op.getNode();
11178 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011179 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011180
11181 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011182 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11183 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11184 // (The only way to get a 16-byte store is cmpxchg16b)
11185 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11186 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11187 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011188 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11189 cast<AtomicSDNode>(Node)->getMemoryVT(),
11190 Node->getOperand(0),
11191 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011192 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011193 cast<AtomicSDNode>(Node)->getOrdering(),
11194 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011195 return Swap.getValue(1);
11196 }
11197 // Other atomic stores have a simple pattern.
11198 return Op;
11199}
11200
Chris Lattner5b856542010-12-20 00:59:46 +000011201static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11202 EVT VT = Op.getNode()->getValueType(0);
11203
11204 // Let legalize expand this if it isn't a legal type yet.
11205 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11206 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011207
Chris Lattner5b856542010-12-20 00:59:46 +000011208 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011209
Chris Lattner5b856542010-12-20 00:59:46 +000011210 unsigned Opc;
11211 bool ExtraOp = false;
11212 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011213 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011214 case ISD::ADDC: Opc = X86ISD::ADD; break;
11215 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11216 case ISD::SUBC: Opc = X86ISD::SUB; break;
11217 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11218 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011219
Chris Lattner5b856542010-12-20 00:59:46 +000011220 if (!ExtraOp)
11221 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11222 Op.getOperand(1));
11223 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11224 Op.getOperand(1), Op.getOperand(2));
11225}
11226
Evan Cheng0db9fe62006-04-25 20:13:52 +000011227/// LowerOperation - Provide custom lowering hooks for some operations.
11228///
Dan Gohmand858e902010-04-17 15:26:15 +000011229SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011230 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011231 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011232 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000011233 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000011234 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011235 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
11236 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011237 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011238 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011239 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011240 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11241 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11242 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000011243 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000011244 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011245 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11246 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11247 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011248 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011249 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011250 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011251 case ISD::SHL_PARTS:
11252 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011253 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011254 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011255 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011256 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011257 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011258 case ISD::FABS: return LowerFABS(Op, DAG);
11259 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011260 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011261 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011262 case ISD::SETCC: return LowerSETCC(Op, DAG);
11263 case ISD::SELECT: return LowerSELECT(Op, DAG);
11264 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011265 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011266 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011267 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011268 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011269 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011270 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011271 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11272 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011273 case ISD::FRAME_TO_ARGS_OFFSET:
11274 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011275 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011276 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011277 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11278 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011279 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011280 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011281 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011282 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011283 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011284 case ISD::SRA:
11285 case ISD::SRL:
11286 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011287 case ISD::SADDO:
11288 case ISD::UADDO:
11289 case ISD::SSUBO:
11290 case ISD::USUBO:
11291 case ISD::SMULO:
11292 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011293 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011294 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011295 case ISD::ADDC:
11296 case ISD::ADDE:
11297 case ISD::SUBC:
11298 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011299 case ISD::ADD: return LowerADD(Op, DAG);
11300 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011301 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011302}
11303
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011304static void ReplaceATOMIC_LOAD(SDNode *Node,
11305 SmallVectorImpl<SDValue> &Results,
11306 SelectionDAG &DAG) {
11307 DebugLoc dl = Node->getDebugLoc();
11308 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11309
11310 // Convert wide load -> cmpxchg8b/cmpxchg16b
11311 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11312 // (The only way to get a 16-byte load is cmpxchg16b)
11313 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011314 SDValue Zero = DAG.getConstant(0, VT);
11315 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011316 Node->getOperand(0),
11317 Node->getOperand(1), Zero, Zero,
11318 cast<AtomicSDNode>(Node)->getMemOperand(),
11319 cast<AtomicSDNode>(Node)->getOrdering(),
11320 cast<AtomicSDNode>(Node)->getSynchScope());
11321 Results.push_back(Swap.getValue(0));
11322 Results.push_back(Swap.getValue(1));
11323}
11324
Craig Topperc0878702012-08-17 06:55:11 +000011325static void
Duncan Sands1607f052008-12-01 11:39:25 +000011326ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011327 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011328 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011329 assert (Node->getValueType(0) == MVT::i64 &&
11330 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011331
11332 SDValue Chain = Node->getOperand(0);
11333 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011334 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011335 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011336 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011337 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011338 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011339 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011340 SDValue Result =
11341 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11342 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011343 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011344 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011345 Results.push_back(Result.getValue(2));
11346}
11347
Duncan Sands126d9072008-07-04 11:47:58 +000011348/// ReplaceNodeResults - Replace a node with an illegal result type
11349/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011350void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11351 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011352 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011353 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011354 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011355 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011356 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011357 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011358 case ISD::ADDC:
11359 case ISD::ADDE:
11360 case ISD::SUBC:
11361 case ISD::SUBE:
11362 // We don't want to expand or promote these.
11363 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011364 case ISD::FP_TO_SINT:
11365 case ISD::FP_TO_UINT: {
11366 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11367
11368 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11369 return;
11370
Eli Friedman948e95a2009-05-23 09:59:16 +000011371 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011372 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011373 SDValue FIST = Vals.first, StackSlot = Vals.second;
11374 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011375 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011376 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011377 if (StackSlot.getNode() != 0)
11378 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11379 MachinePointerInfo(),
11380 false, false, false, 0));
11381 else
11382 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011383 }
11384 return;
11385 }
11386 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011387 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011388 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011389 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011390 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011391 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011392 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011393 eax.getValue(2));
11394 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11395 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011396 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011397 Results.push_back(edx.getValue(1));
11398 return;
11399 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011400 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011401 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011402 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011403 bool Regs64bit = T == MVT::i128;
11404 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011405 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011406 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11407 DAG.getConstant(0, HalfT));
11408 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11409 DAG.getConstant(1, HalfT));
11410 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11411 Regs64bit ? X86::RAX : X86::EAX,
11412 cpInL, SDValue());
11413 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11414 Regs64bit ? X86::RDX : X86::EDX,
11415 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011416 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011417 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11418 DAG.getConstant(0, HalfT));
11419 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11420 DAG.getConstant(1, HalfT));
11421 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11422 Regs64bit ? X86::RBX : X86::EBX,
11423 swapInL, cpInH.getValue(1));
11424 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011425 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011426 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011427 SDValue Ops[] = { swapInH.getValue(0),
11428 N->getOperand(1),
11429 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011430 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011431 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011432 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11433 X86ISD::LCMPXCHG8_DAG;
11434 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011435 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011436 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11437 Regs64bit ? X86::RAX : X86::EAX,
11438 HalfT, Result.getValue(1));
11439 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11440 Regs64bit ? X86::RDX : X86::EDX,
11441 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011442 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011443 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011444 Results.push_back(cpOutH.getValue(1));
11445 return;
11446 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011447 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011448 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011449 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011450 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011451 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011452 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011453 case ISD::ATOMIC_SWAP: {
11454 unsigned Opc;
11455 switch (N->getOpcode()) {
11456 default: llvm_unreachable("Unexpected opcode");
11457 case ISD::ATOMIC_LOAD_ADD:
11458 Opc = X86ISD::ATOMADD64_DAG;
11459 break;
11460 case ISD::ATOMIC_LOAD_AND:
11461 Opc = X86ISD::ATOMAND64_DAG;
11462 break;
11463 case ISD::ATOMIC_LOAD_NAND:
11464 Opc = X86ISD::ATOMNAND64_DAG;
11465 break;
11466 case ISD::ATOMIC_LOAD_OR:
11467 Opc = X86ISD::ATOMOR64_DAG;
11468 break;
11469 case ISD::ATOMIC_LOAD_SUB:
11470 Opc = X86ISD::ATOMSUB64_DAG;
11471 break;
11472 case ISD::ATOMIC_LOAD_XOR:
11473 Opc = X86ISD::ATOMXOR64_DAG;
11474 break;
11475 case ISD::ATOMIC_SWAP:
11476 Opc = X86ISD::ATOMSWAP64_DAG;
11477 break;
11478 }
11479 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011480 return;
Craig Topperc0878702012-08-17 06:55:11 +000011481 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011482 case ISD::ATOMIC_LOAD:
11483 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011484 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011485}
11486
Evan Cheng72261582005-12-20 06:22:03 +000011487const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11488 switch (Opcode) {
11489 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011490 case X86ISD::BSF: return "X86ISD::BSF";
11491 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011492 case X86ISD::SHLD: return "X86ISD::SHLD";
11493 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011494 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011495 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011496 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011497 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011498 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011499 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011500 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11501 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11502 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011503 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011504 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011505 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011506 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011507 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011508 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011509 case X86ISD::COMI: return "X86ISD::COMI";
11510 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011511 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011512 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011513 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11514 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011515 case X86ISD::CMOV: return "X86ISD::CMOV";
11516 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011517 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011518 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11519 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011520 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011521 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011522 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011523 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011524 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011525 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11526 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011527 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011528 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011529 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011530 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011531 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011532 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11533 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11534 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011535 case X86ISD::HADD: return "X86ISD::HADD";
11536 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011537 case X86ISD::FHADD: return "X86ISD::FHADD";
11538 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011539 case X86ISD::FMAX: return "X86ISD::FMAX";
11540 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011541 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11542 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011543 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11544 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011545 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011546 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011547 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011548 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011549 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011550 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011551 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011552 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11553 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011554 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11555 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11556 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11557 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11558 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11559 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011560 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011561 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011562 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011563 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011564 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11565 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011566 case X86ISD::VSHL: return "X86ISD::VSHL";
11567 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011568 case X86ISD::VSRA: return "X86ISD::VSRA";
11569 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11570 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11571 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011572 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011573 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11574 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011575 case X86ISD::ADD: return "X86ISD::ADD";
11576 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011577 case X86ISD::ADC: return "X86ISD::ADC";
11578 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011579 case X86ISD::SMUL: return "X86ISD::SMUL";
11580 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011581 case X86ISD::INC: return "X86ISD::INC";
11582 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011583 case X86ISD::OR: return "X86ISD::OR";
11584 case X86ISD::XOR: return "X86ISD::XOR";
11585 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011586 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011587 case X86ISD::BLSI: return "X86ISD::BLSI";
11588 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11589 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011590 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011591 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011592 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011593 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11594 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11595 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011596 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011597 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011598 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011599 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011600 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011601 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11602 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011603 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11604 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11605 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011606 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11607 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011608 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11609 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011610 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011611 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011612 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011613 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11614 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011615 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011616 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011617 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011618 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011619 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011620 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011621 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011622 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011623 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011624 case X86ISD::FMADD: return "X86ISD::FMADD";
11625 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11626 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11627 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11628 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11629 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011630 }
11631}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011632
Chris Lattnerc9addb72007-03-30 23:15:24 +000011633// isLegalAddressingMode - Return true if the addressing mode represented
11634// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011635bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011636 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011637 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011638 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011639 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011640
Chris Lattnerc9addb72007-03-30 23:15:24 +000011641 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011642 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011643 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011644
Chris Lattnerc9addb72007-03-30 23:15:24 +000011645 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011646 unsigned GVFlags =
11647 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011648
Chris Lattnerdfed4132009-07-10 07:38:24 +000011649 // If a reference to this global requires an extra load, we can't fold it.
11650 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011651 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011652
Chris Lattnerdfed4132009-07-10 07:38:24 +000011653 // If BaseGV requires a register for the PIC base, we cannot also have a
11654 // BaseReg specified.
11655 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011656 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011657
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011658 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011659 if ((M != CodeModel::Small || R != Reloc::Static) &&
11660 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011661 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011662 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011663
Chris Lattnerc9addb72007-03-30 23:15:24 +000011664 switch (AM.Scale) {
11665 case 0:
11666 case 1:
11667 case 2:
11668 case 4:
11669 case 8:
11670 // These scales always work.
11671 break;
11672 case 3:
11673 case 5:
11674 case 9:
11675 // These scales are formed with basereg+scalereg. Only accept if there is
11676 // no basereg yet.
11677 if (AM.HasBaseReg)
11678 return false;
11679 break;
11680 default: // Other stuff never works.
11681 return false;
11682 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011683
Chris Lattnerc9addb72007-03-30 23:15:24 +000011684 return true;
11685}
11686
11687
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011688bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011689 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011690 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011691 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11692 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011693 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011694 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011695 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011696}
11697
Evan Cheng70e10d32012-07-17 06:53:39 +000011698bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11699 return Imm == (int32_t)Imm;
11700}
11701
11702bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011703 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011704 return Imm == (int32_t)Imm;
11705}
11706
Owen Andersone50ed302009-08-10 22:56:29 +000011707bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011708 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011709 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011710 unsigned NumBits1 = VT1.getSizeInBits();
11711 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011712 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011713 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011714 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011715}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011716
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011717bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011718 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011719 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011720}
11721
Owen Andersone50ed302009-08-10 22:56:29 +000011722bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011723 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011724 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011725}
11726
Owen Andersone50ed302009-08-10 22:56:29 +000011727bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011728 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011729 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011730}
11731
Evan Cheng60c07e12006-07-05 22:17:51 +000011732/// isShuffleMaskLegal - Targets can use this to indicate that they only
11733/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11734/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11735/// are assumed to be legal.
11736bool
Eric Christopherfd179292009-08-27 18:07:15 +000011737X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011738 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011739 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011740 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011741 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011742
Nate Begemana09008b2009-10-19 02:17:23 +000011743 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011744 return (VT.getVectorNumElements() == 2 ||
11745 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11746 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011747 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011748 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011749 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11750 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011751 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011752 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11753 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011754 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11755 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011756}
11757
Dan Gohman7d8143f2008-04-09 20:09:42 +000011758bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011759X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011760 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011761 unsigned NumElts = VT.getVectorNumElements();
11762 // FIXME: This collection of masks seems suspect.
11763 if (NumElts == 2)
11764 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011765 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011766 return (isMOVLMask(Mask, VT) ||
11767 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011768 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11769 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011770 }
11771 return false;
11772}
11773
11774//===----------------------------------------------------------------------===//
11775// X86 Scheduler Hooks
11776//===----------------------------------------------------------------------===//
11777
Mon P Wang63307c32008-05-05 19:05:59 +000011778// private utility function
11779MachineBasicBlock *
11780X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11781 MachineBasicBlock *MBB,
11782 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011783 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011784 unsigned LoadOpc,
11785 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011786 unsigned notOpc,
11787 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011788 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011789 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011790 // For the atomic bitwise operator, we generate
11791 // thisMBB:
11792 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011793 // ld t1 = [bitinstr.addr]
11794 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011795 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011796 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011797 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011798 // bz newMBB
11799 // fallthrough -->nextMBB
11800 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11801 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011802 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011803 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011804
Mon P Wang63307c32008-05-05 19:05:59 +000011805 /// First build the CFG
11806 MachineFunction *F = MBB->getParent();
11807 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011808 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11809 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11810 F->insert(MBBIter, newMBB);
11811 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011812
Dan Gohman14152b42010-07-06 20:24:04 +000011813 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11814 nextMBB->splice(nextMBB->begin(), thisMBB,
11815 llvm::next(MachineBasicBlock::iterator(bInstr)),
11816 thisMBB->end());
11817 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011818
Mon P Wang63307c32008-05-05 19:05:59 +000011819 // Update thisMBB to fall through to newMBB
11820 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011821
Mon P Wang63307c32008-05-05 19:05:59 +000011822 // newMBB jumps to itself and fall through to nextMBB
11823 newMBB->addSuccessor(nextMBB);
11824 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011825
Mon P Wang63307c32008-05-05 19:05:59 +000011826 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011827 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011828 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011829 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011830 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011831 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011832 int numArgs = bInstr->getNumOperands() - 1;
11833 for (int i=0; i < numArgs; ++i)
11834 argOpers[i] = &bInstr->getOperand(i+1);
11835
11836 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011837 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011838 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011839
Dale Johannesen140be2d2008-08-19 18:47:28 +000011840 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011841 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011842 for (int i=0; i <= lastAddrIndx; ++i)
11843 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011844
Dale Johannesen140be2d2008-08-19 18:47:28 +000011845 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011846 assert((argOpers[valArgIndx]->isReg() ||
11847 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011848 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011849 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011850 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011851 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011852 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011853 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011854 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011855
Richard Smith42fc29e2012-04-13 22:47:00 +000011856 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11857 if (Invert) {
11858 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11859 }
11860 else
11861 t3 = t2;
11862
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011863 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011864 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011865
Dale Johannesene4d209d2009-02-03 20:21:25 +000011866 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011867 for (int i=0; i <= lastAddrIndx; ++i)
11868 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011869 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011870 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011871 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11872 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011873
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011874 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011875 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011876
Mon P Wang63307c32008-05-05 19:05:59 +000011877 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011878 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011879
Dan Gohman14152b42010-07-06 20:24:04 +000011880 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011881 return nextMBB;
11882}
11883
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011884// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011885MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011886X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11887 MachineBasicBlock *MBB,
11888 unsigned regOpcL,
11889 unsigned regOpcH,
11890 unsigned immOpcL,
11891 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011892 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011893 // For the atomic bitwise operator, we generate
11894 // thisMBB (instructions are in pairs, except cmpxchg8b)
11895 // ld t1,t2 = [bitinstr.addr]
11896 // newMBB:
11897 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11898 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011899 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011900 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011901 // mov ECX, EBX <- t5, t6
11902 // mov EAX, EDX <- t1, t2
11903 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11904 // mov t3, t4 <- EAX, EDX
11905 // bz newMBB
11906 // result in out1, out2
11907 // fallthrough -->nextMBB
11908
Craig Topperc9099502012-04-20 06:31:50 +000011909 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011910 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011911 const unsigned NotOpc = X86::NOT32r;
11912 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11913 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11914 MachineFunction::iterator MBBIter = MBB;
11915 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011916
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011917 /// First build the CFG
11918 MachineFunction *F = MBB->getParent();
11919 MachineBasicBlock *thisMBB = MBB;
11920 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11921 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11922 F->insert(MBBIter, newMBB);
11923 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011924
Dan Gohman14152b42010-07-06 20:24:04 +000011925 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11926 nextMBB->splice(nextMBB->begin(), thisMBB,
11927 llvm::next(MachineBasicBlock::iterator(bInstr)),
11928 thisMBB->end());
11929 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011930
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011931 // Update thisMBB to fall through to newMBB
11932 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011933
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011934 // newMBB jumps to itself and fall through to nextMBB
11935 newMBB->addSuccessor(nextMBB);
11936 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011937
Dale Johannesene4d209d2009-02-03 20:21:25 +000011938 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011939 // Insert instructions into newMBB based on incoming instruction
11940 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011941 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011942 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011943 MachineOperand& dest1Oper = bInstr->getOperand(0);
11944 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011945 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11946 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011947 argOpers[i] = &bInstr->getOperand(i+2);
11948
Dan Gohman71ea4e52010-05-14 21:01:44 +000011949 // We use some of the operands multiple times, so conservatively just
11950 // clear any kill flags that might be present.
11951 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11952 argOpers[i]->setIsKill(false);
11953 }
11954
Evan Chengad5b52f2010-01-08 19:14:57 +000011955 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011956 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011957
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011958 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011959 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011960 for (int i=0; i <= lastAddrIndx; ++i)
11961 (*MIB).addOperand(*argOpers[i]);
11962 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011963 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011964 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011965 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011966 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011967 MachineOperand newOp3 = *(argOpers[3]);
11968 if (newOp3.isImm())
11969 newOp3.setImm(newOp3.getImm()+4);
11970 else
11971 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011972 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011973 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011974
11975 // t3/4 are defined later, at the bottom of the loop
11976 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11977 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011978 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011979 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011980 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011981 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11982
Evan Cheng306b4ca2010-01-08 23:41:50 +000011983 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000011984 // the PHI instructions.
11985 t1 = dest1Oper.getReg();
11986 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011987
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011988 int valArgIndx = lastAddrIndx + 1;
11989 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011990 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011991 "invalid operand");
11992 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11993 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011994 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011995 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011996 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011997 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011998 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000011999 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012000 (*MIB).addOperand(*argOpers[valArgIndx]);
12001 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012002 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012003 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012004 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012005 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012006 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012007 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012008 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000012009 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012010 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012011 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012012
Richard Smith42fc29e2012-04-13 22:47:00 +000012013 unsigned t7, t8;
12014 if (Invert) {
12015 t7 = F->getRegInfo().createVirtualRegister(RC);
12016 t8 = F->getRegInfo().createVirtualRegister(RC);
12017 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
12018 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
12019 } else {
12020 t7 = t5;
12021 t8 = t6;
12022 }
12023
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012024 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012025 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012026 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012027 MIB.addReg(t2);
12028
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012029 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012030 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012031 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012032 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000012033
Dale Johannesene4d209d2009-02-03 20:21:25 +000012034 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012035 for (int i=0; i <= lastAddrIndx; ++i)
12036 (*MIB).addOperand(*argOpers[i]);
12037
12038 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012039 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12040 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012041
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012042 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012043 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012044 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012045 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012046
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012047 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012048 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012049
Dan Gohman14152b42010-07-06 20:24:04 +000012050 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012051 return nextMBB;
12052}
12053
12054// private utility function
12055MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012056X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12057 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012058 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012059 // For the atomic min/max operator, we generate
12060 // thisMBB:
12061 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012062 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012063 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012064 // cmp t1, t2
12065 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012066 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012067 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12068 // bz newMBB
12069 // fallthrough -->nextMBB
12070 //
12071 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12072 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012073 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012074 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012075
Mon P Wang63307c32008-05-05 19:05:59 +000012076 /// First build the CFG
12077 MachineFunction *F = MBB->getParent();
12078 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012079 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12080 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12081 F->insert(MBBIter, newMBB);
12082 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012083
Dan Gohman14152b42010-07-06 20:24:04 +000012084 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12085 nextMBB->splice(nextMBB->begin(), thisMBB,
12086 llvm::next(MachineBasicBlock::iterator(mInstr)),
12087 thisMBB->end());
12088 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012089
Mon P Wang63307c32008-05-05 19:05:59 +000012090 // Update thisMBB to fall through to newMBB
12091 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012092
Mon P Wang63307c32008-05-05 19:05:59 +000012093 // newMBB jumps to newMBB and fall through to nextMBB
12094 newMBB->addSuccessor(nextMBB);
12095 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012096
Dale Johannesene4d209d2009-02-03 20:21:25 +000012097 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012098 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012099 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012100 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012101 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012102 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012103 int numArgs = mInstr->getNumOperands() - 1;
12104 for (int i=0; i < numArgs; ++i)
12105 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012106
Mon P Wang63307c32008-05-05 19:05:59 +000012107 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012108 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012109 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012110
Craig Topperc9099502012-04-20 06:31:50 +000012111 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012112 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012113 for (int i=0; i <= lastAddrIndx; ++i)
12114 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012115
Mon P Wang63307c32008-05-05 19:05:59 +000012116 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012117 assert((argOpers[valArgIndx]->isReg() ||
12118 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012119 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012120
Craig Topperc9099502012-04-20 06:31:50 +000012121 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012122 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012123 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012124 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012125 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012126 (*MIB).addOperand(*argOpers[valArgIndx]);
12127
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012128 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012129 MIB.addReg(t1);
12130
Dale Johannesene4d209d2009-02-03 20:21:25 +000012131 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012132 MIB.addReg(t1);
12133 MIB.addReg(t2);
12134
12135 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012136 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012137 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012138 MIB.addReg(t2);
12139 MIB.addReg(t1);
12140
12141 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012142 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012143 for (int i=0; i <= lastAddrIndx; ++i)
12144 (*MIB).addOperand(*argOpers[i]);
12145 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012146 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012147 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12148 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012149
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012150 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012151 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012152
Mon P Wang63307c32008-05-05 19:05:59 +000012153 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012154 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012155
Dan Gohman14152b42010-07-06 20:24:04 +000012156 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012157 return nextMBB;
12158}
12159
Eric Christopherf83a5de2009-08-27 18:08:16 +000012160// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012161// or XMM0_V32I8 in AVX all of this code can be replaced with that
12162// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012163MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012164X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012165 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012166 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012167 "Target must have SSE4.2 or AVX features enabled");
12168
Eric Christopherb120ab42009-08-18 22:50:32 +000012169 DebugLoc dl = MI->getDebugLoc();
12170 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012171 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012172 if (!Subtarget->hasAVX()) {
12173 if (memArg)
12174 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12175 else
12176 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12177 } else {
12178 if (memArg)
12179 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12180 else
12181 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12182 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012183
Eric Christopher41c902f2010-11-30 08:20:21 +000012184 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012185 for (unsigned i = 0; i < numArgs; ++i) {
12186 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012187 if (!(Op.isReg() && Op.isImplicit()))
12188 MIB.addOperand(Op);
12189 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012190 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012191 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012192 .addReg(X86::XMM0);
12193
Dan Gohman14152b42010-07-06 20:24:04 +000012194 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012195 return BB;
12196}
12197
12198MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012199X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012200 DebugLoc dl = MI->getDebugLoc();
12201 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012202
Eric Christopher228232b2010-11-30 07:20:12 +000012203 // Address into RAX/EAX, other two args into ECX, EDX.
12204 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12205 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12206 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12207 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012208 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012209
Eric Christopher228232b2010-11-30 07:20:12 +000012210 unsigned ValOps = X86::AddrNumOperands;
12211 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12212 .addReg(MI->getOperand(ValOps).getReg());
12213 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12214 .addReg(MI->getOperand(ValOps+1).getReg());
12215
12216 // The instruction doesn't actually take any operands though.
12217 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012218
Eric Christopher228232b2010-11-30 07:20:12 +000012219 MI->eraseFromParent(); // The pseudo is gone now.
12220 return BB;
12221}
12222
12223MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012224X86TargetLowering::EmitVAARG64WithCustomInserter(
12225 MachineInstr *MI,
12226 MachineBasicBlock *MBB) const {
12227 // Emit va_arg instruction on X86-64.
12228
12229 // Operands to this pseudo-instruction:
12230 // 0 ) Output : destination address (reg)
12231 // 1-5) Input : va_list address (addr, i64mem)
12232 // 6 ) ArgSize : Size (in bytes) of vararg type
12233 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12234 // 8 ) Align : Alignment of type
12235 // 9 ) EFLAGS (implicit-def)
12236
12237 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12238 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12239
12240 unsigned DestReg = MI->getOperand(0).getReg();
12241 MachineOperand &Base = MI->getOperand(1);
12242 MachineOperand &Scale = MI->getOperand(2);
12243 MachineOperand &Index = MI->getOperand(3);
12244 MachineOperand &Disp = MI->getOperand(4);
12245 MachineOperand &Segment = MI->getOperand(5);
12246 unsigned ArgSize = MI->getOperand(6).getImm();
12247 unsigned ArgMode = MI->getOperand(7).getImm();
12248 unsigned Align = MI->getOperand(8).getImm();
12249
12250 // Memory Reference
12251 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12252 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12253 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12254
12255 // Machine Information
12256 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12257 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12258 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12259 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12260 DebugLoc DL = MI->getDebugLoc();
12261
12262 // struct va_list {
12263 // i32 gp_offset
12264 // i32 fp_offset
12265 // i64 overflow_area (address)
12266 // i64 reg_save_area (address)
12267 // }
12268 // sizeof(va_list) = 24
12269 // alignment(va_list) = 8
12270
12271 unsigned TotalNumIntRegs = 6;
12272 unsigned TotalNumXMMRegs = 8;
12273 bool UseGPOffset = (ArgMode == 1);
12274 bool UseFPOffset = (ArgMode == 2);
12275 unsigned MaxOffset = TotalNumIntRegs * 8 +
12276 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12277
12278 /* Align ArgSize to a multiple of 8 */
12279 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12280 bool NeedsAlign = (Align > 8);
12281
12282 MachineBasicBlock *thisMBB = MBB;
12283 MachineBasicBlock *overflowMBB;
12284 MachineBasicBlock *offsetMBB;
12285 MachineBasicBlock *endMBB;
12286
12287 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12288 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12289 unsigned OffsetReg = 0;
12290
12291 if (!UseGPOffset && !UseFPOffset) {
12292 // If we only pull from the overflow region, we don't create a branch.
12293 // We don't need to alter control flow.
12294 OffsetDestReg = 0; // unused
12295 OverflowDestReg = DestReg;
12296
12297 offsetMBB = NULL;
12298 overflowMBB = thisMBB;
12299 endMBB = thisMBB;
12300 } else {
12301 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12302 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12303 // If not, pull from overflow_area. (branch to overflowMBB)
12304 //
12305 // thisMBB
12306 // | .
12307 // | .
12308 // offsetMBB overflowMBB
12309 // | .
12310 // | .
12311 // endMBB
12312
12313 // Registers for the PHI in endMBB
12314 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12315 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12316
12317 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12318 MachineFunction *MF = MBB->getParent();
12319 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12320 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12321 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12322
12323 MachineFunction::iterator MBBIter = MBB;
12324 ++MBBIter;
12325
12326 // Insert the new basic blocks
12327 MF->insert(MBBIter, offsetMBB);
12328 MF->insert(MBBIter, overflowMBB);
12329 MF->insert(MBBIter, endMBB);
12330
12331 // Transfer the remainder of MBB and its successor edges to endMBB.
12332 endMBB->splice(endMBB->begin(), thisMBB,
12333 llvm::next(MachineBasicBlock::iterator(MI)),
12334 thisMBB->end());
12335 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12336
12337 // Make offsetMBB and overflowMBB successors of thisMBB
12338 thisMBB->addSuccessor(offsetMBB);
12339 thisMBB->addSuccessor(overflowMBB);
12340
12341 // endMBB is a successor of both offsetMBB and overflowMBB
12342 offsetMBB->addSuccessor(endMBB);
12343 overflowMBB->addSuccessor(endMBB);
12344
12345 // Load the offset value into a register
12346 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12347 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12348 .addOperand(Base)
12349 .addOperand(Scale)
12350 .addOperand(Index)
12351 .addDisp(Disp, UseFPOffset ? 4 : 0)
12352 .addOperand(Segment)
12353 .setMemRefs(MMOBegin, MMOEnd);
12354
12355 // Check if there is enough room left to pull this argument.
12356 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12357 .addReg(OffsetReg)
12358 .addImm(MaxOffset + 8 - ArgSizeA8);
12359
12360 // Branch to "overflowMBB" if offset >= max
12361 // Fall through to "offsetMBB" otherwise
12362 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12363 .addMBB(overflowMBB);
12364 }
12365
12366 // In offsetMBB, emit code to use the reg_save_area.
12367 if (offsetMBB) {
12368 assert(OffsetReg != 0);
12369
12370 // Read the reg_save_area address.
12371 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12372 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12373 .addOperand(Base)
12374 .addOperand(Scale)
12375 .addOperand(Index)
12376 .addDisp(Disp, 16)
12377 .addOperand(Segment)
12378 .setMemRefs(MMOBegin, MMOEnd);
12379
12380 // Zero-extend the offset
12381 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12382 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12383 .addImm(0)
12384 .addReg(OffsetReg)
12385 .addImm(X86::sub_32bit);
12386
12387 // Add the offset to the reg_save_area to get the final address.
12388 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12389 .addReg(OffsetReg64)
12390 .addReg(RegSaveReg);
12391
12392 // Compute the offset for the next argument
12393 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12394 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12395 .addReg(OffsetReg)
12396 .addImm(UseFPOffset ? 16 : 8);
12397
12398 // Store it back into the va_list.
12399 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12400 .addOperand(Base)
12401 .addOperand(Scale)
12402 .addOperand(Index)
12403 .addDisp(Disp, UseFPOffset ? 4 : 0)
12404 .addOperand(Segment)
12405 .addReg(NextOffsetReg)
12406 .setMemRefs(MMOBegin, MMOEnd);
12407
12408 // Jump to endMBB
12409 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12410 .addMBB(endMBB);
12411 }
12412
12413 //
12414 // Emit code to use overflow area
12415 //
12416
12417 // Load the overflow_area address into a register.
12418 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12419 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12420 .addOperand(Base)
12421 .addOperand(Scale)
12422 .addOperand(Index)
12423 .addDisp(Disp, 8)
12424 .addOperand(Segment)
12425 .setMemRefs(MMOBegin, MMOEnd);
12426
12427 // If we need to align it, do so. Otherwise, just copy the address
12428 // to OverflowDestReg.
12429 if (NeedsAlign) {
12430 // Align the overflow address
12431 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12432 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12433
12434 // aligned_addr = (addr + (align-1)) & ~(align-1)
12435 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12436 .addReg(OverflowAddrReg)
12437 .addImm(Align-1);
12438
12439 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12440 .addReg(TmpReg)
12441 .addImm(~(uint64_t)(Align-1));
12442 } else {
12443 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12444 .addReg(OverflowAddrReg);
12445 }
12446
12447 // Compute the next overflow address after this argument.
12448 // (the overflow address should be kept 8-byte aligned)
12449 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12450 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12451 .addReg(OverflowDestReg)
12452 .addImm(ArgSizeA8);
12453
12454 // Store the new overflow address.
12455 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12456 .addOperand(Base)
12457 .addOperand(Scale)
12458 .addOperand(Index)
12459 .addDisp(Disp, 8)
12460 .addOperand(Segment)
12461 .addReg(NextAddrReg)
12462 .setMemRefs(MMOBegin, MMOEnd);
12463
12464 // If we branched, emit the PHI to the front of endMBB.
12465 if (offsetMBB) {
12466 BuildMI(*endMBB, endMBB->begin(), DL,
12467 TII->get(X86::PHI), DestReg)
12468 .addReg(OffsetDestReg).addMBB(offsetMBB)
12469 .addReg(OverflowDestReg).addMBB(overflowMBB);
12470 }
12471
12472 // Erase the pseudo instruction
12473 MI->eraseFromParent();
12474
12475 return endMBB;
12476}
12477
12478MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012479X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12480 MachineInstr *MI,
12481 MachineBasicBlock *MBB) const {
12482 // Emit code to save XMM registers to the stack. The ABI says that the
12483 // number of registers to save is given in %al, so it's theoretically
12484 // possible to do an indirect jump trick to avoid saving all of them,
12485 // however this code takes a simpler approach and just executes all
12486 // of the stores if %al is non-zero. It's less code, and it's probably
12487 // easier on the hardware branch predictor, and stores aren't all that
12488 // expensive anyway.
12489
12490 // Create the new basic blocks. One block contains all the XMM stores,
12491 // and one block is the final destination regardless of whether any
12492 // stores were performed.
12493 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12494 MachineFunction *F = MBB->getParent();
12495 MachineFunction::iterator MBBIter = MBB;
12496 ++MBBIter;
12497 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12498 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12499 F->insert(MBBIter, XMMSaveMBB);
12500 F->insert(MBBIter, EndMBB);
12501
Dan Gohman14152b42010-07-06 20:24:04 +000012502 // Transfer the remainder of MBB and its successor edges to EndMBB.
12503 EndMBB->splice(EndMBB->begin(), MBB,
12504 llvm::next(MachineBasicBlock::iterator(MI)),
12505 MBB->end());
12506 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12507
Dan Gohmand6708ea2009-08-15 01:38:56 +000012508 // The original block will now fall through to the XMM save block.
12509 MBB->addSuccessor(XMMSaveMBB);
12510 // The XMMSaveMBB will fall through to the end block.
12511 XMMSaveMBB->addSuccessor(EndMBB);
12512
12513 // Now add the instructions.
12514 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12515 DebugLoc DL = MI->getDebugLoc();
12516
12517 unsigned CountReg = MI->getOperand(0).getReg();
12518 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12519 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12520
12521 if (!Subtarget->isTargetWin64()) {
12522 // If %al is 0, branch around the XMM save block.
12523 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012524 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012525 MBB->addSuccessor(EndMBB);
12526 }
12527
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012528 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012529 // In the XMM save block, save all the XMM argument registers.
12530 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12531 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012532 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012533 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012534 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012535 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012536 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012537 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012538 .addFrameIndex(RegSaveFrameIndex)
12539 .addImm(/*Scale=*/1)
12540 .addReg(/*IndexReg=*/0)
12541 .addImm(/*Disp=*/Offset)
12542 .addReg(/*Segment=*/0)
12543 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012544 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012545 }
12546
Dan Gohman14152b42010-07-06 20:24:04 +000012547 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012548
12549 return EndMBB;
12550}
Mon P Wang63307c32008-05-05 19:05:59 +000012551
Lang Hames6e3f7e42012-02-03 01:13:49 +000012552// The EFLAGS operand of SelectItr might be missing a kill marker
12553// because there were multiple uses of EFLAGS, and ISel didn't know
12554// which to mark. Figure out whether SelectItr should have had a
12555// kill marker, and set it if it should. Returns the correct kill
12556// marker value.
12557static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12558 MachineBasicBlock* BB,
12559 const TargetRegisterInfo* TRI) {
12560 // Scan forward through BB for a use/def of EFLAGS.
12561 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12562 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012563 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012564 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012565 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012566 if (mi.definesRegister(X86::EFLAGS))
12567 break; // Should have kill-flag - update below.
12568 }
12569
12570 // If we hit the end of the block, check whether EFLAGS is live into a
12571 // successor.
12572 if (miI == BB->end()) {
12573 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12574 sEnd = BB->succ_end();
12575 sItr != sEnd; ++sItr) {
12576 MachineBasicBlock* succ = *sItr;
12577 if (succ->isLiveIn(X86::EFLAGS))
12578 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012579 }
12580 }
12581
Lang Hames6e3f7e42012-02-03 01:13:49 +000012582 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12583 // out. SelectMI should have a kill flag on EFLAGS.
12584 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012585 return true;
12586}
12587
Evan Cheng60c07e12006-07-05 22:17:51 +000012588MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012589X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012590 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012591 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12592 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012593
Chris Lattner52600972009-09-02 05:57:00 +000012594 // To "insert" a SELECT_CC instruction, we actually have to insert the
12595 // diamond control-flow pattern. The incoming instruction knows the
12596 // destination vreg to set, the condition code register to branch on, the
12597 // true/false values to select between, and a branch opcode to use.
12598 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12599 MachineFunction::iterator It = BB;
12600 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012601
Chris Lattner52600972009-09-02 05:57:00 +000012602 // thisMBB:
12603 // ...
12604 // TrueVal = ...
12605 // cmpTY ccX, r1, r2
12606 // bCC copy1MBB
12607 // fallthrough --> copy0MBB
12608 MachineBasicBlock *thisMBB = BB;
12609 MachineFunction *F = BB->getParent();
12610 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12611 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012612 F->insert(It, copy0MBB);
12613 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012614
Bill Wendling730c07e2010-06-25 20:48:10 +000012615 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12616 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012617 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12618 if (!MI->killsRegister(X86::EFLAGS) &&
12619 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12620 copy0MBB->addLiveIn(X86::EFLAGS);
12621 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012622 }
12623
Dan Gohman14152b42010-07-06 20:24:04 +000012624 // Transfer the remainder of BB and its successor edges to sinkMBB.
12625 sinkMBB->splice(sinkMBB->begin(), BB,
12626 llvm::next(MachineBasicBlock::iterator(MI)),
12627 BB->end());
12628 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12629
12630 // Add the true and fallthrough blocks as its successors.
12631 BB->addSuccessor(copy0MBB);
12632 BB->addSuccessor(sinkMBB);
12633
12634 // Create the conditional branch instruction.
12635 unsigned Opc =
12636 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12637 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12638
Chris Lattner52600972009-09-02 05:57:00 +000012639 // copy0MBB:
12640 // %FalseValue = ...
12641 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012642 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012643
Chris Lattner52600972009-09-02 05:57:00 +000012644 // sinkMBB:
12645 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12646 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012647 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12648 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012649 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12650 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12651
Dan Gohman14152b42010-07-06 20:24:04 +000012652 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012653 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012654}
12655
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012656MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012657X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12658 bool Is64Bit) const {
12659 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12660 DebugLoc DL = MI->getDebugLoc();
12661 MachineFunction *MF = BB->getParent();
12662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12663
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012664 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012665
12666 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12667 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12668
12669 // BB:
12670 // ... [Till the alloca]
12671 // If stacklet is not large enough, jump to mallocMBB
12672 //
12673 // bumpMBB:
12674 // Allocate by subtracting from RSP
12675 // Jump to continueMBB
12676 //
12677 // mallocMBB:
12678 // Allocate by call to runtime
12679 //
12680 // continueMBB:
12681 // ...
12682 // [rest of original BB]
12683 //
12684
12685 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12686 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12687 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12688
12689 MachineRegisterInfo &MRI = MF->getRegInfo();
12690 const TargetRegisterClass *AddrRegClass =
12691 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12692
12693 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12694 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12695 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012696 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012697 sizeVReg = MI->getOperand(1).getReg(),
12698 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12699
12700 MachineFunction::iterator MBBIter = BB;
12701 ++MBBIter;
12702
12703 MF->insert(MBBIter, bumpMBB);
12704 MF->insert(MBBIter, mallocMBB);
12705 MF->insert(MBBIter, continueMBB);
12706
12707 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12708 (MachineBasicBlock::iterator(MI)), BB->end());
12709 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12710
12711 // Add code to the main basic block to check if the stack limit has been hit,
12712 // and if so, jump to mallocMBB otherwise to bumpMBB.
12713 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012714 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012715 .addReg(tmpSPVReg).addReg(sizeVReg);
12716 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012717 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012718 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012719 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12720
12721 // bumpMBB simply decreases the stack pointer, since we know the current
12722 // stacklet has enough space.
12723 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012724 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012725 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012726 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012727 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12728
12729 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012730 const uint32_t *RegMask =
12731 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012732 if (Is64Bit) {
12733 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12734 .addReg(sizeVReg);
12735 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012736 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012737 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012738 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012739 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012740 } else {
12741 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12742 .addImm(12);
12743 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12744 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012745 .addExternalSymbol("__morestack_allocate_stack_space")
12746 .addRegMask(RegMask)
12747 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012748 }
12749
12750 if (!Is64Bit)
12751 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12752 .addImm(16);
12753
12754 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12755 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12756 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12757
12758 // Set up the CFG correctly.
12759 BB->addSuccessor(bumpMBB);
12760 BB->addSuccessor(mallocMBB);
12761 mallocMBB->addSuccessor(continueMBB);
12762 bumpMBB->addSuccessor(continueMBB);
12763
12764 // Take care of the PHI nodes.
12765 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12766 MI->getOperand(0).getReg())
12767 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12768 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12769
12770 // Delete the original pseudo instruction.
12771 MI->eraseFromParent();
12772
12773 // And we're done.
12774 return continueMBB;
12775}
12776
12777MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012778X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012779 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012780 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12781 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012782
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012783 assert(!Subtarget->isTargetEnvMacho());
12784
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012785 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12786 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012787
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012788 if (Subtarget->isTargetWin64()) {
12789 if (Subtarget->isTargetCygMing()) {
12790 // ___chkstk(Mingw64):
12791 // Clobbers R10, R11, RAX and EFLAGS.
12792 // Updates RSP.
12793 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12794 .addExternalSymbol("___chkstk")
12795 .addReg(X86::RAX, RegState::Implicit)
12796 .addReg(X86::RSP, RegState::Implicit)
12797 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12798 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12799 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12800 } else {
12801 // __chkstk(MSVCRT): does not update stack pointer.
12802 // Clobbers R10, R11 and EFLAGS.
12803 // FIXME: RAX(allocated size) might be reused and not killed.
12804 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12805 .addExternalSymbol("__chkstk")
12806 .addReg(X86::RAX, RegState::Implicit)
12807 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12808 // RAX has the offset to subtracted from RSP.
12809 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12810 .addReg(X86::RSP)
12811 .addReg(X86::RAX);
12812 }
12813 } else {
12814 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012815 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12816
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012817 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12818 .addExternalSymbol(StackProbeSymbol)
12819 .addReg(X86::EAX, RegState::Implicit)
12820 .addReg(X86::ESP, RegState::Implicit)
12821 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12822 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12823 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12824 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012825
Dan Gohman14152b42010-07-06 20:24:04 +000012826 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012827 return BB;
12828}
Chris Lattner52600972009-09-02 05:57:00 +000012829
12830MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012831X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12832 MachineBasicBlock *BB) const {
12833 // This is pretty easy. We're taking the value that we received from
12834 // our load from the relocation, sticking it in either RDI (x86-64)
12835 // or EAX and doing an indirect call. The return value will then
12836 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012837 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012838 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012839 DebugLoc DL = MI->getDebugLoc();
12840 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012841
12842 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012843 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012844
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012845 // Get a register mask for the lowered call.
12846 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12847 // proper register mask.
12848 const uint32_t *RegMask =
12849 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012850 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012851 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12852 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012853 .addReg(X86::RIP)
12854 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012855 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012856 MI->getOperand(3).getTargetFlags())
12857 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012858 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012859 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012860 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012861 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012862 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12863 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012864 .addReg(0)
12865 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012866 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012867 MI->getOperand(3).getTargetFlags())
12868 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012869 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012870 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012871 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012872 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012873 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12874 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012875 .addReg(TII->getGlobalBaseReg(F))
12876 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012877 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012878 MI->getOperand(3).getTargetFlags())
12879 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012880 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012881 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012882 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012883 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012884
Dan Gohman14152b42010-07-06 20:24:04 +000012885 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012886 return BB;
12887}
12888
12889MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012890X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012891 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012892 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012893 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012894 case X86::TAILJMPd64:
12895 case X86::TAILJMPr64:
12896 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012897 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012898 case X86::TCRETURNdi64:
12899 case X86::TCRETURNri64:
12900 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012901 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012902 case X86::WIN_ALLOCA:
12903 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012904 case X86::SEG_ALLOCA_32:
12905 return EmitLoweredSegAlloca(MI, BB, false);
12906 case X86::SEG_ALLOCA_64:
12907 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012908 case X86::TLSCall_32:
12909 case X86::TLSCall_64:
12910 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012911 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012912 case X86::CMOV_FR32:
12913 case X86::CMOV_FR64:
12914 case X86::CMOV_V4F32:
12915 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012916 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012917 case X86::CMOV_V8F32:
12918 case X86::CMOV_V4F64:
12919 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012920 case X86::CMOV_GR16:
12921 case X86::CMOV_GR32:
12922 case X86::CMOV_RFP32:
12923 case X86::CMOV_RFP64:
12924 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012925 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012926
Dale Johannesen849f2142007-07-03 00:53:03 +000012927 case X86::FP32_TO_INT16_IN_MEM:
12928 case X86::FP32_TO_INT32_IN_MEM:
12929 case X86::FP32_TO_INT64_IN_MEM:
12930 case X86::FP64_TO_INT16_IN_MEM:
12931 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012932 case X86::FP64_TO_INT64_IN_MEM:
12933 case X86::FP80_TO_INT16_IN_MEM:
12934 case X86::FP80_TO_INT32_IN_MEM:
12935 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012936 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12937 DebugLoc DL = MI->getDebugLoc();
12938
Evan Cheng60c07e12006-07-05 22:17:51 +000012939 // Change the floating point control register to use "round towards zero"
12940 // mode when truncating to an integer value.
12941 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012942 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012943 addFrameReference(BuildMI(*BB, MI, DL,
12944 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012945
12946 // Load the old value of the high byte of the control word...
12947 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012948 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012949 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012950 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012951
12952 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012953 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012954 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012955
12956 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012957 addFrameReference(BuildMI(*BB, MI, DL,
12958 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012959
12960 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012961 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012962 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012963
12964 // Get the X86 opcode to use.
12965 unsigned Opc;
12966 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012967 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012968 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12969 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12970 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12971 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12972 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12973 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012974 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12975 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12976 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012977 }
12978
12979 X86AddressMode AM;
12980 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012981 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012982 AM.BaseType = X86AddressMode::RegBase;
12983 AM.Base.Reg = Op.getReg();
12984 } else {
12985 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012986 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012987 }
12988 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012989 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012990 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012991 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012992 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012993 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012994 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012995 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012996 AM.GV = Op.getGlobal();
12997 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012998 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012999 }
Dan Gohman14152b42010-07-06 20:24:04 +000013000 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013001 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013002
13003 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013004 addFrameReference(BuildMI(*BB, MI, DL,
13005 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013006
Dan Gohman14152b42010-07-06 20:24:04 +000013007 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013008 return BB;
13009 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013010 // String/text processing lowering.
13011 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013012 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013013 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013014 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013015 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013016 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013017 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013018 case X86::VPCMPESTRM128MEM: {
13019 unsigned NumArgs;
13020 bool MemArg;
13021 switch (MI->getOpcode()) {
13022 default: llvm_unreachable("illegal opcode!");
13023 case X86::PCMPISTRM128REG:
13024 case X86::VPCMPISTRM128REG:
13025 NumArgs = 3; MemArg = false; break;
13026 case X86::PCMPISTRM128MEM:
13027 case X86::VPCMPISTRM128MEM:
13028 NumArgs = 3; MemArg = true; break;
13029 case X86::PCMPESTRM128REG:
13030 case X86::VPCMPESTRM128REG:
13031 NumArgs = 5; MemArg = false; break;
13032 case X86::PCMPESTRM128MEM:
13033 case X86::VPCMPESTRM128MEM:
13034 NumArgs = 5; MemArg = true; break;
13035 }
13036 return EmitPCMP(MI, BB, NumArgs, MemArg);
13037 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013038
Eric Christopher228232b2010-11-30 07:20:12 +000013039 // Thread synchronization.
13040 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013041 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013042
Eric Christopherb120ab42009-08-18 22:50:32 +000013043 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013044 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013045 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013046 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013047 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013048 case X86::ATOMMIN16:
13049 case X86::ATOMMAX16:
13050 case X86::ATOMUMIN16:
13051 case X86::ATOMUMAX16:
13052 case X86::ATOMMIN64:
13053 case X86::ATOMMAX64:
13054 case X86::ATOMUMIN64:
13055 case X86::ATOMUMAX64: {
13056 unsigned Opc;
13057 switch (MI->getOpcode()) {
13058 default: llvm_unreachable("illegal opcode!");
13059 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13060 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13061 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13062 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13063 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13064 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13065 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13066 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13067 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13068 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13069 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13070 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13071 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13072 }
13073 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13074 }
13075
13076 case X86::ATOMAND32:
13077 case X86::ATOMOR32:
13078 case X86::ATOMXOR32:
13079 case X86::ATOMNAND32: {
13080 bool Invert = false;
13081 unsigned RegOpc, ImmOpc;
13082 switch (MI->getOpcode()) {
13083 default: llvm_unreachable("illegal opcode!");
13084 case X86::ATOMAND32:
13085 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13086 case X86::ATOMOR32:
13087 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13088 case X86::ATOMXOR32:
13089 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13090 case X86::ATOMNAND32:
13091 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13092 }
13093 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13094 X86::MOV32rm, X86::LCMPXCHG32,
13095 X86::NOT32r, X86::EAX,
13096 &X86::GR32RegClass, Invert);
13097 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013098
13099 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013100 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013101 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013102 case X86::ATOMNAND16: {
13103 bool Invert = false;
13104 unsigned RegOpc, ImmOpc;
13105 switch (MI->getOpcode()) {
13106 default: llvm_unreachable("illegal opcode!");
13107 case X86::ATOMAND16:
13108 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13109 case X86::ATOMOR16:
13110 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13111 case X86::ATOMXOR16:
13112 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13113 case X86::ATOMNAND16:
13114 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13115 }
13116 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13117 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013118 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013119 &X86::GR16RegClass, Invert);
13120 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013121
13122 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013123 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013124 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013125 case X86::ATOMNAND8: {
13126 bool Invert = false;
13127 unsigned RegOpc, ImmOpc;
13128 switch (MI->getOpcode()) {
13129 default: llvm_unreachable("illegal opcode!");
13130 case X86::ATOMAND8:
13131 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13132 case X86::ATOMOR8:
13133 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13134 case X86::ATOMXOR8:
13135 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13136 case X86::ATOMNAND8:
13137 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13138 }
13139 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13140 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013141 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013142 &X86::GR8RegClass, Invert);
13143 }
13144
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013145 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013146 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013147 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013148 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013149 case X86::ATOMNAND64: {
13150 bool Invert = false;
13151 unsigned RegOpc, ImmOpc;
13152 switch (MI->getOpcode()) {
13153 default: llvm_unreachable("illegal opcode!");
13154 case X86::ATOMAND64:
13155 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13156 case X86::ATOMOR64:
13157 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13158 case X86::ATOMXOR64:
13159 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13160 case X86::ATOMNAND64:
13161 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13162 }
13163 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13164 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013165 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013166 &X86::GR64RegClass, Invert);
13167 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013168
13169 // This group does 64-bit operations on a 32-bit host.
13170 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013171 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013172 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013173 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013174 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013175 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013176 case X86::ATOMSWAP6432: {
13177 bool Invert = false;
13178 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13179 switch (MI->getOpcode()) {
13180 default: llvm_unreachable("illegal opcode!");
13181 case X86::ATOMAND6432:
13182 RegOpcL = RegOpcH = X86::AND32rr;
13183 ImmOpcL = ImmOpcH = X86::AND32ri;
13184 break;
13185 case X86::ATOMOR6432:
13186 RegOpcL = RegOpcH = X86::OR32rr;
13187 ImmOpcL = ImmOpcH = X86::OR32ri;
13188 break;
13189 case X86::ATOMXOR6432:
13190 RegOpcL = RegOpcH = X86::XOR32rr;
13191 ImmOpcL = ImmOpcH = X86::XOR32ri;
13192 break;
13193 case X86::ATOMNAND6432:
13194 RegOpcL = RegOpcH = X86::AND32rr;
13195 ImmOpcL = ImmOpcH = X86::AND32ri;
13196 Invert = true;
13197 break;
13198 case X86::ATOMADD6432:
13199 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13200 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13201 break;
13202 case X86::ATOMSUB6432:
13203 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13204 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13205 break;
13206 case X86::ATOMSWAP6432:
13207 RegOpcL = RegOpcH = X86::MOV32rr;
13208 ImmOpcL = ImmOpcH = X86::MOV32ri;
13209 break;
13210 }
13211 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13212 ImmOpcL, ImmOpcH, Invert);
13213 }
13214
Dan Gohmand6708ea2009-08-15 01:38:56 +000013215 case X86::VASTART_SAVE_XMM_REGS:
13216 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013217
13218 case X86::VAARG_64:
13219 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013220 }
13221}
13222
13223//===----------------------------------------------------------------------===//
13224// X86 Optimization Hooks
13225//===----------------------------------------------------------------------===//
13226
Dan Gohman475871a2008-07-27 21:46:04 +000013227void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013228 APInt &KnownZero,
13229 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013230 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013231 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013232 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013233 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013234 assert((Opc >= ISD::BUILTIN_OP_END ||
13235 Opc == ISD::INTRINSIC_WO_CHAIN ||
13236 Opc == ISD::INTRINSIC_W_CHAIN ||
13237 Opc == ISD::INTRINSIC_VOID) &&
13238 "Should use MaskedValueIsZero if you don't know whether Op"
13239 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013240
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013241 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013242 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013243 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013244 case X86ISD::ADD:
13245 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013246 case X86ISD::ADC:
13247 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013248 case X86ISD::SMUL:
13249 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013250 case X86ISD::INC:
13251 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013252 case X86ISD::OR:
13253 case X86ISD::XOR:
13254 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013255 // These nodes' second result is a boolean.
13256 if (Op.getResNo() == 0)
13257 break;
13258 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013259 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013260 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013261 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013262 case ISD::INTRINSIC_WO_CHAIN: {
13263 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13264 unsigned NumLoBits = 0;
13265 switch (IntId) {
13266 default: break;
13267 case Intrinsic::x86_sse_movmsk_ps:
13268 case Intrinsic::x86_avx_movmsk_ps_256:
13269 case Intrinsic::x86_sse2_movmsk_pd:
13270 case Intrinsic::x86_avx_movmsk_pd_256:
13271 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013272 case Intrinsic::x86_sse2_pmovmskb_128:
13273 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013274 // High bits of movmskp{s|d}, pmovmskb are known zero.
13275 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013276 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013277 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13278 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13279 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13280 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13281 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13282 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013283 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013284 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013285 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013286 break;
13287 }
13288 }
13289 break;
13290 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013291 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013292}
Chris Lattner259e97c2006-01-31 19:43:35 +000013293
Owen Andersonbc146b02010-09-21 20:42:50 +000013294unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13295 unsigned Depth) const {
13296 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13297 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13298 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013299
Owen Andersonbc146b02010-09-21 20:42:50 +000013300 // Fallback case.
13301 return 1;
13302}
13303
Evan Cheng206ee9d2006-07-07 08:33:52 +000013304/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013305/// node is a GlobalAddress + offset.
13306bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013307 const GlobalValue* &GA,
13308 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013309 if (N->getOpcode() == X86ISD::Wrapper) {
13310 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013311 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013312 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013313 return true;
13314 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013315 }
Evan Chengad4196b2008-05-12 19:56:52 +000013316 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013317}
13318
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013319/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13320/// same as extracting the high 128-bit part of 256-bit vector and then
13321/// inserting the result into the low part of a new 256-bit vector
13322static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13323 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013324 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013325
13326 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013327 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013328 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13329 SVOp->getMaskElt(j) >= 0)
13330 return false;
13331
13332 return true;
13333}
13334
13335/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13336/// same as extracting the low 128-bit part of 256-bit vector and then
13337/// inserting the result into the high part of a new 256-bit vector
13338static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13339 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013340 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013341
13342 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013343 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013344 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13345 SVOp->getMaskElt(j) >= 0)
13346 return false;
13347
13348 return true;
13349}
13350
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013351/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13352static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013353 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013354 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013355 DebugLoc dl = N->getDebugLoc();
13356 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13357 SDValue V1 = SVOp->getOperand(0);
13358 SDValue V2 = SVOp->getOperand(1);
13359 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013360 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013361
13362 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13363 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13364 //
13365 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013366 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013367 // V UNDEF BUILD_VECTOR UNDEF
13368 // \ / \ /
13369 // CONCAT_VECTOR CONCAT_VECTOR
13370 // \ /
13371 // \ /
13372 // RESULT: V + zero extended
13373 //
13374 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13375 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13376 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13377 return SDValue();
13378
13379 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13380 return SDValue();
13381
13382 // To match the shuffle mask, the first half of the mask should
13383 // be exactly the first vector, and all the rest a splat with the
13384 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013385 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013386 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13387 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13388 return SDValue();
13389
Chad Rosier3d1161e2012-01-03 21:05:52 +000013390 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13391 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013392 if (Ld->hasNUsesOfValue(1, 0)) {
13393 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13394 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13395 SDValue ResNode =
13396 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13397 Ld->getMemoryVT(),
13398 Ld->getPointerInfo(),
13399 Ld->getAlignment(),
13400 false/*isVolatile*/, true/*ReadMem*/,
13401 false/*WriteMem*/);
13402 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13403 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013404 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013405
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013406 // Emit a zeroed vector and insert the desired subvector on its
13407 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013408 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013409 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013410 return DCI.CombineTo(N, InsV);
13411 }
13412
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013413 //===--------------------------------------------------------------------===//
13414 // Combine some shuffles into subvector extracts and inserts:
13415 //
13416
13417 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13418 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013419 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13420 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013421 return DCI.CombineTo(N, InsV);
13422 }
13423
13424 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13425 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013426 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13427 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013428 return DCI.CombineTo(N, InsV);
13429 }
13430
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013431 return SDValue();
13432}
13433
13434/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013435static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013436 TargetLowering::DAGCombinerInfo &DCI,
13437 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013438 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013439 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013440
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013441 // Don't create instructions with illegal types after legalize types has run.
13442 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13443 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13444 return SDValue();
13445
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013446 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013447 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013448 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013449 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013450
13451 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013452 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013453 return SDValue();
13454
13455 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13456 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13457 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013458 SmallVector<SDValue, 16> Elts;
13459 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013460 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013461
Nate Begemanfdea31a2010-03-24 20:49:50 +000013462 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013463}
Evan Chengd880b972008-05-09 21:53:03 +000013464
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013465
Craig Topperc16f8512012-04-25 06:39:39 +000013466/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013467/// a sequence of vector shuffle operations.
13468/// It is possible when we truncate 256-bit vector to 128-bit vector
13469
Chad Rosiera20e1e72012-08-01 18:39:17 +000013470SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013471 DAGCombinerInfo &DCI) const {
13472 if (!DCI.isBeforeLegalizeOps())
13473 return SDValue();
13474
Craig Topper3ef43cf2012-04-24 06:36:35 +000013475 if (!Subtarget->hasAVX())
13476 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013477
13478 EVT VT = N->getValueType(0);
13479 SDValue Op = N->getOperand(0);
13480 EVT OpVT = Op.getValueType();
13481 DebugLoc dl = N->getDebugLoc();
13482
13483 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13484
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013485 if (Subtarget->hasAVX2()) {
13486 // AVX2: v4i64 -> v4i32
13487
13488 // VPERMD
13489 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13490
13491 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13492 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13493 ShufMask);
13494
Craig Topperd63fa652012-04-22 18:51:37 +000013495 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13496 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013497 }
13498
13499 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013500 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013501 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013502
13503 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013504 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013505
13506 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13507 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13508
13509 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013510 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013511
Craig Toppercacafd42012-08-14 08:18:43 +000013512 SDValue Undef = DAG.getUNDEF(VT);
13513 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13514 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013515
13516 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013517 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013518
Elena Demikhovsky73252572012-02-01 10:33:05 +000013519 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013520 }
Craig Topperd63fa652012-04-22 18:51:37 +000013521
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013522 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13523
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013524 if (Subtarget->hasAVX2()) {
13525 // AVX2: v8i32 -> v8i16
13526
13527 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013528
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013529 // PSHUFB
13530 SmallVector<SDValue,32> pshufbMask;
13531 for (unsigned i = 0; i < 2; ++i) {
13532 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13533 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13534 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13535 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13536 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13537 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13538 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13539 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13540 for (unsigned j = 0; j < 8; ++j)
13541 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13542 }
Craig Topperd63fa652012-04-22 18:51:37 +000013543 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13544 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013545 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13546
13547 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13548
13549 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013550 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013551 &ShufMask[0]);
13552
Craig Topperd63fa652012-04-22 18:51:37 +000013553 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13554 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013555
13556 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13557 }
13558
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013559 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013560 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013561
13562 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013563 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013564
13565 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13566 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13567
13568 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013569 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13570 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013571
Craig Toppercacafd42012-08-14 08:18:43 +000013572 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13573 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13574 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013575
13576 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13577 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13578
13579 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013580 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013581
Elena Demikhovsky73252572012-02-01 10:33:05 +000013582 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013583 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013584 }
13585
13586 return SDValue();
13587}
13588
Craig Topper89f4e662012-03-20 07:17:59 +000013589/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13590/// specific shuffle of a load can be folded into a single element load.
13591/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13592/// shuffles have been customed lowered so we need to handle those here.
13593static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13594 TargetLowering::DAGCombinerInfo &DCI) {
13595 if (DCI.isBeforeLegalizeOps())
13596 return SDValue();
13597
13598 SDValue InVec = N->getOperand(0);
13599 SDValue EltNo = N->getOperand(1);
13600
13601 if (!isa<ConstantSDNode>(EltNo))
13602 return SDValue();
13603
13604 EVT VT = InVec.getValueType();
13605
13606 bool HasShuffleIntoBitcast = false;
13607 if (InVec.getOpcode() == ISD::BITCAST) {
13608 // Don't duplicate a load with other uses.
13609 if (!InVec.hasOneUse())
13610 return SDValue();
13611 EVT BCVT = InVec.getOperand(0).getValueType();
13612 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13613 return SDValue();
13614 InVec = InVec.getOperand(0);
13615 HasShuffleIntoBitcast = true;
13616 }
13617
13618 if (!isTargetShuffle(InVec.getOpcode()))
13619 return SDValue();
13620
13621 // Don't duplicate a load with other uses.
13622 if (!InVec.hasOneUse())
13623 return SDValue();
13624
13625 SmallVector<int, 16> ShuffleMask;
13626 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013627 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13628 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013629 return SDValue();
13630
13631 // Select the input vector, guarding against out of range extract vector.
13632 unsigned NumElems = VT.getVectorNumElements();
13633 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13634 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13635 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13636 : InVec.getOperand(1);
13637
13638 // If inputs to shuffle are the same for both ops, then allow 2 uses
13639 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13640
13641 if (LdNode.getOpcode() == ISD::BITCAST) {
13642 // Don't duplicate a load with other uses.
13643 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13644 return SDValue();
13645
13646 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13647 LdNode = LdNode.getOperand(0);
13648 }
13649
13650 if (!ISD::isNormalLoad(LdNode.getNode()))
13651 return SDValue();
13652
13653 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13654
13655 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13656 return SDValue();
13657
13658 if (HasShuffleIntoBitcast) {
13659 // If there's a bitcast before the shuffle, check if the load type and
13660 // alignment is valid.
13661 unsigned Align = LN0->getAlignment();
13662 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13663 unsigned NewAlign = TLI.getTargetData()->
13664 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13665
13666 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13667 return SDValue();
13668 }
13669
13670 // All checks match so transform back to vector_shuffle so that DAG combiner
13671 // can finish the job
13672 DebugLoc dl = N->getDebugLoc();
13673
13674 // Create shuffle node taking into account the case that its a unary shuffle
13675 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13676 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13677 InVec.getOperand(0), Shuffle,
13678 &ShuffleMask[0]);
13679 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13680 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13681 EltNo);
13682}
13683
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013684/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13685/// generation and convert it from being a bunch of shuffles and extracts
13686/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013687static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013688 TargetLowering::DAGCombinerInfo &DCI) {
13689 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13690 if (NewOp.getNode())
13691 return NewOp;
13692
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013693 SDValue InputVector = N->getOperand(0);
13694
13695 // Only operate on vectors of 4 elements, where the alternative shuffling
13696 // gets to be more expensive.
13697 if (InputVector.getValueType() != MVT::v4i32)
13698 return SDValue();
13699
13700 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13701 // single use which is a sign-extend or zero-extend, and all elements are
13702 // used.
13703 SmallVector<SDNode *, 4> Uses;
13704 unsigned ExtractedElements = 0;
13705 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13706 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13707 if (UI.getUse().getResNo() != InputVector.getResNo())
13708 return SDValue();
13709
13710 SDNode *Extract = *UI;
13711 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13712 return SDValue();
13713
13714 if (Extract->getValueType(0) != MVT::i32)
13715 return SDValue();
13716 if (!Extract->hasOneUse())
13717 return SDValue();
13718 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13719 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13720 return SDValue();
13721 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13722 return SDValue();
13723
13724 // Record which element was extracted.
13725 ExtractedElements |=
13726 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13727
13728 Uses.push_back(Extract);
13729 }
13730
13731 // If not all the elements were used, this may not be worthwhile.
13732 if (ExtractedElements != 15)
13733 return SDValue();
13734
13735 // Ok, we've now decided to do the transformation.
13736 DebugLoc dl = InputVector.getDebugLoc();
13737
13738 // Store the value to a temporary stack slot.
13739 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013740 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13741 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013742
13743 // Replace each use (extract) with a load of the appropriate element.
13744 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13745 UE = Uses.end(); UI != UE; ++UI) {
13746 SDNode *Extract = *UI;
13747
Nadav Rotem86694292011-05-17 08:31:57 +000013748 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013749 SDValue Idx = Extract->getOperand(1);
13750 unsigned EltSize =
13751 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13752 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013753 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013754 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13755
Nadav Rotem86694292011-05-17 08:31:57 +000013756 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013757 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013758
13759 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013760 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013761 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013762 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013763
13764 // Replace the exact with the load.
13765 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13766 }
13767
13768 // The replacement was made in place; don't return anything.
13769 return SDValue();
13770}
13771
Duncan Sands6bcd2192011-09-17 16:49:39 +000013772/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13773/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013774static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013775 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013776 const X86Subtarget *Subtarget) {
13777 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013778 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013779 // Get the LHS/RHS of the select.
13780 SDValue LHS = N->getOperand(1);
13781 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013782 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013783
Dan Gohman670e5392009-09-21 18:03:22 +000013784 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013785 // instructions match the semantics of the common C idiom x<y?x:y but not
13786 // x<=y?x:y, because of how they handle negative zero (which can be
13787 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013788 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13789 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013790 (Subtarget->hasSSE2() ||
13791 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013792 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013793
Chris Lattner47b4ce82009-03-11 05:48:52 +000013794 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013795 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013796 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13797 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013798 switch (CC) {
13799 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013800 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013801 // Converting this to a min would handle NaNs incorrectly, and swapping
13802 // the operands would cause it to handle comparisons between positive
13803 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013804 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013805 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013806 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13807 break;
13808 std::swap(LHS, RHS);
13809 }
Dan Gohman670e5392009-09-21 18:03:22 +000013810 Opcode = X86ISD::FMIN;
13811 break;
13812 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013813 // Converting this to a min would handle comparisons between positive
13814 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013815 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013816 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13817 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013818 Opcode = X86ISD::FMIN;
13819 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013820 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013821 // Converting this to a min would handle both negative zeros and NaNs
13822 // incorrectly, but we can swap the operands to fix both.
13823 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013824 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013825 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013826 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013827 Opcode = X86ISD::FMIN;
13828 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013829
Dan Gohman670e5392009-09-21 18:03:22 +000013830 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013831 // Converting this to a max would handle comparisons between positive
13832 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013833 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013834 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013835 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013836 Opcode = X86ISD::FMAX;
13837 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013838 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013839 // Converting this to a max would handle NaNs incorrectly, and swapping
13840 // the operands would cause it to handle comparisons between positive
13841 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013842 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013843 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013844 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13845 break;
13846 std::swap(LHS, RHS);
13847 }
Dan Gohman670e5392009-09-21 18:03:22 +000013848 Opcode = X86ISD::FMAX;
13849 break;
13850 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013851 // Converting this to a max would handle both negative zeros and NaNs
13852 // incorrectly, but we can swap the operands to fix both.
13853 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013854 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013855 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013856 case ISD::SETGE:
13857 Opcode = X86ISD::FMAX;
13858 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013859 }
Dan Gohman670e5392009-09-21 18:03:22 +000013860 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013861 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13862 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013863 switch (CC) {
13864 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013865 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013866 // Converting this to a min would handle comparisons between positive
13867 // and negative zero incorrectly, and swapping the operands would
13868 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013869 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013870 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013871 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013872 break;
13873 std::swap(LHS, RHS);
13874 }
Dan Gohman670e5392009-09-21 18:03:22 +000013875 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013876 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013877 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013878 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013879 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013880 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13881 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013882 Opcode = X86ISD::FMIN;
13883 break;
13884 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013885 // Converting this to a min would handle both negative zeros and NaNs
13886 // incorrectly, but we can swap the operands to fix both.
13887 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013888 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013889 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013890 case ISD::SETGE:
13891 Opcode = X86ISD::FMIN;
13892 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013893
Dan Gohman670e5392009-09-21 18:03:22 +000013894 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013895 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013896 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013897 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013898 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013899 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013900 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013901 // Converting this to a max would handle comparisons between positive
13902 // and negative zero incorrectly, and swapping the operands would
13903 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013904 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013905 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013906 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013907 break;
13908 std::swap(LHS, RHS);
13909 }
Dan Gohman670e5392009-09-21 18:03:22 +000013910 Opcode = X86ISD::FMAX;
13911 break;
13912 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013913 // Converting this to a max would handle both negative zeros and NaNs
13914 // incorrectly, but we can swap the operands to fix both.
13915 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013916 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013917 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013918 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013919 Opcode = X86ISD::FMAX;
13920 break;
13921 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013922 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013923
Chris Lattner47b4ce82009-03-11 05:48:52 +000013924 if (Opcode)
13925 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013926 }
Eric Christopherfd179292009-08-27 18:07:15 +000013927
Chris Lattnerd1980a52009-03-12 06:52:53 +000013928 // If this is a select between two integer constants, try to do some
13929 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013930 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13931 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013932 // Don't do this for crazy integer types.
13933 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13934 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013935 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013936 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013937
Chris Lattnercee56e72009-03-13 05:53:31 +000013938 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013939 // Efficiently invertible.
13940 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13941 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13942 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13943 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013944 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013945 }
Eric Christopherfd179292009-08-27 18:07:15 +000013946
Chris Lattnerd1980a52009-03-12 06:52:53 +000013947 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013948 if (FalseC->getAPIntValue() == 0 &&
13949 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013950 if (NeedsCondInvert) // Invert the condition if needed.
13951 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13952 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013953
Chris Lattnerd1980a52009-03-12 06:52:53 +000013954 // Zero extend the condition if needed.
13955 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013956
Chris Lattnercee56e72009-03-13 05:53:31 +000013957 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013958 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013959 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013960 }
Eric Christopherfd179292009-08-27 18:07:15 +000013961
Chris Lattner97a29a52009-03-13 05:22:11 +000013962 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013963 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013964 if (NeedsCondInvert) // Invert the condition if needed.
13965 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13966 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013967
Chris Lattner97a29a52009-03-13 05:22:11 +000013968 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013969 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13970 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013971 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013972 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013973 }
Eric Christopherfd179292009-08-27 18:07:15 +000013974
Chris Lattnercee56e72009-03-13 05:53:31 +000013975 // Optimize cases that will turn into an LEA instruction. This requires
13976 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013977 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013978 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013979 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013980
Chris Lattnercee56e72009-03-13 05:53:31 +000013981 bool isFastMultiplier = false;
13982 if (Diff < 10) {
13983 switch ((unsigned char)Diff) {
13984 default: break;
13985 case 1: // result = add base, cond
13986 case 2: // result = lea base( , cond*2)
13987 case 3: // result = lea base(cond, cond*2)
13988 case 4: // result = lea base( , cond*4)
13989 case 5: // result = lea base(cond, cond*4)
13990 case 8: // result = lea base( , cond*8)
13991 case 9: // result = lea base(cond, cond*8)
13992 isFastMultiplier = true;
13993 break;
13994 }
13995 }
Eric Christopherfd179292009-08-27 18:07:15 +000013996
Chris Lattnercee56e72009-03-13 05:53:31 +000013997 if (isFastMultiplier) {
13998 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
13999 if (NeedsCondInvert) // Invert the condition if needed.
14000 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14001 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014002
Chris Lattnercee56e72009-03-13 05:53:31 +000014003 // Zero extend the condition if needed.
14004 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14005 Cond);
14006 // Scale the condition by the difference.
14007 if (Diff != 1)
14008 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14009 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014010
Chris Lattnercee56e72009-03-13 05:53:31 +000014011 // Add the base if non-zero.
14012 if (FalseC->getAPIntValue() != 0)
14013 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14014 SDValue(FalseC, 0));
14015 return Cond;
14016 }
Eric Christopherfd179292009-08-27 18:07:15 +000014017 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014018 }
14019 }
Eric Christopherfd179292009-08-27 18:07:15 +000014020
Evan Cheng56f582d2012-01-04 01:41:39 +000014021 // Canonicalize max and min:
14022 // (x > y) ? x : y -> (x >= y) ? x : y
14023 // (x < y) ? x : y -> (x <= y) ? x : y
14024 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14025 // the need for an extra compare
14026 // against zero. e.g.
14027 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14028 // subl %esi, %edi
14029 // testl %edi, %edi
14030 // movl $0, %eax
14031 // cmovgl %edi, %eax
14032 // =>
14033 // xorl %eax, %eax
14034 // subl %esi, $edi
14035 // cmovsl %eax, %edi
14036 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14037 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14038 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14039 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14040 switch (CC) {
14041 default: break;
14042 case ISD::SETLT:
14043 case ISD::SETGT: {
14044 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14045 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14046 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14047 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14048 }
14049 }
14050 }
14051
Nadav Rotemcc616562012-01-15 19:27:55 +000014052 // If we know that this node is legal then we know that it is going to be
14053 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14054 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14055 // to simplify previous instructions.
14056 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14057 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014058 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014059 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014060
14061 // Don't optimize vector selects that map to mask-registers.
14062 if (BitWidth == 1)
14063 return SDValue();
14064
Nadav Rotemcc616562012-01-15 19:27:55 +000014065 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14066 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14067
14068 APInt KnownZero, KnownOne;
14069 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14070 DCI.isBeforeLegalizeOps());
14071 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14072 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14073 DCI.CommitTargetLoweringOpt(TLO);
14074 }
14075
Dan Gohman475871a2008-07-27 21:46:04 +000014076 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014077}
14078
Michael Liao2a33cec2012-08-10 19:58:13 +000014079// Check whether a boolean test is testing a boolean value generated by
14080// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14081// code.
14082//
14083// Simplify the following patterns:
14084// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14085// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14086// to (Op EFLAGS Cond)
14087//
14088// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14089// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14090// to (Op EFLAGS !Cond)
14091//
14092// where Op could be BRCOND or CMOV.
14093//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014094static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014095 // Quit if not CMP and SUB with its value result used.
14096 if (Cmp.getOpcode() != X86ISD::CMP &&
14097 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14098 return SDValue();
14099
14100 // Quit if not used as a boolean value.
14101 if (CC != X86::COND_E && CC != X86::COND_NE)
14102 return SDValue();
14103
14104 // Check CMP operands. One of them should be 0 or 1 and the other should be
14105 // an SetCC or extended from it.
14106 SDValue Op1 = Cmp.getOperand(0);
14107 SDValue Op2 = Cmp.getOperand(1);
14108
14109 SDValue SetCC;
14110 const ConstantSDNode* C = 0;
14111 bool needOppositeCond = (CC == X86::COND_E);
14112
14113 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14114 SetCC = Op2;
14115 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14116 SetCC = Op1;
14117 else // Quit if all operands are not constants.
14118 return SDValue();
14119
14120 if (C->getZExtValue() == 1)
14121 needOppositeCond = !needOppositeCond;
14122 else if (C->getZExtValue() != 0)
14123 // Quit if the constant is neither 0 or 1.
14124 return SDValue();
14125
14126 // Skip 'zext' node.
14127 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14128 SetCC = SetCC.getOperand(0);
14129
14130 // Quit if not SETCC.
14131 // FIXME: So far we only handle the boolean value generated from SETCC. If
14132 // there is other ways to generate boolean values, we need handle them here
14133 // as well.
14134 if (SetCC.getOpcode() != X86ISD::SETCC)
14135 return SDValue();
14136
14137 // Set the condition code or opposite one if necessary.
14138 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14139 if (needOppositeCond)
14140 CC = X86::GetOppositeBranchCondition(CC);
14141
14142 return SetCC.getOperand(1);
14143}
14144
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014145/// checkFlaggedOrCombine - DAG combination on X86ISD::OR, i.e. with EFLAGS
14146/// updated. If only flag result is used and the result is evaluated from a
14147/// series of element extraction, try to combine it into a PTEST.
14148static SDValue checkFlaggedOrCombine(SDValue Or, X86::CondCode &CC,
14149 SelectionDAG &DAG,
14150 const X86Subtarget *Subtarget) {
14151 SDNode *N = Or.getNode();
14152 DebugLoc DL = N->getDebugLoc();
14153
14154 // Only SSE4.1 and beyond supports PTEST or like.
14155 if (!Subtarget->hasSSE41())
14156 return SDValue();
14157
14158 if (N->getOpcode() != X86ISD::OR)
14159 return SDValue();
14160
14161 // Quit if the value result of OR is used.
14162 if (N->hasAnyUseOfValue(0))
14163 return SDValue();
14164
14165 // Quit if not used as a boolean value.
14166 if (CC != X86::COND_E && CC != X86::COND_NE)
14167 return SDValue();
14168
14169 SmallVector<SDValue, 8> Opnds;
14170 SDValue VecIn;
14171 EVT VT = MVT::Other;
14172 unsigned Mask = 0;
14173
14174 // Recognize a special case where a vector is casted into wide integer to
14175 // test all 0s.
14176 Opnds.push_back(N->getOperand(0));
14177 Opnds.push_back(N->getOperand(1));
14178
14179 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
14180 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
14181 // BFS traverse all OR'd operands.
14182 if (I->getOpcode() == ISD::OR) {
14183 Opnds.push_back(I->getOperand(0));
14184 Opnds.push_back(I->getOperand(1));
14185 // Re-evaluate the number of nodes to be traversed.
Michael Liao95c22a32012-08-28 23:42:17 +000014186 e += 2; // 2 more nodes (LHS and RHS) are pushed.
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014187 continue;
14188 }
14189
14190 // Quit if a non-EXTRACT_VECTOR_ELT
14191 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14192 return SDValue();
14193
14194 // Quit if without a constant index.
14195 SDValue Idx = I->getOperand(1);
14196 if (!isa<ConstantSDNode>(Idx))
14197 return SDValue();
14198
14199 // Check if all elements are extracted from the same vector.
14200 SDValue ExtractedFromVec = I->getOperand(0);
14201 if (VecIn.getNode() == 0) {
14202 VT = ExtractedFromVec.getValueType();
14203 // FIXME: only 128-bit vector is supported so far.
14204 if (!VT.is128BitVector())
14205 return SDValue();
14206 VecIn = ExtractedFromVec;
14207 } else if (VecIn != ExtractedFromVec)
14208 return SDValue();
14209
14210 // Record the constant index.
14211 Mask |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
14212 }
14213
14214 assert(VT.is128BitVector() && "Only 128-bit vector PTEST is supported so far.");
14215
14216 // Quit if not all elements are used.
14217 if (Mask != (1U << VT.getVectorNumElements()) - 1U)
14218 return SDValue();
14219
14220 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, VecIn, VecIn);
14221}
14222
Chris Lattnerd1980a52009-03-12 06:52:53 +000014223/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14224static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014225 TargetLowering::DAGCombinerInfo &DCI,
14226 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014227 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014228
Chris Lattnerd1980a52009-03-12 06:52:53 +000014229 // If the flag operand isn't dead, don't touch this CMOV.
14230 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14231 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014232
Evan Chengb5a55d92011-05-24 01:48:22 +000014233 SDValue FalseOp = N->getOperand(0);
14234 SDValue TrueOp = N->getOperand(1);
14235 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14236 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014237
Evan Chengb5a55d92011-05-24 01:48:22 +000014238 if (CC == X86::COND_E || CC == X86::COND_NE) {
14239 switch (Cond.getOpcode()) {
14240 default: break;
14241 case X86ISD::BSR:
14242 case X86ISD::BSF:
14243 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14244 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14245 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14246 }
14247 }
14248
Michael Liao2a33cec2012-08-10 19:58:13 +000014249 SDValue Flags;
14250
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014251 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014252 if (Flags.getNode() &&
14253 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014254 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014255 SDValue Ops[] = { FalseOp, TrueOp,
14256 DAG.getConstant(CC, MVT::i8), Flags };
14257 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14258 Ops, array_lengthof(Ops));
14259 }
14260
14261 Flags = checkFlaggedOrCombine(Cond, CC, DAG, Subtarget);
14262 if (Flags.getNode()) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014263 SDValue Ops[] = { FalseOp, TrueOp,
14264 DAG.getConstant(CC, MVT::i8), Flags };
14265 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14266 Ops, array_lengthof(Ops));
14267 }
14268
Chris Lattnerd1980a52009-03-12 06:52:53 +000014269 // If this is a select between two integer constants, try to do some
14270 // optimizations. Note that the operands are ordered the opposite of SELECT
14271 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014272 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14273 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014274 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14275 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014276 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14277 CC = X86::GetOppositeBranchCondition(CC);
14278 std::swap(TrueC, FalseC);
14279 }
Eric Christopherfd179292009-08-27 18:07:15 +000014280
Chris Lattnerd1980a52009-03-12 06:52:53 +000014281 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014282 // This is efficient for any integer data type (including i8/i16) and
14283 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014284 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014285 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14286 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014287
Chris Lattnerd1980a52009-03-12 06:52:53 +000014288 // Zero extend the condition if needed.
14289 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014290
Chris Lattnerd1980a52009-03-12 06:52:53 +000014291 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14292 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014293 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014294 if (N->getNumValues() == 2) // Dead flag value?
14295 return DCI.CombineTo(N, Cond, SDValue());
14296 return Cond;
14297 }
Eric Christopherfd179292009-08-27 18:07:15 +000014298
Chris Lattnercee56e72009-03-13 05:53:31 +000014299 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14300 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014301 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014302 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14303 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014304
Chris Lattner97a29a52009-03-13 05:22:11 +000014305 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014306 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14307 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014308 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14309 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014310
Chris Lattner97a29a52009-03-13 05:22:11 +000014311 if (N->getNumValues() == 2) // Dead flag value?
14312 return DCI.CombineTo(N, Cond, SDValue());
14313 return Cond;
14314 }
Eric Christopherfd179292009-08-27 18:07:15 +000014315
Chris Lattnercee56e72009-03-13 05:53:31 +000014316 // Optimize cases that will turn into an LEA instruction. This requires
14317 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014318 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014319 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014320 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014321
Chris Lattnercee56e72009-03-13 05:53:31 +000014322 bool isFastMultiplier = false;
14323 if (Diff < 10) {
14324 switch ((unsigned char)Diff) {
14325 default: break;
14326 case 1: // result = add base, cond
14327 case 2: // result = lea base( , cond*2)
14328 case 3: // result = lea base(cond, cond*2)
14329 case 4: // result = lea base( , cond*4)
14330 case 5: // result = lea base(cond, cond*4)
14331 case 8: // result = lea base( , cond*8)
14332 case 9: // result = lea base(cond, cond*8)
14333 isFastMultiplier = true;
14334 break;
14335 }
14336 }
Eric Christopherfd179292009-08-27 18:07:15 +000014337
Chris Lattnercee56e72009-03-13 05:53:31 +000014338 if (isFastMultiplier) {
14339 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014340 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14341 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014342 // Zero extend the condition if needed.
14343 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14344 Cond);
14345 // Scale the condition by the difference.
14346 if (Diff != 1)
14347 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14348 DAG.getConstant(Diff, Cond.getValueType()));
14349
14350 // Add the base if non-zero.
14351 if (FalseC->getAPIntValue() != 0)
14352 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14353 SDValue(FalseC, 0));
14354 if (N->getNumValues() == 2) // Dead flag value?
14355 return DCI.CombineTo(N, Cond, SDValue());
14356 return Cond;
14357 }
Eric Christopherfd179292009-08-27 18:07:15 +000014358 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014359 }
14360 }
14361 return SDValue();
14362}
14363
14364
Evan Cheng0b0cd912009-03-28 05:57:29 +000014365/// PerformMulCombine - Optimize a single multiply with constant into two
14366/// in order to implement it with two cheaper instructions, e.g.
14367/// LEA + SHL, LEA + LEA.
14368static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14369 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014370 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14371 return SDValue();
14372
Owen Andersone50ed302009-08-10 22:56:29 +000014373 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014374 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014375 return SDValue();
14376
14377 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14378 if (!C)
14379 return SDValue();
14380 uint64_t MulAmt = C->getZExtValue();
14381 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14382 return SDValue();
14383
14384 uint64_t MulAmt1 = 0;
14385 uint64_t MulAmt2 = 0;
14386 if ((MulAmt % 9) == 0) {
14387 MulAmt1 = 9;
14388 MulAmt2 = MulAmt / 9;
14389 } else if ((MulAmt % 5) == 0) {
14390 MulAmt1 = 5;
14391 MulAmt2 = MulAmt / 5;
14392 } else if ((MulAmt % 3) == 0) {
14393 MulAmt1 = 3;
14394 MulAmt2 = MulAmt / 3;
14395 }
14396 if (MulAmt2 &&
14397 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14398 DebugLoc DL = N->getDebugLoc();
14399
14400 if (isPowerOf2_64(MulAmt2) &&
14401 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14402 // If second multiplifer is pow2, issue it first. We want the multiply by
14403 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14404 // is an add.
14405 std::swap(MulAmt1, MulAmt2);
14406
14407 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014408 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014409 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014410 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014411 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014412 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014413 DAG.getConstant(MulAmt1, VT));
14414
Eric Christopherfd179292009-08-27 18:07:15 +000014415 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014416 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014417 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014418 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014419 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014420 DAG.getConstant(MulAmt2, VT));
14421
14422 // Do not add new nodes to DAG combiner worklist.
14423 DCI.CombineTo(N, NewMul, false);
14424 }
14425 return SDValue();
14426}
14427
Evan Chengad9c0a32009-12-15 00:53:42 +000014428static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14429 SDValue N0 = N->getOperand(0);
14430 SDValue N1 = N->getOperand(1);
14431 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14432 EVT VT = N0.getValueType();
14433
14434 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14435 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014436 if (VT.isInteger() && !VT.isVector() &&
14437 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014438 N0.getOperand(1).getOpcode() == ISD::Constant) {
14439 SDValue N00 = N0.getOperand(0);
14440 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14441 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14442 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14443 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14444 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14445 APInt ShAmt = N1C->getAPIntValue();
14446 Mask = Mask.shl(ShAmt);
14447 if (Mask != 0)
14448 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14449 N00, DAG.getConstant(Mask, VT));
14450 }
14451 }
14452
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014453
14454 // Hardware support for vector shifts is sparse which makes us scalarize the
14455 // vector operations in many cases. Also, on sandybridge ADD is faster than
14456 // shl.
14457 // (shl V, 1) -> add V,V
14458 if (isSplatVector(N1.getNode())) {
14459 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14460 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14461 // We shift all of the values by one. In many cases we do not have
14462 // hardware support for this operation. This is better expressed as an ADD
14463 // of two values.
14464 if (N1C && (1 == N1C->getZExtValue())) {
14465 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14466 }
14467 }
14468
Evan Chengad9c0a32009-12-15 00:53:42 +000014469 return SDValue();
14470}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014471
Nate Begeman740ab032009-01-26 00:52:55 +000014472/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14473/// when possible.
14474static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014475 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014476 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014477 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014478 if (N->getOpcode() == ISD::SHL) {
14479 SDValue V = PerformSHLCombine(N, DAG);
14480 if (V.getNode()) return V;
14481 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014482
Nate Begeman740ab032009-01-26 00:52:55 +000014483 // On X86 with SSE2 support, we can transform this to a vector shift if
14484 // all elements are shifted by the same amount. We can't do this in legalize
14485 // because the a constant vector is typically transformed to a constant pool
14486 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014487 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014488 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014489
Craig Topper7be5dfd2011-11-12 09:58:49 +000014490 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14491 (!Subtarget->hasAVX2() ||
14492 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014493 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014494
Mon P Wang3becd092009-01-28 08:12:05 +000014495 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014496 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014497 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014498 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014499 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14500 unsigned NumElts = VT.getVectorNumElements();
14501 unsigned i = 0;
14502 for (; i != NumElts; ++i) {
14503 SDValue Arg = ShAmtOp.getOperand(i);
14504 if (Arg.getOpcode() == ISD::UNDEF) continue;
14505 BaseShAmt = Arg;
14506 break;
14507 }
Craig Topper37c26772012-01-17 04:44:50 +000014508 // Handle the case where the build_vector is all undef
14509 // FIXME: Should DAG allow this?
14510 if (i == NumElts)
14511 return SDValue();
14512
Mon P Wang3becd092009-01-28 08:12:05 +000014513 for (; i != NumElts; ++i) {
14514 SDValue Arg = ShAmtOp.getOperand(i);
14515 if (Arg.getOpcode() == ISD::UNDEF) continue;
14516 if (Arg != BaseShAmt) {
14517 return SDValue();
14518 }
14519 }
14520 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014521 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014522 SDValue InVec = ShAmtOp.getOperand(0);
14523 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14524 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14525 unsigned i = 0;
14526 for (; i != NumElts; ++i) {
14527 SDValue Arg = InVec.getOperand(i);
14528 if (Arg.getOpcode() == ISD::UNDEF) continue;
14529 BaseShAmt = Arg;
14530 break;
14531 }
14532 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14533 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014534 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014535 if (C->getZExtValue() == SplatIdx)
14536 BaseShAmt = InVec.getOperand(1);
14537 }
14538 }
Mon P Wang845b1892012-02-01 22:15:20 +000014539 if (BaseShAmt.getNode() == 0) {
14540 // Don't create instructions with illegal types after legalize
14541 // types has run.
14542 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14543 !DCI.isBeforeLegalize())
14544 return SDValue();
14545
Mon P Wangefa42202009-09-03 19:56:25 +000014546 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14547 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014548 }
Mon P Wang3becd092009-01-28 08:12:05 +000014549 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014550 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014551
Mon P Wangefa42202009-09-03 19:56:25 +000014552 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014553 if (EltVT.bitsGT(MVT::i32))
14554 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14555 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014556 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014557
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014558 // The shift amount is identical so we can do a vector shift.
14559 SDValue ValOp = N->getOperand(0);
14560 switch (N->getOpcode()) {
14561 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014562 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014563 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014564 switch (VT.getSimpleVT().SimpleTy) {
14565 default: return SDValue();
14566 case MVT::v2i64:
14567 case MVT::v4i32:
14568 case MVT::v8i16:
14569 case MVT::v4i64:
14570 case MVT::v8i32:
14571 case MVT::v16i16:
14572 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14573 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014574 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014575 switch (VT.getSimpleVT().SimpleTy) {
14576 default: return SDValue();
14577 case MVT::v4i32:
14578 case MVT::v8i16:
14579 case MVT::v8i32:
14580 case MVT::v16i16:
14581 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14582 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014583 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014584 switch (VT.getSimpleVT().SimpleTy) {
14585 default: return SDValue();
14586 case MVT::v2i64:
14587 case MVT::v4i32:
14588 case MVT::v8i16:
14589 case MVT::v4i64:
14590 case MVT::v8i32:
14591 case MVT::v16i16:
14592 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14593 }
Nate Begeman740ab032009-01-26 00:52:55 +000014594 }
Nate Begeman740ab032009-01-26 00:52:55 +000014595}
14596
Nate Begemanb65c1752010-12-17 22:55:37 +000014597
Stuart Hastings865f0932011-06-03 23:53:54 +000014598// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14599// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14600// and friends. Likewise for OR -> CMPNEQSS.
14601static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14602 TargetLowering::DAGCombinerInfo &DCI,
14603 const X86Subtarget *Subtarget) {
14604 unsigned opcode;
14605
14606 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14607 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014608 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014609 SDValue N0 = N->getOperand(0);
14610 SDValue N1 = N->getOperand(1);
14611 SDValue CMP0 = N0->getOperand(1);
14612 SDValue CMP1 = N1->getOperand(1);
14613 DebugLoc DL = N->getDebugLoc();
14614
14615 // The SETCCs should both refer to the same CMP.
14616 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14617 return SDValue();
14618
14619 SDValue CMP00 = CMP0->getOperand(0);
14620 SDValue CMP01 = CMP0->getOperand(1);
14621 EVT VT = CMP00.getValueType();
14622
14623 if (VT == MVT::f32 || VT == MVT::f64) {
14624 bool ExpectingFlags = false;
14625 // Check for any users that want flags:
14626 for (SDNode::use_iterator UI = N->use_begin(),
14627 UE = N->use_end();
14628 !ExpectingFlags && UI != UE; ++UI)
14629 switch (UI->getOpcode()) {
14630 default:
14631 case ISD::BR_CC:
14632 case ISD::BRCOND:
14633 case ISD::SELECT:
14634 ExpectingFlags = true;
14635 break;
14636 case ISD::CopyToReg:
14637 case ISD::SIGN_EXTEND:
14638 case ISD::ZERO_EXTEND:
14639 case ISD::ANY_EXTEND:
14640 break;
14641 }
14642
14643 if (!ExpectingFlags) {
14644 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14645 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14646
14647 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14648 X86::CondCode tmp = cc0;
14649 cc0 = cc1;
14650 cc1 = tmp;
14651 }
14652
14653 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14654 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14655 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14656 X86ISD::NodeType NTOperator = is64BitFP ?
14657 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14658 // FIXME: need symbolic constants for these magic numbers.
14659 // See X86ATTInstPrinter.cpp:printSSECC().
14660 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14661 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14662 DAG.getConstant(x86cc, MVT::i8));
14663 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14664 OnesOrZeroesF);
14665 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14666 DAG.getConstant(1, MVT::i32));
14667 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14668 return OneBitOfTruth;
14669 }
14670 }
14671 }
14672 }
14673 return SDValue();
14674}
14675
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014676/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14677/// so it can be folded inside ANDNP.
14678static bool CanFoldXORWithAllOnes(const SDNode *N) {
14679 EVT VT = N->getValueType(0);
14680
14681 // Match direct AllOnes for 128 and 256-bit vectors
14682 if (ISD::isBuildVectorAllOnes(N))
14683 return true;
14684
14685 // Look through a bit convert.
14686 if (N->getOpcode() == ISD::BITCAST)
14687 N = N->getOperand(0).getNode();
14688
14689 // Sometimes the operand may come from a insert_subvector building a 256-bit
14690 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014691 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014692 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14693 SDValue V1 = N->getOperand(0);
14694 SDValue V2 = N->getOperand(1);
14695
14696 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14697 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14698 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14699 ISD::isBuildVectorAllOnes(V2.getNode()))
14700 return true;
14701 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014702
14703 return false;
14704}
14705
Nate Begemanb65c1752010-12-17 22:55:37 +000014706static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14707 TargetLowering::DAGCombinerInfo &DCI,
14708 const X86Subtarget *Subtarget) {
14709 if (DCI.isBeforeLegalizeOps())
14710 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014711
Stuart Hastings865f0932011-06-03 23:53:54 +000014712 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14713 if (R.getNode())
14714 return R;
14715
Craig Topper54a11172011-10-14 07:06:56 +000014716 EVT VT = N->getValueType(0);
14717
Craig Topperb4c94572011-10-21 06:55:01 +000014718 // Create ANDN, BLSI, and BLSR instructions
14719 // BLSI is X & (-X)
14720 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014721 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14722 SDValue N0 = N->getOperand(0);
14723 SDValue N1 = N->getOperand(1);
14724 DebugLoc DL = N->getDebugLoc();
14725
14726 // Check LHS for not
14727 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14728 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14729 // Check RHS for not
14730 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14731 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14732
Craig Topperb4c94572011-10-21 06:55:01 +000014733 // Check LHS for neg
14734 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14735 isZero(N0.getOperand(0)))
14736 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14737
14738 // Check RHS for neg
14739 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14740 isZero(N1.getOperand(0)))
14741 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14742
14743 // Check LHS for X-1
14744 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14745 isAllOnes(N0.getOperand(1)))
14746 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14747
14748 // Check RHS for X-1
14749 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14750 isAllOnes(N1.getOperand(1)))
14751 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14752
Craig Topper54a11172011-10-14 07:06:56 +000014753 return SDValue();
14754 }
14755
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014756 // Want to form ANDNP nodes:
14757 // 1) In the hopes of then easily combining them with OR and AND nodes
14758 // to form PBLEND/PSIGN.
14759 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014760 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014761 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014762
Nate Begemanb65c1752010-12-17 22:55:37 +000014763 SDValue N0 = N->getOperand(0);
14764 SDValue N1 = N->getOperand(1);
14765 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014766
Nate Begemanb65c1752010-12-17 22:55:37 +000014767 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014768 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014769 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14770 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014771 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014772
14773 // Check RHS for vnot
14774 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014775 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14776 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014777 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014778
Nate Begemanb65c1752010-12-17 22:55:37 +000014779 return SDValue();
14780}
14781
Evan Cheng760d1942010-01-04 21:22:48 +000014782static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014783 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014784 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014785 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014786 return SDValue();
14787
Stuart Hastings865f0932011-06-03 23:53:54 +000014788 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14789 if (R.getNode())
14790 return R;
14791
Evan Cheng760d1942010-01-04 21:22:48 +000014792 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014793
Evan Cheng760d1942010-01-04 21:22:48 +000014794 SDValue N0 = N->getOperand(0);
14795 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014796
Nate Begemanb65c1752010-12-17 22:55:37 +000014797 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014798 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014799 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014800 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14801 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014802
Craig Topper1666cb62011-11-19 07:07:26 +000014803 // Canonicalize pandn to RHS
14804 if (N0.getOpcode() == X86ISD::ANDNP)
14805 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014806 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014807 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14808 SDValue Mask = N1.getOperand(0);
14809 SDValue X = N1.getOperand(1);
14810 SDValue Y;
14811 if (N0.getOperand(0) == Mask)
14812 Y = N0.getOperand(1);
14813 if (N0.getOperand(1) == Mask)
14814 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014815
Craig Topper1666cb62011-11-19 07:07:26 +000014816 // Check to see if the mask appeared in both the AND and ANDNP and
14817 if (!Y.getNode())
14818 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014819
Craig Topper1666cb62011-11-19 07:07:26 +000014820 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014821 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014822 if (Mask.getOpcode() == ISD::BITCAST)
14823 Mask = Mask.getOperand(0);
14824 if (X.getOpcode() == ISD::BITCAST)
14825 X = X.getOperand(0);
14826 if (Y.getOpcode() == ISD::BITCAST)
14827 Y = Y.getOperand(0);
14828
Craig Topper1666cb62011-11-19 07:07:26 +000014829 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014830
Craig Toppered2e13d2012-01-22 19:15:14 +000014831 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014832 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14833 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014834 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014835 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014836
14837 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014838 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014839 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14840 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14841 if ((SraAmt + 1) != EltBits)
14842 return SDValue();
14843
14844 DebugLoc DL = N->getDebugLoc();
14845
14846 // Now we know we at least have a plendvb with the mask val. See if
14847 // we can form a psignb/w/d.
14848 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014849 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14850 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014851 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14852 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14853 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014854 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014855 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014856 }
14857 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014858 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014859 return SDValue();
14860
14861 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14862
14863 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14864 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14865 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014866 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014867 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014868 }
14869 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014870
Craig Topper1666cb62011-11-19 07:07:26 +000014871 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14872 return SDValue();
14873
Nate Begemanb65c1752010-12-17 22:55:37 +000014874 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014875 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14876 std::swap(N0, N1);
14877 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14878 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014879 if (!N0.hasOneUse() || !N1.hasOneUse())
14880 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014881
14882 SDValue ShAmt0 = N0.getOperand(1);
14883 if (ShAmt0.getValueType() != MVT::i8)
14884 return SDValue();
14885 SDValue ShAmt1 = N1.getOperand(1);
14886 if (ShAmt1.getValueType() != MVT::i8)
14887 return SDValue();
14888 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14889 ShAmt0 = ShAmt0.getOperand(0);
14890 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14891 ShAmt1 = ShAmt1.getOperand(0);
14892
14893 DebugLoc DL = N->getDebugLoc();
14894 unsigned Opc = X86ISD::SHLD;
14895 SDValue Op0 = N0.getOperand(0);
14896 SDValue Op1 = N1.getOperand(0);
14897 if (ShAmt0.getOpcode() == ISD::SUB) {
14898 Opc = X86ISD::SHRD;
14899 std::swap(Op0, Op1);
14900 std::swap(ShAmt0, ShAmt1);
14901 }
14902
Evan Cheng8b1190a2010-04-28 01:18:01 +000014903 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014904 if (ShAmt1.getOpcode() == ISD::SUB) {
14905 SDValue Sum = ShAmt1.getOperand(0);
14906 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014907 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14908 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14909 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14910 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014911 return DAG.getNode(Opc, DL, VT,
14912 Op0, Op1,
14913 DAG.getNode(ISD::TRUNCATE, DL,
14914 MVT::i8, ShAmt0));
14915 }
14916 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14917 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14918 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014919 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014920 return DAG.getNode(Opc, DL, VT,
14921 N0.getOperand(0), N1.getOperand(0),
14922 DAG.getNode(ISD::TRUNCATE, DL,
14923 MVT::i8, ShAmt0));
14924 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014925
Evan Cheng760d1942010-01-04 21:22:48 +000014926 return SDValue();
14927}
14928
Manman Ren92363622012-06-07 22:39:10 +000014929// Generate NEG and CMOV for integer abs.
14930static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14931 EVT VT = N->getValueType(0);
14932
14933 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14934 // 8-bit integer abs to NEG and CMOV.
14935 if (VT.isInteger() && VT.getSizeInBits() == 8)
14936 return SDValue();
14937
14938 SDValue N0 = N->getOperand(0);
14939 SDValue N1 = N->getOperand(1);
14940 DebugLoc DL = N->getDebugLoc();
14941
14942 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14943 // and change it to SUB and CMOV.
14944 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14945 N0.getOpcode() == ISD::ADD &&
14946 N0.getOperand(1) == N1 &&
14947 N1.getOpcode() == ISD::SRA &&
14948 N1.getOperand(0) == N0.getOperand(0))
14949 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14950 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14951 // Generate SUB & CMOV.
14952 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14953 DAG.getConstant(0, VT), N0.getOperand(0));
14954
14955 SDValue Ops[] = { N0.getOperand(0), Neg,
14956 DAG.getConstant(X86::COND_GE, MVT::i8),
14957 SDValue(Neg.getNode(), 1) };
14958 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14959 Ops, array_lengthof(Ops));
14960 }
14961 return SDValue();
14962}
14963
Craig Topper3738ccd2011-12-27 06:27:23 +000014964// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014965static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14966 TargetLowering::DAGCombinerInfo &DCI,
14967 const X86Subtarget *Subtarget) {
14968 if (DCI.isBeforeLegalizeOps())
14969 return SDValue();
14970
Manman Ren45d53b82012-06-08 18:58:26 +000014971 if (Subtarget->hasCMov()) {
14972 SDValue RV = performIntegerAbsCombine(N, DAG);
14973 if (RV.getNode())
14974 return RV;
14975 }
Manman Ren92363622012-06-07 22:39:10 +000014976
14977 // Try forming BMI if it is available.
14978 if (!Subtarget->hasBMI())
14979 return SDValue();
14980
Craig Topperb4c94572011-10-21 06:55:01 +000014981 EVT VT = N->getValueType(0);
14982
14983 if (VT != MVT::i32 && VT != MVT::i64)
14984 return SDValue();
14985
Craig Topper3738ccd2011-12-27 06:27:23 +000014986 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14987
Craig Topperb4c94572011-10-21 06:55:01 +000014988 // Create BLSMSK instructions by finding X ^ (X-1)
14989 SDValue N0 = N->getOperand(0);
14990 SDValue N1 = N->getOperand(1);
14991 DebugLoc DL = N->getDebugLoc();
14992
14993 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14994 isAllOnes(N0.getOperand(1)))
14995 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14996
14997 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14998 isAllOnes(N1.getOperand(1)))
14999 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15000
15001 return SDValue();
15002}
15003
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015004/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15005static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015006 TargetLowering::DAGCombinerInfo &DCI,
15007 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015008 LoadSDNode *Ld = cast<LoadSDNode>(N);
15009 EVT RegVT = Ld->getValueType(0);
15010 EVT MemVT = Ld->getMemoryVT();
15011 DebugLoc dl = Ld->getDebugLoc();
15012 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15013
15014 ISD::LoadExtType Ext = Ld->getExtensionType();
15015
Nadav Rotemca6f2962011-09-18 19:00:23 +000015016 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015017 // shuffle. We need SSE4 for the shuffles.
15018 // TODO: It is possible to support ZExt by zeroing the undef values
15019 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015020 if (RegVT.isVector() && RegVT.isInteger() &&
15021 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015022 assert(MemVT != RegVT && "Cannot extend to the same type");
15023 assert(MemVT.isVector() && "Must load a vector from memory");
15024
15025 unsigned NumElems = RegVT.getVectorNumElements();
15026 unsigned RegSz = RegVT.getSizeInBits();
15027 unsigned MemSz = MemVT.getSizeInBits();
15028 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015029
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015030 // All sizes must be a power of two.
15031 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15032 return SDValue();
15033
15034 // Attempt to load the original value using scalar loads.
15035 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015036 MVT SclrLoadTy = MVT::i8;
15037 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15038 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15039 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015040 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015041 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015042 }
15043 }
15044
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015045 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15046 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15047 (64 <= MemSz))
15048 SclrLoadTy = MVT::f64;
15049
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015050 // Calculate the number of scalar loads that we need to perform
15051 // in order to load our vector from memory.
15052 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015053
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015054 // Represent our vector as a sequence of elements which are the
15055 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015056 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15057 RegSz/SclrLoadTy.getSizeInBits());
15058
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015059 // Represent the data using the same element type that is stored in
15060 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015061 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15062 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015063
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015064 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15065 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015066
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015067 // We can't shuffle using an illegal type.
15068 if (!TLI.isTypeLegal(WideVecVT))
15069 return SDValue();
15070
15071 SmallVector<SDValue, 8> Chains;
15072 SDValue Ptr = Ld->getBasePtr();
15073 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15074 TLI.getPointerTy());
15075 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15076
15077 for (unsigned i = 0; i < NumLoads; ++i) {
15078 // Perform a single load.
15079 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15080 Ptr, Ld->getPointerInfo(),
15081 Ld->isVolatile(), Ld->isNonTemporal(),
15082 Ld->isInvariant(), Ld->getAlignment());
15083 Chains.push_back(ScalarLoad.getValue(1));
15084 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15085 // another round of DAGCombining.
15086 if (i == 0)
15087 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15088 else
15089 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15090 ScalarLoad, DAG.getIntPtrConstant(i));
15091
15092 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15093 }
15094
15095 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15096 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015097
15098 // Bitcast the loaded value to a vector of the original element type, in
15099 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015100 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015101 unsigned SizeRatio = RegSz/MemSz;
15102
15103 // Redistribute the loaded elements into the different locations.
15104 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015105 for (unsigned i = 0; i != NumElems; ++i)
15106 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015107
15108 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015109 DAG.getUNDEF(WideVecVT),
15110 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015111
15112 // Bitcast to the requested type.
15113 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15114 // Replace the original load with the new sequence
15115 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015116 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015117 }
15118
15119 return SDValue();
15120}
15121
Chris Lattner149a4e52008-02-22 02:09:43 +000015122/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015123static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015124 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015125 StoreSDNode *St = cast<StoreSDNode>(N);
15126 EVT VT = St->getValue().getValueType();
15127 EVT StVT = St->getMemoryVT();
15128 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015129 SDValue StoredVal = St->getOperand(1);
15130 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15131
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015132 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015133 // On Sandy Bridge, 256-bit memory operations are executed by two
15134 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15135 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015136 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015137 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15138 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015139 SDValue Value0 = StoredVal.getOperand(0);
15140 SDValue Value1 = StoredVal.getOperand(1);
15141
15142 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15143 SDValue Ptr0 = St->getBasePtr();
15144 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15145
15146 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15147 St->getPointerInfo(), St->isVolatile(),
15148 St->isNonTemporal(), St->getAlignment());
15149 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15150 St->getPointerInfo(), St->isVolatile(),
15151 St->isNonTemporal(), St->getAlignment());
15152 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15153 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015154
15155 // Optimize trunc store (of multiple scalars) to shuffle and store.
15156 // First, pack all of the elements in one place. Next, store to memory
15157 // in fewer chunks.
15158 if (St->isTruncatingStore() && VT.isVector()) {
15159 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15160 unsigned NumElems = VT.getVectorNumElements();
15161 assert(StVT != VT && "Cannot truncate to the same type");
15162 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15163 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15164
15165 // From, To sizes and ElemCount must be pow of two
15166 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015167 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015168 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015169 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015170
Nadav Rotem614061b2011-08-10 19:30:14 +000015171 unsigned SizeRatio = FromSz / ToSz;
15172
15173 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15174
15175 // Create a type on which we perform the shuffle
15176 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15177 StVT.getScalarType(), NumElems*SizeRatio);
15178
15179 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15180
15181 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15182 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015183 for (unsigned i = 0; i != NumElems; ++i)
15184 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015185
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015186 // Can't shuffle using an illegal type.
15187 if (!TLI.isTypeLegal(WideVecVT))
15188 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015189
15190 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015191 DAG.getUNDEF(WideVecVT),
15192 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015193 // At this point all of the data is stored at the bottom of the
15194 // register. We now need to save it to mem.
15195
15196 // Find the largest store unit
15197 MVT StoreType = MVT::i8;
15198 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15199 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15200 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015201 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015202 StoreType = Tp;
15203 }
15204
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015205 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15206 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15207 (64 <= NumElems * ToSz))
15208 StoreType = MVT::f64;
15209
Nadav Rotem614061b2011-08-10 19:30:14 +000015210 // Bitcast the original vector into a vector of store-size units
15211 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015212 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015213 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15214 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15215 SmallVector<SDValue, 8> Chains;
15216 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15217 TLI.getPointerTy());
15218 SDValue Ptr = St->getBasePtr();
15219
15220 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015221 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015222 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15223 StoreType, ShuffWide,
15224 DAG.getIntPtrConstant(i));
15225 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15226 St->getPointerInfo(), St->isVolatile(),
15227 St->isNonTemporal(), St->getAlignment());
15228 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15229 Chains.push_back(Ch);
15230 }
15231
15232 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15233 Chains.size());
15234 }
15235
15236
Chris Lattner149a4e52008-02-22 02:09:43 +000015237 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15238 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015239 // A preferable solution to the general problem is to figure out the right
15240 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015241
15242 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015243 if (VT.getSizeInBits() != 64)
15244 return SDValue();
15245
Devang Patel578efa92009-06-05 21:57:13 +000015246 const Function *F = DAG.getMachineFunction().getFunction();
15247 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015248 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015249 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015250 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015251 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015252 isa<LoadSDNode>(St->getValue()) &&
15253 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15254 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015255 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015256 LoadSDNode *Ld = 0;
15257 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015258 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015259 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015260 // Must be a store of a load. We currently handle two cases: the load
15261 // is a direct child, and it's under an intervening TokenFactor. It is
15262 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015263 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015264 Ld = cast<LoadSDNode>(St->getChain());
15265 else if (St->getValue().hasOneUse() &&
15266 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015267 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015268 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015269 TokenFactorIndex = i;
15270 Ld = cast<LoadSDNode>(St->getValue());
15271 } else
15272 Ops.push_back(ChainVal->getOperand(i));
15273 }
15274 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015275
Evan Cheng536e6672009-03-12 05:59:15 +000015276 if (!Ld || !ISD::isNormalLoad(Ld))
15277 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015278
Evan Cheng536e6672009-03-12 05:59:15 +000015279 // If this is not the MMX case, i.e. we are just turning i64 load/store
15280 // into f64 load/store, avoid the transformation if there are multiple
15281 // uses of the loaded value.
15282 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15283 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015284
Evan Cheng536e6672009-03-12 05:59:15 +000015285 DebugLoc LdDL = Ld->getDebugLoc();
15286 DebugLoc StDL = N->getDebugLoc();
15287 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15288 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15289 // pair instead.
15290 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015291 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015292 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15293 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015294 Ld->isNonTemporal(), Ld->isInvariant(),
15295 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015296 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015297 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015298 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015299 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015300 Ops.size());
15301 }
Evan Cheng536e6672009-03-12 05:59:15 +000015302 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015303 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015304 St->isVolatile(), St->isNonTemporal(),
15305 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015306 }
Evan Cheng536e6672009-03-12 05:59:15 +000015307
15308 // Otherwise, lower to two pairs of 32-bit loads / stores.
15309 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015310 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15311 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015312
Owen Anderson825b72b2009-08-11 20:47:22 +000015313 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015314 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015315 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015316 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015317 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015318 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015319 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015320 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015321 MinAlign(Ld->getAlignment(), 4));
15322
15323 SDValue NewChain = LoLd.getValue(1);
15324 if (TokenFactorIndex != -1) {
15325 Ops.push_back(LoLd);
15326 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015327 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015328 Ops.size());
15329 }
15330
15331 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015332 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15333 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015334
15335 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015336 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015337 St->isVolatile(), St->isNonTemporal(),
15338 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015339 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015340 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015341 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015342 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015343 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015344 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015345 }
Dan Gohman475871a2008-07-27 21:46:04 +000015346 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015347}
15348
Duncan Sands17470be2011-09-22 20:15:48 +000015349/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15350/// and return the operands for the horizontal operation in LHS and RHS. A
15351/// horizontal operation performs the binary operation on successive elements
15352/// of its first operand, then on successive elements of its second operand,
15353/// returning the resulting values in a vector. For example, if
15354/// A = < float a0, float a1, float a2, float a3 >
15355/// and
15356/// B = < float b0, float b1, float b2, float b3 >
15357/// then the result of doing a horizontal operation on A and B is
15358/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15359/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15360/// A horizontal-op B, for some already available A and B, and if so then LHS is
15361/// set to A, RHS to B, and the routine returns 'true'.
15362/// Note that the binary operation should have the property that if one of the
15363/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015364static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015365 // Look for the following pattern: if
15366 // A = < float a0, float a1, float a2, float a3 >
15367 // B = < float b0, float b1, float b2, float b3 >
15368 // and
15369 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15370 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15371 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15372 // which is A horizontal-op B.
15373
15374 // At least one of the operands should be a vector shuffle.
15375 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15376 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15377 return false;
15378
15379 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015380
15381 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15382 "Unsupported vector type for horizontal add/sub");
15383
15384 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15385 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015386 unsigned NumElts = VT.getVectorNumElements();
15387 unsigned NumLanes = VT.getSizeInBits()/128;
15388 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015389 assert((NumLaneElts % 2 == 0) &&
15390 "Vector type should have an even number of elements in each lane");
15391 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015392
15393 // View LHS in the form
15394 // LHS = VECTOR_SHUFFLE A, B, LMask
15395 // If LHS is not a shuffle then pretend it is the shuffle
15396 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15397 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15398 // type VT.
15399 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015400 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015401 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15402 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15403 A = LHS.getOperand(0);
15404 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15405 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015406 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15407 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015408 } else {
15409 if (LHS.getOpcode() != ISD::UNDEF)
15410 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015411 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015412 LMask[i] = i;
15413 }
15414
15415 // Likewise, view RHS in the form
15416 // RHS = VECTOR_SHUFFLE C, D, RMask
15417 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015418 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015419 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15420 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15421 C = RHS.getOperand(0);
15422 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15423 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015424 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15425 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015426 } else {
15427 if (RHS.getOpcode() != ISD::UNDEF)
15428 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015429 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015430 RMask[i] = i;
15431 }
15432
15433 // Check that the shuffles are both shuffling the same vectors.
15434 if (!(A == C && B == D) && !(A == D && B == C))
15435 return false;
15436
15437 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15438 if (!A.getNode() && !B.getNode())
15439 return false;
15440
15441 // If A and B occur in reverse order in RHS, then "swap" them (which means
15442 // rewriting the mask).
15443 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015444 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015445
15446 // At this point LHS and RHS are equivalent to
15447 // LHS = VECTOR_SHUFFLE A, B, LMask
15448 // RHS = VECTOR_SHUFFLE A, B, RMask
15449 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015450 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015451 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015452
Craig Topperf8363302011-12-02 08:18:41 +000015453 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015454 if (LIdx < 0 || RIdx < 0 ||
15455 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15456 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015457 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015458
Craig Topperf8363302011-12-02 08:18:41 +000015459 // Check that successive elements are being operated on. If not, this is
15460 // not a horizontal operation.
15461 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15462 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015463 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015464 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015465 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015466 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015467 }
15468
15469 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15470 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15471 return true;
15472}
15473
15474/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15475static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15476 const X86Subtarget *Subtarget) {
15477 EVT VT = N->getValueType(0);
15478 SDValue LHS = N->getOperand(0);
15479 SDValue RHS = N->getOperand(1);
15480
15481 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015482 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015483 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015484 isHorizontalBinOp(LHS, RHS, true))
15485 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15486 return SDValue();
15487}
15488
15489/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15490static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15491 const X86Subtarget *Subtarget) {
15492 EVT VT = N->getValueType(0);
15493 SDValue LHS = N->getOperand(0);
15494 SDValue RHS = N->getOperand(1);
15495
15496 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015497 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015498 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015499 isHorizontalBinOp(LHS, RHS, false))
15500 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15501 return SDValue();
15502}
15503
Chris Lattner6cf73262008-01-25 06:14:17 +000015504/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15505/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015506static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015507 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15508 // F[X]OR(0.0, x) -> x
15509 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015510 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15511 if (C->getValueAPF().isPosZero())
15512 return N->getOperand(1);
15513 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15514 if (C->getValueAPF().isPosZero())
15515 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015516 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015517}
15518
Nadav Rotemd60cb112012-08-19 13:06:16 +000015519/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15520/// X86ISD::FMAX nodes.
15521static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15522 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15523
15524 // Only perform optimizations if UnsafeMath is used.
15525 if (!DAG.getTarget().Options.UnsafeFPMath)
15526 return SDValue();
15527
15528 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015529 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015530 unsigned NewOp = 0;
15531 switch (N->getOpcode()) {
15532 default: llvm_unreachable("unknown opcode");
15533 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15534 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15535 }
15536
15537 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15538 N->getOperand(0), N->getOperand(1));
15539}
15540
15541
Chris Lattneraf723b92008-01-25 05:46:26 +000015542/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015543static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015544 // FAND(0.0, x) -> 0.0
15545 // FAND(x, 0.0) -> 0.0
15546 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15547 if (C->getValueAPF().isPosZero())
15548 return N->getOperand(0);
15549 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15550 if (C->getValueAPF().isPosZero())
15551 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015552 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015553}
15554
Dan Gohmane5af2d32009-01-29 01:59:02 +000015555static SDValue PerformBTCombine(SDNode *N,
15556 SelectionDAG &DAG,
15557 TargetLowering::DAGCombinerInfo &DCI) {
15558 // BT ignores high bits in the bit index operand.
15559 SDValue Op1 = N->getOperand(1);
15560 if (Op1.hasOneUse()) {
15561 unsigned BitWidth = Op1.getValueSizeInBits();
15562 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15563 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015564 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15565 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015566 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015567 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15568 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15569 DCI.CommitTargetLoweringOpt(TLO);
15570 }
15571 return SDValue();
15572}
Chris Lattner83e6c992006-10-04 06:57:07 +000015573
Eli Friedman7a5e5552009-06-07 06:52:44 +000015574static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15575 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015576 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015577 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015578 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015579 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015580 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015581 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015582 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015583 }
15584 return SDValue();
15585}
15586
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015587static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15588 TargetLowering::DAGCombinerInfo &DCI,
15589 const X86Subtarget *Subtarget) {
15590 if (!DCI.isBeforeLegalizeOps())
15591 return SDValue();
15592
Craig Topper3ef43cf2012-04-24 06:36:35 +000015593 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015594 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015595
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015596 EVT VT = N->getValueType(0);
15597 SDValue Op = N->getOperand(0);
15598 EVT OpVT = Op.getValueType();
15599 DebugLoc dl = N->getDebugLoc();
15600
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015601 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15602 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015603
Craig Topper3ef43cf2012-04-24 06:36:35 +000015604 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015605 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015606
15607 // Optimize vectors in AVX mode
15608 // Sign extend v8i16 to v8i32 and
15609 // v4i32 to v4i64
15610 //
15611 // Divide input vector into two parts
15612 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15613 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15614 // concat the vectors to original VT
15615
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015616 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015617 SDValue Undef = DAG.getUNDEF(OpVT);
15618
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015619 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015620 for (unsigned i = 0; i != NumElems/2; ++i)
15621 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015622
Craig Toppercacafd42012-08-14 08:18:43 +000015623 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015624
15625 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015626 for (unsigned i = 0; i != NumElems/2; ++i)
15627 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015628
Craig Toppercacafd42012-08-14 08:18:43 +000015629 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015630
Craig Topper3ef43cf2012-04-24 06:36:35 +000015631 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015632 VT.getVectorNumElements()/2);
15633
Craig Topper3ef43cf2012-04-24 06:36:35 +000015634 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015635 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15636
15637 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15638 }
15639 return SDValue();
15640}
15641
Michael Liaof6c24ee2012-08-10 14:39:24 +000015642static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015643 const X86Subtarget* Subtarget) {
15644 DebugLoc dl = N->getDebugLoc();
15645 EVT VT = N->getValueType(0);
15646
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015647 // Let legalize expand this if it isn't a legal type yet.
15648 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15649 return SDValue();
15650
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015651 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015652 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15653 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015654 return SDValue();
15655
15656 SDValue A = N->getOperand(0);
15657 SDValue B = N->getOperand(1);
15658 SDValue C = N->getOperand(2);
15659
15660 bool NegA = (A.getOpcode() == ISD::FNEG);
15661 bool NegB = (B.getOpcode() == ISD::FNEG);
15662 bool NegC = (C.getOpcode() == ISD::FNEG);
15663
Michael Liaof6c24ee2012-08-10 14:39:24 +000015664 // Negative multiplication when NegA xor NegB
15665 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015666 if (NegA)
15667 A = A.getOperand(0);
15668 if (NegB)
15669 B = B.getOperand(0);
15670 if (NegC)
15671 C = C.getOperand(0);
15672
15673 unsigned Opcode;
15674 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015675 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015676 else
Craig Topperbf404372012-08-31 15:40:30 +000015677 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15678
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015679 return DAG.getNode(Opcode, dl, VT, A, B, C);
15680}
15681
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015682static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015683 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015684 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015685 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15686 // (and (i32 x86isd::setcc_carry), 1)
15687 // This eliminates the zext. This transformation is necessary because
15688 // ISD::SETCC is always legalized to i8.
15689 DebugLoc dl = N->getDebugLoc();
15690 SDValue N0 = N->getOperand(0);
15691 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015692 EVT OpVT = N0.getValueType();
15693
Evan Cheng2e489c42009-12-16 00:53:11 +000015694 if (N0.getOpcode() == ISD::AND &&
15695 N0.hasOneUse() &&
15696 N0.getOperand(0).hasOneUse()) {
15697 SDValue N00 = N0.getOperand(0);
15698 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15699 return SDValue();
15700 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15701 if (!C || C->getZExtValue() != 1)
15702 return SDValue();
15703 return DAG.getNode(ISD::AND, dl, VT,
15704 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15705 N00.getOperand(0), N00.getOperand(1)),
15706 DAG.getConstant(1, VT));
15707 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015708
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015709 // Optimize vectors in AVX mode:
15710 //
15711 // v8i16 -> v8i32
15712 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15713 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15714 // Concat upper and lower parts.
15715 //
15716 // v4i32 -> v4i64
15717 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15718 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15719 // Concat upper and lower parts.
15720 //
Craig Topperc16f8512012-04-25 06:39:39 +000015721 if (!DCI.isBeforeLegalizeOps())
15722 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015723
Craig Topperc16f8512012-04-25 06:39:39 +000015724 if (!Subtarget->hasAVX())
15725 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015726
Craig Topperc16f8512012-04-25 06:39:39 +000015727 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15728 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015729
Craig Topperc16f8512012-04-25 06:39:39 +000015730 if (Subtarget->hasAVX2())
15731 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015732
Craig Topperc16f8512012-04-25 06:39:39 +000015733 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15734 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15735 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015736
Craig Topperc16f8512012-04-25 06:39:39 +000015737 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15738 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015739
Craig Topperc16f8512012-04-25 06:39:39 +000015740 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15741 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15742
15743 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015744 }
15745
Evan Cheng2e489c42009-12-16 00:53:11 +000015746 return SDValue();
15747}
15748
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015749// Optimize x == -y --> x+y == 0
15750// x != -y --> x+y != 0
15751static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15752 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15753 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015754 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015755
15756 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15758 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15759 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15760 LHS.getValueType(), RHS, LHS.getOperand(1));
15761 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15762 addV, DAG.getConstant(0, addV.getValueType()), CC);
15763 }
15764 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15765 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15766 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15767 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15768 RHS.getValueType(), LHS, RHS.getOperand(1));
15769 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15770 addV, DAG.getConstant(0, addV.getValueType()), CC);
15771 }
15772 return SDValue();
15773}
15774
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015775// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015776static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15777 TargetLowering::DAGCombinerInfo &DCI,
15778 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015779 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015780 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15781 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015782
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015783 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15784 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15785 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015786 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015787 return DAG.getNode(ISD::AND, DL, MVT::i8,
15788 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015789 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015790 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015791
Michael Liao2a33cec2012-08-10 19:58:13 +000015792 SDValue Flags;
15793
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015794 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15795 if (Flags.getNode()) {
15796 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15797 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15798 }
15799
15800 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015801 if (Flags.getNode()) {
15802 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15803 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15804 }
15805
15806 return SDValue();
15807}
15808
15809// Optimize branch condition evaluation.
15810//
15811static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15812 TargetLowering::DAGCombinerInfo &DCI,
15813 const X86Subtarget *Subtarget) {
15814 DebugLoc DL = N->getDebugLoc();
15815 SDValue Chain = N->getOperand(0);
15816 SDValue Dest = N->getOperand(1);
15817 SDValue EFLAGS = N->getOperand(3);
15818 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15819
15820 SDValue Flags;
15821
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015822 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15823 if (Flags.getNode()) {
15824 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15825 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15826 Flags);
15827 }
15828
15829 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015830 if (Flags.getNode()) {
15831 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15832 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15833 Flags);
15834 }
15835
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015836 return SDValue();
15837}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015838
Craig Topper7fd5e162012-04-24 06:02:29 +000015839static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015840 SDValue Op0 = N->getOperand(0);
15841 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015842
15843 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015844 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015845 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015846 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015847 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15848 // Notice that we use SINT_TO_FP because we know that the high bits
15849 // are zero and SINT_TO_FP is better supported by the hardware.
15850 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15851 }
15852
15853 return SDValue();
15854}
15855
Benjamin Kramer1396c402011-06-18 11:09:41 +000015856static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15857 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015858 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015859 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015860
15861 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015862 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015863 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015864 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015865 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15866 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15867 }
15868
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015869 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15870 // a 32-bit target where SSE doesn't support i64->FP operations.
15871 if (Op0.getOpcode() == ISD::LOAD) {
15872 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15873 EVT VT = Ld->getValueType(0);
15874 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15875 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15876 !XTLI->getSubtarget()->is64Bit() &&
15877 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015878 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15879 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015880 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15881 return FILDChain;
15882 }
15883 }
15884 return SDValue();
15885}
15886
Craig Topper7fd5e162012-04-24 06:02:29 +000015887static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15888 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015889
15890 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015891 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15892 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015893 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015894 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15895 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15896 }
15897
15898 return SDValue();
15899}
15900
Chris Lattner23a01992010-12-20 01:37:09 +000015901// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15902static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15903 X86TargetLowering::DAGCombinerInfo &DCI) {
15904 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15905 // the result is either zero or one (depending on the input carry bit).
15906 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15907 if (X86::isZeroNode(N->getOperand(0)) &&
15908 X86::isZeroNode(N->getOperand(1)) &&
15909 // We don't have a good way to replace an EFLAGS use, so only do this when
15910 // dead right now.
15911 SDValue(N, 1).use_empty()) {
15912 DebugLoc DL = N->getDebugLoc();
15913 EVT VT = N->getValueType(0);
15914 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15915 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15916 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15917 DAG.getConstant(X86::COND_B,MVT::i8),
15918 N->getOperand(2)),
15919 DAG.getConstant(1, VT));
15920 return DCI.CombineTo(N, Res1, CarryOut);
15921 }
15922
15923 return SDValue();
15924}
15925
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015926// fold (add Y, (sete X, 0)) -> adc 0, Y
15927// (add Y, (setne X, 0)) -> sbb -1, Y
15928// (sub (sete X, 0), Y) -> sbb 0, Y
15929// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015930static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015931 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015932
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015933 // Look through ZExts.
15934 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15935 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15936 return SDValue();
15937
15938 SDValue SetCC = Ext.getOperand(0);
15939 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15940 return SDValue();
15941
15942 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15943 if (CC != X86::COND_E && CC != X86::COND_NE)
15944 return SDValue();
15945
15946 SDValue Cmp = SetCC.getOperand(1);
15947 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015948 !X86::isZeroNode(Cmp.getOperand(1)) ||
15949 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015950 return SDValue();
15951
15952 SDValue CmpOp0 = Cmp.getOperand(0);
15953 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15954 DAG.getConstant(1, CmpOp0.getValueType()));
15955
15956 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15957 if (CC == X86::COND_NE)
15958 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15959 DL, OtherVal.getValueType(), OtherVal,
15960 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15961 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15962 DL, OtherVal.getValueType(), OtherVal,
15963 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15964}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015965
Craig Topper54f952a2011-11-19 09:02:40 +000015966/// PerformADDCombine - Do target-specific dag combines on integer adds.
15967static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15968 const X86Subtarget *Subtarget) {
15969 EVT VT = N->getValueType(0);
15970 SDValue Op0 = N->getOperand(0);
15971 SDValue Op1 = N->getOperand(1);
15972
15973 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015974 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000015975 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000015976 isHorizontalBinOp(Op0, Op1, true))
15977 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
15978
15979 return OptimizeConditionalInDecrement(N, DAG);
15980}
15981
15982static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
15983 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015984 SDValue Op0 = N->getOperand(0);
15985 SDValue Op1 = N->getOperand(1);
15986
15987 // X86 can't encode an immediate LHS of a sub. See if we can push the
15988 // negation into a preceding instruction.
15989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015990 // If the RHS of the sub is a XOR with one use and a constant, invert the
15991 // immediate. Then add one to the LHS of the sub so we can turn
15992 // X-Y -> X+~Y+1, saving one register.
15993 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
15994 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000015995 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015996 EVT VT = Op0.getValueType();
15997 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
15998 Op1.getOperand(0),
15999 DAG.getConstant(~XorC, VT));
16000 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016001 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016002 }
16003 }
16004
Craig Topper54f952a2011-11-19 09:02:40 +000016005 // Try to synthesize horizontal adds from adds of shuffles.
16006 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016007 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016008 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16009 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016010 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16011
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016012 return OptimizeConditionalInDecrement(N, DAG);
16013}
16014
Dan Gohman475871a2008-07-27 21:46:04 +000016015SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016016 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016017 SelectionDAG &DAG = DCI.DAG;
16018 switch (N->getOpcode()) {
16019 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016020 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016021 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016022 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016023 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016024 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016025 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16026 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016027 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016028 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016029 case ISD::SHL:
16030 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016031 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016032 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016033 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016034 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016035 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016036 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016037 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016038 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016039 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016040 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16041 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016042 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016043 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016044 case X86ISD::FMIN:
16045 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016046 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016047 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016048 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016049 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016050 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016051 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000016052 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016053 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016054 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016055 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016056 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016057 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016058 case X86ISD::UNPCKH:
16059 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016060 case X86ISD::MOVHLPS:
16061 case X86ISD::MOVLHPS:
16062 case X86ISD::PSHUFD:
16063 case X86ISD::PSHUFHW:
16064 case X86ISD::PSHUFLW:
16065 case X86ISD::MOVSS:
16066 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016067 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016068 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016069 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016070 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016071 }
16072
Dan Gohman475871a2008-07-27 21:46:04 +000016073 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016074}
16075
Evan Chenge5b51ac2010-04-17 06:13:15 +000016076/// isTypeDesirableForOp - Return true if the target has native support for
16077/// the specified value type and it is 'desirable' to use the type for the
16078/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16079/// instruction encodings are longer and some i16 instructions are slow.
16080bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16081 if (!isTypeLegal(VT))
16082 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016083 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016084 return true;
16085
16086 switch (Opc) {
16087 default:
16088 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016089 case ISD::LOAD:
16090 case ISD::SIGN_EXTEND:
16091 case ISD::ZERO_EXTEND:
16092 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016093 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016094 case ISD::SRL:
16095 case ISD::SUB:
16096 case ISD::ADD:
16097 case ISD::MUL:
16098 case ISD::AND:
16099 case ISD::OR:
16100 case ISD::XOR:
16101 return false;
16102 }
16103}
16104
16105/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016106/// beneficial for dag combiner to promote the specified node. If true, it
16107/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016108bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016109 EVT VT = Op.getValueType();
16110 if (VT != MVT::i16)
16111 return false;
16112
Evan Cheng4c26e932010-04-19 19:29:22 +000016113 bool Promote = false;
16114 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016115 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016116 default: break;
16117 case ISD::LOAD: {
16118 LoadSDNode *LD = cast<LoadSDNode>(Op);
16119 // If the non-extending load has a single use and it's not live out, then it
16120 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016121 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16122 Op.hasOneUse()*/) {
16123 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16124 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16125 // The only case where we'd want to promote LOAD (rather then it being
16126 // promoted as an operand is when it's only use is liveout.
16127 if (UI->getOpcode() != ISD::CopyToReg)
16128 return false;
16129 }
16130 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016131 Promote = true;
16132 break;
16133 }
16134 case ISD::SIGN_EXTEND:
16135 case ISD::ZERO_EXTEND:
16136 case ISD::ANY_EXTEND:
16137 Promote = true;
16138 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016139 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016140 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016141 SDValue N0 = Op.getOperand(0);
16142 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016143 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016144 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016145 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016146 break;
16147 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016148 case ISD::ADD:
16149 case ISD::MUL:
16150 case ISD::AND:
16151 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016152 case ISD::XOR:
16153 Commute = true;
16154 // fallthrough
16155 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016156 SDValue N0 = Op.getOperand(0);
16157 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016158 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016159 return false;
16160 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016161 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016162 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016163 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016164 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016165 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016166 }
16167 }
16168
16169 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016170 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016171}
16172
Evan Cheng60c07e12006-07-05 22:17:51 +000016173//===----------------------------------------------------------------------===//
16174// X86 Inline Assembly Support
16175//===----------------------------------------------------------------------===//
16176
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016177namespace {
16178 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016179 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016180 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016181
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016182 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016183 StringRef piece(*args[i]);
16184 if (!s.startswith(piece)) // Check if the piece matches.
16185 return false;
16186
16187 s = s.substr(piece.size());
16188 StringRef::size_type pos = s.find_first_not_of(" \t");
16189 if (pos == 0) // We matched a prefix.
16190 return false;
16191
16192 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016193 }
16194
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016195 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016196 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016197 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016198}
16199
Chris Lattnerb8105652009-07-20 17:51:36 +000016200bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16201 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016202
16203 std::string AsmStr = IA->getAsmString();
16204
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016205 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16206 if (!Ty || Ty->getBitWidth() % 16 != 0)
16207 return false;
16208
Chris Lattnerb8105652009-07-20 17:51:36 +000016209 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016210 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016211 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016212
16213 switch (AsmPieces.size()) {
16214 default: return false;
16215 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016216 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016217 // we will turn this bswap into something that will be lowered to logical
16218 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16219 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016220 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016221 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16222 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16223 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16224 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16225 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16226 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016227 // No need to check constraints, nothing other than the equivalent of
16228 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016229 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016230 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016231
Chris Lattnerb8105652009-07-20 17:51:36 +000016232 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016233 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016234 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016235 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16236 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016237 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016238 const std::string &ConstraintsStr = IA->getConstraintString();
16239 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016240 std::sort(AsmPieces.begin(), AsmPieces.end());
16241 if (AsmPieces.size() == 4 &&
16242 AsmPieces[0] == "~{cc}" &&
16243 AsmPieces[1] == "~{dirflag}" &&
16244 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016245 AsmPieces[3] == "~{fpsr}")
16246 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016247 }
16248 break;
16249 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016250 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016251 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016252 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16253 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16254 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016255 AsmPieces.clear();
16256 const std::string &ConstraintsStr = IA->getConstraintString();
16257 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16258 std::sort(AsmPieces.begin(), AsmPieces.end());
16259 if (AsmPieces.size() == 4 &&
16260 AsmPieces[0] == "~{cc}" &&
16261 AsmPieces[1] == "~{dirflag}" &&
16262 AsmPieces[2] == "~{flags}" &&
16263 AsmPieces[3] == "~{fpsr}")
16264 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016265 }
Evan Cheng55d42002011-01-08 01:24:27 +000016266
16267 if (CI->getType()->isIntegerTy(64)) {
16268 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16269 if (Constraints.size() >= 2 &&
16270 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16271 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16272 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016273 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16274 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16275 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016276 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016277 }
16278 }
16279 break;
16280 }
16281 return false;
16282}
16283
16284
16285
Chris Lattnerf4dff842006-07-11 02:54:03 +000016286/// getConstraintType - Given a constraint letter, return the type of
16287/// constraint it is for this target.
16288X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016289X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16290 if (Constraint.size() == 1) {
16291 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016292 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016293 case 'q':
16294 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016295 case 'f':
16296 case 't':
16297 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016298 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016299 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016300 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016301 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016302 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016303 case 'a':
16304 case 'b':
16305 case 'c':
16306 case 'd':
16307 case 'S':
16308 case 'D':
16309 case 'A':
16310 return C_Register;
16311 case 'I':
16312 case 'J':
16313 case 'K':
16314 case 'L':
16315 case 'M':
16316 case 'N':
16317 case 'G':
16318 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016319 case 'e':
16320 case 'Z':
16321 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016322 default:
16323 break;
16324 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016325 }
Chris Lattner4234f572007-03-25 02:14:49 +000016326 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016327}
16328
John Thompson44ab89e2010-10-29 17:29:13 +000016329/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016330/// This object must already have been set up with the operand type
16331/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016332TargetLowering::ConstraintWeight
16333 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016334 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016335 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016336 Value *CallOperandVal = info.CallOperandVal;
16337 // If we don't have a value, we can't do a match,
16338 // but allow it at the lowest weight.
16339 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016340 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016341 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016342 // Look at the constraint type.
16343 switch (*constraint) {
16344 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016345 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16346 case 'R':
16347 case 'q':
16348 case 'Q':
16349 case 'a':
16350 case 'b':
16351 case 'c':
16352 case 'd':
16353 case 'S':
16354 case 'D':
16355 case 'A':
16356 if (CallOperandVal->getType()->isIntegerTy())
16357 weight = CW_SpecificReg;
16358 break;
16359 case 'f':
16360 case 't':
16361 case 'u':
16362 if (type->isFloatingPointTy())
16363 weight = CW_SpecificReg;
16364 break;
16365 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016366 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016367 weight = CW_SpecificReg;
16368 break;
16369 case 'x':
16370 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016371 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016372 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016373 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016374 break;
16375 case 'I':
16376 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16377 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016378 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016379 }
16380 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016381 case 'J':
16382 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16383 if (C->getZExtValue() <= 63)
16384 weight = CW_Constant;
16385 }
16386 break;
16387 case 'K':
16388 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16389 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16390 weight = CW_Constant;
16391 }
16392 break;
16393 case 'L':
16394 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16395 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16396 weight = CW_Constant;
16397 }
16398 break;
16399 case 'M':
16400 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16401 if (C->getZExtValue() <= 3)
16402 weight = CW_Constant;
16403 }
16404 break;
16405 case 'N':
16406 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16407 if (C->getZExtValue() <= 0xff)
16408 weight = CW_Constant;
16409 }
16410 break;
16411 case 'G':
16412 case 'C':
16413 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16414 weight = CW_Constant;
16415 }
16416 break;
16417 case 'e':
16418 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16419 if ((C->getSExtValue() >= -0x80000000LL) &&
16420 (C->getSExtValue() <= 0x7fffffffLL))
16421 weight = CW_Constant;
16422 }
16423 break;
16424 case 'Z':
16425 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16426 if (C->getZExtValue() <= 0xffffffff)
16427 weight = CW_Constant;
16428 }
16429 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016430 }
16431 return weight;
16432}
16433
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016434/// LowerXConstraint - try to replace an X constraint, which matches anything,
16435/// with another that has more specific requirements based on the type of the
16436/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016437const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016438LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016439 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16440 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016441 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016442 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016443 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016444 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016445 return "x";
16446 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016447
Chris Lattner5e764232008-04-26 23:02:14 +000016448 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016449}
16450
Chris Lattner48884cd2007-08-25 00:47:38 +000016451/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16452/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016453void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016454 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016455 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016456 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016457 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016458
Eric Christopher100c8332011-06-02 23:16:42 +000016459 // Only support length 1 constraints for now.
16460 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016461
Eric Christopher100c8332011-06-02 23:16:42 +000016462 char ConstraintLetter = Constraint[0];
16463 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016464 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016465 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016467 if (C->getZExtValue() <= 31) {
16468 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016469 break;
16470 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016471 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016472 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016473 case 'J':
16474 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016475 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016476 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16477 break;
16478 }
16479 }
16480 return;
16481 case 'K':
16482 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016483 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016484 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16485 break;
16486 }
16487 }
16488 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016489 case 'N':
16490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016491 if (C->getZExtValue() <= 255) {
16492 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016493 break;
16494 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016495 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016496 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016497 case 'e': {
16498 // 32-bit signed value
16499 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016500 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16501 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016502 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016503 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016504 break;
16505 }
16506 // FIXME gcc accepts some relocatable values here too, but only in certain
16507 // memory models; it's complicated.
16508 }
16509 return;
16510 }
16511 case 'Z': {
16512 // 32-bit unsigned value
16513 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016514 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16515 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016516 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16517 break;
16518 }
16519 }
16520 // FIXME gcc accepts some relocatable values here too, but only in certain
16521 // memory models; it's complicated.
16522 return;
16523 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016524 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016525 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016526 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016527 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016528 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016529 break;
16530 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016531
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016532 // In any sort of PIC mode addresses need to be computed at runtime by
16533 // adding in a register or some sort of table lookup. These can't
16534 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016535 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016536 return;
16537
Chris Lattnerdc43a882007-05-03 16:52:29 +000016538 // If we are in non-pic codegen mode, we allow the address of a global (with
16539 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016540 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016541 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016542
Chris Lattner49921962009-05-08 18:23:14 +000016543 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16544 while (1) {
16545 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16546 Offset += GA->getOffset();
16547 break;
16548 } else if (Op.getOpcode() == ISD::ADD) {
16549 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16550 Offset += C->getZExtValue();
16551 Op = Op.getOperand(0);
16552 continue;
16553 }
16554 } else if (Op.getOpcode() == ISD::SUB) {
16555 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16556 Offset += -C->getZExtValue();
16557 Op = Op.getOperand(0);
16558 continue;
16559 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016560 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016561
Chris Lattner49921962009-05-08 18:23:14 +000016562 // Otherwise, this isn't something we can handle, reject it.
16563 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016564 }
Eric Christopherfd179292009-08-27 18:07:15 +000016565
Dan Gohman46510a72010-04-15 01:51:59 +000016566 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016567 // If we require an extra load to get this address, as in PIC mode, we
16568 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016569 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16570 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016571 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016572
Devang Patel0d881da2010-07-06 22:08:15 +000016573 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16574 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016575 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016576 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016577 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016578
Gabor Greifba36cb52008-08-28 21:40:38 +000016579 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016580 Ops.push_back(Result);
16581 return;
16582 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016583 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016584}
16585
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016586std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016587X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016588 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016589 // First, see if this is a constraint that directly corresponds to an LLVM
16590 // register class.
16591 if (Constraint.size() == 1) {
16592 // GCC Constraint Letters
16593 switch (Constraint[0]) {
16594 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016595 // TODO: Slight differences here in allocation order and leaving
16596 // RIP in the class. Do they matter any more here than they do
16597 // in the normal allocation?
16598 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16599 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016600 if (VT == MVT::i32 || VT == MVT::f32)
16601 return std::make_pair(0U, &X86::GR32RegClass);
16602 if (VT == MVT::i16)
16603 return std::make_pair(0U, &X86::GR16RegClass);
16604 if (VT == MVT::i8 || VT == MVT::i1)
16605 return std::make_pair(0U, &X86::GR8RegClass);
16606 if (VT == MVT::i64 || VT == MVT::f64)
16607 return std::make_pair(0U, &X86::GR64RegClass);
16608 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016609 }
16610 // 32-bit fallthrough
16611 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016612 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016613 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16614 if (VT == MVT::i16)
16615 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16616 if (VT == MVT::i8 || VT == MVT::i1)
16617 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16618 if (VT == MVT::i64)
16619 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016620 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016621 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016622 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016623 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016624 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016625 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016626 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016627 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016628 return std::make_pair(0U, &X86::GR32RegClass);
16629 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016630 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016631 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016632 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016633 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016634 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016635 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016636 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16637 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016638 case 'f': // FP Stack registers.
16639 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16640 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016641 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016642 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016643 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016644 return std::make_pair(0U, &X86::RFP64RegClass);
16645 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016646 case 'y': // MMX_REGS if MMX allowed.
16647 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016648 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016649 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016650 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016651 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016652 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016653 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016654
Owen Anderson825b72b2009-08-11 20:47:22 +000016655 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016656 default: break;
16657 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016658 case MVT::f32:
16659 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016660 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016661 case MVT::f64:
16662 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016663 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016664 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016665 case MVT::v16i8:
16666 case MVT::v8i16:
16667 case MVT::v4i32:
16668 case MVT::v2i64:
16669 case MVT::v4f32:
16670 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016671 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016672 // AVX types.
16673 case MVT::v32i8:
16674 case MVT::v16i16:
16675 case MVT::v8i32:
16676 case MVT::v4i64:
16677 case MVT::v8f32:
16678 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016679 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016680 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016681 break;
16682 }
16683 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016684
Chris Lattnerf76d1802006-07-31 23:26:50 +000016685 // Use the default implementation in TargetLowering to convert the register
16686 // constraint into a member of a register class.
16687 std::pair<unsigned, const TargetRegisterClass*> Res;
16688 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016689
16690 // Not found as a standard register?
16691 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016692 // Map st(0) -> st(7) -> ST0
16693 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16694 tolower(Constraint[1]) == 's' &&
16695 tolower(Constraint[2]) == 't' &&
16696 Constraint[3] == '(' &&
16697 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16698 Constraint[5] == ')' &&
16699 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016700
Chris Lattner56d77c72009-09-13 22:41:48 +000016701 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016702 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016703 return Res;
16704 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016705
Chris Lattner56d77c72009-09-13 22:41:48 +000016706 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016707 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016708 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016709 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016710 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016711 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016712
16713 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016714 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016715 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016716 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016717 return Res;
16718 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016719
Dale Johannesen330169f2008-11-13 21:52:36 +000016720 // 'A' means EAX + EDX.
16721 if (Constraint == "A") {
16722 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016723 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016724 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016725 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016726 return Res;
16727 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016728
Chris Lattnerf76d1802006-07-31 23:26:50 +000016729 // Otherwise, check to see if this is a register class of the wrong value
16730 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16731 // turn into {ax},{dx}.
16732 if (Res.second->hasType(VT))
16733 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016734
Chris Lattnerf76d1802006-07-31 23:26:50 +000016735 // All of the single-register GCC register classes map their values onto
16736 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16737 // really want an 8-bit or 32-bit register, map to the appropriate register
16738 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016739 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016740 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016741 unsigned DestReg = 0;
16742 switch (Res.first) {
16743 default: break;
16744 case X86::AX: DestReg = X86::AL; break;
16745 case X86::DX: DestReg = X86::DL; break;
16746 case X86::CX: DestReg = X86::CL; break;
16747 case X86::BX: DestReg = X86::BL; break;
16748 }
16749 if (DestReg) {
16750 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016751 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016752 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016753 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016754 unsigned DestReg = 0;
16755 switch (Res.first) {
16756 default: break;
16757 case X86::AX: DestReg = X86::EAX; break;
16758 case X86::DX: DestReg = X86::EDX; break;
16759 case X86::CX: DestReg = X86::ECX; break;
16760 case X86::BX: DestReg = X86::EBX; break;
16761 case X86::SI: DestReg = X86::ESI; break;
16762 case X86::DI: DestReg = X86::EDI; break;
16763 case X86::BP: DestReg = X86::EBP; break;
16764 case X86::SP: DestReg = X86::ESP; break;
16765 }
16766 if (DestReg) {
16767 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016768 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016769 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016770 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016771 unsigned DestReg = 0;
16772 switch (Res.first) {
16773 default: break;
16774 case X86::AX: DestReg = X86::RAX; break;
16775 case X86::DX: DestReg = X86::RDX; break;
16776 case X86::CX: DestReg = X86::RCX; break;
16777 case X86::BX: DestReg = X86::RBX; break;
16778 case X86::SI: DestReg = X86::RSI; break;
16779 case X86::DI: DestReg = X86::RDI; break;
16780 case X86::BP: DestReg = X86::RBP; break;
16781 case X86::SP: DestReg = X86::RSP; break;
16782 }
16783 if (DestReg) {
16784 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016785 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016786 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016787 }
Craig Topperc9099502012-04-20 06:31:50 +000016788 } else if (Res.second == &X86::FR32RegClass ||
16789 Res.second == &X86::FR64RegClass ||
16790 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016791 // Handle references to XMM physical registers that got mapped into the
16792 // wrong class. This can happen with constraints like {xmm0} where the
16793 // target independent register mapper will just pick the first match it can
16794 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016795
16796 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016797 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016798 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016799 Res.second = &X86::FR64RegClass;
16800 else if (X86::VR128RegClass.hasType(VT))
16801 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016802 else if (X86::VR256RegClass.hasType(VT))
16803 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016804 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016805
Chris Lattnerf76d1802006-07-31 23:26:50 +000016806 return Res;
16807}