blob: cb803f8915761e62dcc148c7f5a1c60347de97cd [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) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000650 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000652 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000654 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000655 addLegalFPImmediate(APFloat(+0.0)); // FLD0
656 addLegalFPImmediate(APFloat(+1.0)); // FLD1
657 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
658 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000659 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
660 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
661 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
662 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000663 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000664
Cameron Zwarich33390842011-07-08 21:39:21 +0000665 // We don't support FMA.
666 setOperationAction(ISD::FMA, MVT::f64, Expand);
667 setOperationAction(ISD::FMA, MVT::f32, Expand);
668
Dale Johannesen59a58732007-08-05 18:49:15 +0000669 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000670 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000671 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
673 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000674 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000675 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000676 addLegalFPImmediate(TmpFlt); // FLD0
677 TmpFlt.changeSign();
678 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000679
680 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000681 APFloat TmpFlt2(+1.0);
682 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
683 &ignored);
684 addLegalFPImmediate(TmpFlt2); // FLD1
685 TmpFlt2.changeSign();
686 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
687 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000688
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000689 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
691 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000692 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000693
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000694 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
695 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
696 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
697 setOperationAction(ISD::FRINT, MVT::f80, Expand);
698 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000699 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000700 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000701
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000702 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
704 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
705 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::FLOG, MVT::f80, Expand);
708 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
709 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
710 setOperationAction(ISD::FEXP, MVT::f80, Expand);
711 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000712
Mon P Wangf007a8b2008-11-06 05:31:54 +0000713 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000714 // (for widening) or expand (for scalarization). Then we will selectively
715 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000716 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
717 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000734 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
735 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000740 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000744 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000752 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000754 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000761 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000771 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000772 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
774 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
775 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000776 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000777 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
778 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000779 setTruncStoreAction((MVT::SimpleValueType)VT,
780 (MVT::SimpleValueType)InnerVT, Expand);
781 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
782 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
783 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000784 }
785
Evan Chengc7ce29b2009-02-13 22:36:38 +0000786 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
787 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000788 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000789 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000790 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791 }
792
Dale Johannesen0488fb62010-09-30 23:57:10 +0000793 // MMX-sized vectors (other than x86mmx) are expected to be expanded
794 // into smaller operations.
795 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
796 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
797 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
798 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
799 setOperationAction(ISD::AND, MVT::v8i8, Expand);
800 setOperationAction(ISD::AND, MVT::v4i16, Expand);
801 setOperationAction(ISD::AND, MVT::v2i32, Expand);
802 setOperationAction(ISD::AND, MVT::v1i64, Expand);
803 setOperationAction(ISD::OR, MVT::v8i8, Expand);
804 setOperationAction(ISD::OR, MVT::v4i16, Expand);
805 setOperationAction(ISD::OR, MVT::v2i32, Expand);
806 setOperationAction(ISD::OR, MVT::v1i64, Expand);
807 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
808 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
809 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
810 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
812 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
813 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
814 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
816 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
817 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
818 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
819 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000820 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
821 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
822 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
823 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000824
Craig Topper1accb7e2012-01-10 06:54:16 +0000825 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000826 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000827
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
829 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
830 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
831 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
832 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
833 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000834 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
836 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
837 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
839 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000840 }
841
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000844
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000845 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
846 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000847 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
848 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
849 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
850 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000851
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
853 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
854 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
855 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
856 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
857 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
858 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
859 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
860 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
861 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
862 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
863 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
864 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
865 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
866 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
867 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000868 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869
Nadav Rotem354efd82011-09-18 14:57:03 +0000870 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000871 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
872 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
873 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000874
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
876 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
877 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
878 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000880
Evan Cheng2c3ae372006-04-12 21:21:57 +0000881 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000882 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000883 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000884 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000886 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000887 // Do not attempt to custom lower non-128-bit vectors
888 if (!VT.is128BitVector())
889 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000890 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
891 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
892 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000894
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
896 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
897 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
898 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
900 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000901
Nate Begemancdd1eec2008-02-12 22:51:28 +0000902 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
904 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000905 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000907 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000908 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000909 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000910
911 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000912 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000913 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000914
Craig Topper0d1f1762012-08-12 00:34:56 +0000915 setOperationAction(ISD::AND, VT, Promote);
916 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
917 setOperationAction(ISD::OR, VT, Promote);
918 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
919 setOperationAction(ISD::XOR, VT, Promote);
920 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
921 setOperationAction(ISD::LOAD, VT, Promote);
922 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
923 setOperationAction(ISD::SELECT, VT, Promote);
924 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000925 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000926
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000928
Evan Cheng2c3ae372006-04-12 21:21:57 +0000929 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
931 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
932 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
933 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
936 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000937
938 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000939 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000940
Craig Topperd0a31172012-01-10 06:37:29 +0000941 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000942 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
943 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
944 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
945 setOperationAction(ISD::FRINT, MVT::f32, Legal);
946 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
947 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
948 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
949 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
950 setOperationAction(ISD::FRINT, MVT::f64, Legal);
951 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
952
Craig Topper12fb5c62012-09-08 17:42:27 +0000953 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
954 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
955
Nate Begeman14d12ca2008-02-11 04:19:36 +0000956 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000958
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000959 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
960 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
961 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
962 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
963 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000964
Nate Begeman14d12ca2008-02-11 04:19:36 +0000965 // i8 and i16 vectors are custom , because the source register and source
966 // source memory operand types are not the same width. f32 vectors are
967 // custom since the immediate controlling the insert encodes additional
968 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
970 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
971 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
972 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000973
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
975 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
976 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
977 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000978
Pete Coopera77214a2011-11-14 19:38:42 +0000979 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000980 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000981 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000982 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
983 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000984 }
985 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000986
Craig Topper1accb7e2012-01-10 06:54:16 +0000987 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000988 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000989 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000990
Nadav Rotem43012222011-05-11 08:12:09 +0000991 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000992 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000993
Nadav Rotem43012222011-05-11 08:12:09 +0000994 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000995 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000996
997 if (Subtarget->hasAVX2()) {
998 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
999 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1000
1001 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1002 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1003
1004 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1005 } else {
1006 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1007 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1008
1009 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1010 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1011
1012 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1013 }
Nadav Rotem43012222011-05-11 08:12:09 +00001014 }
1015
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001016 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001017 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1018 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1019 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1020 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1021 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1022 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001023
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1026 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001027
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1029 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1030 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1031 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1032 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001033 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001035 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001036
Owen Anderson825b72b2009-08-11 20:47:22 +00001037 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1038 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1039 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1040 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1041 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001042 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001044 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001045
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001046 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1047 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001048 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001049
Michael Liaob8150d82012-09-10 18:33:51 +00001050 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1051
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001052 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1053 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1054
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001055 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1056 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1057
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001058 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001059 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001060
Duncan Sands28b77e92011-09-06 19:07:46 +00001061 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1062 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1063 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1064 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001065
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001066 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1067 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1068 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1069
Craig Topperaaa643c2011-11-09 07:28:55 +00001070 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1071 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1072 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1073 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001074
Craig Topperbf404372012-08-31 15:40:30 +00001075 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001076 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1077 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1078 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1079 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1080 setOperationAction(ISD::FMA, MVT::f32, Custom);
1081 setOperationAction(ISD::FMA, MVT::f64, Custom);
1082 }
Craig Topper880ef452012-08-11 22:34:26 +00001083
Craig Topperaaa643c2011-11-09 07:28:55 +00001084 if (Subtarget->hasAVX2()) {
1085 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1086 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1087 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1088 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001089
Craig Topperaaa643c2011-11-09 07:28:55 +00001090 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1091 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1092 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1093 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001094
Craig Topperaaa643c2011-11-09 07:28:55 +00001095 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1096 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1097 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001098 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001099
1100 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001101
1102 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1103 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1104
1105 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1106 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1107
1108 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001109 } else {
1110 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1111 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1112 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1113 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1114
1115 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1116 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1117 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1118 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1119
1120 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1121 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1122 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1123 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001124
1125 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1126 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1127
1128 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1129 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1130
1131 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001132 }
Craig Topper13894fa2011-08-24 06:14:18 +00001133
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001134 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001135 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1136 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001137 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001138
1139 // Extract subvector is special because the value type
1140 // (result) is 128-bit but the source is 256-bit wide.
1141 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001142 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001143
1144 // Do not attempt to custom lower other non-256-bit vectors
1145 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001146 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001147
Craig Topper0d1f1762012-08-12 00:34:56 +00001148 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1149 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1150 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1151 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1152 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1153 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1154 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001155 }
1156
David Greene54d8eba2011-01-27 22:38:56 +00001157 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001158 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001159 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001160
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001161 // Do not attempt to promote non-256-bit vectors
1162 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001163 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001164
Craig Topper0d1f1762012-08-12 00:34:56 +00001165 setOperationAction(ISD::AND, VT, Promote);
1166 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1167 setOperationAction(ISD::OR, VT, Promote);
1168 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1169 setOperationAction(ISD::XOR, VT, Promote);
1170 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1171 setOperationAction(ISD::LOAD, VT, Promote);
1172 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1173 setOperationAction(ISD::SELECT, VT, Promote);
1174 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001175 }
David Greene9b9838d2009-06-29 16:47:10 +00001176 }
1177
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001178 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1179 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001180 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1181 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001182 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1183 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001184 }
1185
Evan Cheng6be2c582006-04-05 23:38:46 +00001186 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001187 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001188 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001189
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001190
Eli Friedman962f5492010-06-02 19:35:46 +00001191 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1192 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001193 //
Eli Friedman962f5492010-06-02 19:35:46 +00001194 // FIXME: We really should do custom legalization for addition and
1195 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1196 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001197 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1198 // Add/Sub/Mul with overflow operations are custom lowered.
1199 MVT VT = IntVTs[i];
1200 setOperationAction(ISD::SADDO, VT, Custom);
1201 setOperationAction(ISD::UADDO, VT, Custom);
1202 setOperationAction(ISD::SSUBO, VT, Custom);
1203 setOperationAction(ISD::USUBO, VT, Custom);
1204 setOperationAction(ISD::SMULO, VT, Custom);
1205 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001206 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001207
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001208 // There are no 8-bit 3-address imul/mul instructions
1209 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1210 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001211
Evan Chengd54f2d52009-03-31 19:38:51 +00001212 if (!Subtarget->is64Bit()) {
1213 // These libcalls are not available in 32-bit.
1214 setLibcallName(RTLIB::SHL_I128, 0);
1215 setLibcallName(RTLIB::SRL_I128, 0);
1216 setLibcallName(RTLIB::SRA_I128, 0);
1217 }
1218
Evan Cheng206ee9d2006-07-07 08:33:52 +00001219 // We have target-specific dag combine patterns for the following nodes:
1220 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001221 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001222 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001223 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001224 setTargetDAGCombine(ISD::SHL);
1225 setTargetDAGCombine(ISD::SRA);
1226 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001227 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001228 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001229 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001230 setTargetDAGCombine(ISD::FADD);
1231 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001232 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001233 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001234 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001235 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001236 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001237 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001238 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001239 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001240 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001241 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001242 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001243 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001244 if (Subtarget->is64Bit())
1245 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001246 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001248 computeRegisterProperties();
1249
Evan Cheng05219282011-01-06 06:52:41 +00001250 // On Darwin, -Os means optimize for size without hurting performance,
1251 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001252 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001253 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001254 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001255 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1256 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1257 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001258 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001259 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001260
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001261 // Predictable cmov don't hurt on atom because it's in-order.
1262 predictableSelectIsExpensive = !Subtarget->isAtom();
1263
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001264 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001265}
1266
Scott Michel5b8f82e2008-03-10 15:42:14 +00001267
Duncan Sands28b77e92011-09-06 19:07:46 +00001268EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1269 if (!VT.isVector()) return MVT::i8;
1270 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001271}
1272
1273
Evan Cheng29286502008-01-23 23:17:41 +00001274/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1275/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001276static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001277 if (MaxAlign == 16)
1278 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001279 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001280 if (VTy->getBitWidth() == 128)
1281 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001282 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001283 unsigned EltAlign = 0;
1284 getMaxByValAlign(ATy->getElementType(), EltAlign);
1285 if (EltAlign > MaxAlign)
1286 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001287 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001288 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1289 unsigned EltAlign = 0;
1290 getMaxByValAlign(STy->getElementType(i), EltAlign);
1291 if (EltAlign > MaxAlign)
1292 MaxAlign = EltAlign;
1293 if (MaxAlign == 16)
1294 break;
1295 }
1296 }
Evan Cheng29286502008-01-23 23:17:41 +00001297}
1298
1299/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1300/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001301/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1302/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001303unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001304 if (Subtarget->is64Bit()) {
1305 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001306 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001307 if (TyAlign > 8)
1308 return TyAlign;
1309 return 8;
1310 }
1311
Evan Cheng29286502008-01-23 23:17:41 +00001312 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001313 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001314 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001315 return Align;
1316}
Chris Lattner2b02a442007-02-25 08:29:00 +00001317
Evan Chengf0df0312008-05-15 08:39:06 +00001318/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001319/// and store operations as a result of memset, memcpy, and memmove
1320/// lowering. If DstAlign is zero that means it's safe to destination
1321/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1322/// means there isn't a need to check it against alignment requirement,
1323/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001324/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001325/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1326/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1327/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001328/// It returns EVT::Other if the type should be determined using generic
1329/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001330EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001331X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1332 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001333 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001334 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001335 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001336 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1337 // linux. This is because the stack realignment code can't handle certain
1338 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001339 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001340 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001341 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001342 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001343 (Subtarget->isUnalignedMemAccessFast() ||
1344 ((DstAlign == 0 || DstAlign >= 16) &&
1345 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001346 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001347 if (Subtarget->getStackAlignment() >= 32) {
1348 if (Subtarget->hasAVX2())
1349 return MVT::v8i32;
1350 if (Subtarget->hasAVX())
1351 return MVT::v8f32;
1352 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001353 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001354 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001355 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001356 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001357 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001358 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001359 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001360 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001361 // Do not use f64 to lower memcpy if source is string constant. It's
1362 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001363 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001364 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001365 }
Evan Chengf0df0312008-05-15 08:39:06 +00001366 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 return MVT::i64;
1368 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001369}
1370
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001371/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1372/// current function. The returned value is a member of the
1373/// MachineJumpTableInfo::JTEntryKind enum.
1374unsigned X86TargetLowering::getJumpTableEncoding() const {
1375 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1376 // symbol.
1377 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1378 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001379 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001380
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001381 // Otherwise, use the normal jump table encoding heuristics.
1382 return TargetLowering::getJumpTableEncoding();
1383}
1384
Chris Lattnerc64daab2010-01-26 05:02:42 +00001385const MCExpr *
1386X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1387 const MachineBasicBlock *MBB,
1388 unsigned uid,MCContext &Ctx) const{
1389 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1390 Subtarget->isPICStyleGOT());
1391 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1392 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001393 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1394 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001395}
1396
Evan Chengcc415862007-11-09 01:32:10 +00001397/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1398/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001399SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001400 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001401 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001402 // This doesn't have DebugLoc associated with it, but is not really the
1403 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001404 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001405 return Table;
1406}
1407
Chris Lattner589c6f62010-01-26 06:28:43 +00001408/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1409/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1410/// MCExpr.
1411const MCExpr *X86TargetLowering::
1412getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1413 MCContext &Ctx) const {
1414 // X86-64 uses RIP relative addressing based on the jump table label.
1415 if (Subtarget->isPICStyleRIPRel())
1416 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1417
1418 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001419 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001420}
1421
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001422// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001423std::pair<const TargetRegisterClass*, uint8_t>
1424X86TargetLowering::findRepresentativeClass(EVT VT) const{
1425 const TargetRegisterClass *RRC = 0;
1426 uint8_t Cost = 1;
1427 switch (VT.getSimpleVT().SimpleTy) {
1428 default:
1429 return TargetLowering::findRepresentativeClass(VT);
1430 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001431 RRC = Subtarget->is64Bit() ?
1432 (const TargetRegisterClass*)&X86::GR64RegClass :
1433 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001434 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001435 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001436 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001437 break;
1438 case MVT::f32: case MVT::f64:
1439 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1440 case MVT::v4f32: case MVT::v2f64:
1441 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1442 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001443 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001444 break;
1445 }
1446 return std::make_pair(RRC, Cost);
1447}
1448
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001449bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1450 unsigned &Offset) const {
1451 if (!Subtarget->isTargetLinux())
1452 return false;
1453
1454 if (Subtarget->is64Bit()) {
1455 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1456 Offset = 0x28;
1457 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1458 AddressSpace = 256;
1459 else
1460 AddressSpace = 257;
1461 } else {
1462 // %gs:0x14 on i386
1463 Offset = 0x14;
1464 AddressSpace = 256;
1465 }
1466 return true;
1467}
1468
1469
Chris Lattner2b02a442007-02-25 08:29:00 +00001470//===----------------------------------------------------------------------===//
1471// Return Value Calling Convention Implementation
1472//===----------------------------------------------------------------------===//
1473
Chris Lattner59ed56b2007-02-28 04:55:35 +00001474#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001475
Michael J. Spencerec38de22010-10-10 22:04:20 +00001476bool
Eric Christopher471e4222011-06-08 23:55:35 +00001477X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001478 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001479 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001480 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001481 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001482 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001483 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001484 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001485}
1486
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487SDValue
1488X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001489 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001490 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001491 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001492 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001493 MachineFunction &MF = DAG.getMachineFunction();
1494 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Chris Lattner9774c912007-02-27 05:28:59 +00001496 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001497 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498 RVLocs, *DAG.getContext());
1499 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Evan Chengdcea1632010-02-04 02:40:39 +00001501 // Add the regs to the liveout set for the function.
1502 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1503 for (unsigned i = 0; i != RVLocs.size(); ++i)
1504 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1505 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001506
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001508
Dan Gohman475871a2008-07-27 21:46:04 +00001509 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001510 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1511 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001512 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1513 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001515 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001516 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1517 CCValAssign &VA = RVLocs[i];
1518 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001519 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001520 EVT ValVT = ValToCopy.getValueType();
1521
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001522 // Promote values to the appropriate types
1523 if (VA.getLocInfo() == CCValAssign::SExt)
1524 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1525 else if (VA.getLocInfo() == CCValAssign::ZExt)
1526 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1527 else if (VA.getLocInfo() == CCValAssign::AExt)
1528 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1529 else if (VA.getLocInfo() == CCValAssign::BCvt)
1530 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1531
Dale Johannesenc4510512010-09-24 19:05:48 +00001532 // If this is x86-64, and we disabled SSE, we can't return FP values,
1533 // or SSE or MMX vectors.
1534 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1535 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001536 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001537 report_fatal_error("SSE register return with SSE disabled");
1538 }
1539 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1540 // llvm-gcc has never done it right and no one has noticed, so this
1541 // should be OK for now.
1542 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001543 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001544 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattner447ff682008-03-11 03:23:40 +00001546 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1547 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001548 if (VA.getLocReg() == X86::ST0 ||
1549 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001550 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1551 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001552 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001554 RetOps.push_back(ValToCopy);
1555 // Don't emit a copytoreg.
1556 continue;
1557 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001558
Evan Cheng242b38b2009-02-23 09:03:22 +00001559 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1560 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001561 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001562 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001563 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001564 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001565 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1566 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001567 // If we don't have SSE2 available, convert to v4f32 so the generated
1568 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001569 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001570 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001571 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001572 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001573 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001574
Dale Johannesendd64c412009-02-04 00:33:20 +00001575 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001576 Flag = Chain.getValue(1);
1577 }
Dan Gohman61a92132008-04-21 23:59:07 +00001578
1579 // The x86-64 ABI for returning structs by value requires that we copy
1580 // the sret argument into %rax for the return. We saved the argument into
1581 // a virtual register in the entry block, so now we copy the value out
1582 // and into %rax.
1583 if (Subtarget->is64Bit() &&
1584 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1585 MachineFunction &MF = DAG.getMachineFunction();
1586 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1587 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001588 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001589 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001590 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001591
Dale Johannesendd64c412009-02-04 00:33:20 +00001592 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001593 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001594
1595 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001596 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001597 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001598
Chris Lattner447ff682008-03-11 03:23:40 +00001599 RetOps[0] = Chain; // Update chain.
1600
1601 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001602 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001603 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001604
1605 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001606 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001607}
1608
Evan Chengbf010eb2012-04-10 01:51:00 +00001609bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001610 if (N->getNumValues() != 1)
1611 return false;
1612 if (!N->hasNUsesOfValue(1, 0))
1613 return false;
1614
Evan Chengbf010eb2012-04-10 01:51:00 +00001615 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001616 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001617 if (Copy->getOpcode() == ISD::CopyToReg) {
1618 // If the copy has a glue operand, we conservatively assume it isn't safe to
1619 // perform a tail call.
1620 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1621 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001622 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001623 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001624 return false;
1625
Evan Cheng1bf891a2010-12-01 22:59:46 +00001626 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001627 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001628 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001629 if (UI->getOpcode() != X86ISD::RET_FLAG)
1630 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001631 HasRet = true;
1632 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001633
Evan Chengbf010eb2012-04-10 01:51:00 +00001634 if (!HasRet)
1635 return false;
1636
1637 Chain = TCChain;
1638 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001639}
1640
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001641EVT
1642X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001643 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001644 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001645 // TODO: Is this also valid on 32-bit?
1646 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001647 ReturnMVT = MVT::i8;
1648 else
1649 ReturnMVT = MVT::i32;
1650
1651 EVT MinVT = getRegisterType(Context, ReturnMVT);
1652 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001653}
1654
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655/// LowerCallResult - Lower the result values of a call into the
1656/// appropriate copies out of appropriate physical registers.
1657///
1658SDValue
1659X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001660 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001661 const SmallVectorImpl<ISD::InputArg> &Ins,
1662 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001663 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001664
Chris Lattnere32bbf62007-02-28 07:09:55 +00001665 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001666 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001667 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001668 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001669 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001670 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001671
Chris Lattner3085e152007-02-25 08:59:22 +00001672 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001673 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001674 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001675 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001676
Torok Edwin3f142c32009-02-01 18:15:56 +00001677 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001679 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001680 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001681 }
1682
Evan Cheng79fb3b42009-02-20 20:43:02 +00001683 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001684
1685 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001686 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001687 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001688 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001689 // instead.
1690 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1691 // If we prefer to use the value in xmm registers, copy it out as f80 and
1692 // use a truncate to move it from fp stack reg to xmm reg.
1693 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001694 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001695 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1696 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001697 Val = Chain.getValue(0);
1698
1699 // Round the f80 to the right size, which also moves it to the appropriate
1700 // xmm register.
1701 if (CopyVT != VA.getValVT())
1702 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1703 // This truncation won't change the value.
1704 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001705 } else {
1706 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1707 CopyVT, InFlag).getValue(1);
1708 Val = Chain.getValue(0);
1709 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001710 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001712 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001713
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001715}
1716
1717
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001718//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001719// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001720//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001721// StdCall calling convention seems to be standard for many Windows' API
1722// routines and around. It differs from C calling convention just a little:
1723// callee should clean up the stack, not caller. Symbols should be also
1724// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001725// For info on fast calling convention see Fast Calling Convention (tail call)
1726// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001727
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001729/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001730enum StructReturnType {
1731 NotStructReturn,
1732 RegStructReturn,
1733 StackStructReturn
1734};
1735static StructReturnType
1736callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001738 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001739
Rafael Espindola1cee7102012-07-25 13:41:10 +00001740 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1741 if (!Flags.isSRet())
1742 return NotStructReturn;
1743 if (Flags.isInReg())
1744 return RegStructReturn;
1745 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001746}
1747
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001748/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001749/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001750static StructReturnType
1751argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001753 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001754
Rafael Espindola1cee7102012-07-25 13:41:10 +00001755 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1756 if (!Flags.isSRet())
1757 return NotStructReturn;
1758 if (Flags.isInReg())
1759 return RegStructReturn;
1760 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001761}
1762
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001763/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1764/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001765/// the specific parameter attribute. The copy will be passed as a byval
1766/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001767static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001768CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001769 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1770 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001771 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001772
Dale Johannesendd64c412009-02-04 00:33:20 +00001773 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001774 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001775 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001776}
1777
Chris Lattner29689432010-03-11 00:22:57 +00001778/// IsTailCallConvention - Return true if the calling convention is one that
1779/// supports tail call optimization.
1780static bool IsTailCallConvention(CallingConv::ID CC) {
1781 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1782}
1783
Evan Cheng485fafc2011-03-21 01:19:09 +00001784bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001785 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001786 return false;
1787
1788 CallSite CS(CI);
1789 CallingConv::ID CalleeCC = CS.getCallingConv();
1790 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1791 return false;
1792
1793 return true;
1794}
1795
Evan Cheng0c439eb2010-01-27 00:07:07 +00001796/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1797/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001798static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1799 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001800 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001801}
1802
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803SDValue
1804X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001805 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 const SmallVectorImpl<ISD::InputArg> &Ins,
1807 DebugLoc dl, SelectionDAG &DAG,
1808 const CCValAssign &VA,
1809 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001811 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001812 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001813 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1814 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001815 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001816 EVT ValVT;
1817
1818 // If value is passed by pointer we have address passed instead of the value
1819 // itself.
1820 if (VA.getLocInfo() == CCValAssign::Indirect)
1821 ValVT = VA.getLocVT();
1822 else
1823 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001824
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001825 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001826 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001827 // In case of tail call optimization mark all arguments mutable. Since they
1828 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001829 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001830 unsigned Bytes = Flags.getByValSize();
1831 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1832 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001833 return DAG.getFrameIndex(FI, getPointerTy());
1834 } else {
1835 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001836 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001837 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1838 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001839 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001840 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001841 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001842}
1843
Dan Gohman475871a2008-07-27 21:46:04 +00001844SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001846 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 bool isVarArg,
1848 const SmallVectorImpl<ISD::InputArg> &Ins,
1849 DebugLoc dl,
1850 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001851 SmallVectorImpl<SDValue> &InVals)
1852 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001853 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 const Function* Fn = MF.getFunction();
1857 if (Fn->hasExternalLinkage() &&
1858 Subtarget->isTargetCygMing() &&
1859 Fn->getName() == "main")
1860 FuncInfo->setForceFramePointer(true);
1861
Evan Cheng1bc78042006-04-26 01:20:17 +00001862 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001864 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001865 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001866
Chris Lattner29689432010-03-11 00:22:57 +00001867 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1868 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001869
Chris Lattner638402b2007-02-28 07:00:42 +00001870 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001871 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001872 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001874
1875 // Allocate shadow area for Win64
1876 if (IsWin64) {
1877 CCInfo.AllocateStack(32, 8);
1878 }
1879
Duncan Sands45907662010-10-31 13:21:44 +00001880 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
Chris Lattnerf39f7712007-02-28 05:46:49 +00001882 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001883 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001884 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1885 CCValAssign &VA = ArgLocs[i];
1886 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1887 // places.
1888 assert(VA.getValNo() != LastVal &&
1889 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001890 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001891 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001892
Chris Lattnerf39f7712007-02-28 05:46:49 +00001893 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001894 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001895 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001897 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001899 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001901 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001903 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001904 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001905 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001906 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001907 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001908 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001909 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001910 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001911 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001912
Devang Patel68e6bee2011-02-21 23:21:26 +00001913 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001915
Chris Lattnerf39f7712007-02-28 05:46:49 +00001916 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1917 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1918 // right size.
1919 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001920 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001921 DAG.getValueType(VA.getValVT()));
1922 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001923 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001924 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001925 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001926 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001928 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 // Handle MMX values passed in XMM regs.
1930 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001931 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1932 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001933 } else
1934 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001935 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001936 } else {
1937 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001939 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001940
1941 // If value is passed via pointer - do a load.
1942 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001943 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001944 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001945
Dan Gohman98ca4f22009-08-05 01:29:28 +00001946 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001947 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001948
Dan Gohman61a92132008-04-21 23:59:07 +00001949 // The x86-64 ABI for returning structs by value requires that we copy
1950 // the sret argument into %rax for the return. Save the argument into
1951 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001952 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001953 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1954 unsigned Reg = FuncInfo->getSRetReturnReg();
1955 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001957 FuncInfo->setSRetReturnReg(Reg);
1958 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001961 }
1962
Chris Lattnerf39f7712007-02-28 05:46:49 +00001963 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001964 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001965 if (FuncIsMadeTailCallSafe(CallConv,
1966 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001968
Evan Cheng1bc78042006-04-26 01:20:17 +00001969 // If the function takes variable number of arguments, make a frame index for
1970 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001972 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1973 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001974 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001975 }
1976 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001977 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1978
1979 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001980 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001981 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001982 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001983 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001984 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1985 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001986 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1988 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1989 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001990 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001991 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992
1993 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001994 // The XMM registers which might contain var arg parameters are shadowed
1995 // in their paired GPR. So we only need to save the GPR to their home
1996 // slots.
1997 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001998 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001999 } else {
2000 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2001 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002002
Chad Rosier30450e82011-12-22 22:35:21 +00002003 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2004 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002005 }
2006 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2007 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002008
Devang Patel578efa92009-06-05 21:57:13 +00002009 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002010 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002011 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002012 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2013 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002014 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002015 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002016 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002017 // Kernel mode asks for SSE to be disabled, so don't push them
2018 // on the stack.
2019 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002020
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002021 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002022 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002023 // Get to the caller-allocated home save location. Add 8 to account
2024 // for the return address.
2025 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002026 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002027 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002028 // Fixup to set vararg frame on shadow area (4 x i64).
2029 if (NumIntRegs < 4)
2030 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002031 } else {
2032 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002033 // registers, then we must store them to their spots on the stack so
2034 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002035 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2036 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2037 FuncInfo->setRegSaveFrameIndex(
2038 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002039 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002040 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002041
Gordon Henriksen86737662008-01-05 16:56:59 +00002042 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002044 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2045 getPointerTy());
2046 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002047 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002048 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2049 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002050 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002051 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002053 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002054 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002055 MachinePointerInfo::getFixedStack(
2056 FuncInfo->getRegSaveFrameIndex(), Offset),
2057 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002058 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002059 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002060 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061
Dan Gohmanface41a2009-08-16 21:24:25 +00002062 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2063 // Now store the XMM (fp + vector) parameter registers.
2064 SmallVector<SDValue, 11> SaveXMMOps;
2065 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002066
Craig Topperc9099502012-04-20 06:31:50 +00002067 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002068 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2069 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002070
Dan Gohman1e93df62010-04-17 14:41:14 +00002071 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2072 FuncInfo->getRegSaveFrameIndex()));
2073 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2074 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002075
Dan Gohmanface41a2009-08-16 21:24:25 +00002076 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002077 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002078 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002079 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2080 SaveXMMOps.push_back(Val);
2081 }
2082 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2083 MVT::Other,
2084 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002086
2087 if (!MemOps.empty())
2088 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2089 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Gordon Henriksen86737662008-01-05 16:56:59 +00002093 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002094 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2095 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002096 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002097 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002098 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002099 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002100 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002101 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002102 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002103 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002104
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002106 // RegSaveFrameIndex is X86-64 only.
2107 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002108 if (CallConv == CallingConv::X86_FastCall ||
2109 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002110 // fastcc functions can't have varargs.
2111 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002112 }
Evan Cheng25caf632006-05-23 21:06:34 +00002113
Rafael Espindola76927d752011-08-30 19:39:58 +00002114 FuncInfo->setArgumentStackSize(StackSize);
2115
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002117}
2118
Dan Gohman475871a2008-07-27 21:46:04 +00002119SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2121 SDValue StackPtr, SDValue Arg,
2122 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002123 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002124 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002125 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002126 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002127 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002128 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002129 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002130
2131 return DAG.getStore(Chain, dl, Arg, PtrOff,
2132 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002133 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002134}
2135
Bill Wendling64e87322009-01-16 19:25:27 +00002136/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002137/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002138SDValue
2139X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002140 SDValue &OutRetAddr, SDValue Chain,
2141 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002142 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002144 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002146
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002147 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002148 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002149 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002151}
2152
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002153/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002154/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002155static SDValue
2156EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002157 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002158 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002159 // Store the return address to the appropriate stack slot.
2160 if (!FPDiff) return Chain;
2161 // Calculate the new stack slot for the return address.
2162 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002163 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002164 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002165 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002166 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002167 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002168 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002169 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002170 return Chain;
2171}
2172
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002174X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002175 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002176 SelectionDAG &DAG = CLI.DAG;
2177 DebugLoc &dl = CLI.DL;
2178 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2179 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2180 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2181 SDValue Chain = CLI.Chain;
2182 SDValue Callee = CLI.Callee;
2183 CallingConv::ID CallConv = CLI.CallConv;
2184 bool &isTailCall = CLI.IsTailCall;
2185 bool isVarArg = CLI.IsVarArg;
2186
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 MachineFunction &MF = DAG.getMachineFunction();
2188 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002189 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002190 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002191 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002192 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193
Nick Lewycky22de16d2012-01-19 00:34:10 +00002194 if (MF.getTarget().Options.DisableTailCalls)
2195 isTailCall = false;
2196
Evan Cheng5f941932010-02-05 02:21:12 +00002197 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002198 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002199 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002200 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002201 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002202 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002203
2204 // Sibcalls are automatically detected tailcalls which do not require
2205 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002206 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002207 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002208
2209 if (isTailCall)
2210 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002211 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002212
Chris Lattner29689432010-03-11 00:22:57 +00002213 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2214 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002215
Chris Lattner638402b2007-02-28 07:00:42 +00002216 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002217 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002218 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002220
2221 // Allocate shadow area for Win64
2222 if (IsWin64) {
2223 CCInfo.AllocateStack(32, 8);
2224 }
2225
Duncan Sands45907662010-10-31 13:21:44 +00002226 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Chris Lattner423c5f42007-02-28 05:31:48 +00002228 // Get a count of how many bytes are to be pushed on the stack.
2229 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002230 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002231 // This is a sibcall. The memory operands are available in caller's
2232 // own caller's stack.
2233 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002234 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2235 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002236 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002237
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002239 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002240 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002241 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2243 FPDiff = NumBytesCallerPushed - NumBytes;
2244
2245 // Set the delta of movement of the returnaddr stackslot.
2246 // But only set if delta is greater than previous delta.
2247 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2248 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2249 }
2250
Evan Chengf22f9b32010-02-06 03:28:46 +00002251 if (!IsSibcall)
2252 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002253
Dan Gohman475871a2008-07-27 21:46:04 +00002254 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002255 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002256 if (isTailCall && FPDiff)
2257 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2258 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002259
Dan Gohman475871a2008-07-27 21:46:04 +00002260 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2261 SmallVector<SDValue, 8> MemOpChains;
2262 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002263
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002264 // Walk the register/memloc assignments, inserting copies/loads. In the case
2265 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002266 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2267 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002268 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002269 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002270 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002271 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002272
Chris Lattner423c5f42007-02-28 05:31:48 +00002273 // Promote the value if needed.
2274 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002275 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002276 case CCValAssign::Full: break;
2277 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002278 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002279 break;
2280 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002281 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002282 break;
2283 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002284 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002285 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002286 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2288 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002289 } else
2290 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2291 break;
2292 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002293 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002294 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002295 case CCValAssign::Indirect: {
2296 // Store the argument.
2297 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002298 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002299 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002300 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002301 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002302 Arg = SpillSlot;
2303 break;
2304 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002306
Chris Lattner423c5f42007-02-28 05:31:48 +00002307 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002308 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2309 if (isVarArg && IsWin64) {
2310 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2311 // shadow reg if callee is a varargs function.
2312 unsigned ShadowReg = 0;
2313 switch (VA.getLocReg()) {
2314 case X86::XMM0: ShadowReg = X86::RCX; break;
2315 case X86::XMM1: ShadowReg = X86::RDX; break;
2316 case X86::XMM2: ShadowReg = X86::R8; break;
2317 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002318 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002319 if (ShadowReg)
2320 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002321 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002322 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002323 assert(VA.isMemLoc());
2324 if (StackPtr.getNode() == 0)
2325 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2326 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2327 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002328 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002330
Evan Cheng32fe1032006-05-25 00:59:30 +00002331 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002333 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002334
Chris Lattner88e1fd52009-07-09 04:24:46 +00002335 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002336 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2337 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002338 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002339 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2340 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002341 } else {
2342 // If we are tail calling and generating PIC/GOT style code load the
2343 // address of the callee into ECX. The value in ecx is used as target of
2344 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2345 // for tail calls on PIC/GOT architectures. Normally we would just put the
2346 // address of GOT into ebx and then call target@PLT. But for tail calls
2347 // ebx would be restored (since ebx is callee saved) before jumping to the
2348 // target@PLT.
2349
2350 // Note: The actual moving to ECX is done further down.
2351 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2352 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2353 !G->getGlobal()->hasProtectedVisibility())
2354 Callee = LowerGlobalAddress(Callee, DAG);
2355 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002356 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002357 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002358 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002359
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002360 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002361 // From AMD64 ABI document:
2362 // For calls that may call functions that use varargs or stdargs
2363 // (prototype-less calls or calls to functions containing ellipsis (...) in
2364 // the declaration) %al is used as hidden argument to specify the number
2365 // of SSE registers used. The contents of %al do not need to match exactly
2366 // the number of registers, but must be an ubound on the number of SSE
2367 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002368
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002370 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002371 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2372 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2373 };
2374 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002375 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002376 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002377
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002378 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2379 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002380 }
2381
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002382 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002383 if (isTailCall) {
2384 // Force all the incoming stack arguments to be loaded from the stack
2385 // before any new outgoing arguments are stored to the stack, because the
2386 // outgoing stack slots may alias the incoming argument stack slots, and
2387 // the alias isn't otherwise explicit. This is slightly more conservative
2388 // than necessary, because it means that each store effectively depends
2389 // on every argument instead of just those arguments it would clobber.
2390 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2391
Dan Gohman475871a2008-07-27 21:46:04 +00002392 SmallVector<SDValue, 8> MemOpChains2;
2393 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002394 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002395 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002396 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2397 CCValAssign &VA = ArgLocs[i];
2398 if (VA.isRegLoc())
2399 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002400 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002401 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002403 // Create frame index.
2404 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002405 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002406 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002407 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002408
Duncan Sands276dcbd2008-03-21 09:14:45 +00002409 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002410 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002411 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002412 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002413 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002414 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002415 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002416
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2418 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002419 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002420 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002421 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002422 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002423 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002424 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002425 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002426 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002427 }
2428 }
2429
2430 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002431 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002432 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002433
2434 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002435 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002436 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002437 }
2438
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002439 // Build a sequence of copy-to-reg nodes chained together with token chain
2440 // and flag operands which copy the outgoing args into registers.
2441 SDValue InFlag;
2442 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2443 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2444 RegsToPass[i].second, InFlag);
2445 InFlag = Chain.getValue(1);
2446 }
2447
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002448 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2449 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2450 // In the 64-bit large code model, we have to make all calls
2451 // through a register, since the call instruction's 32-bit
2452 // pc-relative offset may not be large enough to hold the whole
2453 // address.
2454 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002455 // If the callee is a GlobalAddress node (quite common, every direct call
2456 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2457 // it.
2458
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002459 // We should use extra load for direct calls to dllimported functions in
2460 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002461 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002462 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002463 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002464 bool ExtraLoad = false;
2465 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002466
Chris Lattner48a7d022009-07-09 05:02:21 +00002467 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2468 // external symbols most go through the PLT in PIC mode. If the symbol
2469 // has hidden or protected visibility, or if it is static or local, then
2470 // we don't need to use the PLT - we can directly call it.
2471 if (Subtarget->isTargetELF() &&
2472 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002473 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002474 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002475 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002476 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002477 (!Subtarget->getTargetTriple().isMacOSX() ||
2478 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002479 // PC-relative references to external symbols should go through $stub,
2480 // unless we're building with the leopard linker or later, which
2481 // automatically synthesizes these stubs.
2482 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002483 } else if (Subtarget->isPICStyleRIPRel() &&
2484 isa<Function>(GV) &&
2485 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2486 // If the function is marked as non-lazy, generate an indirect call
2487 // which loads from the GOT directly. This avoids runtime overhead
2488 // at the cost of eager binding (and one extra byte of encoding).
2489 OpFlags = X86II::MO_GOTPCREL;
2490 WrapperKind = X86ISD::WrapperRIP;
2491 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002492 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002493
Devang Patel0d881da2010-07-06 22:08:15 +00002494 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002495 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002496
2497 // Add a wrapper if needed.
2498 if (WrapperKind != ISD::DELETED_NODE)
2499 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2500 // Add extra indirection if needed.
2501 if (ExtraLoad)
2502 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2503 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002504 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002505 }
Bill Wendling056292f2008-09-16 21:48:12 +00002506 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002507 unsigned char OpFlags = 0;
2508
Evan Cheng1bf891a2010-12-01 22:59:46 +00002509 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2510 // external symbols should go through the PLT.
2511 if (Subtarget->isTargetELF() &&
2512 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2513 OpFlags = X86II::MO_PLT;
2514 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002515 (!Subtarget->getTargetTriple().isMacOSX() ||
2516 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002517 // PC-relative references to external symbols should go through $stub,
2518 // unless we're building with the leopard linker or later, which
2519 // automatically synthesizes these stubs.
2520 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002521 }
Eric Christopherfd179292009-08-27 18:07:15 +00002522
Chris Lattner48a7d022009-07-09 05:02:21 +00002523 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2524 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002525 }
2526
Chris Lattnerd96d0722007-02-25 06:40:16 +00002527 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002528 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002529 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002530
Evan Chengf22f9b32010-02-06 03:28:46 +00002531 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002532 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2533 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002536
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002537 Ops.push_back(Chain);
2538 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002539
Dan Gohman98ca4f22009-08-05 01:29:28 +00002540 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002542
Gordon Henriksen86737662008-01-05 16:56:59 +00002543 // Add argument registers to the end of the list so that they are known live
2544 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002545 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2546 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2547 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002548
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002549 // Add a register mask operand representing the call-preserved registers.
2550 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2551 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2552 assert(Mask && "Missing call preserved mask for calling convention");
2553 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002554
Gabor Greifba36cb52008-08-28 21:40:38 +00002555 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002556 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002557
Dan Gohman98ca4f22009-08-05 01:29:28 +00002558 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002559 // We used to do:
2560 //// If this is the first return lowered for this function, add the regs
2561 //// to the liveout set for the function.
2562 // This isn't right, although it's probably harmless on x86; liveouts
2563 // should be computed from returns not tail calls. Consider a void
2564 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 return DAG.getNode(X86ISD::TC_RETURN, dl,
2566 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002567 }
2568
Dale Johannesenace16102009-02-03 19:33:06 +00002569 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002570 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002571
Chris Lattner2d297092006-05-23 18:50:38 +00002572 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002573 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002574 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2575 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002576 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002577 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002578 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002579 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002580 // pops the hidden struct pointer, so we have to push it back.
2581 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002582 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002583 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002584 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002585 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002586
Gordon Henriksenae636f82008-01-03 16:47:34 +00002587 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002588 if (!IsSibcall) {
2589 Chain = DAG.getCALLSEQ_END(Chain,
2590 DAG.getIntPtrConstant(NumBytes, true),
2591 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2592 true),
2593 InFlag);
2594 InFlag = Chain.getValue(1);
2595 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002596
Chris Lattner3085e152007-02-25 08:59:22 +00002597 // Handle result values, copying them out of physregs into vregs that we
2598 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002599 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2600 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002601}
2602
Evan Cheng25ab6902006-09-08 06:48:29 +00002603
2604//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002605// Fast Calling Convention (tail call) implementation
2606//===----------------------------------------------------------------------===//
2607
2608// Like std call, callee cleans arguments, convention except that ECX is
2609// reserved for storing the tail called function address. Only 2 registers are
2610// free for argument passing (inreg). Tail call optimization is performed
2611// provided:
2612// * tailcallopt is enabled
2613// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002614// On X86_64 architecture with GOT-style position independent code only local
2615// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002616// To keep the stack aligned according to platform abi the function
2617// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2618// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002619// If a tail called function callee has more arguments than the caller the
2620// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002621// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002622// original REtADDR, but before the saved framepointer or the spilled registers
2623// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2624// stack layout:
2625// arg1
2626// arg2
2627// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002628// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002629// move area ]
2630// (possible EBP)
2631// ESI
2632// EDI
2633// local1 ..
2634
2635/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2636/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002637unsigned
2638X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2639 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002640 MachineFunction &MF = DAG.getMachineFunction();
2641 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002642 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002643 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002644 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002645 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002646 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002647 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2648 // Number smaller than 12 so just add the difference.
2649 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2650 } else {
2651 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002652 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002653 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002654 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002655 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002656}
2657
Evan Cheng5f941932010-02-05 02:21:12 +00002658/// MatchingStackOffset - Return true if the given stack call argument is
2659/// already available in the same position (relatively) of the caller's
2660/// incoming argument stack.
2661static
2662bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2663 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2664 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002665 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2666 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002667 if (Arg.getOpcode() == ISD::CopyFromReg) {
2668 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002669 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002670 return false;
2671 MachineInstr *Def = MRI->getVRegDef(VR);
2672 if (!Def)
2673 return false;
2674 if (!Flags.isByVal()) {
2675 if (!TII->isLoadFromStackSlot(Def, FI))
2676 return false;
2677 } else {
2678 unsigned Opcode = Def->getOpcode();
2679 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2680 Def->getOperand(1).isFI()) {
2681 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002682 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002683 } else
2684 return false;
2685 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002686 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2687 if (Flags.isByVal())
2688 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002689 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002690 // define @foo(%struct.X* %A) {
2691 // tail call @bar(%struct.X* byval %A)
2692 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002693 return false;
2694 SDValue Ptr = Ld->getBasePtr();
2695 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2696 if (!FINode)
2697 return false;
2698 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002699 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002700 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002701 FI = FINode->getIndex();
2702 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002703 } else
2704 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002705
Evan Cheng4cae1332010-03-05 08:38:04 +00002706 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002707 if (!MFI->isFixedObjectIndex(FI))
2708 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002709 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002710}
2711
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2713/// for tail call optimization. Targets which want to do tail call
2714/// optimization should implement this function.
2715bool
2716X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002717 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002719 bool isCalleeStructRet,
2720 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002721 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002722 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002723 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002724 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002726 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002727 CalleeCC != CallingConv::C)
2728 return false;
2729
Evan Cheng7096ae42010-01-29 06:45:59 +00002730 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002731 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002732 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002733
2734 // If the function return type is x86_fp80 and the callee return type is not,
2735 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2736 // perform a tailcall optimization here.
2737 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2738 return false;
2739
Evan Cheng13617962010-04-30 01:12:32 +00002740 CallingConv::ID CallerCC = CallerF->getCallingConv();
2741 bool CCMatch = CallerCC == CalleeCC;
2742
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002743 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002744 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002745 return true;
2746 return false;
2747 }
2748
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002749 // Look for obvious safe cases to perform tail call optimization that do not
2750 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002751
Evan Cheng2c12cb42010-03-26 16:26:03 +00002752 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2753 // emit a special epilogue.
2754 if (RegInfo->needsStackRealignment(MF))
2755 return false;
2756
Evan Chenga375d472010-03-15 18:54:48 +00002757 // Also avoid sibcall optimization if either caller or callee uses struct
2758 // return semantics.
2759 if (isCalleeStructRet || isCallerStructRet)
2760 return false;
2761
Chad Rosier2416da32011-06-24 21:15:36 +00002762 // An stdcall caller is expected to clean up its arguments; the callee
2763 // isn't going to do that.
2764 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2765 return false;
2766
Chad Rosier871f6642011-05-18 19:59:50 +00002767 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002768 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002769 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002770
2771 // Optimizing for varargs on Win64 is unlikely to be safe without
2772 // additional testing.
2773 if (Subtarget->isTargetWin64())
2774 return false;
2775
Chad Rosier871f6642011-05-18 19:59:50 +00002776 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002777 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002778 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002779
Chad Rosier871f6642011-05-18 19:59:50 +00002780 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2781 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2782 if (!ArgLocs[i].isRegLoc())
2783 return false;
2784 }
2785
Chad Rosier30450e82011-12-22 22:35:21 +00002786 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2787 // stack. Therefore, if it's not used by the call it is not safe to optimize
2788 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002789 bool Unused = false;
2790 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2791 if (!Ins[i].Used) {
2792 Unused = true;
2793 break;
2794 }
2795 }
2796 if (Unused) {
2797 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002798 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002799 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002800 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002801 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002802 CCValAssign &VA = RVLocs[i];
2803 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2804 return false;
2805 }
2806 }
2807
Evan Cheng13617962010-04-30 01:12:32 +00002808 // If the calling conventions do not match, then we'd better make sure the
2809 // results are returned in the same way as what the caller expects.
2810 if (!CCMatch) {
2811 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002812 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002813 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002814 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2815
2816 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002817 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002818 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002819 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2820
2821 if (RVLocs1.size() != RVLocs2.size())
2822 return false;
2823 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2824 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2825 return false;
2826 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2827 return false;
2828 if (RVLocs1[i].isRegLoc()) {
2829 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2830 return false;
2831 } else {
2832 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2833 return false;
2834 }
2835 }
2836 }
2837
Evan Chenga6bff982010-01-30 01:22:00 +00002838 // If the callee takes no arguments then go on to check the results of the
2839 // call.
2840 if (!Outs.empty()) {
2841 // Check if stack adjustment is needed. For now, do not do this if any
2842 // argument is passed on the stack.
2843 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002844 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002845 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002846
2847 // Allocate shadow area for Win64
2848 if (Subtarget->isTargetWin64()) {
2849 CCInfo.AllocateStack(32, 8);
2850 }
2851
Duncan Sands45907662010-10-31 13:21:44 +00002852 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002853 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002854 MachineFunction &MF = DAG.getMachineFunction();
2855 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2856 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002857
2858 // Check if the arguments are already laid out in the right way as
2859 // the caller's fixed stack objects.
2860 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002861 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2862 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002863 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002864 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2865 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002866 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002867 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002868 if (VA.getLocInfo() == CCValAssign::Indirect)
2869 return false;
2870 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002871 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2872 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002873 return false;
2874 }
2875 }
2876 }
Evan Cheng9c044672010-05-29 01:35:22 +00002877
2878 // If the tailcall address may be in a register, then make sure it's
2879 // possible to register allocate for it. In 32-bit, the call address can
2880 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002881 // callee-saved registers are restored. These happen to be the same
2882 // registers used to pass 'inreg' arguments so watch out for those.
2883 if (!Subtarget->is64Bit() &&
2884 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002885 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002886 unsigned NumInRegs = 0;
2887 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2888 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002889 if (!VA.isRegLoc())
2890 continue;
2891 unsigned Reg = VA.getLocReg();
2892 switch (Reg) {
2893 default: break;
2894 case X86::EAX: case X86::EDX: case X86::ECX:
2895 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002896 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002897 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002898 }
2899 }
2900 }
Evan Chenga6bff982010-01-30 01:22:00 +00002901 }
Evan Chengb1712452010-01-27 06:25:16 +00002902
Evan Cheng86809cc2010-02-03 03:28:02 +00002903 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002904}
2905
Dan Gohman3df24e62008-09-03 23:12:08 +00002906FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002907X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2908 const TargetLibraryInfo *libInfo) const {
2909 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002910}
2911
2912
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002913//===----------------------------------------------------------------------===//
2914// Other Lowering Hooks
2915//===----------------------------------------------------------------------===//
2916
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002917static bool MayFoldLoad(SDValue Op) {
2918 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2919}
2920
2921static bool MayFoldIntoStore(SDValue Op) {
2922 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2923}
2924
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002925static bool isTargetShuffle(unsigned Opcode) {
2926 switch(Opcode) {
2927 default: return false;
2928 case X86ISD::PSHUFD:
2929 case X86ISD::PSHUFHW:
2930 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002931 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002932 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002933 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002934 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002935 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002936 case X86ISD::MOVLPS:
2937 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002938 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002939 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002940 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002941 case X86ISD::MOVSS:
2942 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002943 case X86ISD::UNPCKL:
2944 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002945 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002946 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002947 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002948 return true;
2949 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002950}
2951
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002952static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002953 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002954 switch(Opc) {
2955 default: llvm_unreachable("Unknown x86 shuffle node");
2956 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002957 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002958 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002959 return DAG.getNode(Opc, dl, VT, V1);
2960 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002961}
2962
2963static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002964 SDValue V1, unsigned TargetMask,
2965 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002966 switch(Opc) {
2967 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002968 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002969 case X86ISD::PSHUFHW:
2970 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002971 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002972 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002973 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2974 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002975}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002976
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002977static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002978 SDValue V1, SDValue V2, unsigned TargetMask,
2979 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002980 switch(Opc) {
2981 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002982 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002983 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002984 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002985 return DAG.getNode(Opc, dl, VT, V1, V2,
2986 DAG.getConstant(TargetMask, MVT::i8));
2987 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002988}
2989
2990static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2991 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2992 switch(Opc) {
2993 default: llvm_unreachable("Unknown x86 shuffle node");
2994 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002995 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002996 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002997 case X86ISD::MOVLPS:
2998 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002999 case X86ISD::MOVSS:
3000 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003001 case X86ISD::UNPCKL:
3002 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003003 return DAG.getNode(Opc, dl, VT, V1, V2);
3004 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003005}
3006
Dan Gohmand858e902010-04-17 15:26:15 +00003007SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003008 MachineFunction &MF = DAG.getMachineFunction();
3009 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3010 int ReturnAddrIndex = FuncInfo->getRAIndex();
3011
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003012 if (ReturnAddrIndex == 0) {
3013 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00003014 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003015 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003016 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003017 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003018 }
3019
Evan Cheng25ab6902006-09-08 06:48:29 +00003020 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003021}
3022
3023
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003024bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3025 bool hasSymbolicDisplacement) {
3026 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003027 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003028 return false;
3029
3030 // If we don't have a symbolic displacement - we don't have any extra
3031 // restrictions.
3032 if (!hasSymbolicDisplacement)
3033 return true;
3034
3035 // FIXME: Some tweaks might be needed for medium code model.
3036 if (M != CodeModel::Small && M != CodeModel::Kernel)
3037 return false;
3038
3039 // For small code model we assume that latest object is 16MB before end of 31
3040 // bits boundary. We may also accept pretty large negative constants knowing
3041 // that all objects are in the positive half of address space.
3042 if (M == CodeModel::Small && Offset < 16*1024*1024)
3043 return true;
3044
3045 // For kernel code model we know that all object resist in the negative half
3046 // of 32bits address space. We may not accept negative offsets, since they may
3047 // be just off and we may accept pretty large positive ones.
3048 if (M == CodeModel::Kernel && Offset > 0)
3049 return true;
3050
3051 return false;
3052}
3053
Evan Chengef41ff62011-06-23 17:54:54 +00003054/// isCalleePop - Determines whether the callee is required to pop its
3055/// own arguments. Callee pop is necessary to support tail calls.
3056bool X86::isCalleePop(CallingConv::ID CallingConv,
3057 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3058 if (IsVarArg)
3059 return false;
3060
3061 switch (CallingConv) {
3062 default:
3063 return false;
3064 case CallingConv::X86_StdCall:
3065 return !is64Bit;
3066 case CallingConv::X86_FastCall:
3067 return !is64Bit;
3068 case CallingConv::X86_ThisCall:
3069 return !is64Bit;
3070 case CallingConv::Fast:
3071 return TailCallOpt;
3072 case CallingConv::GHC:
3073 return TailCallOpt;
3074 }
3075}
3076
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003077/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3078/// specific condition code, returning the condition code and the LHS/RHS of the
3079/// comparison to make.
3080static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3081 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003082 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003083 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3084 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3085 // X > -1 -> X == 0, jump !sign.
3086 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003087 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003088 }
3089 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003090 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003091 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003092 }
3093 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003094 // X < 1 -> X <= 0
3095 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003096 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003097 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003098 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003099
Evan Chengd9558e02006-01-06 00:43:03 +00003100 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003101 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003102 case ISD::SETEQ: return X86::COND_E;
3103 case ISD::SETGT: return X86::COND_G;
3104 case ISD::SETGE: return X86::COND_GE;
3105 case ISD::SETLT: return X86::COND_L;
3106 case ISD::SETLE: return X86::COND_LE;
3107 case ISD::SETNE: return X86::COND_NE;
3108 case ISD::SETULT: return X86::COND_B;
3109 case ISD::SETUGT: return X86::COND_A;
3110 case ISD::SETULE: return X86::COND_BE;
3111 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003112 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003114
Chris Lattner4c78e022008-12-23 23:42:27 +00003115 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003116
Chris Lattner4c78e022008-12-23 23:42:27 +00003117 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003118 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3119 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003120 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3121 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003122 }
3123
Chris Lattner4c78e022008-12-23 23:42:27 +00003124 switch (SetCCOpcode) {
3125 default: break;
3126 case ISD::SETOLT:
3127 case ISD::SETOLE:
3128 case ISD::SETUGT:
3129 case ISD::SETUGE:
3130 std::swap(LHS, RHS);
3131 break;
3132 }
3133
3134 // On a floating point condition, the flags are set as follows:
3135 // ZF PF CF op
3136 // 0 | 0 | 0 | X > Y
3137 // 0 | 0 | 1 | X < Y
3138 // 1 | 0 | 0 | X == Y
3139 // 1 | 1 | 1 | unordered
3140 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003141 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003142 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003143 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003144 case ISD::SETOLT: // flipped
3145 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003146 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003147 case ISD::SETOLE: // flipped
3148 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003149 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003150 case ISD::SETUGT: // flipped
3151 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003152 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003153 case ISD::SETUGE: // flipped
3154 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003155 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003156 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003157 case ISD::SETNE: return X86::COND_NE;
3158 case ISD::SETUO: return X86::COND_P;
3159 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003160 case ISD::SETOEQ:
3161 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003162 }
Evan Chengd9558e02006-01-06 00:43:03 +00003163}
3164
Evan Cheng4a460802006-01-11 00:33:36 +00003165/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3166/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003167/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003168static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003169 switch (X86CC) {
3170 default:
3171 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003172 case X86::COND_B:
3173 case X86::COND_BE:
3174 case X86::COND_E:
3175 case X86::COND_P:
3176 case X86::COND_A:
3177 case X86::COND_AE:
3178 case X86::COND_NE:
3179 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003180 return true;
3181 }
3182}
3183
Evan Chengeb2f9692009-10-27 19:56:55 +00003184/// isFPImmLegal - Returns true if the target can instruction select the
3185/// specified FP immediate natively. If false, the legalizer will
3186/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003187bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003188 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3189 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3190 return true;
3191 }
3192 return false;
3193}
3194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3196/// the specified range (L, H].
3197static bool isUndefOrInRange(int Val, int Low, int Hi) {
3198 return (Val < 0) || (Val >= Low && Val < Hi);
3199}
3200
3201/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3202/// specified value.
3203static bool isUndefOrEqual(int Val, int CmpVal) {
3204 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003207}
3208
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003209/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003210/// from position Pos and ending in Pos+Size, falls within the specified
3211/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003212static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003213 unsigned Pos, unsigned Size, int Low) {
3214 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003215 if (!isUndefOrEqual(Mask[i], Low))
3216 return false;
3217 return true;
3218}
3219
Nate Begeman9008ca62009-04-27 18:41:29 +00003220/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3221/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3222/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003223static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003224 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 return (Mask[0] < 2 && Mask[1] < 2);
3228 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003229}
3230
Nate Begeman9008ca62009-04-27 18:41:29 +00003231/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3232/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003233static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3234 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003235 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003236
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003238 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3239 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003240
Evan Cheng506d3df2006-03-29 23:07:14 +00003241 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003242 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003243 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003244 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003245
Craig Toppera9a568a2012-05-02 08:03:44 +00003246 if (VT == MVT::v16i16) {
3247 // Lower quadword copied in order or undef.
3248 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3249 return false;
3250
3251 // Upper quadword shuffled.
3252 for (unsigned i = 12; i != 16; ++i)
3253 if (!isUndefOrInRange(Mask[i], 12, 16))
3254 return false;
3255 }
3256
Evan Cheng506d3df2006-03-29 23:07:14 +00003257 return true;
3258}
3259
Nate Begeman9008ca62009-04-27 18:41:29 +00003260/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3261/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003262static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3263 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003264 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003265
Rafael Espindola15684b22009-04-24 12:40:33 +00003266 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003267 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3268 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003269
Rafael Espindola15684b22009-04-24 12:40:33 +00003270 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003271 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003272 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003273 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003274
Craig Toppera9a568a2012-05-02 08:03:44 +00003275 if (VT == MVT::v16i16) {
3276 // Upper quadword copied in order.
3277 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3278 return false;
3279
3280 // Lower quadword shuffled.
3281 for (unsigned i = 8; i != 12; ++i)
3282 if (!isUndefOrInRange(Mask[i], 8, 12))
3283 return false;
3284 }
3285
Rafael Espindola15684b22009-04-24 12:40:33 +00003286 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003287}
3288
Nate Begemana09008b2009-10-19 02:17:23 +00003289/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3290/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003291static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3292 const X86Subtarget *Subtarget) {
3293 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3294 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003295 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003296
Craig Topper0e2037b2012-01-20 05:53:00 +00003297 unsigned NumElts = VT.getVectorNumElements();
3298 unsigned NumLanes = VT.getSizeInBits()/128;
3299 unsigned NumLaneElts = NumElts/NumLanes;
3300
3301 // Do not handle 64-bit element shuffles with palignr.
3302 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003303 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003304
Craig Topper0e2037b2012-01-20 05:53:00 +00003305 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3306 unsigned i;
3307 for (i = 0; i != NumLaneElts; ++i) {
3308 if (Mask[i+l] >= 0)
3309 break;
3310 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003311
Craig Topper0e2037b2012-01-20 05:53:00 +00003312 // Lane is all undef, go to next lane
3313 if (i == NumLaneElts)
3314 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003315
Craig Topper0e2037b2012-01-20 05:53:00 +00003316 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003317
Craig Topper0e2037b2012-01-20 05:53:00 +00003318 // Make sure its in this lane in one of the sources
3319 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3320 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003321 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003322
3323 // If not lane 0, then we must match lane 0
3324 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3325 return false;
3326
3327 // Correct second source to be contiguous with first source
3328 if (Start >= (int)NumElts)
3329 Start -= NumElts - NumLaneElts;
3330
3331 // Make sure we're shifting in the right direction.
3332 if (Start <= (int)(i+l))
3333 return false;
3334
3335 Start -= i;
3336
3337 // Check the rest of the elements to see if they are consecutive.
3338 for (++i; i != NumLaneElts; ++i) {
3339 int Idx = Mask[i+l];
3340
3341 // Make sure its in this lane
3342 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3343 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3344 return false;
3345
3346 // If not lane 0, then we must match lane 0
3347 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3348 return false;
3349
3350 if (Idx >= (int)NumElts)
3351 Idx -= NumElts - NumLaneElts;
3352
3353 if (!isUndefOrEqual(Idx, Start+i))
3354 return false;
3355
3356 }
Nate Begemana09008b2009-10-19 02:17:23 +00003357 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003358
Nate Begemana09008b2009-10-19 02:17:23 +00003359 return true;
3360}
3361
Craig Topper1a7700a2012-01-19 08:19:12 +00003362/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3363/// the two vector operands have swapped position.
3364static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3365 unsigned NumElems) {
3366 for (unsigned i = 0; i != NumElems; ++i) {
3367 int idx = Mask[i];
3368 if (idx < 0)
3369 continue;
3370 else if (idx < (int)NumElems)
3371 Mask[i] = idx + NumElems;
3372 else
3373 Mask[i] = idx - NumElems;
3374 }
3375}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003376
Craig Topper1a7700a2012-01-19 08:19:12 +00003377/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3378/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3379/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3380/// reverse of what x86 shuffles want.
3381static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3382 bool Commuted = false) {
3383 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003384 return false;
3385
Craig Topper1a7700a2012-01-19 08:19:12 +00003386 unsigned NumElems = VT.getVectorNumElements();
3387 unsigned NumLanes = VT.getSizeInBits()/128;
3388 unsigned NumLaneElems = NumElems/NumLanes;
3389
3390 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003391 return false;
3392
3393 // VSHUFPSY divides the resulting vector into 4 chunks.
3394 // The sources are also splitted into 4 chunks, and each destination
3395 // chunk must come from a different source chunk.
3396 //
3397 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3398 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3399 //
3400 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3401 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3402 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003403 // VSHUFPDY divides the resulting vector into 4 chunks.
3404 // The sources are also splitted into 4 chunks, and each destination
3405 // chunk must come from a different source chunk.
3406 //
3407 // SRC1 => X3 X2 X1 X0
3408 // SRC2 => Y3 Y2 Y1 Y0
3409 //
3410 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3411 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003412 unsigned HalfLaneElems = NumLaneElems/2;
3413 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3414 for (unsigned i = 0; i != NumLaneElems; ++i) {
3415 int Idx = Mask[i+l];
3416 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3417 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3418 return false;
3419 // For VSHUFPSY, the mask of the second half must be the same as the
3420 // first but with the appropriate offsets. This works in the same way as
3421 // VPERMILPS works with masks.
3422 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3423 continue;
3424 if (!isUndefOrEqual(Idx, Mask[i]+l))
3425 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003426 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003427 }
3428
3429 return true;
3430}
3431
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003432/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3433/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003434static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003435 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003436 return false;
3437
Craig Topper7a9a28b2012-08-12 02:23:29 +00003438 unsigned NumElems = VT.getVectorNumElements();
3439
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003440 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003441 return false;
3442
Evan Cheng2064a2b2006-03-28 06:50:32 +00003443 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003444 return isUndefOrEqual(Mask[0], 6) &&
3445 isUndefOrEqual(Mask[1], 7) &&
3446 isUndefOrEqual(Mask[2], 2) &&
3447 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003448}
3449
Nate Begeman0b10b912009-11-07 23:17:15 +00003450/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3451/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3452/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003453static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003454 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003455 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003456
Craig Topper7a9a28b2012-08-12 02:23:29 +00003457 unsigned NumElems = VT.getVectorNumElements();
3458
Nate Begeman0b10b912009-11-07 23:17:15 +00003459 if (NumElems != 4)
3460 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003461
Craig Topperdd637ae2012-02-19 05:41:45 +00003462 return isUndefOrEqual(Mask[0], 2) &&
3463 isUndefOrEqual(Mask[1], 3) &&
3464 isUndefOrEqual(Mask[2], 2) &&
3465 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003466}
3467
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3469/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003470static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003471 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003472 return false;
3473
Craig Topperdd637ae2012-02-19 05:41:45 +00003474 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003475
Evan Cheng5ced1d82006-04-06 23:23:56 +00003476 if (NumElems != 2 && NumElems != 4)
3477 return false;
3478
Chad Rosier238ae312012-04-30 17:47:15 +00003479 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003480 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003481 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003482
Chad Rosier238ae312012-04-30 17:47:15 +00003483 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003484 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003485 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003486
3487 return true;
3488}
3489
Nate Begeman0b10b912009-11-07 23:17:15 +00003490/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3491/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003492static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003493 if (!VT.is128BitVector())
3494 return false;
3495
Craig Topperdd637ae2012-02-19 05:41:45 +00003496 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003497
Craig Topper7a9a28b2012-08-12 02:23:29 +00003498 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003499 return false;
3500
Chad Rosier238ae312012-04-30 17:47:15 +00003501 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003502 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003503 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003504
Chad Rosier238ae312012-04-30 17:47:15 +00003505 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3506 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003507 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003508
3509 return true;
3510}
3511
Elena Demikhovsky15963732012-06-26 08:04:10 +00003512//
3513// Some special combinations that can be optimized.
3514//
3515static
3516SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3517 SelectionDAG &DAG) {
3518 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003519 DebugLoc dl = SVOp->getDebugLoc();
3520
3521 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3522 return SDValue();
3523
3524 ArrayRef<int> Mask = SVOp->getMask();
3525
3526 // These are the special masks that may be optimized.
3527 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3528 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3529 bool MatchEvenMask = true;
3530 bool MatchOddMask = true;
3531 for (int i=0; i<8; ++i) {
3532 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3533 MatchEvenMask = false;
3534 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3535 MatchOddMask = false;
3536 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003537
Elena Demikhovsky32510202012-09-04 12:49:02 +00003538 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003539 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003540
Elena Demikhovsky15963732012-06-26 08:04:10 +00003541 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3542
Elena Demikhovsky32510202012-09-04 12:49:02 +00003543 SDValue Op0 = SVOp->getOperand(0);
3544 SDValue Op1 = SVOp->getOperand(1);
3545
3546 if (MatchEvenMask) {
3547 // Shift the second operand right to 32 bits.
3548 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3549 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3550 } else {
3551 // Shift the first operand left to 32 bits.
3552 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3553 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3554 }
3555 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3556 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003557}
3558
Evan Cheng0038e592006-03-28 00:39:58 +00003559/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3560/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003561static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003562 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003563 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003564
3565 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3566 "Unsupported vector type for unpckh");
3567
Craig Topper6347e862011-11-21 06:57:39 +00003568 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003569 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003570 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003571
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003572 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3573 // independently on 128-bit lanes.
3574 unsigned NumLanes = VT.getSizeInBits()/128;
3575 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003576
Craig Topper94438ba2011-12-16 08:06:31 +00003577 for (unsigned l = 0; l != NumLanes; ++l) {
3578 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3579 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003580 i += 2, ++j) {
3581 int BitI = Mask[i];
3582 int BitI1 = Mask[i+1];
3583 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003584 return false;
David Greenea20244d2011-03-02 17:23:43 +00003585 if (V2IsSplat) {
3586 if (!isUndefOrEqual(BitI1, NumElts))
3587 return false;
3588 } else {
3589 if (!isUndefOrEqual(BitI1, j + NumElts))
3590 return false;
3591 }
Evan Cheng39623da2006-04-20 08:58:49 +00003592 }
Evan Cheng0038e592006-03-28 00:39:58 +00003593 }
David Greenea20244d2011-03-02 17:23:43 +00003594
Evan Cheng0038e592006-03-28 00:39:58 +00003595 return true;
3596}
3597
Evan Cheng4fcb9222006-03-28 02:43:26 +00003598/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3599/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003600static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003601 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003602 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603
3604 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3605 "Unsupported vector type for unpckh");
3606
Craig Topper6347e862011-11-21 06:57:39 +00003607 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003608 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003609 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003610
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003611 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3612 // independently on 128-bit lanes.
3613 unsigned NumLanes = VT.getSizeInBits()/128;
3614 unsigned NumLaneElts = NumElts/NumLanes;
3615
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003616 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003617 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3618 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003619 int BitI = Mask[i];
3620 int BitI1 = Mask[i+1];
3621 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003622 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003623 if (V2IsSplat) {
3624 if (isUndefOrEqual(BitI1, NumElts))
3625 return false;
3626 } else {
3627 if (!isUndefOrEqual(BitI1, j+NumElts))
3628 return false;
3629 }
Evan Cheng39623da2006-04-20 08:58:49 +00003630 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003631 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003632 return true;
3633}
3634
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003635/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3636/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3637/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003638static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003639 bool HasAVX2) {
3640 unsigned NumElts = VT.getVectorNumElements();
3641
3642 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3643 "Unsupported vector type for unpckh");
3644
3645 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3646 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003647 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003648
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003649 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3650 // FIXME: Need a better way to get rid of this, there's no latency difference
3651 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3652 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003653 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003654 return false;
3655
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003656 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3657 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003658 unsigned NumLanes = VT.getSizeInBits()/128;
3659 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003660
Craig Topper94438ba2011-12-16 08:06:31 +00003661 for (unsigned l = 0; l != NumLanes; ++l) {
3662 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3663 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003664 i += 2, ++j) {
3665 int BitI = Mask[i];
3666 int BitI1 = Mask[i+1];
3667
3668 if (!isUndefOrEqual(BitI, j))
3669 return false;
3670 if (!isUndefOrEqual(BitI1, j))
3671 return false;
3672 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003673 }
David Greenea20244d2011-03-02 17:23:43 +00003674
Rafael Espindola15684b22009-04-24 12:40:33 +00003675 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003676}
3677
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003678/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3679/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3680/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003681static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003682 unsigned NumElts = VT.getVectorNumElements();
3683
3684 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3685 "Unsupported vector type for unpckh");
3686
3687 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3688 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003689 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003690
Craig Topper94438ba2011-12-16 08:06:31 +00003691 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3692 // independently on 128-bit lanes.
3693 unsigned NumLanes = VT.getSizeInBits()/128;
3694 unsigned NumLaneElts = NumElts/NumLanes;
3695
3696 for (unsigned l = 0; l != NumLanes; ++l) {
3697 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3698 i != (l+1)*NumLaneElts; i += 2, ++j) {
3699 int BitI = Mask[i];
3700 int BitI1 = Mask[i+1];
3701 if (!isUndefOrEqual(BitI, j))
3702 return false;
3703 if (!isUndefOrEqual(BitI1, j))
3704 return false;
3705 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003706 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003707 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003708}
3709
Evan Cheng017dcc62006-04-21 01:05:10 +00003710/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3711/// specifies a shuffle of elements that is suitable for input to MOVSS,
3712/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003713static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003714 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003715 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003716 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003717 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003718
Craig Topperc612d792012-01-02 09:17:37 +00003719 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003720
Nate Begeman9008ca62009-04-27 18:41:29 +00003721 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003722 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003723
Craig Topperc612d792012-01-02 09:17:37 +00003724 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003726 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003727
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003728 return true;
3729}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003730
Craig Topper70b883b2011-11-28 10:14:51 +00003731/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003732/// as permutations between 128-bit chunks or halves. As an example: this
3733/// shuffle bellow:
3734/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3735/// The first half comes from the second half of V1 and the second half from the
3736/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003737static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003738 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003739 return false;
3740
3741 // The shuffle result is divided into half A and half B. In total the two
3742 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3743 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003744 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003745 bool MatchA = false, MatchB = false;
3746
3747 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003748 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003749 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3750 MatchA = true;
3751 break;
3752 }
3753 }
3754
3755 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003756 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003757 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3758 MatchB = true;
3759 break;
3760 }
3761 }
3762
3763 return MatchA && MatchB;
3764}
3765
Craig Topper70b883b2011-11-28 10:14:51 +00003766/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3767/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003768static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003769 EVT VT = SVOp->getValueType(0);
3770
Craig Topperc612d792012-01-02 09:17:37 +00003771 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003772
Craig Topperc612d792012-01-02 09:17:37 +00003773 unsigned FstHalf = 0, SndHalf = 0;
3774 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003775 if (SVOp->getMaskElt(i) > 0) {
3776 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3777 break;
3778 }
3779 }
Craig Topperc612d792012-01-02 09:17:37 +00003780 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003781 if (SVOp->getMaskElt(i) > 0) {
3782 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3783 break;
3784 }
3785 }
3786
3787 return (FstHalf | (SndHalf << 4));
3788}
3789
Craig Topper70b883b2011-11-28 10:14:51 +00003790/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003791/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3792/// Note that VPERMIL mask matching is different depending whether theunderlying
3793/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3794/// to the same elements of the low, but to the higher half of the source.
3795/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003796/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003797static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003798 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003799 return false;
3800
Craig Topperc612d792012-01-02 09:17:37 +00003801 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003802 // Only match 256-bit with 32/64-bit types
3803 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003804 return false;
3805
Craig Topperc612d792012-01-02 09:17:37 +00003806 unsigned NumLanes = VT.getSizeInBits()/128;
3807 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003808 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003809 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003810 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003811 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003812 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003813 continue;
3814 // VPERMILPS handling
3815 if (Mask[i] < 0)
3816 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003817 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003818 return false;
3819 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003820 }
3821
3822 return true;
3823}
3824
Craig Topper5aaffa82012-02-19 02:53:47 +00003825/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003826/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003827/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003828static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003829 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003830 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003831 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003832
3833 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003834 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003835 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003836
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003838 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003839
Craig Topperc612d792012-01-02 09:17:37 +00003840 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003841 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3842 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3843 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003844 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003845
Evan Cheng39623da2006-04-20 08:58:49 +00003846 return true;
3847}
3848
Evan Chengd9539472006-04-14 21:59:03 +00003849/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3850/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003851/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003852static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003853 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003854 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003855 return false;
3856
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003857 unsigned NumElems = VT.getVectorNumElements();
3858
3859 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3860 (VT.getSizeInBits() == 256 && NumElems != 8))
3861 return false;
3862
3863 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003864 for (unsigned i = 0; i != NumElems; i += 2)
3865 if (!isUndefOrEqual(Mask[i], i+1) ||
3866 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003868
3869 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003870}
3871
3872/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3873/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003874/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003875static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003876 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003877 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003878 return false;
3879
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003880 unsigned NumElems = VT.getVectorNumElements();
3881
3882 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3883 (VT.getSizeInBits() == 256 && NumElems != 8))
3884 return false;
3885
3886 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003887 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003888 if (!isUndefOrEqual(Mask[i], i) ||
3889 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003891
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003892 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003893}
3894
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003895/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3896/// specifies a shuffle of elements that is suitable for input to 256-bit
3897/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003898static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003899 if (!HasAVX || !VT.is256BitVector())
3900 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003901
Craig Topper7a9a28b2012-08-12 02:23:29 +00003902 unsigned NumElts = VT.getVectorNumElements();
3903 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003904 return false;
3905
Craig Topperc612d792012-01-02 09:17:37 +00003906 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003907 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003908 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003909 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003910 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003911 return false;
3912 return true;
3913}
3914
Evan Cheng0b457f02008-09-25 20:50:48 +00003915/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003916/// specifies a shuffle of elements that is suitable for input to 128-bit
3917/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003918static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003919 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003920 return false;
3921
Craig Topperc612d792012-01-02 09:17:37 +00003922 unsigned e = VT.getVectorNumElements() / 2;
3923 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003924 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003925 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003926 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003927 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003928 return false;
3929 return true;
3930}
3931
David Greenec38a03e2011-02-03 15:50:00 +00003932/// isVEXTRACTF128Index - Return true if the specified
3933/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3934/// suitable for input to VEXTRACTF128.
3935bool X86::isVEXTRACTF128Index(SDNode *N) {
3936 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3937 return false;
3938
3939 // The index should be aligned on a 128-bit boundary.
3940 uint64_t Index =
3941 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3942
3943 unsigned VL = N->getValueType(0).getVectorNumElements();
3944 unsigned VBits = N->getValueType(0).getSizeInBits();
3945 unsigned ElSize = VBits / VL;
3946 bool Result = (Index * ElSize) % 128 == 0;
3947
3948 return Result;
3949}
3950
David Greeneccacdc12011-02-04 16:08:29 +00003951/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3952/// operand specifies a subvector insert that is suitable for input to
3953/// VINSERTF128.
3954bool X86::isVINSERTF128Index(SDNode *N) {
3955 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3956 return false;
3957
3958 // The index should be aligned on a 128-bit boundary.
3959 uint64_t Index =
3960 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3961
3962 unsigned VL = N->getValueType(0).getVectorNumElements();
3963 unsigned VBits = N->getValueType(0).getSizeInBits();
3964 unsigned ElSize = VBits / VL;
3965 bool Result = (Index * ElSize) % 128 == 0;
3966
3967 return Result;
3968}
3969
Evan Cheng63d33002006-03-22 08:01:21 +00003970/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003971/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003972/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003973static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003974 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003975
Craig Topper1a7700a2012-01-19 08:19:12 +00003976 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3977 "Unsupported vector type for PSHUF/SHUFP");
3978
3979 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3980 // independently on 128-bit lanes.
3981 unsigned NumElts = VT.getVectorNumElements();
3982 unsigned NumLanes = VT.getSizeInBits()/128;
3983 unsigned NumLaneElts = NumElts/NumLanes;
3984
3985 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3986 "Only supports 2 or 4 elements per lane");
3987
3988 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003989 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003990 for (unsigned i = 0; i != NumElts; ++i) {
3991 int Elt = N->getMaskElt(i);
3992 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003993 Elt &= NumLaneElts - 1;
3994 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003995 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003996 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003997
Evan Cheng63d33002006-03-22 08:01:21 +00003998 return Mask;
3999}
4000
Evan Cheng506d3df2006-03-29 23:07:14 +00004001/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004002/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004003static unsigned getShufflePSHUFHWImmediate(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 last 4.
4014 for (unsigned i = 0; i < 4; ++i) {
4015 int Elt = N->getMaskElt(l+i+4);
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
4025/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004026/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004027static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004028 EVT VT = N->getValueType(0);
4029
4030 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4031 "Unsupported vector type for PSHUFHW");
4032
4033 unsigned NumElts = VT.getVectorNumElements();
4034
Evan Cheng506d3df2006-03-29 23:07:14 +00004035 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004036 for (unsigned l = 0; l != NumElts; l += 8) {
4037 // 8 nodes per lane, but we only care about the first 4.
4038 for (unsigned i = 0; i < 4; ++i) {
4039 int Elt = N->getMaskElt(l+i);
4040 if (Elt < 0) continue;
4041 Elt &= 0x3; // only 2-bits
4042 Mask |= Elt << (i * 2);
4043 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004044 }
Craig Topper6b28d352012-05-03 07:12:59 +00004045
Evan Cheng506d3df2006-03-29 23:07:14 +00004046 return Mask;
4047}
4048
Nate Begemana09008b2009-10-19 02:17:23 +00004049/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4050/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004051static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4052 EVT VT = SVOp->getValueType(0);
4053 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004054
Craig Topper0e2037b2012-01-20 05:53:00 +00004055 unsigned NumElts = VT.getVectorNumElements();
4056 unsigned NumLanes = VT.getSizeInBits()/128;
4057 unsigned NumLaneElts = NumElts/NumLanes;
4058
4059 int Val = 0;
4060 unsigned i;
4061 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004062 Val = SVOp->getMaskElt(i);
4063 if (Val >= 0)
4064 break;
4065 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004066 if (Val >= (int)NumElts)
4067 Val -= NumElts - NumLaneElts;
4068
Eli Friedman63f8dde2011-07-25 21:36:45 +00004069 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004070 return (Val - i) * EltSize;
4071}
4072
David Greenec38a03e2011-02-03 15:50:00 +00004073/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4074/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4075/// instructions.
4076unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4077 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4078 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4079
4080 uint64_t Index =
4081 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4082
4083 EVT VecVT = N->getOperand(0).getValueType();
4084 EVT ElVT = VecVT.getVectorElementType();
4085
4086 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004087 return Index / NumElemsPerChunk;
4088}
4089
David Greeneccacdc12011-02-04 16:08:29 +00004090/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4091/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4092/// instructions.
4093unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4094 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4095 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4096
4097 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004098 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004099
4100 EVT VecVT = N->getValueType(0);
4101 EVT ElVT = VecVT.getVectorElementType();
4102
4103 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004104 return Index / NumElemsPerChunk;
4105}
4106
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004107/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4108/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4109/// Handles 256-bit.
4110static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4111 EVT VT = N->getValueType(0);
4112
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004113 unsigned NumElts = VT.getVectorNumElements();
4114
Craig Topper095c5282012-04-15 23:48:57 +00004115 assert((VT.is256BitVector() && NumElts == 4) &&
4116 "Unsupported vector type for VPERMQ/VPERMPD");
4117
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004118 unsigned Mask = 0;
4119 for (unsigned i = 0; i != NumElts; ++i) {
4120 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004121 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004122 continue;
4123 Mask |= Elt << (i*2);
4124 }
4125
4126 return Mask;
4127}
Evan Cheng37b73872009-07-30 08:33:02 +00004128/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4129/// constant +0.0.
4130bool X86::isZeroNode(SDValue Elt) {
4131 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004132 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004133 (isa<ConstantFPSDNode>(Elt) &&
4134 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4135}
4136
Nate Begeman9008ca62009-04-27 18:41:29 +00004137/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4138/// their permute mask.
4139static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4140 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004141 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004142 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004144
Nate Begeman5a5ca152009-04-29 05:20:52 +00004145 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004146 int Idx = SVOp->getMaskElt(i);
4147 if (Idx >= 0) {
4148 if (Idx < (int)NumElems)
4149 Idx += NumElems;
4150 else
4151 Idx -= NumElems;
4152 }
4153 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004154 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004155 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4156 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004157}
4158
Evan Cheng533a0aa2006-04-19 20:35:22 +00004159/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4160/// match movhlps. The lower half elements should come from upper half of
4161/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004162/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004163static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004164 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004165 return false;
4166 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004167 return false;
4168 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004169 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004170 return false;
4171 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004172 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004173 return false;
4174 return true;
4175}
4176
Evan Cheng5ced1d82006-04-06 23:23:56 +00004177/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004178/// is promoted to a vector. It also returns the LoadSDNode by reference if
4179/// required.
4180static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004181 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4182 return false;
4183 N = N->getOperand(0).getNode();
4184 if (!ISD::isNON_EXTLoad(N))
4185 return false;
4186 if (LD)
4187 *LD = cast<LoadSDNode>(N);
4188 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004189}
4190
Dan Gohman65fd6562011-11-03 21:49:52 +00004191// Test whether the given value is a vector value which will be legalized
4192// into a load.
4193static bool WillBeConstantPoolLoad(SDNode *N) {
4194 if (N->getOpcode() != ISD::BUILD_VECTOR)
4195 return false;
4196
4197 // Check for any non-constant elements.
4198 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4199 switch (N->getOperand(i).getNode()->getOpcode()) {
4200 case ISD::UNDEF:
4201 case ISD::ConstantFP:
4202 case ISD::Constant:
4203 break;
4204 default:
4205 return false;
4206 }
4207
4208 // Vectors of all-zeros and all-ones are materialized with special
4209 // instructions rather than being loaded.
4210 return !ISD::isBuildVectorAllZeros(N) &&
4211 !ISD::isBuildVectorAllOnes(N);
4212}
4213
Evan Cheng533a0aa2006-04-19 20:35:22 +00004214/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4215/// match movlp{s|d}. The lower half elements should come from lower half of
4216/// V1 (and in order), and the upper half elements should come from the upper
4217/// half of V2 (and in order). And since V1 will become the source of the
4218/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004219static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004220 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004221 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004222 return false;
4223
Evan Cheng466685d2006-10-09 20:57:25 +00004224 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004225 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004226 // Is V2 is a vector load, don't do this transformation. We will try to use
4227 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004228 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004229 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004230
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004231 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004232
Evan Cheng533a0aa2006-04-19 20:35:22 +00004233 if (NumElems != 2 && NumElems != 4)
4234 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004235 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004236 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004237 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004238 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004239 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004240 return false;
4241 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004242}
4243
Evan Cheng39623da2006-04-20 08:58:49 +00004244/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4245/// all the same.
4246static bool isSplatVector(SDNode *N) {
4247 if (N->getOpcode() != ISD::BUILD_VECTOR)
4248 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004249
Dan Gohman475871a2008-07-27 21:46:04 +00004250 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004251 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4252 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004253 return false;
4254 return true;
4255}
4256
Evan Cheng213d2cf2007-05-17 18:45:50 +00004257/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004258/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004259/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004260static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004261 SDValue V1 = N->getOperand(0);
4262 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004263 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4264 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004266 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004267 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004268 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4269 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004270 if (Opc != ISD::BUILD_VECTOR ||
4271 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004272 return false;
4273 } else if (Idx >= 0) {
4274 unsigned Opc = V1.getOpcode();
4275 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4276 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004277 if (Opc != ISD::BUILD_VECTOR ||
4278 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004279 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004280 }
4281 }
4282 return true;
4283}
4284
4285/// getZeroVector - Returns a vector of specified type with all zero elements.
4286///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004287static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004288 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004289 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004290 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004291
Dale Johannesen0488fb62010-09-30 23:57:10 +00004292 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004293 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004294 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004295 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004296 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004297 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4298 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4299 } else { // SSE1
4300 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4302 }
Craig Topper9d352402012-04-23 07:24:41 +00004303 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004304 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004305 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4306 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4307 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4308 } else {
4309 // 256-bit logic and arithmetic instructions in AVX are all
4310 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4311 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4312 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4313 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4314 }
Craig Topper9d352402012-04-23 07:24:41 +00004315 } else
4316 llvm_unreachable("Unexpected vector type");
4317
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004318 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004319}
4320
Chris Lattner8a594482007-11-25 00:24:49 +00004321/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004322/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4323/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4324/// Then bitcast to their original type, ensuring they get CSE'd.
4325static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4326 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004327 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004328 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004329
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004331 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004332 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004333 if (HasAVX2) { // AVX2
4334 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4335 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4336 } else { // AVX
4337 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004338 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004339 }
Craig Topper9d352402012-04-23 07:24:41 +00004340 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004341 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004342 } else
4343 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004344
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004345 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004346}
4347
Evan Cheng39623da2006-04-20 08:58:49 +00004348/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4349/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004350static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004351 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004352 if (Mask[i] > (int)NumElems) {
4353 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004354 }
Evan Cheng39623da2006-04-20 08:58:49 +00004355 }
Evan Cheng39623da2006-04-20 08:58:49 +00004356}
4357
Evan Cheng017dcc62006-04-21 01:05:10 +00004358/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4359/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004360static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 SDValue V2) {
4362 unsigned NumElems = VT.getVectorNumElements();
4363 SmallVector<int, 8> Mask;
4364 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004365 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 Mask.push_back(i);
4367 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004368}
4369
Nate Begeman9008ca62009-04-27 18:41:29 +00004370/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004371static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 SDValue V2) {
4373 unsigned NumElems = VT.getVectorNumElements();
4374 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004375 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 Mask.push_back(i);
4377 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004378 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004380}
4381
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004382/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004383static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 SDValue V2) {
4385 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004387 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 Mask.push_back(i + Half);
4389 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004390 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004392}
4393
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004394// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004395// a generic shuffle instruction because the target has no such instructions.
4396// Generate shuffles which repeat i16 and i8 several times until they can be
4397// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004398static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004401 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004402
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 while (NumElems > 4) {
4404 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004405 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004407 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004408 EltNo -= NumElems/2;
4409 }
4410 NumElems >>= 1;
4411 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004412 return V;
4413}
Eric Christopherfd179292009-08-27 18:07:15 +00004414
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4416static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4417 EVT VT = V.getValueType();
4418 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004419 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420
Craig Topper9d352402012-04-23 07:24:41 +00004421 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004422 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004423 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004424 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4425 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004426 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004427 // To use VPERMILPS to splat scalars, the second half of indicies must
4428 // refer to the higher part, which is a duplication of the lower one,
4429 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4431 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004432
4433 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4434 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4435 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004436 } else
4437 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004438
4439 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4440}
4441
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004442/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004443static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4444 EVT SrcVT = SV->getValueType(0);
4445 SDValue V1 = SV->getOperand(0);
4446 DebugLoc dl = SV->getDebugLoc();
4447
4448 int EltNo = SV->getSplatIndex();
4449 int NumElems = SrcVT.getVectorNumElements();
4450 unsigned Size = SrcVT.getSizeInBits();
4451
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004452 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4453 "Unknown how to promote splat for type");
4454
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004455 // Extract the 128-bit part containing the splat element and update
4456 // the splat element index when it refers to the higher register.
4457 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004458 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4459 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004460 EltNo -= NumElems/2;
4461 }
4462
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004463 // All i16 and i8 vector types can't be used directly by a generic shuffle
4464 // instruction because the target has no such instruction. Generate shuffles
4465 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004466 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004467 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004468 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004469 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004470
4471 // Recreate the 256-bit vector and place the same 128-bit vector
4472 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004473 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004474 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004475 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004476 }
4477
4478 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004479}
4480
Evan Chengba05f722006-04-21 23:03:30 +00004481/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004482/// vector of zero or undef vector. This produces a shuffle where the low
4483/// element of V2 is swizzled into the zero/undef vector, landing at element
4484/// 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 +00004485static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004486 bool IsZero,
4487 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004488 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004489 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004490 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004491 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 unsigned NumElems = VT.getVectorNumElements();
4493 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004494 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004495 // If this is the insertion idx, put the low elt of V2 here.
4496 MaskVec.push_back(i == Idx ? NumElems : i);
4497 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004498}
4499
Craig Toppera1ffc682012-03-20 06:42:26 +00004500/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4501/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004502/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004503static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004504 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004505 unsigned NumElems = VT.getVectorNumElements();
4506 SDValue ImmN;
4507
Craig Topper89f4e662012-03-20 07:17:59 +00004508 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004509 switch(N->getOpcode()) {
4510 case X86ISD::SHUFP:
4511 ImmN = N->getOperand(N->getNumOperands()-1);
4512 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4513 break;
4514 case X86ISD::UNPCKH:
4515 DecodeUNPCKHMask(VT, Mask);
4516 break;
4517 case X86ISD::UNPCKL:
4518 DecodeUNPCKLMask(VT, Mask);
4519 break;
4520 case X86ISD::MOVHLPS:
4521 DecodeMOVHLPSMask(NumElems, Mask);
4522 break;
4523 case X86ISD::MOVLHPS:
4524 DecodeMOVLHPSMask(NumElems, Mask);
4525 break;
4526 case X86ISD::PSHUFD:
4527 case X86ISD::VPERMILP:
4528 ImmN = N->getOperand(N->getNumOperands()-1);
4529 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004530 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004531 break;
4532 case X86ISD::PSHUFHW:
4533 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004534 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004535 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004536 break;
4537 case X86ISD::PSHUFLW:
4538 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004539 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004540 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004541 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004542 case X86ISD::VPERMI:
4543 ImmN = N->getOperand(N->getNumOperands()-1);
4544 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4545 IsUnary = true;
4546 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004547 case X86ISD::MOVSS:
4548 case X86ISD::MOVSD: {
4549 // The index 0 always comes from the first element of the second source,
4550 // this is why MOVSS and MOVSD are used in the first place. The other
4551 // elements come from the other positions of the first source vector
4552 Mask.push_back(NumElems);
4553 for (unsigned i = 1; i != NumElems; ++i) {
4554 Mask.push_back(i);
4555 }
4556 break;
4557 }
4558 case X86ISD::VPERM2X128:
4559 ImmN = N->getOperand(N->getNumOperands()-1);
4560 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004561 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004562 break;
4563 case X86ISD::MOVDDUP:
4564 case X86ISD::MOVLHPD:
4565 case X86ISD::MOVLPD:
4566 case X86ISD::MOVLPS:
4567 case X86ISD::MOVSHDUP:
4568 case X86ISD::MOVSLDUP:
4569 case X86ISD::PALIGN:
4570 // Not yet implemented
4571 return false;
4572 default: llvm_unreachable("unknown target shuffle node");
4573 }
4574
4575 return true;
4576}
4577
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004578/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4579/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004580static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004581 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004582 if (Depth == 6)
4583 return SDValue(); // Limit search depth.
4584
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004585 SDValue V = SDValue(N, 0);
4586 EVT VT = V.getValueType();
4587 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004588
4589 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4590 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004591 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004592
Craig Topper3d092db2012-03-21 02:14:01 +00004593 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004594 return DAG.getUNDEF(VT.getVectorElementType());
4595
Craig Topperd156dc12012-02-06 07:17:51 +00004596 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004597 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4598 : SV->getOperand(1);
4599 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004600 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004601
4602 // Recurse into target specific vector shuffles to find scalars.
4603 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004604 MVT ShufVT = V.getValueType().getSimpleVT();
4605 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004606 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004607 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004608 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004609
Craig Topperd978c542012-05-06 19:46:21 +00004610 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004611 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004612
Craig Topper3d092db2012-03-21 02:14:01 +00004613 int Elt = ShuffleMask[Index];
4614 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004615 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004616
Craig Topper3d092db2012-03-21 02:14:01 +00004617 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004618 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004619 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004620 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004621 }
4622
4623 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004624 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625 V = V.getOperand(0);
4626 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004627 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004628
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004629 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004630 return SDValue();
4631 }
4632
4633 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4634 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004635 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004636
4637 if (V.getOpcode() == ISD::BUILD_VECTOR)
4638 return V.getOperand(Index);
4639
4640 return SDValue();
4641}
4642
4643/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4644/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004645/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004646static
Craig Topper3d092db2012-03-21 02:14:01 +00004647unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004649 unsigned i;
4650 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004651 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004652 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004653 if (!(Elt.getNode() &&
4654 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4655 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004656 }
4657
4658 return i;
4659}
4660
Craig Topper3d092db2012-03-21 02:14:01 +00004661/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4662/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004663/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4664static
Craig Topper3d092db2012-03-21 02:14:01 +00004665bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4666 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4667 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004668 bool SeenV1 = false;
4669 bool SeenV2 = false;
4670
Craig Topper3d092db2012-03-21 02:14:01 +00004671 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004672 int Idx = SVOp->getMaskElt(i);
4673 // Ignore undef indicies
4674 if (Idx < 0)
4675 continue;
4676
Craig Topper3d092db2012-03-21 02:14:01 +00004677 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678 SeenV1 = true;
4679 else
4680 SeenV2 = true;
4681
4682 // Only accept consecutive elements from the same vector
4683 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4684 return false;
4685 }
4686
4687 OpNum = SeenV1 ? 0 : 1;
4688 return true;
4689}
4690
4691/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4692/// logical left shift of a vector.
4693static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4694 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4695 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4696 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4697 false /* check zeros from right */, DAG);
4698 unsigned OpSrc;
4699
4700 if (!NumZeros)
4701 return false;
4702
4703 // Considering the elements in the mask that are not consecutive zeros,
4704 // check if they consecutively come from only one of the source vectors.
4705 //
4706 // V1 = {X, A, B, C} 0
4707 // \ \ \ /
4708 // vector_shuffle V1, V2 <1, 2, 3, X>
4709 //
4710 if (!isShuffleMaskConsecutive(SVOp,
4711 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004712 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004713 NumZeros, // Where to start looking in the src vector
4714 NumElems, // Number of elements in vector
4715 OpSrc)) // Which source operand ?
4716 return false;
4717
4718 isLeft = false;
4719 ShAmt = NumZeros;
4720 ShVal = SVOp->getOperand(OpSrc);
4721 return true;
4722}
4723
4724/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4725/// logical left shift of a vector.
4726static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4727 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4728 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4729 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4730 true /* check zeros from left */, DAG);
4731 unsigned OpSrc;
4732
4733 if (!NumZeros)
4734 return false;
4735
4736 // Considering the elements in the mask that are not consecutive zeros,
4737 // check if they consecutively come from only one of the source vectors.
4738 //
4739 // 0 { A, B, X, X } = V2
4740 // / \ / /
4741 // vector_shuffle V1, V2 <X, X, 4, 5>
4742 //
4743 if (!isShuffleMaskConsecutive(SVOp,
4744 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004745 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746 0, // Where to start looking in the src vector
4747 NumElems, // Number of elements in vector
4748 OpSrc)) // Which source operand ?
4749 return false;
4750
4751 isLeft = true;
4752 ShAmt = NumZeros;
4753 ShVal = SVOp->getOperand(OpSrc);
4754 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004755}
4756
4757/// isVectorShift - Returns true if the shuffle can be implemented as a
4758/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004759static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004760 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004761 // Although the logic below support any bitwidth size, there are no
4762 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004763 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004764 return false;
4765
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004766 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4767 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4768 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004769
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004770 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004771}
4772
Evan Chengc78d3b42006-04-24 18:01:45 +00004773/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4774///
Dan Gohman475871a2008-07-27 21:46:04 +00004775static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004776 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004777 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004778 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004779 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004780 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004781 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004782
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004783 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004784 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004785 bool First = true;
4786 for (unsigned i = 0; i < 16; ++i) {
4787 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4788 if (ThisIsNonZero && First) {
4789 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004790 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004791 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004792 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004793 First = false;
4794 }
4795
4796 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004797 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004798 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4799 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004800 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004801 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004802 }
4803 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004804 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4805 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4806 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004808 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 } else
4810 ThisElt = LastElt;
4811
Gabor Greifba36cb52008-08-28 21:40:38 +00004812 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004814 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004815 }
4816 }
4817
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004818 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819}
4820
Bill Wendlinga348c562007-03-22 18:42:45 +00004821/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004822///
Dan Gohman475871a2008-07-27 21:46:04 +00004823static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004824 unsigned NumNonZero, unsigned NumZero,
4825 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004826 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004827 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004829 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004830
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004831 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004832 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 bool First = true;
4834 for (unsigned i = 0; i < 8; ++i) {
4835 bool isNonZero = (NonZeros & (1 << i)) != 0;
4836 if (isNonZero) {
4837 if (First) {
4838 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004839 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004840 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004842 First = false;
4843 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004844 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004846 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004847 }
4848 }
4849
4850 return V;
4851}
4852
Evan Chengf26ffe92008-05-29 08:22:04 +00004853/// getVShift - Return a vector logical shift node.
4854///
Owen Andersone50ed302009-08-10 22:56:29 +00004855static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004856 unsigned NumBits, SelectionDAG &DAG,
4857 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004858 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004859 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004860 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004861 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4862 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004863 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004864 DAG.getConstant(NumBits,
4865 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004866}
4867
Dan Gohman475871a2008-07-27 21:46:04 +00004868SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004869X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004870 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004871
Evan Chengc3630942009-12-09 21:00:30 +00004872 // Check if the scalar load can be widened into a vector load. And if
4873 // the address is "base + cst" see if the cst can be "absorbed" into
4874 // the shuffle mask.
4875 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4876 SDValue Ptr = LD->getBasePtr();
4877 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4878 return SDValue();
4879 EVT PVT = LD->getValueType(0);
4880 if (PVT != MVT::i32 && PVT != MVT::f32)
4881 return SDValue();
4882
4883 int FI = -1;
4884 int64_t Offset = 0;
4885 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4886 FI = FINode->getIndex();
4887 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004888 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004889 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4890 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4891 Offset = Ptr.getConstantOperandVal(1);
4892 Ptr = Ptr.getOperand(0);
4893 } else {
4894 return SDValue();
4895 }
4896
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004897 // FIXME: 256-bit vector instructions don't require a strict alignment,
4898 // improve this code to support it better.
4899 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004900 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004901 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004902 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004903 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004904 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004905 // Can't change the alignment. FIXME: It's possible to compute
4906 // the exact stack offset and reference FI + adjust offset instead.
4907 // If someone *really* cares about this. That's the way to implement it.
4908 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004909 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004910 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004911 }
4912 }
4913
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004915 // Ptr + (Offset & ~15).
4916 if (Offset < 0)
4917 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004919 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004921 if (StartOffset)
4922 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4923 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4924
4925 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004926 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004927
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004928 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4929 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004930 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004931 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004932
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004933 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004934 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004935 Mask.push_back(EltNo);
4936
Craig Toppercc3000632012-01-30 07:50:31 +00004937 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004938 }
4939
4940 return SDValue();
4941}
4942
Michael J. Spencerec38de22010-10-10 22:04:20 +00004943/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4944/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004945/// load which has the same value as a build_vector whose operands are 'elts'.
4946///
4947/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004948///
Nate Begeman1449f292010-03-24 22:19:06 +00004949/// FIXME: we'd also like to handle the case where the last elements are zero
4950/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4951/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004952static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004953 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004954 EVT EltVT = VT.getVectorElementType();
4955 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004956
Nate Begemanfdea31a2010-03-24 20:49:50 +00004957 LoadSDNode *LDBase = NULL;
4958 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004959
Nate Begeman1449f292010-03-24 22:19:06 +00004960 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004961 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004962 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004963 for (unsigned i = 0; i < NumElems; ++i) {
4964 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004965
Nate Begemanfdea31a2010-03-24 20:49:50 +00004966 if (!Elt.getNode() ||
4967 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4968 return SDValue();
4969 if (!LDBase) {
4970 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4971 return SDValue();
4972 LDBase = cast<LoadSDNode>(Elt.getNode());
4973 LastLoadedElt = i;
4974 continue;
4975 }
4976 if (Elt.getOpcode() == ISD::UNDEF)
4977 continue;
4978
4979 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4980 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4981 return SDValue();
4982 LastLoadedElt = i;
4983 }
Nate Begeman1449f292010-03-24 22:19:06 +00004984
4985 // If we have found an entire vector of loads and undefs, then return a large
4986 // load of the entire vector width starting at the base pointer. If we found
4987 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 if (LastLoadedElt == NumElems - 1) {
4989 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004990 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004991 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004992 LDBase->isVolatile(), LDBase->isNonTemporal(),
4993 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004994 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004995 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004996 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004997 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004998 }
4999 if (NumElems == 4 && LastLoadedElt == 1 &&
5000 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005001 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5002 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005003 SDValue ResNode =
5004 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5005 LDBase->getPointerInfo(),
5006 LDBase->getAlignment(),
5007 false/*isVolatile*/, true/*ReadMem*/,
5008 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005009
5010 // Make sure the newly-created LOAD is in the same position as LDBase in
5011 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5012 // update uses of LDBase's output chain to use the TokenFactor.
5013 if (LDBase->hasAnyUseOfValue(1)) {
5014 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5015 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5016 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5017 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5018 SDValue(ResNode.getNode(), 1));
5019 }
5020
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005021 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005022 }
5023 return SDValue();
5024}
5025
Nadav Rotem9d68b062012-04-08 12:54:54 +00005026/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5027/// to generate a splat value for the following cases:
5028/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005029/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005030/// a scalar load, or a constant.
5031/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005032/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005033SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005034X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005035 if (!Subtarget->hasAVX())
5036 return SDValue();
5037
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005039 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005040
Craig Topper5da8a802012-05-04 05:49:51 +00005041 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5042 "Unsupported vector type for broadcast.");
5043
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005044 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005045 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005046
Nadav Rotem9d68b062012-04-08 12:54:54 +00005047 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005048 default:
5049 // Unknown pattern found.
5050 return SDValue();
5051
5052 case ISD::BUILD_VECTOR: {
5053 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005054 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005055 return SDValue();
5056
Nadav Rotem9d68b062012-04-08 12:54:54 +00005057 Ld = Op.getOperand(0);
5058 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5059 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005060
5061 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005062 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005063 // Constants may have multiple users.
5064 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005065 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005066 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005067 }
5068
5069 case ISD::VECTOR_SHUFFLE: {
5070 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5071
5072 // Shuffles must have a splat mask where the first element is
5073 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005074 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005075 return SDValue();
5076
5077 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005078 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005079 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5080
5081 if (!Subtarget->hasAVX2())
5082 return SDValue();
5083
5084 // Use the register form of the broadcast instruction available on AVX2.
5085 if (VT.is256BitVector())
5086 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5087 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5088 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005089
5090 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005091 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005092 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005093
5094 // The scalar_to_vector node and the suspected
5095 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005096 // Constants may have multiple users.
5097 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005098 return SDValue();
5099 break;
5100 }
5101 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005102
Craig Topper7a9a28b2012-08-12 02:23:29 +00005103 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104
5105 // Handle the broadcasting a single constant scalar from the constant pool
5106 // into a vector. On Sandybridge it is still better to load a constant vector
5107 // from the constant pool and not to broadcast it from a scalar.
5108 if (ConstSplatVal && Subtarget->hasAVX2()) {
5109 EVT CVT = Ld.getValueType();
5110 assert(!CVT.isVector() && "Must not broadcast a vector type");
5111 unsigned ScalarSize = CVT.getSizeInBits();
5112
Craig Topper5da8a802012-05-04 05:49:51 +00005113 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005114 const Constant *C = 0;
5115 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5116 C = CI->getConstantIntValue();
5117 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5118 C = CF->getConstantFPValue();
5119
5120 assert(C && "Invalid constant type");
5121
Nadav Rotem154819d2012-04-09 07:45:58 +00005122 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005123 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005124 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005125 MachinePointerInfo::getConstantPool(),
5126 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005127
Nadav Rotem9d68b062012-04-08 12:54:54 +00005128 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5129 }
5130 }
5131
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005132 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005133 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5134
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005135 // Handle AVX2 in-register broadcasts.
5136 if (!IsLoad && Subtarget->hasAVX2() &&
5137 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5138 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5139
5140 // The scalar source must be a normal load.
5141 if (!IsLoad)
5142 return SDValue();
5143
Craig Topper5da8a802012-05-04 05:49:51 +00005144 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005145 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005146
Craig Toppera9376332012-01-10 08:23:59 +00005147 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005148 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005149 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005150 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005151 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005152 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005153
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 // Unsupported broadcast.
5155 return SDValue();
5156}
5157
Michael Liao7091b242012-08-14 21:24:47 +00005158// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5159// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5160// constraint of matching input/output vector elements.
5161SDValue
5162X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5163 DebugLoc DL = Op.getDebugLoc();
5164 SDNode *N = Op.getNode();
5165 EVT VT = Op.getValueType();
5166 unsigned NumElts = Op.getNumOperands();
5167
5168 // Check supported types and sub-targets.
5169 //
5170 // Only v2f32 -> v2f64 needs special handling.
5171 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5172 return SDValue();
5173
5174 SDValue VecIn;
5175 EVT VecInVT;
5176 SmallVector<int, 8> Mask;
5177 EVT SrcVT = MVT::Other;
5178
5179 // Check the patterns could be translated into X86vfpext.
5180 for (unsigned i = 0; i < NumElts; ++i) {
5181 SDValue In = N->getOperand(i);
5182 unsigned Opcode = In.getOpcode();
5183
5184 // Skip if the element is undefined.
5185 if (Opcode == ISD::UNDEF) {
5186 Mask.push_back(-1);
5187 continue;
5188 }
5189
5190 // Quit if one of the elements is not defined from 'fpext'.
5191 if (Opcode != ISD::FP_EXTEND)
5192 return SDValue();
5193
5194 // Check how the source of 'fpext' is defined.
5195 SDValue L2In = In.getOperand(0);
5196 EVT L2InVT = L2In.getValueType();
5197
5198 // Check the original type
5199 if (SrcVT == MVT::Other)
5200 SrcVT = L2InVT;
5201 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5202 return SDValue();
5203
5204 // Check whether the value being 'fpext'ed is extracted from the same
5205 // source.
5206 Opcode = L2In.getOpcode();
5207
5208 // Quit if it's not extracted with a constant index.
5209 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5210 !isa<ConstantSDNode>(L2In.getOperand(1)))
5211 return SDValue();
5212
5213 SDValue ExtractedFromVec = L2In.getOperand(0);
5214
5215 if (VecIn.getNode() == 0) {
5216 VecIn = ExtractedFromVec;
5217 VecInVT = ExtractedFromVec.getValueType();
5218 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5219 return SDValue();
5220
5221 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5222 }
5223
Michael Liao24438b82012-08-20 17:59:18 +00005224 // Quit if all operands of BUILD_VECTOR are undefined.
5225 if (!VecIn.getNode())
5226 return SDValue();
5227
Michael Liao7091b242012-08-14 21:24:47 +00005228 // Fill the remaining mask as undef.
5229 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5230 Mask.push_back(-1);
5231
5232 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5233 DAG.getVectorShuffle(VecInVT, DL,
5234 VecIn, DAG.getUNDEF(VecInVT),
5235 &Mask[0]));
5236}
5237
Evan Chengc3630942009-12-09 21:00:30 +00005238SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005239X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005240 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005241
David Greenef125a292011-02-08 19:04:41 +00005242 EVT VT = Op.getValueType();
5243 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005244 unsigned NumElems = Op.getNumOperands();
5245
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005246 // Vectors containing all zeros can be matched by pxor and xorps later
5247 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5248 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5249 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005250 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005251 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005253 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005254 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005255
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005256 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005257 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5258 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005259 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005260 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005261 return Op;
5262
Craig Topper07a27622012-01-22 03:07:48 +00005263 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005264 }
5265
Nadav Rotem154819d2012-04-09 07:45:58 +00005266 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005267 if (Broadcast.getNode())
5268 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005269
Michael Liao7091b242012-08-14 21:24:47 +00005270 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5271 if (FpExt.getNode())
5272 return FpExt;
5273
Owen Andersone50ed302009-08-10 22:56:29 +00005274 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005275
Evan Cheng0db9fe62006-04-25 20:13:52 +00005276 unsigned NumZero = 0;
5277 unsigned NumNonZero = 0;
5278 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005279 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005280 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005282 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005283 if (Elt.getOpcode() == ISD::UNDEF)
5284 continue;
5285 Values.insert(Elt);
5286 if (Elt.getOpcode() != ISD::Constant &&
5287 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005288 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005289 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005290 NumZero++;
5291 else {
5292 NonZeros |= (1 << i);
5293 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005294 }
5295 }
5296
Chris Lattner97a2a562010-08-26 05:24:29 +00005297 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5298 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005299 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005300
Chris Lattner67f453a2008-03-09 05:42:06 +00005301 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005302 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005303 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005304 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005305
Chris Lattner62098042008-03-09 01:05:04 +00005306 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5307 // the value are obviously zero, truncate the value to i32 and do the
5308 // insertion that way. Only do this if the value is non-constant or if the
5309 // value is a constant being inserted into element 0. It is cheaper to do
5310 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005312 (!IsAllConstants || Idx == 0)) {
5313 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005314 // Handle SSE only.
5315 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5316 EVT VecVT = MVT::v4i32;
5317 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005318
Chris Lattner62098042008-03-09 01:05:04 +00005319 // Truncate the value (which may itself be a constant) to i32, and
5320 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005321 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005322 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005323 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005324
Chris Lattner62098042008-03-09 01:05:04 +00005325 // Now we have our 32-bit value zero extended in the low element of
5326 // a vector. If Idx != 0, swizzle it into place.
5327 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005328 SmallVector<int, 4> Mask;
5329 Mask.push_back(Idx);
5330 for (unsigned i = 1; i != VecElts; ++i)
5331 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005332 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005333 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005334 }
Craig Topper07a27622012-01-22 03:07:48 +00005335 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005336 }
5337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005338
Chris Lattner19f79692008-03-08 22:59:52 +00005339 // If we have a constant or non-constant insertion into the low element of
5340 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5341 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005342 // depending on what the source datatype is.
5343 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005344 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005345 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005346
5347 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005348 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005349 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005350 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005351 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5352 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005353 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005354 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005355 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5356 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005357 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005358 }
5359
5360 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005361 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005362 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005363 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005364 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005365 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005366 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005367 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005368 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005369 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005370 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005371 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005372 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005373
5374 // Is it a vector logical left shift?
5375 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005376 X86::isZeroNode(Op.getOperand(0)) &&
5377 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005378 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005379 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005380 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005381 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005382 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005384
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005385 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005386 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005387
Chris Lattner19f79692008-03-08 22:59:52 +00005388 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5389 // is a non-constant being inserted into an element other than the low one,
5390 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5391 // movd/movss) to move this into the low element, then shuffle it into
5392 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005393 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005394 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005395
Evan Cheng0db9fe62006-04-25 20:13:52 +00005396 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005397 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005398 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005399 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005400 MaskVec.push_back(i == Idx ? 0 : 1);
5401 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005402 }
5403 }
5404
Chris Lattner67f453a2008-03-09 05:42:06 +00005405 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005406 if (Values.size() == 1) {
5407 if (EVTBits == 32) {
5408 // Instead of a shuffle like this:
5409 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5410 // Check if it's possible to issue this instead.
5411 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5412 unsigned Idx = CountTrailingZeros_32(NonZeros);
5413 SDValue Item = Op.getOperand(Idx);
5414 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5415 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5416 }
Dan Gohman475871a2008-07-27 21:46:04 +00005417 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005419
Dan Gohmana3941172007-07-24 22:55:08 +00005420 // A vector full of immediates; various special cases are already
5421 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005422 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005423 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005424
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005425 // For AVX-length vectors, build the individual 128-bit pieces and use
5426 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005427 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005428 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005429 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005430 V.push_back(Op.getOperand(i));
5431
5432 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5433
5434 // Build both the lower and upper subvector.
5435 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5436 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5437 NumElems/2);
5438
5439 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005440 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005441 }
5442
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005443 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005444 if (EVTBits == 64) {
5445 if (NumNonZero == 1) {
5446 // One half is zero or undef.
5447 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005448 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005449 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005450 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005451 }
Dan Gohman475871a2008-07-27 21:46:04 +00005452 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005453 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454
5455 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005456 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005457 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005458 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005459 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005460 }
5461
Bill Wendling826f36f2007-03-28 00:57:11 +00005462 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005463 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005464 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005465 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005466 }
5467
5468 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005469 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470 if (NumElems == 4 && NumZero > 0) {
5471 for (unsigned i = 0; i < 4; ++i) {
5472 bool isZero = !(NonZeros & (1 << i));
5473 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005474 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005475 else
Dale Johannesenace16102009-02-03 19:33:06 +00005476 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477 }
5478
5479 for (unsigned i = 0; i < 2; ++i) {
5480 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5481 default: break;
5482 case 0:
5483 V[i] = V[i*2]; // Must be a zero vector.
5484 break;
5485 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005486 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 break;
5488 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005489 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005490 break;
5491 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005492 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005493 break;
5494 }
5495 }
5496
Benjamin Kramer9c683542012-01-30 15:16:21 +00005497 bool Reverse1 = (NonZeros & 0x3) == 2;
5498 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5499 int MaskVec[] = {
5500 Reverse1 ? 1 : 0,
5501 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005502 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5503 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005504 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005505 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506 }
5507
Craig Topper7a9a28b2012-08-12 02:23:29 +00005508 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005509 // Check for a build vector of consecutive loads.
5510 for (unsigned i = 0; i < NumElems; ++i)
5511 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005512
Nate Begemanfdea31a2010-03-24 20:49:50 +00005513 // Check for elements which are consecutive loads.
5514 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5515 if (LD.getNode())
5516 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005517
5518 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005519 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005520 SDValue Result;
5521 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5522 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5523 else
5524 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005525
Chris Lattner24faf612010-08-28 17:59:08 +00005526 for (unsigned i = 1; i < NumElems; ++i) {
5527 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5528 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005529 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005530 }
5531 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005533
Chris Lattner6e80e442010-08-28 17:15:43 +00005534 // Otherwise, expand into a number of unpckl*, start by extending each of
5535 // our (non-undef) elements to the full vector width with the element in the
5536 // bottom slot of the vector (which generates no code for SSE).
5537 for (unsigned i = 0; i < NumElems; ++i) {
5538 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5539 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5540 else
5541 V[i] = DAG.getUNDEF(VT);
5542 }
5543
5544 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005545 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5546 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5547 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005548 unsigned EltStride = NumElems >> 1;
5549 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005550 for (unsigned i = 0; i < EltStride; ++i) {
5551 // If V[i+EltStride] is undef and this is the first round of mixing,
5552 // then it is safe to just drop this shuffle: V[i] is already in the
5553 // right place, the one element (since it's the first round) being
5554 // inserted as undef can be dropped. This isn't safe for successive
5555 // rounds because they will permute elements within both vectors.
5556 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5557 EltStride == NumElems/2)
5558 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005559
Chris Lattner6e80e442010-08-28 17:15:43 +00005560 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005561 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005562 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005563 }
5564 return V[0];
5565 }
Dan Gohman475871a2008-07-27 21:46:04 +00005566 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005567}
5568
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005569// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5570// to create 256-bit vectors from two other 128-bit ones.
5571static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5572 DebugLoc dl = Op.getDebugLoc();
5573 EVT ResVT = Op.getValueType();
5574
Craig Topper7a9a28b2012-08-12 02:23:29 +00005575 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005576
5577 SDValue V1 = Op.getOperand(0);
5578 SDValue V2 = Op.getOperand(1);
5579 unsigned NumElems = ResVT.getVectorNumElements();
5580
Craig Topper4c7972d2012-04-22 18:15:59 +00005581 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005582}
5583
Craig Topper55b24052012-09-11 06:15:32 +00005584static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005585 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005586
5587 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5588 // from two other 128-bit ones.
5589 return LowerAVXCONCAT_VECTORS(Op, DAG);
5590}
5591
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005592// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005593static SDValue
5594LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5595 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005596 SDValue V1 = SVOp->getOperand(0);
5597 SDValue V2 = SVOp->getOperand(1);
5598 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005599 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005600 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005601
Nadav Roteme6113782012-04-11 06:40:27 +00005602 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005603 return SDValue();
5604
Craig Topper1842ba02012-04-23 06:38:28 +00005605 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005606 MVT OpTy;
5607
Craig Topper708e44f2012-04-23 07:36:33 +00005608 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005609 default: return SDValue();
5610 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005611 ISDNo = X86ISD::BLENDPW;
5612 OpTy = MVT::v8i16;
5613 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005614 case MVT::v4i32:
5615 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005616 ISDNo = X86ISD::BLENDPS;
5617 OpTy = MVT::v4f32;
5618 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005619 case MVT::v2i64:
5620 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005621 ISDNo = X86ISD::BLENDPD;
5622 OpTy = MVT::v2f64;
5623 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005624 case MVT::v8i32:
5625 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005626 if (!Subtarget->hasAVX())
5627 return SDValue();
5628 ISDNo = X86ISD::BLENDPS;
5629 OpTy = MVT::v8f32;
5630 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005631 case MVT::v4i64:
5632 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005633 if (!Subtarget->hasAVX())
5634 return SDValue();
5635 ISDNo = X86ISD::BLENDPD;
5636 OpTy = MVT::v4f64;
5637 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005638 }
5639 assert(ISDNo && "Invalid Op Number");
5640
5641 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005642
Craig Topper1842ba02012-04-23 06:38:28 +00005643 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005644 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005645 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005646 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005647 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005648 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005649 else
5650 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005651 }
5652
Nadav Roteme6113782012-04-11 06:40:27 +00005653 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5654 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5655 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5656 DAG.getConstant(MaskVals, MVT::i32));
5657 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005658}
5659
Nate Begemanb9a47b82009-02-23 08:49:38 +00005660// v8i16 shuffles - Prefer shuffles in the following order:
5661// 1. [all] pshuflw, pshufhw, optional move
5662// 2. [ssse3] 1 x pshufb
5663// 3. [ssse3] 2 x pshufb + 1 x por
5664// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005665static SDValue
5666LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5667 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005668 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 SDValue V1 = SVOp->getOperand(0);
5670 SDValue V2 = SVOp->getOperand(1);
5671 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005673
Nate Begemanb9a47b82009-02-23 08:49:38 +00005674 // Determine if more than 1 of the words in each of the low and high quadwords
5675 // of the result come from the same quadword of one of the two inputs. Undef
5676 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005677 unsigned LoQuad[] = { 0, 0, 0, 0 };
5678 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005679 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005681 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005682 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005683 MaskVals.push_back(EltIdx);
5684 if (EltIdx < 0) {
5685 ++Quad[0];
5686 ++Quad[1];
5687 ++Quad[2];
5688 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005689 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 }
5691 ++Quad[EltIdx / 4];
5692 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005693 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005694
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005696 unsigned MaxQuad = 1;
5697 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 if (LoQuad[i] > MaxQuad) {
5699 BestLoQuad = i;
5700 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005701 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005702 }
5703
Nate Begemanb9a47b82009-02-23 08:49:38 +00005704 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005705 MaxQuad = 1;
5706 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 if (HiQuad[i] > MaxQuad) {
5708 BestHiQuad = i;
5709 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005710 }
5711 }
5712
Nate Begemanb9a47b82009-02-23 08:49:38 +00005713 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005714 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 // single pshufb instruction is necessary. If There are more than 2 input
5716 // quads, disable the next transformation since it does not help SSSE3.
5717 bool V1Used = InputQuads[0] || InputQuads[1];
5718 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005719 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005720 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005721 BestLoQuad = InputQuads[0] ? 0 : 1;
5722 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005723 }
5724 if (InputQuads.count() > 2) {
5725 BestLoQuad = -1;
5726 BestHiQuad = -1;
5727 }
5728 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005729
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5731 // the shuffle mask. If a quad is scored as -1, that means that it contains
5732 // words from all 4 input quadwords.
5733 SDValue NewV;
5734 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005735 int MaskV[] = {
5736 BestLoQuad < 0 ? 0 : BestLoQuad,
5737 BestHiQuad < 0 ? 1 : BestHiQuad
5738 };
Eric Christopherfd179292009-08-27 18:07:15 +00005739 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005740 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5741 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5742 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005743
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5745 // source words for the shuffle, to aid later transformations.
5746 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005747 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005748 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005749 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005750 if (idx != (int)i)
5751 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005753 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 AllWordsInNewV = false;
5755 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005756 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005757
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5759 if (AllWordsInNewV) {
5760 for (int i = 0; i != 8; ++i) {
5761 int idx = MaskVals[i];
5762 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005763 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005764 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 if ((idx != i) && idx < 4)
5766 pshufhw = false;
5767 if ((idx != i) && idx > 3)
5768 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005769 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005770 V1 = NewV;
5771 V2Used = false;
5772 BestLoQuad = 0;
5773 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005774 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005775
Nate Begemanb9a47b82009-02-23 08:49:38 +00005776 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5777 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005778 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005779 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5780 unsigned TargetMask = 0;
5781 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005782 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005783 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5784 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5785 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005786 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005787 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005788 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005789 }
Eric Christopherfd179292009-08-27 18:07:15 +00005790
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 // If we have SSSE3, and all words of the result are from 1 input vector,
5792 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5793 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005794 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005796
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005798 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 // mask, and elements that come from V1 in the V2 mask, so that the two
5800 // results can be OR'd together.
5801 bool TwoInputs = V1Used && V2Used;
5802 for (unsigned i = 0; i != 8; ++i) {
5803 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005804 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5805 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5806 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5807 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005809 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005810 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005811 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005812 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005813 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005814 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005815
Nate Begemanb9a47b82009-02-23 08:49:38 +00005816 // Calculate the shuffle mask for the second input, shuffle it, and
5817 // OR it with the first shuffled input.
5818 pshufbMask.clear();
5819 for (unsigned i = 0; i != 8; ++i) {
5820 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005821 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5822 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5823 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5824 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005825 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005826 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005827 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005828 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 MVT::v16i8, &pshufbMask[0], 16));
5830 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005831 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 }
5833
5834 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5835 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005836 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005838 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 for (int i = 0; i != 4; ++i) {
5840 int idx = MaskVals[i];
5841 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005842 InOrder.set(i);
5843 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005844 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 }
5847 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005849 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005850
Craig Topperdd637ae2012-02-19 05:41:45 +00005851 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5852 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005853 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005854 NewV.getOperand(0),
5855 getShufflePSHUFLWImmediate(SVOp), DAG);
5856 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 }
Eric Christopherfd179292009-08-27 18:07:15 +00005858
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5860 // and update MaskVals with the new element order.
5861 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005862 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005863 for (unsigned i = 4; i != 8; ++i) {
5864 int idx = MaskVals[i];
5865 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005866 InOrder.set(i);
5867 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005868 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005869 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 }
5871 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005873 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005874
Craig Topperdd637ae2012-02-19 05:41:45 +00005875 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5876 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005877 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005878 NewV.getOperand(0),
5879 getShufflePSHUFHWImmediate(SVOp), DAG);
5880 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005881 }
Eric Christopherfd179292009-08-27 18:07:15 +00005882
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 // In case BestHi & BestLo were both -1, which means each quadword has a word
5884 // from each of the four input quadwords, calculate the InOrder bitvector now
5885 // before falling through to the insert/extract cleanup.
5886 if (BestLoQuad == -1 && BestHiQuad == -1) {
5887 NewV = V1;
5888 for (int i = 0; i != 8; ++i)
5889 if (MaskVals[i] < 0 || MaskVals[i] == i)
5890 InOrder.set(i);
5891 }
Eric Christopherfd179292009-08-27 18:07:15 +00005892
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 // The other elements are put in the right place using pextrw and pinsrw.
5894 for (unsigned i = 0; i != 8; ++i) {
5895 if (InOrder[i])
5896 continue;
5897 int EltIdx = MaskVals[i];
5898 if (EltIdx < 0)
5899 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005900 SDValue ExtOp = (EltIdx < 8) ?
5901 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5902 DAG.getIntPtrConstant(EltIdx)) :
5903 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 DAG.getIntPtrConstant(i));
5907 }
5908 return NewV;
5909}
5910
5911// v16i8 shuffles - Prefer shuffles in the following order:
5912// 1. [ssse3] 1 x pshufb
5913// 2. [ssse3] 2 x pshufb + 1 x por
5914// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5915static
Nate Begeman9008ca62009-04-27 18:41:29 +00005916SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005917 SelectionDAG &DAG,
5918 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005919 SDValue V1 = SVOp->getOperand(0);
5920 SDValue V2 = SVOp->getOperand(1);
5921 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005922 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005923
Nate Begemanb9a47b82009-02-23 08:49:38 +00005924 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005925 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005926 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005927
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005929 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005933 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 //
5935 // Otherwise, we have elements from both input vectors, and must zero out
5936 // elements that come from V2 in the first mask, and V1 in the second mask
5937 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 for (unsigned i = 0; i != 16; ++i) {
5939 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005940 if (EltIdx < 0 || EltIdx >= 16)
5941 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005944 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005945 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005947
5948 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5949 // the 2nd operand if it's undefined or zero.
5950 if (V2.getOpcode() == ISD::UNDEF ||
5951 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005953
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 // Calculate the shuffle mask for the second input, shuffle it, and
5955 // OR it with the first shuffled input.
5956 pshufbMask.clear();
5957 for (unsigned i = 0; i != 16; ++i) {
5958 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005959 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005960 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005961 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005962 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005963 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005964 MVT::v16i8, &pshufbMask[0], 16));
5965 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 }
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 // No SSSE3 - Calculate in place words and then fix all out of place words
5969 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5970 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005971 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5972 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005973 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 for (int i = 0; i != 8; ++i) {
5975 int Elt0 = MaskVals[i*2];
5976 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005977
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 // This word of the result is all undef, skip it.
5979 if (Elt0 < 0 && Elt1 < 0)
5980 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005981
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005983 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005984 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005985
Nate Begemanb9a47b82009-02-23 08:49:38 +00005986 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5987 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5988 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005989
5990 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5991 // using a single extract together, load it and store it.
5992 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005993 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005994 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005995 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005996 DAG.getIntPtrConstant(i));
5997 continue;
5998 }
5999
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006001 // source byte is not also odd, shift the extracted word left 8 bits
6002 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006003 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006005 DAG.getIntPtrConstant(Elt1 / 2));
6006 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006008 DAG.getConstant(8,
6009 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006010 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6012 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 }
6014 // If Elt0 is defined, extract it from the appropriate source. If the
6015 // source byte is not also even, shift the extracted word right 8 bits. If
6016 // Elt1 was also defined, OR the extracted values together before
6017 // inserting them in the result.
6018 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006019 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6021 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006023 DAG.getConstant(8,
6024 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006025 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006026 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6027 DAG.getConstant(0x00FF, MVT::i16));
6028 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 : InsElt0;
6030 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006031 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006032 DAG.getIntPtrConstant(i));
6033 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006034 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006035}
6036
Elena Demikhovsky41789462012-09-06 12:42:01 +00006037// v32i8 shuffles - Translate to VPSHUFB if possible.
6038static
6039SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006040 const X86Subtarget *Subtarget,
6041 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006042 EVT VT = SVOp->getValueType(0);
6043 SDValue V1 = SVOp->getOperand(0);
6044 SDValue V2 = SVOp->getOperand(1);
6045 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006046 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006047
6048 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006049 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6050 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006051
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006052 // VPSHUFB may be generated if
6053 // (1) one of input vector is undefined or zeroinitializer.
6054 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6055 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006056 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006057 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006058 return SDValue();
6059
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006060 if (V1IsAllZero && !V2IsAllZero) {
6061 CommuteVectorShuffleMask(MaskVals, 32);
6062 V1 = V2;
6063 }
6064 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006065 for (unsigned i = 0; i != 32; i++) {
6066 int EltIdx = MaskVals[i];
6067 if (EltIdx < 0 || EltIdx >= 32)
6068 EltIdx = 0x80;
6069 else {
6070 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6071 // Cross lane is not allowed.
6072 return SDValue();
6073 EltIdx &= 0xf;
6074 }
6075 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6076 }
6077 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6078 DAG.getNode(ISD::BUILD_VECTOR, dl,
6079 MVT::v32i8, &pshufbMask[0], 32));
6080}
6081
Evan Cheng7a831ce2007-12-15 03:00:47 +00006082/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006083/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006084/// done when every pair / quad of shuffle mask elements point to elements in
6085/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006086/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006087static
Nate Begeman9008ca62009-04-27 18:41:29 +00006088SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006089 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006090 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006091 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006092 MVT NewVT;
6093 unsigned Scale;
6094 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006095 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006096 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6097 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6098 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6099 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6100 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6101 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006102 }
6103
Nate Begeman9008ca62009-04-27 18:41:29 +00006104 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006105 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006106 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006107 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006108 int EltIdx = SVOp->getMaskElt(i+j);
6109 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006110 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006111 if (StartIdx < 0)
6112 StartIdx = (EltIdx / Scale);
6113 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006114 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006115 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006116 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006117 }
6118
Craig Topper11ac1f82012-05-04 04:08:44 +00006119 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6120 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006121 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006122}
6123
Evan Chengd880b972008-05-09 21:53:03 +00006124/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006125///
Owen Andersone50ed302009-08-10 22:56:29 +00006126static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006127 SDValue SrcOp, SelectionDAG &DAG,
6128 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006129 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006130 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006131 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006132 LD = dyn_cast<LoadSDNode>(SrcOp);
6133 if (!LD) {
6134 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6135 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006136 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006137 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006138 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006139 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006140 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006141 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006142 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006143 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006144 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6145 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6146 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006147 SrcOp.getOperand(0)
6148 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006149 }
6150 }
6151 }
6152
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006153 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006154 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006155 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006156 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006157}
6158
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006159/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6160/// which could not be matched by any known target speficic shuffle
6161static SDValue
6162LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006163
6164 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6165 if (NewOp.getNode())
6166 return NewOp;
6167
Craig Topper8f35c132012-01-20 09:29:03 +00006168 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006169
Craig Topper8f35c132012-01-20 09:29:03 +00006170 unsigned NumElems = VT.getVectorNumElements();
6171 unsigned NumLaneElems = NumElems / 2;
6172
Craig Topper8f35c132012-01-20 09:29:03 +00006173 DebugLoc dl = SVOp->getDebugLoc();
6174 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006175 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006176 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006177
Craig Topper9a2b6e12012-04-06 07:45:23 +00006178 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006179 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006180 // Build a shuffle mask for the output, discovering on the fly which
6181 // input vectors to use as shuffle operands (recorded in InputUsed).
6182 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006183 // out with UseBuildVector set.
6184 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006185 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006186 unsigned LaneStart = l * NumLaneElems;
6187 for (unsigned i = 0; i != NumLaneElems; ++i) {
6188 // The mask element. This indexes into the input.
6189 int Idx = SVOp->getMaskElt(i+LaneStart);
6190 if (Idx < 0) {
6191 // the mask element does not index into any input vector.
6192 Mask.push_back(-1);
6193 continue;
6194 }
Craig Topper8f35c132012-01-20 09:29:03 +00006195
Craig Topper9a2b6e12012-04-06 07:45:23 +00006196 // The input vector this mask element indexes into.
6197 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006198
Craig Topper9a2b6e12012-04-06 07:45:23 +00006199 // Turn the index into an offset from the start of the input vector.
6200 Idx -= Input * NumLaneElems;
6201
6202 // Find or create a shuffle vector operand to hold this input.
6203 unsigned OpNo;
6204 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6205 if (InputUsed[OpNo] == Input)
6206 // This input vector is already an operand.
6207 break;
6208 if (InputUsed[OpNo] < 0) {
6209 // Create a new operand for this input vector.
6210 InputUsed[OpNo] = Input;
6211 break;
6212 }
6213 }
6214
6215 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006216 // More than two input vectors used! Give up on trying to create a
6217 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6218 UseBuildVector = true;
6219 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006220 }
6221
6222 // Add the mask index for the new shuffle vector.
6223 Mask.push_back(Idx + OpNo * NumLaneElems);
6224 }
6225
Craig Topper8ae97ba2012-05-21 06:40:16 +00006226 if (UseBuildVector) {
6227 SmallVector<SDValue, 16> SVOps;
6228 for (unsigned i = 0; i != NumLaneElems; ++i) {
6229 // The mask element. This indexes into the input.
6230 int Idx = SVOp->getMaskElt(i+LaneStart);
6231 if (Idx < 0) {
6232 SVOps.push_back(DAG.getUNDEF(EltVT));
6233 continue;
6234 }
6235
6236 // The input vector this mask element indexes into.
6237 int Input = Idx / NumElems;
6238
6239 // Turn the index into an offset from the start of the input vector.
6240 Idx -= Input * NumElems;
6241
6242 // Extract the vector element by hand.
6243 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6244 SVOp->getOperand(Input),
6245 DAG.getIntPtrConstant(Idx)));
6246 }
6247
6248 // Construct the output using a BUILD_VECTOR.
6249 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6250 SVOps.size());
6251 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006252 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006253 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006254 } else {
6255 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006256 (InputUsed[0] % 2) * NumLaneElems,
6257 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006258 // If only one input was used, use an undefined vector for the other.
6259 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6260 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006261 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006262 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006263 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006264 }
6265
6266 Mask.clear();
6267 }
Craig Topper8f35c132012-01-20 09:29:03 +00006268
6269 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006270 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006271}
6272
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006273/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6274/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006275static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006276LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006277 SDValue V1 = SVOp->getOperand(0);
6278 SDValue V2 = SVOp->getOperand(1);
6279 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006280 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006281
Craig Topper7a9a28b2012-08-12 02:23:29 +00006282 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006283
Benjamin Kramer9c683542012-01-30 15:16:21 +00006284 std::pair<int, int> Locs[4];
6285 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006286 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006287
Evan Chengace3c172008-07-22 21:13:36 +00006288 unsigned NumHi = 0;
6289 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006290 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006291 int Idx = PermMask[i];
6292 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006293 Locs[i] = std::make_pair(-1, -1);
6294 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006295 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6296 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006297 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006298 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006299 NumLo++;
6300 } else {
6301 Locs[i] = std::make_pair(1, NumHi);
6302 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006303 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006304 NumHi++;
6305 }
6306 }
6307 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006308
Evan Chengace3c172008-07-22 21:13:36 +00006309 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006310 // If no more than two elements come from either vector. This can be
6311 // implemented with two shuffles. First shuffle gather the elements.
6312 // The second shuffle, which takes the first shuffle as both of its
6313 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006314 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006315
Benjamin Kramer9c683542012-01-30 15:16:21 +00006316 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006317
Benjamin Kramer9c683542012-01-30 15:16:21 +00006318 for (unsigned i = 0; i != 4; ++i)
6319 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006320 unsigned Idx = (i < 2) ? 0 : 4;
6321 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006322 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006323 }
Evan Chengace3c172008-07-22 21:13:36 +00006324
Nate Begeman9008ca62009-04-27 18:41:29 +00006325 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006326 }
6327
6328 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006329 // Otherwise, we must have three elements from one vector, call it X, and
6330 // one element from the other, call it Y. First, use a shufps to build an
6331 // intermediate vector with the one element from Y and the element from X
6332 // that will be in the same half in the final destination (the indexes don't
6333 // matter). Then, use a shufps to build the final vector, taking the half
6334 // containing the element from Y from the intermediate, and the other half
6335 // from X.
6336 if (NumHi == 3) {
6337 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006338 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006339 std::swap(V1, V2);
6340 }
6341
6342 // Find the element from V2.
6343 unsigned HiIndex;
6344 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006345 int Val = PermMask[HiIndex];
6346 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006347 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006348 if (Val >= 4)
6349 break;
6350 }
6351
Nate Begeman9008ca62009-04-27 18:41:29 +00006352 Mask1[0] = PermMask[HiIndex];
6353 Mask1[1] = -1;
6354 Mask1[2] = PermMask[HiIndex^1];
6355 Mask1[3] = -1;
6356 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006357
6358 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006359 Mask1[0] = PermMask[0];
6360 Mask1[1] = PermMask[1];
6361 Mask1[2] = HiIndex & 1 ? 6 : 4;
6362 Mask1[3] = HiIndex & 1 ? 4 : 6;
6363 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006364 }
Craig Topper69947b92012-04-23 06:57:04 +00006365
6366 Mask1[0] = HiIndex & 1 ? 2 : 0;
6367 Mask1[1] = HiIndex & 1 ? 0 : 2;
6368 Mask1[2] = PermMask[2];
6369 Mask1[3] = PermMask[3];
6370 if (Mask1[2] >= 0)
6371 Mask1[2] += 4;
6372 if (Mask1[3] >= 0)
6373 Mask1[3] += 4;
6374 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006375 }
6376
6377 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006378 int LoMask[] = { -1, -1, -1, -1 };
6379 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006380
Benjamin Kramer9c683542012-01-30 15:16:21 +00006381 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006382 unsigned MaskIdx = 0;
6383 unsigned LoIdx = 0;
6384 unsigned HiIdx = 2;
6385 for (unsigned i = 0; i != 4; ++i) {
6386 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006387 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006388 MaskIdx = 1;
6389 LoIdx = 0;
6390 HiIdx = 2;
6391 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006392 int Idx = PermMask[i];
6393 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006394 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006395 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006396 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006397 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006398 LoIdx++;
6399 } else {
6400 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006401 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006402 HiIdx++;
6403 }
6404 }
6405
Nate Begeman9008ca62009-04-27 18:41:29 +00006406 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6407 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006408 int MaskOps[] = { -1, -1, -1, -1 };
6409 for (unsigned i = 0; i != 4; ++i)
6410 if (Locs[i].first != -1)
6411 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006412 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006413}
6414
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006415static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006416 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006417 V = V.getOperand(0);
6418 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6419 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006420 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6421 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6422 // BUILD_VECTOR (load), undef
6423 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006424 if (MayFoldLoad(V))
6425 return true;
6426 return false;
6427}
6428
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006429// FIXME: the version above should always be used. Since there's
6430// a bug where several vector shuffles can't be folded because the
6431// DAG is not updated during lowering and a node claims to have two
6432// uses while it only has one, use this version, and let isel match
6433// another instruction if the load really happens to have more than
6434// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006435// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006436static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006437 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006438 V = V.getOperand(0);
6439 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6440 V = V.getOperand(0);
6441 if (ISD::isNormalLoad(V.getNode()))
6442 return true;
6443 return false;
6444}
6445
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006446static
Evan Cheng835580f2010-10-07 20:50:20 +00006447SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6448 EVT VT = Op.getValueType();
6449
6450 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006451 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6452 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006453 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6454 V1, DAG));
6455}
6456
6457static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006458SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006459 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006460 SDValue V1 = Op.getOperand(0);
6461 SDValue V2 = Op.getOperand(1);
6462 EVT VT = Op.getValueType();
6463
6464 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6465
Craig Topper1accb7e2012-01-10 06:54:16 +00006466 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006467 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6468
Evan Cheng0899f5c2011-08-31 02:05:24 +00006469 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6470 return DAG.getNode(ISD::BITCAST, dl, VT,
6471 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6472 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6473 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006474}
6475
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006476static
6477SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6478 SDValue V1 = Op.getOperand(0);
6479 SDValue V2 = Op.getOperand(1);
6480 EVT VT = Op.getValueType();
6481
6482 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6483 "unsupported shuffle type");
6484
6485 if (V2.getOpcode() == ISD::UNDEF)
6486 V2 = V1;
6487
6488 // v4i32 or v4f32
6489 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6490}
6491
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006492static
Craig Topper1accb7e2012-01-10 06:54:16 +00006493SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006494 SDValue V1 = Op.getOperand(0);
6495 SDValue V2 = Op.getOperand(1);
6496 EVT VT = Op.getValueType();
6497 unsigned NumElems = VT.getVectorNumElements();
6498
6499 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6500 // operand of these instructions is only memory, so check if there's a
6501 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6502 // same masks.
6503 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006504
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006505 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006506 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006507 CanFoldLoad = true;
6508
6509 // When V1 is a load, it can be folded later into a store in isel, example:
6510 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6511 // turns into:
6512 // (MOVLPSmr addr:$src1, VR128:$src2)
6513 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006514 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006515 CanFoldLoad = true;
6516
Dan Gohman65fd6562011-11-03 21:49:52 +00006517 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006518 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006519 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006520 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6521
6522 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006523 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006524 if (SVOp->getMaskElt(1) != -1)
6525 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006526 }
6527
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006528 // movl and movlp will both match v2i64, but v2i64 is never matched by
6529 // movl earlier because we make it strict to avoid messing with the movlp load
6530 // folding logic (see the code above getMOVLP call). Match it here then,
6531 // this is horrible, but will stay like this until we move all shuffle
6532 // matching to x86 specific nodes. Note that for the 1st condition all
6533 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006534 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006535 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6536 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006537 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006538 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006539 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006540 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006541
6542 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6543
6544 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006545 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006546 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006547}
6548
Nadav Rotem154819d2012-04-09 07:45:58 +00006549SDValue
6550X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006551 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6552 EVT VT = Op.getValueType();
6553 DebugLoc dl = Op.getDebugLoc();
6554 SDValue V1 = Op.getOperand(0);
6555 SDValue V2 = Op.getOperand(1);
6556
6557 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006558 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006559
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006560 // Handle splat operations
6561 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006562 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006563 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006564
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006565 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006566 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006567 if (Broadcast.getNode())
6568 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006569
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006570 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006571 if ((Size == 128 && NumElem <= 4) ||
6572 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006573 return SDValue();
6574
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006575 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006576 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006577 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006578
6579 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6580 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006581 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6582 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006583 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6584 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006585 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006586 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006587 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006588 // FIXME: Figure out a cleaner way to do this.
6589 // Try to make use of movq to zero out the top part.
6590 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6591 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6592 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006593 EVT NewVT = NewOp.getValueType();
6594 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6595 NewVT, true, false))
6596 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006597 DAG, Subtarget, dl);
6598 }
6599 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6600 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006601 if (NewOp.getNode()) {
6602 EVT NewVT = NewOp.getValueType();
6603 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6604 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6605 DAG, Subtarget, dl);
6606 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006607 }
6608 }
6609 return SDValue();
6610}
6611
Dan Gohman475871a2008-07-27 21:46:04 +00006612SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006613X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006614 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue V1 = Op.getOperand(0);
6616 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006617 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006618 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006619 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006620 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006621 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006622 bool V1IsSplat = false;
6623 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006624 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006625 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006626 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006627 MachineFunction &MF = DAG.getMachineFunction();
6628 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006629
Craig Topper3426a3e2011-11-14 06:46:21 +00006630 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006631
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006632 if (V1IsUndef && V2IsUndef)
6633 return DAG.getUNDEF(VT);
6634
6635 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006636
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006637 // Vector shuffle lowering takes 3 steps:
6638 //
6639 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6640 // narrowing and commutation of operands should be handled.
6641 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6642 // shuffle nodes.
6643 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6644 // so the shuffle can be broken into other shuffles and the legalizer can
6645 // try the lowering again.
6646 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006647 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006648 // be matched during isel, all of them must be converted to a target specific
6649 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006650
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006651 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6652 // narrowing and commutation of operands should be handled. The actual code
6653 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006654 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006655 if (NewOp.getNode())
6656 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006657
Craig Topper5aaffa82012-02-19 02:53:47 +00006658 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6659
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006660 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6661 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006662 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006663 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006664 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006665 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006666
Craig Topperdd637ae2012-02-19 05:41:45 +00006667 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006668 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006669 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006670
Craig Topperdd637ae2012-02-19 05:41:45 +00006671 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006672 return getMOVHighToLow(Op, dl, DAG);
6673
6674 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006675 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006676 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006677 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006678
Craig Topper5aaffa82012-02-19 02:53:47 +00006679 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006680 // The actual implementation will match the mask in the if above and then
6681 // during isel it can match several different instructions, not only pshufd
6682 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006683 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6684 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006685
Craig Topper5aaffa82012-02-19 02:53:47 +00006686 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006687
Craig Topperdbd98a42012-02-07 06:28:42 +00006688 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6689 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6690
Craig Topper1accb7e2012-01-10 06:54:16 +00006691 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006692 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6693
Craig Topperb3982da2011-12-31 23:50:21 +00006694 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006695 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006696 }
Eric Christopherfd179292009-08-27 18:07:15 +00006697
Evan Chengf26ffe92008-05-29 08:22:04 +00006698 // Check if this can be converted into a logical shift.
6699 bool isLeft = false;
6700 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006701 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006702 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006703 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006704 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006705 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006706 EVT EltVT = VT.getVectorElementType();
6707 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006708 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006709 }
Eric Christopherfd179292009-08-27 18:07:15 +00006710
Craig Topper5aaffa82012-02-19 02:53:47 +00006711 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006712 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006713 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006714 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006715 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006716 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6717
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006718 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006719 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6720 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006721 }
Eric Christopherfd179292009-08-27 18:07:15 +00006722
Nate Begeman9008ca62009-04-27 18:41:29 +00006723 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006724 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006725 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006726
Craig Topperdd637ae2012-02-19 05:41:45 +00006727 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006728 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006729
Craig Topperdd637ae2012-02-19 05:41:45 +00006730 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006731 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006732
Craig Topperdd637ae2012-02-19 05:41:45 +00006733 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006734 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006735
Craig Topperdd637ae2012-02-19 05:41:45 +00006736 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006737 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006738
Craig Topperdd637ae2012-02-19 05:41:45 +00006739 if (ShouldXformToMOVHLPS(M, VT) ||
6740 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006741 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006742
Evan Chengf26ffe92008-05-29 08:22:04 +00006743 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006744 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006745 EVT EltVT = VT.getVectorElementType();
6746 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006747 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006748 }
Eric Christopherfd179292009-08-27 18:07:15 +00006749
Evan Cheng9eca5e82006-10-25 21:49:50 +00006750 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006751 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6752 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006753 V1IsSplat = isSplatVector(V1.getNode());
6754 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006755
Chris Lattner8a594482007-11-25 00:24:49 +00006756 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006757 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6758 CommuteVectorShuffleMask(M, NumElems);
6759 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006760 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006761 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006762 }
6763
Craig Topperbeabc6c2011-12-05 06:56:46 +00006764 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006765 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006766 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006767 return V1;
6768 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6769 // the instruction selector will not match, so get a canonical MOVL with
6770 // swapped operands to undo the commute.
6771 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006772 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006773
Craig Topperbeabc6c2011-12-05 06:56:46 +00006774 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006775 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006776
Craig Topperbeabc6c2011-12-05 06:56:46 +00006777 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006778 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006779
Evan Cheng9bbbb982006-10-25 20:48:19 +00006780 if (V2IsSplat) {
6781 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006782 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006783 // new vector_shuffle with the corrected mask.p
6784 SmallVector<int, 8> NewMask(M.begin(), M.end());
6785 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006786 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006787 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006788 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006789 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006790 }
6791
Evan Cheng9eca5e82006-10-25 21:49:50 +00006792 if (Commuted) {
6793 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006794 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006795 CommuteVectorShuffleMask(M, NumElems);
6796 std::swap(V1, V2);
6797 std::swap(V1IsSplat, V2IsSplat);
6798 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006799
Craig Topper39a9e482012-02-11 06:24:48 +00006800 if (isUNPCKLMask(M, VT, HasAVX2))
6801 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006802
Craig Topper39a9e482012-02-11 06:24:48 +00006803 if (isUNPCKHMask(M, VT, HasAVX2))
6804 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006805 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006806
Nate Begeman9008ca62009-04-27 18:41:29 +00006807 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006808 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006809 return CommuteVectorShuffle(SVOp, DAG);
6810
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006811 // The checks below are all present in isShuffleMaskLegal, but they are
6812 // inlined here right now to enable us to directly emit target specific
6813 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006814
Craig Topper0e2037b2012-01-20 05:53:00 +00006815 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006816 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006817 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006818 DAG);
6819
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006820 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6821 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006822 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006823 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006824 }
6825
Craig Toppera9a568a2012-05-02 08:03:44 +00006826 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006827 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006828 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006829 DAG);
6830
Craig Toppera9a568a2012-05-02 08:03:44 +00006831 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006832 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006833 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006834 DAG);
6835
Craig Topper1a7700a2012-01-19 08:19:12 +00006836 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006837 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006838 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006839
Craig Topper94438ba2011-12-16 08:06:31 +00006840 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006841 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006842 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006843 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006844
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006845 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006846 // Generate target specific nodes for 128 or 256-bit shuffles only
6847 // supported in the AVX instruction set.
6848 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006849
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006850 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006851 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006852 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6853
Craig Topper70b883b2011-11-28 10:14:51 +00006854 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006855 if (isVPERMILPMask(M, VT, HasAVX)) {
6856 if (HasAVX2 && VT == MVT::v8i32)
6857 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006858 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006859 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006860 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006861 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006862
Craig Topper70b883b2011-11-28 10:14:51 +00006863 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006864 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006865 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006866 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006867
Craig Topper1842ba02012-04-23 06:38:28 +00006868 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006869 if (BlendOp.getNode())
6870 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006871
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006872 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006873 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006874 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006875 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006876 }
Craig Topper92040742012-04-16 06:43:40 +00006877 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6878 &permclMask[0], 8);
6879 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006880 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006881 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006882 }
Craig Topper095c5282012-04-15 23:48:57 +00006883
Craig Topper8325c112012-04-16 00:41:45 +00006884 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6885 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006886 getShuffleCLImmediate(SVOp), DAG);
6887
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006888
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006889 //===--------------------------------------------------------------------===//
6890 // Since no target specific shuffle was selected for this generic one,
6891 // lower it into other known shuffles. FIXME: this isn't true yet, but
6892 // this is the plan.
6893 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006894
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006895 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6896 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006897 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006898 if (NewOp.getNode())
6899 return NewOp;
6900 }
6901
6902 if (VT == MVT::v16i8) {
6903 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6904 if (NewOp.getNode())
6905 return NewOp;
6906 }
6907
Elena Demikhovsky41789462012-09-06 12:42:01 +00006908 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006909 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006910 if (NewOp.getNode())
6911 return NewOp;
6912 }
6913
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006914 // Handle all 128-bit wide vectors with 4 elements, and match them with
6915 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006916 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006917 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6918
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006919 // Handle general 256-bit shuffles
6920 if (VT.is256BitVector())
6921 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6922
Dan Gohman475871a2008-07-27 21:46:04 +00006923 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006924}
6925
Dan Gohman475871a2008-07-27 21:46:04 +00006926SDValue
6927X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006928 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006929 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006930 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006931
Craig Topper7a9a28b2012-08-12 02:23:29 +00006932 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006933 return SDValue();
6934
Duncan Sands83ec4b62008-06-06 12:08:01 +00006935 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006936 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006937 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006939 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006940 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006941 }
6942
6943 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006944 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6945 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6946 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6948 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006949 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006950 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006951 Op.getOperand(0)),
6952 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006953 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006954 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006956 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006957 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006958 }
6959
6960 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006961 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6962 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006963 // result has a single use which is a store or a bitcast to i32. And in
6964 // the case of a store, it's not worth it if the index is a constant 0,
6965 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006966 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006967 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006968 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006969 if ((User->getOpcode() != ISD::STORE ||
6970 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6971 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006972 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006973 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006974 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006975 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006976 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006977 Op.getOperand(0)),
6978 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006979 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006980 }
6981
6982 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006983 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006984 if (isa<ConstantSDNode>(Op.getOperand(1)))
6985 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006986 }
Dan Gohman475871a2008-07-27 21:46:04 +00006987 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006988}
6989
6990
Dan Gohman475871a2008-07-27 21:46:04 +00006991SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006992X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6993 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006994 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006995 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006996
David Greene74a579d2011-02-10 16:57:36 +00006997 SDValue Vec = Op.getOperand(0);
6998 EVT VecVT = Vec.getValueType();
6999
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007000 // If this is a 256-bit vector result, first extract the 128-bit vector and
7001 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007002 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007003 DebugLoc dl = Op.getNode()->getDebugLoc();
7004 unsigned NumElems = VecVT.getVectorNumElements();
7005 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007006 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7007
7008 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007009 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007010
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007011 if (IdxVal >= NumElems/2)
7012 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007013 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007014 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007015 }
7016
Craig Topper7a9a28b2012-08-12 02:23:29 +00007017 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007018
Craig Topperd0a31172012-01-10 06:37:29 +00007019 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007020 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007021 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007022 return Res;
7023 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007024
Owen Andersone50ed302009-08-10 22:56:29 +00007025 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007026 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007028 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007029 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007030 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007031 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007032 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7033 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007034 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007035 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007036 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007037 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007038 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007039 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007040 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007041 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007042 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007043 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007044 }
7045
7046 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007047 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007048 if (Idx == 0)
7049 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007050
Evan Cheng0db9fe62006-04-25 20:13:52 +00007051 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007052 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007053 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007054 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007055 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007056 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007057 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007058 }
7059
7060 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007061 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7062 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7063 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007064 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007065 if (Idx == 0)
7066 return Op;
7067
7068 // UNPCKHPD the element to the lowest double word, then movsd.
7069 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7070 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007071 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007072 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007073 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007074 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007075 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007076 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007077 }
7078
Dan Gohman475871a2008-07-27 21:46:04 +00007079 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007080}
7081
Dan Gohman475871a2008-07-27 21:46:04 +00007082SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007083X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7084 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007085 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007086 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007087 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007088
Dan Gohman475871a2008-07-27 21:46:04 +00007089 SDValue N0 = Op.getOperand(0);
7090 SDValue N1 = Op.getOperand(1);
7091 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007092
Craig Topper7a9a28b2012-08-12 02:23:29 +00007093 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007094 return SDValue();
7095
Dan Gohman8a55ce42009-09-23 21:02:20 +00007096 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007097 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007098 unsigned Opc;
7099 if (VT == MVT::v8i16)
7100 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007101 else if (VT == MVT::v16i8)
7102 Opc = X86ISD::PINSRB;
7103 else
7104 Opc = X86ISD::PINSRB;
7105
Nate Begeman14d12ca2008-02-11 04:19:36 +00007106 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7107 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 if (N1.getValueType() != MVT::i32)
7109 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7110 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007111 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007112 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007113 }
7114
7115 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007116 // Bits [7:6] of the constant are the source select. This will always be
7117 // zero here. The DAG Combiner may combine an extract_elt index into these
7118 // bits. For example (insert (extract, 3), 2) could be matched by putting
7119 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007120 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007121 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007122 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007123 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007124 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007125 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007126 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007127 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007128 }
7129
7130 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007131 // PINSR* works with constant index.
7132 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007133 }
Dan Gohman475871a2008-07-27 21:46:04 +00007134 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007135}
7136
Dan Gohman475871a2008-07-27 21:46:04 +00007137SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007138X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007139 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007140 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007141
David Greene6b381262011-02-09 15:32:06 +00007142 DebugLoc dl = Op.getDebugLoc();
7143 SDValue N0 = Op.getOperand(0);
7144 SDValue N1 = Op.getOperand(1);
7145 SDValue N2 = Op.getOperand(2);
7146
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007147 // If this is a 256-bit vector result, first extract the 128-bit vector,
7148 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007149 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007150 if (!isa<ConstantSDNode>(N2))
7151 return SDValue();
7152
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007153 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007154 unsigned NumElems = VT.getVectorNumElements();
7155 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007156 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007157
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007158 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007159 bool Upper = IdxVal >= NumElems/2;
7160 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7161 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007162
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007163 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007164 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007165 }
7166
Craig Topperd0a31172012-01-10 06:37:29 +00007167 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007168 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7169
Dan Gohman8a55ce42009-09-23 21:02:20 +00007170 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007171 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007172
Dan Gohman8a55ce42009-09-23 21:02:20 +00007173 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007174 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7175 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007176 if (N1.getValueType() != MVT::i32)
7177 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7178 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007179 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007180 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007181 }
Dan Gohman475871a2008-07-27 21:46:04 +00007182 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007183}
7184
Craig Topper55b24052012-09-11 06:15:32 +00007185static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007186 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007187 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007188 EVT OpVT = Op.getValueType();
7189
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007190 // If this is a 256-bit vector result, first insert into a 128-bit
7191 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007192 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007193 // Insert into a 128-bit vector.
7194 EVT VT128 = EVT::getVectorVT(*Context,
7195 OpVT.getVectorElementType(),
7196 OpVT.getVectorNumElements() / 2);
7197
7198 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7199
7200 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007201 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007202 }
7203
Craig Topperd77d2fe2012-04-29 20:22:05 +00007204 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007205 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007207
Owen Anderson825b72b2009-08-11 20:47:22 +00007208 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007209 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007210 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007211 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007212}
7213
David Greene91585092011-01-26 15:38:49 +00007214// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7215// a simple subregister reference or explicit instructions to grab
7216// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007217static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7218 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007219 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007220 DebugLoc dl = Op.getNode()->getDebugLoc();
7221 SDValue Vec = Op.getNode()->getOperand(0);
7222 SDValue Idx = Op.getNode()->getOperand(1);
7223
Craig Topper7a9a28b2012-08-12 02:23:29 +00007224 if (Op.getNode()->getValueType(0).is128BitVector() &&
7225 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007226 isa<ConstantSDNode>(Idx)) {
7227 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7228 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007229 }
David Greene91585092011-01-26 15:38:49 +00007230 }
7231 return SDValue();
7232}
7233
David Greenecfe33c42011-01-26 19:13:22 +00007234// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7235// simple superregister reference or explicit instructions to insert
7236// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007237static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7238 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007239 if (Subtarget->hasAVX()) {
7240 DebugLoc dl = Op.getNode()->getDebugLoc();
7241 SDValue Vec = Op.getNode()->getOperand(0);
7242 SDValue SubVec = Op.getNode()->getOperand(1);
7243 SDValue Idx = Op.getNode()->getOperand(2);
7244
Craig Topper7a9a28b2012-08-12 02:23:29 +00007245 if (Op.getNode()->getValueType(0).is256BitVector() &&
7246 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007247 isa<ConstantSDNode>(Idx)) {
7248 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7249 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007250 }
7251 }
7252 return SDValue();
7253}
7254
Bill Wendling056292f2008-09-16 21:48:12 +00007255// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7256// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7257// one of the above mentioned nodes. It has to be wrapped because otherwise
7258// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7259// be used to form addressing mode. These wrapped nodes will be selected
7260// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007261SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007262X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007263 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007264
Chris Lattner41621a22009-06-26 19:22:52 +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;
Chris Lattner18c59872009-06-27 04:16:01 +00007268 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
Evan Cheng1606e8e2009-03-13 07:51:59 +00007279 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007280 CP->getAlignment(),
7281 CP->getOffset(), OpFlag);
7282 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007283 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007284 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007285 if (OpFlag) {
7286 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007287 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007288 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007289 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007290 }
7291
7292 return Result;
7293}
7294
Dan Gohmand858e902010-04-17 15:26:15 +00007295SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007296 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
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() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007305 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007306 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007307 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007308 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007309 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007310 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007311
Chris Lattner18c59872009-06-27 04:16:01 +00007312 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7313 OpFlag);
7314 DebugLoc DL = JT->getDebugLoc();
7315 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007316
Chris Lattner18c59872009-06-27 04:16:01 +00007317 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007318 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007319 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7320 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007321 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007322 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007323
Chris Lattner18c59872009-06-27 04:16:01 +00007324 return Result;
7325}
7326
7327SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007328X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007329 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007330
Chris Lattner18c59872009-06-27 04:16:01 +00007331 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7332 // global base reg.
7333 unsigned char OpFlag = 0;
7334 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007335 CodeModel::Model M = getTargetMachine().getCodeModel();
7336
Chris Lattner4f066492009-07-11 20:29:19 +00007337 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007338 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7339 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7340 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007341 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007342 } else if (Subtarget->isPICStyleGOT()) {
7343 OpFlag = X86II::MO_GOT;
7344 } else if (Subtarget->isPICStyleStubPIC()) {
7345 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7346 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7347 OpFlag = X86II::MO_DARWIN_NONLAZY;
7348 }
Eric Christopherfd179292009-08-27 18:07:15 +00007349
Chris Lattner18c59872009-06-27 04:16:01 +00007350 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007351
Chris Lattner18c59872009-06-27 04:16:01 +00007352 DebugLoc DL = Op.getDebugLoc();
7353 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007354
7355
Chris Lattner18c59872009-06-27 04:16:01 +00007356 // With PIC, the address is actually $g + Offset.
7357 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007358 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007359 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7360 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007361 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007362 Result);
7363 }
Eric Christopherfd179292009-08-27 18:07:15 +00007364
Eli Friedman586272d2011-08-11 01:48:05 +00007365 // For symbols that require a load from a stub to get the address, emit the
7366 // load.
7367 if (isGlobalStubReference(OpFlag))
7368 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007369 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007370
Chris Lattner18c59872009-06-27 04:16:01 +00007371 return Result;
7372}
7373
Dan Gohman475871a2008-07-27 21:46:04 +00007374SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007375X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007376 // Create the TargetBlockAddressAddress node.
7377 unsigned char OpFlags =
7378 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007379 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007380 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007381 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007382 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007383 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7384 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007385
Dan Gohmanf705adb2009-10-30 01:28:02 +00007386 if (Subtarget->isPICStyleRIPRel() &&
7387 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007388 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7389 else
7390 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007391
Dan Gohman29cbade2009-11-20 23:18:13 +00007392 // With PIC, the address is actually $g + Offset.
7393 if (isGlobalRelativeToPICBase(OpFlags)) {
7394 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7395 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7396 Result);
7397 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007398
7399 return Result;
7400}
7401
7402SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007403X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007404 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007405 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007406 // Create the TargetGlobalAddress node, folding in the constant
7407 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007408 unsigned char OpFlags =
7409 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007410 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007411 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007412 if (OpFlags == X86II::MO_NO_FLAG &&
7413 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007414 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007415 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007416 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007417 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007418 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007419 }
Eric Christopherfd179292009-08-27 18:07:15 +00007420
Chris Lattner4f066492009-07-11 20:29:19 +00007421 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007422 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007423 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7424 else
7425 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007426
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007427 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007428 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007429 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7430 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007431 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007433
Chris Lattner36c25012009-07-10 07:34:39 +00007434 // For globals that require a load from a stub to get the address, emit the
7435 // load.
7436 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007437 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007438 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007439
Dan Gohman6520e202008-10-18 02:06:02 +00007440 // If there was a non-zero offset that we didn't fold, create an explicit
7441 // addition for it.
7442 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007443 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007444 DAG.getConstant(Offset, getPointerTy()));
7445
Evan Cheng0db9fe62006-04-25 20:13:52 +00007446 return Result;
7447}
7448
Evan Chengda43bcf2008-09-24 00:05:32 +00007449SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007450X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007451 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007452 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007453 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007454}
7455
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007456static SDValue
7457GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007458 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007459 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007460 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007461 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007462 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007463 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007464 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007465 GA->getOffset(),
7466 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007467
7468 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7469 : X86ISD::TLSADDR;
7470
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007471 if (InFlag) {
7472 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007473 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007474 } else {
7475 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007476 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007477 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007478
7479 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007480 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007481
Rafael Espindola15f1b662009-04-24 12:59:40 +00007482 SDValue Flag = Chain.getValue(1);
7483 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007484}
7485
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007486// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007487static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007488LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007489 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007490 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007491 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7492 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007493 DAG.getNode(X86ISD::GlobalBaseReg,
7494 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007495 InFlag = Chain.getValue(1);
7496
Chris Lattnerb903bed2009-06-26 21:20:29 +00007497 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007498}
7499
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007500// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007501static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007502LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007503 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007504 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7505 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007506}
7507
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007508static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7509 SelectionDAG &DAG,
7510 const EVT PtrVT,
7511 bool is64Bit) {
7512 DebugLoc dl = GA->getDebugLoc();
7513
7514 // Get the start address of the TLS block for this module.
7515 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7516 .getInfo<X86MachineFunctionInfo>();
7517 MFI->incNumLocalDynamicTLSAccesses();
7518
7519 SDValue Base;
7520 if (is64Bit) {
7521 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7522 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7523 } else {
7524 SDValue InFlag;
7525 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7526 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7527 InFlag = Chain.getValue(1);
7528 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7529 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7530 }
7531
7532 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7533 // of Base.
7534
7535 // Build x@dtpoff.
7536 unsigned char OperandFlags = X86II::MO_DTPOFF;
7537 unsigned WrapperKind = X86ISD::Wrapper;
7538 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7539 GA->getValueType(0),
7540 GA->getOffset(), OperandFlags);
7541 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7542
7543 // Add x@dtpoff with the base.
7544 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7545}
7546
Hans Wennborg228756c2012-05-11 10:11:01 +00007547// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007548static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007549 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007550 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007551 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007552
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007553 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7554 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7555 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007556
Michael J. Spencerec38de22010-10-10 22:04:20 +00007557 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007558 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007559 MachinePointerInfo(Ptr),
7560 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007561
Chris Lattnerb903bed2009-06-26 21:20:29 +00007562 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007563 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7564 // initialexec.
7565 unsigned WrapperKind = X86ISD::Wrapper;
7566 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007567 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007568 } else if (model == TLSModel::InitialExec) {
7569 if (is64Bit) {
7570 OperandFlags = X86II::MO_GOTTPOFF;
7571 WrapperKind = X86ISD::WrapperRIP;
7572 } else {
7573 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7574 }
Chris Lattner18c59872009-06-27 04:16:01 +00007575 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007576 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007577 }
Eric Christopherfd179292009-08-27 18:07:15 +00007578
Hans Wennborg228756c2012-05-11 10:11:01 +00007579 // emit "addl x@ntpoff,%eax" (local exec)
7580 // or "addl x@indntpoff,%eax" (initial exec)
7581 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007582 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007583 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007584 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007585 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007586
Hans Wennborg228756c2012-05-11 10:11:01 +00007587 if (model == TLSModel::InitialExec) {
7588 if (isPIC && !is64Bit) {
7589 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7590 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7591 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007592 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007593
7594 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7595 MachinePointerInfo::getGOT(), false, false, false,
7596 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007597 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007598
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007599 // The address of the thread local variable is the add of the thread
7600 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007601 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007602}
7603
Dan Gohman475871a2008-07-27 21:46:04 +00007604SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007605X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007606
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007607 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007608 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007609
Eric Christopher30ef0e52010-06-03 04:07:48 +00007610 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007611 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007612
Eric Christopher30ef0e52010-06-03 04:07:48 +00007613 switch (model) {
7614 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007615 if (Subtarget->is64Bit())
7616 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7617 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007618 case TLSModel::LocalDynamic:
7619 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7620 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007621 case TLSModel::InitialExec:
7622 case TLSModel::LocalExec:
7623 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007624 Subtarget->is64Bit(),
7625 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007626 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007627 llvm_unreachable("Unknown TLS model.");
7628 }
7629
7630 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007631 // Darwin only has one model of TLS. Lower to that.
7632 unsigned char OpFlag = 0;
7633 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7634 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007635
Eric Christopher30ef0e52010-06-03 04:07:48 +00007636 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7637 // global base reg.
7638 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7639 !Subtarget->is64Bit();
7640 if (PIC32)
7641 OpFlag = X86II::MO_TLVP_PIC_BASE;
7642 else
7643 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007644 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007645 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007646 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007647 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007648 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007649
Eric Christopher30ef0e52010-06-03 04:07:48 +00007650 // With PIC32, the address is actually $g + Offset.
7651 if (PIC32)
7652 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7653 DAG.getNode(X86ISD::GlobalBaseReg,
7654 DebugLoc(), getPointerTy()),
7655 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007656
Eric Christopher30ef0e52010-06-03 04:07:48 +00007657 // Lowering the machine isd will make sure everything is in the right
7658 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007659 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007660 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007661 SDValue Args[] = { Chain, Offset };
7662 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007663
Eric Christopher30ef0e52010-06-03 04:07:48 +00007664 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7665 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7666 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007667
Eric Christopher30ef0e52010-06-03 04:07:48 +00007668 // And our return value (tls address) is in the standard call return value
7669 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007670 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007671 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7672 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007673 }
7674
7675 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007676 // Just use the implicit TLS architecture
7677 // Need to generate someting similar to:
7678 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7679 // ; from TEB
7680 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7681 // mov rcx, qword [rdx+rcx*8]
7682 // mov eax, .tls$:tlsvar
7683 // [rax+rcx] contains the address
7684 // Windows 64bit: gs:0x58
7685 // Windows 32bit: fs:__tls_array
7686
7687 // If GV is an alias then use the aliasee for determining
7688 // thread-localness.
7689 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7690 GV = GA->resolveAliasedGlobal(false);
7691 DebugLoc dl = GA->getDebugLoc();
7692 SDValue Chain = DAG.getEntryNode();
7693
7694 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7695 // %gs:0x58 (64-bit).
7696 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7697 ? Type::getInt8PtrTy(*DAG.getContext(),
7698 256)
7699 : Type::getInt32PtrTy(*DAG.getContext(),
7700 257));
7701
7702 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7703 Subtarget->is64Bit()
7704 ? DAG.getIntPtrConstant(0x58)
7705 : DAG.getExternalSymbol("_tls_array",
7706 getPointerTy()),
7707 MachinePointerInfo(Ptr),
7708 false, false, false, 0);
7709
7710 // Load the _tls_index variable
7711 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7712 if (Subtarget->is64Bit())
7713 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7714 IDX, MachinePointerInfo(), MVT::i32,
7715 false, false, 0);
7716 else
7717 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7718 false, false, false, 0);
7719
7720 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007721 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007722 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7723
7724 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7725 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7726 false, false, false, 0);
7727
7728 // Get the offset of start of .tls section
7729 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7730 GA->getValueType(0),
7731 GA->getOffset(), X86II::MO_SECREL);
7732 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7733
7734 // The address of the thread local variable is the add of the thread
7735 // pointer with the offset of the variable.
7736 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007737 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007738
David Blaikie4d6ccb52012-01-20 21:51:11 +00007739 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007740}
7741
Evan Cheng0db9fe62006-04-25 20:13:52 +00007742
Chad Rosierb90d2a92012-01-03 23:19:12 +00007743/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7744/// and take a 2 x i32 value to shift plus a shift amount.
7745SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007746 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007747 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007748 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007749 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007750 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007751 SDValue ShOpLo = Op.getOperand(0);
7752 SDValue ShOpHi = Op.getOperand(1);
7753 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007754 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007755 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007756 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007757
Dan Gohman475871a2008-07-27 21:46:04 +00007758 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007759 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007760 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7761 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007762 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007763 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7764 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007765 }
Evan Chenge3413162006-01-09 18:33:28 +00007766
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7768 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007769 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007771
Dan Gohman475871a2008-07-27 21:46:04 +00007772 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007774 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7775 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007776
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007777 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007778 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7779 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007780 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007781 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7782 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007783 }
7784
Dan Gohman475871a2008-07-27 21:46:04 +00007785 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007786 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007787}
Evan Chenga3195e82006-01-12 22:54:21 +00007788
Dan Gohmand858e902010-04-17 15:26:15 +00007789SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7790 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007791 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007792
Dale Johannesen0488fb62010-09-30 23:57:10 +00007793 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007794 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007795
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007797 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007798
Eli Friedman36df4992009-05-27 00:47:34 +00007799 // These are really Legal; return the operand so the caller accepts it as
7800 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007801 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007802 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007803 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007804 Subtarget->is64Bit()) {
7805 return Op;
7806 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007807
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007808 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007809 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007810 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007811 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007812 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007813 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007814 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007815 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007816 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007817 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7818}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007819
Owen Andersone50ed302009-08-10 22:56:29 +00007820SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007821 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007822 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007823 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007824 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007825 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007826 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007827 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007828 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007829 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007830 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007831
Chris Lattner492a43e2010-09-22 01:28:21 +00007832 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007833
Stuart Hastings84be9582011-06-02 15:57:11 +00007834 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7835 MachineMemOperand *MMO;
7836 if (FI) {
7837 int SSFI = FI->getIndex();
7838 MMO =
7839 DAG.getMachineFunction()
7840 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7841 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7842 } else {
7843 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7844 StackSlot = StackSlot.getOperand(1);
7845 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007846 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007847 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7848 X86ISD::FILD, DL,
7849 Tys, Ops, array_lengthof(Ops),
7850 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007851
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007852 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007853 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007854 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007855
7856 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7857 // shouldn't be necessary except that RFP cannot be live across
7858 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007859 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007860 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7861 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007862 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007863 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007864 SDValue Ops[] = {
7865 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7866 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007867 MachineMemOperand *MMO =
7868 DAG.getMachineFunction()
7869 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007870 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007871
Chris Lattner492a43e2010-09-22 01:28:21 +00007872 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7873 Ops, array_lengthof(Ops),
7874 Op.getValueType(), MMO);
7875 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007876 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007877 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007878 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007879
Evan Cheng0db9fe62006-04-25 20:13:52 +00007880 return Result;
7881}
7882
Bill Wendling8b8a6362009-01-17 03:56:04 +00007883// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007884SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7885 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007886 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007887 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007888 movq %rax, %xmm0
7889 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7890 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7891 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007892 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007893 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007894 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007895 addpd %xmm1, %xmm0
7896 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007897 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007898
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007899 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007900 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007901
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007902 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007903 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7904 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007905 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007906
Chris Lattner97484792012-01-25 09:56:22 +00007907 SmallVector<Constant*,2> CV1;
7908 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007909 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007910 CV1.push_back(
7911 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7912 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007913 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007914
Bill Wendling397ae212012-01-05 02:13:20 +00007915 // Load the 64-bit value into an XMM register.
7916 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7917 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007918 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007919 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007920 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007921 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7922 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7923 CLod0);
7924
Owen Anderson825b72b2009-08-11 20:47:22 +00007925 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007926 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007927 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007928 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007930 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007931
Craig Topperd0a31172012-01-10 06:37:29 +00007932 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007933 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7934 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7935 } else {
7936 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7937 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7938 S2F, 0x4E, DAG);
7939 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7940 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7941 Sub);
7942 }
7943
7944 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007945 DAG.getIntPtrConstant(0));
7946}
7947
Bill Wendling8b8a6362009-01-17 03:56:04 +00007948// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007949SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7950 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007951 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007952 // FP constant to bias correct the final result.
7953 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007954 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007955
7956 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007958 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007959
Eli Friedmanf3704762011-08-29 21:15:46 +00007960 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007961 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007962
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007964 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007965 DAG.getIntPtrConstant(0));
7966
7967 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007968 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007969 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007970 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007971 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007972 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007973 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007974 MVT::v2f64, Bias)));
7975 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007976 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007977 DAG.getIntPtrConstant(0));
7978
7979 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007981
7982 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007983 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007984
Craig Topper69947b92012-04-23 06:57:04 +00007985 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007986 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007987 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007988 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007989 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007990
7991 // Handle final rounding.
7992 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007993}
7994
Dan Gohmand858e902010-04-17 15:26:15 +00007995SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7996 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007997 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007998 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007999
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008000 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008001 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8002 // the optimization here.
8003 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008004 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008005
Owen Andersone50ed302009-08-10 22:56:29 +00008006 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008007 EVT DstVT = Op.getValueType();
8008 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008009 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008010 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008011 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008012 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008013 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008014
8015 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008016 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008017 if (SrcVT == MVT::i32) {
8018 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8019 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8020 getPointerTy(), StackSlot, WordOff);
8021 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008022 StackSlot, MachinePointerInfo(),
8023 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008024 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008025 OffsetSlot, MachinePointerInfo(),
8026 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008027 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8028 return Fild;
8029 }
8030
8031 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8032 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008033 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008034 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008035 // For i64 source, we need to add the appropriate power of 2 if the input
8036 // was negative. This is the same as the optimization in
8037 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8038 // we must be careful to do the computation in x87 extended precision, not
8039 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008040 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8041 MachineMemOperand *MMO =
8042 DAG.getMachineFunction()
8043 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8044 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008045
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008046 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8047 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008048 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8049 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008050
8051 APInt FF(32, 0x5F800000ULL);
8052
8053 // Check whether the sign bit is set.
8054 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8055 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8056 ISD::SETLT);
8057
8058 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8059 SDValue FudgePtr = DAG.getConstantPool(
8060 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8061 getPointerTy());
8062
8063 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8064 SDValue Zero = DAG.getIntPtrConstant(0);
8065 SDValue Four = DAG.getIntPtrConstant(4);
8066 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8067 Zero, Four);
8068 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8069
8070 // Load the value out, extending it from f32 to f80.
8071 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008072 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008073 FudgePtr, MachinePointerInfo::getConstantPool(),
8074 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008075 // Extend everything to 80 bits to force it to be done on x87.
8076 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8077 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008078}
8079
Dan Gohman475871a2008-07-27 21:46:04 +00008080std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008081FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008082 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008083
Owen Andersone50ed302009-08-10 22:56:29 +00008084 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008085
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008086 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008087 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8088 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008089 }
8090
Owen Anderson825b72b2009-08-11 20:47:22 +00008091 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8092 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008093 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008094
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008095 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008096 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008097 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008098 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008099 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008100 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008101 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008102 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008103
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008104 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8105 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008106 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008107 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008108 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008109 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008110
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008112 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8113 Opc = X86ISD::WIN_FTOL;
8114 else
8115 switch (DstTy.getSimpleVT().SimpleTy) {
8116 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8117 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8118 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8119 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8120 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008121
Dan Gohman475871a2008-07-27 21:46:04 +00008122 SDValue Chain = DAG.getEntryNode();
8123 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008124 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008125 // FIXME This causes a redundant load/store if the SSE-class value is already
8126 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008127 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008128 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008129 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008130 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008131 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008132 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008133 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008134 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008135 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008136
Chris Lattner492a43e2010-09-22 01:28:21 +00008137 MachineMemOperand *MMO =
8138 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8139 MachineMemOperand::MOLoad, MemSize, MemSize);
8140 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8141 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008142 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008143 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008144 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8145 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008146
Chris Lattner07290932010-09-22 01:05:16 +00008147 MachineMemOperand *MMO =
8148 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8149 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008150
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008151 if (Opc != X86ISD::WIN_FTOL) {
8152 // Build the FP_TO_INT*_IN_MEM
8153 SDValue Ops[] = { Chain, Value, StackSlot };
8154 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8155 Ops, 3, DstTy, MMO);
8156 return std::make_pair(FIST, StackSlot);
8157 } else {
8158 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8159 DAG.getVTList(MVT::Other, MVT::Glue),
8160 Chain, Value);
8161 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8162 MVT::i32, ftol.getValue(1));
8163 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8164 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008165 SDValue Ops[] = { eax, edx };
8166 SDValue pair = IsReplace
8167 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8168 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008169 return std::make_pair(pair, SDValue());
8170 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008171}
8172
Dan Gohmand858e902010-04-17 15:26:15 +00008173SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8174 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008175 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008176 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008177
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008178 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8179 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008180 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008181 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8182 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008183
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008184 if (StackSlot.getNode())
8185 // Load the result.
8186 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8187 FIST, StackSlot, MachinePointerInfo(),
8188 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008189
8190 // The node is the result.
8191 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008192}
8193
Dan Gohmand858e902010-04-17 15:26:15 +00008194SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8195 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008196 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8197 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008198 SDValue FIST = Vals.first, StackSlot = Vals.second;
8199 assert(FIST.getNode() && "Unexpected failure");
8200
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008201 if (StackSlot.getNode())
8202 // Load the result.
8203 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8204 FIST, StackSlot, MachinePointerInfo(),
8205 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008206
8207 // The node is the result.
8208 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008209}
8210
Craig Topper43620672012-09-08 07:31:51 +00008211SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008212 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008213 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008214 EVT VT = Op.getValueType();
8215 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008216 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8217 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008218 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008219 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008220 }
Craig Topper43620672012-09-08 07:31:51 +00008221 Constant *C;
8222 if (EltVT == MVT::f64)
8223 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8224 else
8225 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8226 C = ConstantVector::getSplat(NumElts, C);
8227 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8228 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008229 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008230 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008231 false, false, false, Alignment);
8232 if (VT.isVector()) {
8233 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8234 return DAG.getNode(ISD::BITCAST, dl, VT,
8235 DAG.getNode(ISD::AND, dl, ANDVT,
8236 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8237 Op.getOperand(0)),
8238 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8239 }
Dale Johannesenace16102009-02-03 19:33:06 +00008240 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008241}
8242
Dan Gohmand858e902010-04-17 15:26:15 +00008243SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008244 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008245 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008246 EVT VT = Op.getValueType();
8247 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008248 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8249 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008250 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008251 NumElts = VT.getVectorNumElements();
8252 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008253 Constant *C;
8254 if (EltVT == MVT::f64)
8255 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8256 else
8257 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8258 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008259 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8260 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008261 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008262 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008263 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008264 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008265 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008266 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008267 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008268 DAG.getNode(ISD::BITCAST, dl, XORVT,
8269 Op.getOperand(0)),
8270 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008271 }
Craig Topper69947b92012-04-23 06:57:04 +00008272
8273 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008274}
8275
Dan Gohmand858e902010-04-17 15:26:15 +00008276SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008277 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008278 SDValue Op0 = Op.getOperand(0);
8279 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008280 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008281 EVT VT = Op.getValueType();
8282 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008283
8284 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008285 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008286 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008287 SrcVT = VT;
8288 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008289 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008290 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008291 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008292 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008293 }
8294
8295 // At this point the operands and the result should have the same
8296 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008297
Evan Cheng68c47cb2007-01-05 07:55:56 +00008298 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008299 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008300 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008301 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8302 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008303 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008304 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8305 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8306 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8307 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008308 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008309 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008310 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008311 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008312 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008313 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008314 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008315
8316 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008317 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008318 // Op0 is MVT::f32, Op1 is MVT::f64.
8319 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8320 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8321 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008322 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008323 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008324 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008325 }
8326
Evan Cheng73d6cf12007-01-05 21:37:56 +00008327 // Clear first operand sign bit.
8328 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008329 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008330 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8331 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008332 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008333 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8334 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8335 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8336 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008337 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008338 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008339 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008340 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008341 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008342 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008343 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008344
8345 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008346 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008347}
8348
Craig Topper55b24052012-09-11 06:15:32 +00008349static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008350 SDValue N0 = Op.getOperand(0);
8351 DebugLoc dl = Op.getDebugLoc();
8352 EVT VT = Op.getValueType();
8353
8354 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8355 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8356 DAG.getConstant(1, VT));
8357 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8358}
8359
Michael Liaof966e4e2012-09-13 20:24:54 +00008360// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8361//
8362SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8363 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8364
8365 if (!Subtarget->hasSSE41())
8366 return SDValue();
8367
8368 if (!Op->hasOneUse())
8369 return SDValue();
8370
8371 SDNode *N = Op.getNode();
8372 DebugLoc DL = N->getDebugLoc();
8373
8374 SmallVector<SDValue, 8> Opnds;
8375 DenseMap<SDValue, unsigned> VecInMap;
8376 EVT VT = MVT::Other;
8377
8378 // Recognize a special case where a vector is casted into wide integer to
8379 // test all 0s.
8380 Opnds.push_back(N->getOperand(0));
8381 Opnds.push_back(N->getOperand(1));
8382
8383 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8384 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8385 // BFS traverse all OR'd operands.
8386 if (I->getOpcode() == ISD::OR) {
8387 Opnds.push_back(I->getOperand(0));
8388 Opnds.push_back(I->getOperand(1));
8389 // Re-evaluate the number of nodes to be traversed.
8390 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8391 continue;
8392 }
8393
8394 // Quit if a non-EXTRACT_VECTOR_ELT
8395 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8396 return SDValue();
8397
8398 // Quit if without a constant index.
8399 SDValue Idx = I->getOperand(1);
8400 if (!isa<ConstantSDNode>(Idx))
8401 return SDValue();
8402
8403 SDValue ExtractedFromVec = I->getOperand(0);
8404 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8405 if (M == VecInMap.end()) {
8406 VT = ExtractedFromVec.getValueType();
8407 // Quit if not 128/256-bit vector.
8408 if (!VT.is128BitVector() && !VT.is256BitVector())
8409 return SDValue();
8410 // Quit if not the same type.
8411 if (VecInMap.begin() != VecInMap.end() &&
8412 VT != VecInMap.begin()->first.getValueType())
8413 return SDValue();
8414 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8415 }
8416 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8417 }
8418
8419 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008420 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008421
8422 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8423 SmallVector<SDValue, 8> VecIns;
8424
8425 for (DenseMap<SDValue, unsigned>::const_iterator
8426 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8427 // Quit if not all elements are used.
8428 if (I->second != FullMask)
8429 return SDValue();
8430 VecIns.push_back(I->first);
8431 }
8432
8433 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8434
8435 // Cast all vectors into TestVT for PTEST.
8436 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8437 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8438
8439 // If more than one full vectors are evaluated, OR them first before PTEST.
8440 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8441 // Each iteration will OR 2 nodes and append the result until there is only
8442 // 1 node left, i.e. the final OR'd value of all vectors.
8443 SDValue LHS = VecIns[Slot];
8444 SDValue RHS = VecIns[Slot + 1];
8445 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8446 }
8447
8448 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8449 VecIns.back(), VecIns.back());
8450}
8451
Dan Gohman076aee32009-03-04 19:44:21 +00008452/// Emit nodes that will be selected as "test Op0,Op0", or something
8453/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008454SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008455 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008456 DebugLoc dl = Op.getDebugLoc();
8457
Dan Gohman31125812009-03-07 01:58:32 +00008458 // CF and OF aren't always set the way we want. Determine which
8459 // of these we need.
8460 bool NeedCF = false;
8461 bool NeedOF = false;
8462 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008463 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008464 case X86::COND_A: case X86::COND_AE:
8465 case X86::COND_B: case X86::COND_BE:
8466 NeedCF = true;
8467 break;
8468 case X86::COND_G: case X86::COND_GE:
8469 case X86::COND_L: case X86::COND_LE:
8470 case X86::COND_O: case X86::COND_NO:
8471 NeedOF = true;
8472 break;
Dan Gohman31125812009-03-07 01:58:32 +00008473 }
8474
Dan Gohman076aee32009-03-04 19:44:21 +00008475 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008476 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8477 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008478 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8479 // Emit a CMP with 0, which is the TEST pattern.
8480 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8481 DAG.getConstant(0, Op.getValueType()));
8482
8483 unsigned Opcode = 0;
8484 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008485
8486 // Truncate operations may prevent the merge of the SETCC instruction
8487 // and the arithmetic intruction before it. Attempt to truncate the operands
8488 // of the arithmetic instruction and use a reduced bit-width instruction.
8489 bool NeedTruncation = false;
8490 SDValue ArithOp = Op;
8491 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8492 SDValue Arith = Op->getOperand(0);
8493 // Both the trunc and the arithmetic op need to have one user each.
8494 if (Arith->hasOneUse())
8495 switch (Arith.getOpcode()) {
8496 default: break;
8497 case ISD::ADD:
8498 case ISD::SUB:
8499 case ISD::AND:
8500 case ISD::OR:
8501 case ISD::XOR: {
8502 NeedTruncation = true;
8503 ArithOp = Arith;
8504 }
8505 }
8506 }
8507
8508 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8509 // which may be the result of a CAST. We use the variable 'Op', which is the
8510 // non-casted variable when we check for possible users.
8511 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008512 case ISD::ADD:
8513 // Due to an isel shortcoming, be conservative if this add is likely to be
8514 // selected as part of a load-modify-store instruction. When the root node
8515 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8516 // uses of other nodes in the match, such as the ADD in this case. This
8517 // leads to the ADD being left around and reselected, with the result being
8518 // two adds in the output. Alas, even if none our users are stores, that
8519 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8520 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8521 // climbing the DAG back to the root, and it doesn't seem to be worth the
8522 // effort.
8523 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008524 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8525 if (UI->getOpcode() != ISD::CopyToReg &&
8526 UI->getOpcode() != ISD::SETCC &&
8527 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008528 goto default_case;
8529
8530 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008531 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008532 // An add of one will be selected as an INC.
8533 if (C->getAPIntValue() == 1) {
8534 Opcode = X86ISD::INC;
8535 NumOperands = 1;
8536 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008537 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008538
8539 // An add of negative one (subtract of one) will be selected as a DEC.
8540 if (C->getAPIntValue().isAllOnesValue()) {
8541 Opcode = X86ISD::DEC;
8542 NumOperands = 1;
8543 break;
8544 }
Dan Gohman076aee32009-03-04 19:44:21 +00008545 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008546
8547 // Otherwise use a regular EFLAGS-setting add.
8548 Opcode = X86ISD::ADD;
8549 NumOperands = 2;
8550 break;
8551 case ISD::AND: {
8552 // If the primary and result isn't used, don't bother using X86ISD::AND,
8553 // because a TEST instruction will be better.
8554 bool NonFlagUse = false;
8555 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8556 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8557 SDNode *User = *UI;
8558 unsigned UOpNo = UI.getOperandNo();
8559 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8560 // Look pass truncate.
8561 UOpNo = User->use_begin().getOperandNo();
8562 User = *User->use_begin();
8563 }
8564
8565 if (User->getOpcode() != ISD::BRCOND &&
8566 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008567 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008568 NonFlagUse = true;
8569 break;
8570 }
Dan Gohman076aee32009-03-04 19:44:21 +00008571 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008572
8573 if (!NonFlagUse)
8574 break;
8575 }
8576 // FALL THROUGH
8577 case ISD::SUB:
8578 case ISD::OR:
8579 case ISD::XOR:
8580 // Due to the ISEL shortcoming noted above, be conservative if this op is
8581 // likely to be selected as part of a load-modify-store instruction.
8582 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8583 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8584 if (UI->getOpcode() == ISD::STORE)
8585 goto default_case;
8586
8587 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008588 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008589 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008590 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008591 case ISD::XOR: Opcode = X86ISD::XOR; break;
8592 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008593 case ISD::OR: {
8594 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8595 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8596 if (EFLAGS.getNode())
8597 return EFLAGS;
8598 }
8599 Opcode = X86ISD::OR;
8600 break;
8601 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008602 }
8603
8604 NumOperands = 2;
8605 break;
8606 case X86ISD::ADD:
8607 case X86ISD::SUB:
8608 case X86ISD::INC:
8609 case X86ISD::DEC:
8610 case X86ISD::OR:
8611 case X86ISD::XOR:
8612 case X86ISD::AND:
8613 return SDValue(Op.getNode(), 1);
8614 default:
8615 default_case:
8616 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008617 }
8618
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008619 // If we found that truncation is beneficial, perform the truncation and
8620 // update 'Op'.
8621 if (NeedTruncation) {
8622 EVT VT = Op.getValueType();
8623 SDValue WideVal = Op->getOperand(0);
8624 EVT WideVT = WideVal.getValueType();
8625 unsigned ConvertedOp = 0;
8626 // Use a target machine opcode to prevent further DAGCombine
8627 // optimizations that may separate the arithmetic operations
8628 // from the setcc node.
8629 switch (WideVal.getOpcode()) {
8630 default: break;
8631 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8632 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8633 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8634 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8635 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8636 }
8637
8638 if (ConvertedOp) {
8639 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8640 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8641 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8642 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8643 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8644 }
8645 }
8646 }
8647
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008648 if (Opcode == 0)
8649 // Emit a CMP with 0, which is the TEST pattern.
8650 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8651 DAG.getConstant(0, Op.getValueType()));
8652
8653 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8654 SmallVector<SDValue, 4> Ops;
8655 for (unsigned i = 0; i != NumOperands; ++i)
8656 Ops.push_back(Op.getOperand(i));
8657
8658 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8659 DAG.ReplaceAllUsesWith(Op, New);
8660 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008661}
8662
8663/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8664/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008665SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008666 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008667 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8668 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008669 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008670
8671 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008672 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8673 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8674 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8675 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8676 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8677 Op0, Op1);
8678 return SDValue(Sub.getNode(), 1);
8679 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008680 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008681}
8682
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008683/// Convert a comparison if required by the subtarget.
8684SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8685 SelectionDAG &DAG) const {
8686 // If the subtarget does not support the FUCOMI instruction, floating-point
8687 // comparisons have to be converted.
8688 if (Subtarget->hasCMov() ||
8689 Cmp.getOpcode() != X86ISD::CMP ||
8690 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8691 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8692 return Cmp;
8693
8694 // The instruction selector will select an FUCOM instruction instead of
8695 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8696 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8697 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8698 DebugLoc dl = Cmp.getDebugLoc();
8699 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8700 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8701 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8702 DAG.getConstant(8, MVT::i8));
8703 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8704 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8705}
8706
Evan Chengd40d03e2010-01-06 19:38:29 +00008707/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8708/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008709SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8710 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008711 SDValue Op0 = And.getOperand(0);
8712 SDValue Op1 = And.getOperand(1);
8713 if (Op0.getOpcode() == ISD::TRUNCATE)
8714 Op0 = Op0.getOperand(0);
8715 if (Op1.getOpcode() == ISD::TRUNCATE)
8716 Op1 = Op1.getOperand(0);
8717
Evan Chengd40d03e2010-01-06 19:38:29 +00008718 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008719 if (Op1.getOpcode() == ISD::SHL)
8720 std::swap(Op0, Op1);
8721 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008722 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8723 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008724 // If we looked past a truncate, check that it's only truncating away
8725 // known zeros.
8726 unsigned BitWidth = Op0.getValueSizeInBits();
8727 unsigned AndBitWidth = And.getValueSizeInBits();
8728 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008729 APInt Zeros, Ones;
8730 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008731 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8732 return SDValue();
8733 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008734 LHS = Op1;
8735 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008736 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008737 } else if (Op1.getOpcode() == ISD::Constant) {
8738 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008739 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008740 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008741
8742 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008743 LHS = AndLHS.getOperand(0);
8744 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008745 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008746
8747 // Use BT if the immediate can't be encoded in a TEST instruction.
8748 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8749 LHS = AndLHS;
8750 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8751 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008752 }
Evan Cheng0488db92007-09-25 01:57:46 +00008753
Evan Chengd40d03e2010-01-06 19:38:29 +00008754 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008755 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008756 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008757 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008758 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008759 // Also promote i16 to i32 for performance / code size reason.
8760 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008761 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008762 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008763
Evan Chengd40d03e2010-01-06 19:38:29 +00008764 // If the operand types disagree, extend the shift amount to match. Since
8765 // BT ignores high bits (like shifts) we can use anyextend.
8766 if (LHS.getValueType() != RHS.getValueType())
8767 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008768
Evan Chengd40d03e2010-01-06 19:38:29 +00008769 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8770 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8771 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8772 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008773 }
8774
Evan Cheng54de3ea2010-01-05 06:52:31 +00008775 return SDValue();
8776}
8777
Dan Gohmand858e902010-04-17 15:26:15 +00008778SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008779
8780 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8781
Evan Cheng54de3ea2010-01-05 06:52:31 +00008782 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8783 SDValue Op0 = Op.getOperand(0);
8784 SDValue Op1 = Op.getOperand(1);
8785 DebugLoc dl = Op.getDebugLoc();
8786 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8787
8788 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008789 // Lower (X & (1 << N)) == 0 to BT(X, N).
8790 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8791 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008792 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008793 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008794 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008795 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8796 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8797 if (NewSetCC.getNode())
8798 return NewSetCC;
8799 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008800
Chris Lattner481eebc2010-12-19 21:23:48 +00008801 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8802 // these.
8803 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008804 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008805 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8806 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008807
Chris Lattner481eebc2010-12-19 21:23:48 +00008808 // If the input is a setcc, then reuse the input setcc or use a new one with
8809 // the inverted condition.
8810 if (Op0.getOpcode() == X86ISD::SETCC) {
8811 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8812 bool Invert = (CC == ISD::SETNE) ^
8813 cast<ConstantSDNode>(Op1)->isNullValue();
8814 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008815
Evan Cheng2c755ba2010-02-27 07:36:59 +00008816 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008817 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8818 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8819 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008820 }
8821
Evan Chenge5b51ac2010-04-17 06:13:15 +00008822 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008823 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008824 if (X86CC == X86::COND_INVALID)
8825 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008826
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008827 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008828 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008829 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008830 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008831}
8832
Craig Topper89af15e2011-09-18 08:03:58 +00008833// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008834// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008835static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008836 EVT VT = Op.getValueType();
8837
Craig Topper7a9a28b2012-08-12 02:23:29 +00008838 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008839 "Unsupported value type for operation");
8840
Craig Topper66ddd152012-04-27 22:54:43 +00008841 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008842 DebugLoc dl = Op.getDebugLoc();
8843 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008844
8845 // Extract the LHS vectors
8846 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008847 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8848 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008849
8850 // Extract the RHS vectors
8851 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008852 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8853 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008854
8855 // Issue the operation on the smaller types and concatenate the result back
8856 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8857 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8858 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8859 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8860 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8861}
8862
8863
Dan Gohmand858e902010-04-17 15:26:15 +00008864SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008865 SDValue Cond;
8866 SDValue Op0 = Op.getOperand(0);
8867 SDValue Op1 = Op.getOperand(1);
8868 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008869 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008870 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8871 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008872 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008873
8874 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008875#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008876 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008877 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8878#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008879
Craig Topper523908d2012-08-13 02:34:03 +00008880 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008881 bool Swap = false;
8882
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008883 // SSE Condition code mapping:
8884 // 0 - EQ
8885 // 1 - LT
8886 // 2 - LE
8887 // 3 - UNORD
8888 // 4 - NEQ
8889 // 5 - NLT
8890 // 6 - NLE
8891 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008892 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008893 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008894 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008895 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008896 case ISD::SETOGT:
8897 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008898 case ISD::SETLT:
8899 case ISD::SETOLT: SSECC = 1; break;
8900 case ISD::SETOGE:
8901 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008902 case ISD::SETLE:
8903 case ISD::SETOLE: SSECC = 2; break;
8904 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008905 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008906 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008907 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008908 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008909 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008910 case ISD::SETUGT: SSECC = 6; break;
8911 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008912 case ISD::SETUEQ:
8913 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008914 }
8915 if (Swap)
8916 std::swap(Op0, Op1);
8917
Nate Begemanfb8ead02008-07-25 19:05:58 +00008918 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008919 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008920 unsigned CC0, CC1;
8921 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008922 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008923 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8924 } else {
8925 assert(SetCCOpcode == ISD::SETONE);
8926 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008927 }
Craig Topper523908d2012-08-13 02:34:03 +00008928
8929 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8930 DAG.getConstant(CC0, MVT::i8));
8931 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8932 DAG.getConstant(CC1, MVT::i8));
8933 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008934 }
8935 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008936 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8937 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008939
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008940 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008941 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008942 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008943
Nate Begeman30a0de92008-07-17 16:51:19 +00008944 // We are handling one of the integer comparisons here. Since SSE only has
8945 // GT and EQ comparisons for integer, swapping operands and multiple
8946 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008947 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008948 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008949
Nate Begeman30a0de92008-07-17 16:51:19 +00008950 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008951 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008952 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008953 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008954 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008955 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008956 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008957 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008958 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008959 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008960 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008961 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008962 }
8963 if (Swap)
8964 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008965
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008966 // Check that the operation in question is available (most are plain SSE2,
8967 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008968 if (VT == MVT::v2i64) {
8969 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8970 return SDValue();
8971 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8972 return SDValue();
8973 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008974
Nate Begeman30a0de92008-07-17 16:51:19 +00008975 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8976 // bits of the inputs before performing those operations.
8977 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008978 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008979 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8980 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008981 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008982 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8983 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008984 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8985 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008986 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008987
Dale Johannesenace16102009-02-03 19:33:06 +00008988 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008989
8990 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008991 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008992 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008993
Nate Begeman30a0de92008-07-17 16:51:19 +00008994 return Result;
8995}
Evan Cheng0488db92007-09-25 01:57:46 +00008996
Evan Cheng370e5342008-12-03 08:38:43 +00008997// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008998static bool isX86LogicalCmp(SDValue Op) {
8999 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009000 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9001 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009002 return true;
9003 if (Op.getResNo() == 1 &&
9004 (Opc == X86ISD::ADD ||
9005 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009006 Opc == X86ISD::ADC ||
9007 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009008 Opc == X86ISD::SMUL ||
9009 Opc == X86ISD::UMUL ||
9010 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009011 Opc == X86ISD::DEC ||
9012 Opc == X86ISD::OR ||
9013 Opc == X86ISD::XOR ||
9014 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009015 return true;
9016
Chris Lattner9637d5b2010-12-05 07:49:54 +00009017 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9018 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009019
Dan Gohman076aee32009-03-04 19:44:21 +00009020 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009021}
9022
Chris Lattnera2b56002010-12-05 01:23:24 +00009023static bool isZero(SDValue V) {
9024 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9025 return C && C->isNullValue();
9026}
9027
Chris Lattner96908b12010-12-05 02:00:51 +00009028static bool isAllOnes(SDValue V) {
9029 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9030 return C && C->isAllOnesValue();
9031}
9032
Evan Chengb64dd5f2012-08-07 22:21:00 +00009033static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9034 if (V.getOpcode() != ISD::TRUNCATE)
9035 return false;
9036
9037 SDValue VOp0 = V.getOperand(0);
9038 unsigned InBits = VOp0.getValueSizeInBits();
9039 unsigned Bits = V.getValueSizeInBits();
9040 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9041}
9042
Dan Gohmand858e902010-04-17 15:26:15 +00009043SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009044 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009045 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009046 SDValue Op1 = Op.getOperand(1);
9047 SDValue Op2 = Op.getOperand(2);
9048 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009049 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009050
Dan Gohman1a492952009-10-20 16:22:37 +00009051 if (Cond.getOpcode() == ISD::SETCC) {
9052 SDValue NewCond = LowerSETCC(Cond, DAG);
9053 if (NewCond.getNode())
9054 Cond = NewCond;
9055 }
Evan Cheng734503b2006-09-11 02:19:56 +00009056
Chris Lattnera2b56002010-12-05 01:23:24 +00009057 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009058 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009059 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009060 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009061 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009062 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9063 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009064 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009065
Chris Lattnera2b56002010-12-05 01:23:24 +00009066 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009067
9068 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009069 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9070 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009071
9072 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009073 // Apply further optimizations for special cases
9074 // (select (x != 0), -1, 0) -> neg & sbb
9075 // (select (x == 0), 0, -1) -> neg & sbb
9076 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009077 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009078 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9079 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009080 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9081 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009082 CmpOp0);
9083 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9084 DAG.getConstant(X86::COND_B, MVT::i8),
9085 SDValue(Neg.getNode(), 1));
9086 return Res;
9087 }
9088
Chris Lattnera2b56002010-12-05 01:23:24 +00009089 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9090 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009091 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009092
Chris Lattner96908b12010-12-05 02:00:51 +00009093 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009094 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9095 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009096
Chris Lattner96908b12010-12-05 02:00:51 +00009097 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9098 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009099
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009100 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009101 if (N2C == 0 || !N2C->isNullValue())
9102 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9103 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009104 }
9105 }
9106
Chris Lattnera2b56002010-12-05 01:23:24 +00009107 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009108 if (Cond.getOpcode() == ISD::AND &&
9109 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9110 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009111 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009112 Cond = Cond.getOperand(0);
9113 }
9114
Evan Cheng3f41d662007-10-08 22:16:29 +00009115 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9116 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009117 unsigned CondOpcode = Cond.getOpcode();
9118 if (CondOpcode == X86ISD::SETCC ||
9119 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009120 CC = Cond.getOperand(0);
9121
Dan Gohman475871a2008-07-27 21:46:04 +00009122 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009123 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009124 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009125
Evan Cheng3f41d662007-10-08 22:16:29 +00009126 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009127 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009128 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009129 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009130
Chris Lattnerd1980a52009-03-12 06:52:53 +00009131 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9132 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009133 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009134 addTest = false;
9135 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009136 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9137 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9138 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9139 Cond.getOperand(0).getValueType() != MVT::i8)) {
9140 SDValue LHS = Cond.getOperand(0);
9141 SDValue RHS = Cond.getOperand(1);
9142 unsigned X86Opcode;
9143 unsigned X86Cond;
9144 SDVTList VTs;
9145 switch (CondOpcode) {
9146 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9147 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9148 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9149 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9150 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9151 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9152 default: llvm_unreachable("unexpected overflowing operator");
9153 }
9154 if (CondOpcode == ISD::UMULO)
9155 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9156 MVT::i32);
9157 else
9158 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9159
9160 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9161
9162 if (CondOpcode == ISD::UMULO)
9163 Cond = X86Op.getValue(2);
9164 else
9165 Cond = X86Op.getValue(1);
9166
9167 CC = DAG.getConstant(X86Cond, MVT::i8);
9168 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009169 }
9170
9171 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009172 // Look pass the truncate if the high bits are known zero.
9173 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9174 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009175
9176 // We know the result of AND is compared against zero. Try to match
9177 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009178 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009179 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009180 if (NewSetCC.getNode()) {
9181 CC = NewSetCC.getOperand(0);
9182 Cond = NewSetCC.getOperand(1);
9183 addTest = false;
9184 }
9185 }
9186 }
9187
9188 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009189 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009190 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009191 }
9192
Benjamin Kramere915ff32010-12-22 23:09:28 +00009193 // a < b ? -1 : 0 -> RES = ~setcc_carry
9194 // a < b ? 0 : -1 -> RES = setcc_carry
9195 // a >= b ? -1 : 0 -> RES = setcc_carry
9196 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009197 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009198 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009199 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9200
9201 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9202 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9203 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9204 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9205 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9206 return DAG.getNOT(DL, Res, Res.getValueType());
9207 return Res;
9208 }
9209 }
9210
Evan Cheng0488db92007-09-25 01:57:46 +00009211 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9212 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009213 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009214 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009215 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009216}
9217
Evan Cheng370e5342008-12-03 08:38:43 +00009218// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9219// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9220// from the AND / OR.
9221static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9222 Opc = Op.getOpcode();
9223 if (Opc != ISD::OR && Opc != ISD::AND)
9224 return false;
9225 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9226 Op.getOperand(0).hasOneUse() &&
9227 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9228 Op.getOperand(1).hasOneUse());
9229}
9230
Evan Cheng961d6d42009-02-02 08:19:07 +00009231// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9232// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009233static bool isXor1OfSetCC(SDValue Op) {
9234 if (Op.getOpcode() != ISD::XOR)
9235 return false;
9236 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9237 if (N1C && N1C->getAPIntValue() == 1) {
9238 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9239 Op.getOperand(0).hasOneUse();
9240 }
9241 return false;
9242}
9243
Dan Gohmand858e902010-04-17 15:26:15 +00009244SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009245 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009246 SDValue Chain = Op.getOperand(0);
9247 SDValue Cond = Op.getOperand(1);
9248 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009249 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009250 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009251 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009252
Dan Gohman1a492952009-10-20 16:22:37 +00009253 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009254 // Check for setcc([su]{add,sub,mul}o == 0).
9255 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9256 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9257 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9258 Cond.getOperand(0).getResNo() == 1 &&
9259 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9260 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9261 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9262 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9263 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9264 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9265 Inverted = true;
9266 Cond = Cond.getOperand(0);
9267 } else {
9268 SDValue NewCond = LowerSETCC(Cond, DAG);
9269 if (NewCond.getNode())
9270 Cond = NewCond;
9271 }
Dan Gohman1a492952009-10-20 16:22:37 +00009272 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009273#if 0
9274 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009275 else if (Cond.getOpcode() == X86ISD::ADD ||
9276 Cond.getOpcode() == X86ISD::SUB ||
9277 Cond.getOpcode() == X86ISD::SMUL ||
9278 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009279 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009280#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009281
Evan Chengad9c0a32009-12-15 00:53:42 +00009282 // Look pass (and (setcc_carry (cmp ...)), 1).
9283 if (Cond.getOpcode() == ISD::AND &&
9284 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9285 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009286 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009287 Cond = Cond.getOperand(0);
9288 }
9289
Evan Cheng3f41d662007-10-08 22:16:29 +00009290 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9291 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009292 unsigned CondOpcode = Cond.getOpcode();
9293 if (CondOpcode == X86ISD::SETCC ||
9294 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009295 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009296
Dan Gohman475871a2008-07-27 21:46:04 +00009297 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009298 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009299 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009300 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009301 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009302 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009303 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009304 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009305 default: break;
9306 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009307 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009308 // These can only come from an arithmetic instruction with overflow,
9309 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009310 Cond = Cond.getNode()->getOperand(1);
9311 addTest = false;
9312 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009313 }
Evan Cheng0488db92007-09-25 01:57:46 +00009314 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009315 }
9316 CondOpcode = Cond.getOpcode();
9317 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9318 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9319 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9320 Cond.getOperand(0).getValueType() != MVT::i8)) {
9321 SDValue LHS = Cond.getOperand(0);
9322 SDValue RHS = Cond.getOperand(1);
9323 unsigned X86Opcode;
9324 unsigned X86Cond;
9325 SDVTList VTs;
9326 switch (CondOpcode) {
9327 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9328 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9329 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9330 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9331 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9332 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9333 default: llvm_unreachable("unexpected overflowing operator");
9334 }
9335 if (Inverted)
9336 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9337 if (CondOpcode == ISD::UMULO)
9338 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9339 MVT::i32);
9340 else
9341 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9342
9343 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9344
9345 if (CondOpcode == ISD::UMULO)
9346 Cond = X86Op.getValue(2);
9347 else
9348 Cond = X86Op.getValue(1);
9349
9350 CC = DAG.getConstant(X86Cond, MVT::i8);
9351 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009352 } else {
9353 unsigned CondOpc;
9354 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9355 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009356 if (CondOpc == ISD::OR) {
9357 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9358 // two branches instead of an explicit OR instruction with a
9359 // separate test.
9360 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009361 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009362 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009363 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009364 Chain, Dest, CC, Cmp);
9365 CC = Cond.getOperand(1).getOperand(0);
9366 Cond = Cmp;
9367 addTest = false;
9368 }
9369 } else { // ISD::AND
9370 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9371 // two branches instead of an explicit AND instruction with a
9372 // separate test. However, we only do this if this block doesn't
9373 // have a fall-through edge, because this requires an explicit
9374 // jmp when the condition is false.
9375 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009376 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009377 Op.getNode()->hasOneUse()) {
9378 X86::CondCode CCode =
9379 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9380 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009381 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009382 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009383 // Look for an unconditional branch following this conditional branch.
9384 // We need this because we need to reverse the successors in order
9385 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009386 if (User->getOpcode() == ISD::BR) {
9387 SDValue FalseBB = User->getOperand(1);
9388 SDNode *NewBR =
9389 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009390 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009391 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009392 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009393
Dale Johannesene4d209d2009-02-03 20:21:25 +00009394 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009395 Chain, Dest, CC, Cmp);
9396 X86::CondCode CCode =
9397 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9398 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009399 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009400 Cond = Cmp;
9401 addTest = false;
9402 }
9403 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009404 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009405 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9406 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9407 // It should be transformed during dag combiner except when the condition
9408 // is set by a arithmetics with overflow node.
9409 X86::CondCode CCode =
9410 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9411 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009412 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009413 Cond = Cond.getOperand(0).getOperand(1);
9414 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009415 } else if (Cond.getOpcode() == ISD::SETCC &&
9416 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9417 // For FCMP_OEQ, we can emit
9418 // two branches instead of an explicit AND instruction with a
9419 // separate test. However, we only do this if this block doesn't
9420 // have a fall-through edge, because this requires an explicit
9421 // jmp when the condition is false.
9422 if (Op.getNode()->hasOneUse()) {
9423 SDNode *User = *Op.getNode()->use_begin();
9424 // Look for an unconditional branch following this conditional branch.
9425 // We need this because we need to reverse the successors in order
9426 // to implement FCMP_OEQ.
9427 if (User->getOpcode() == ISD::BR) {
9428 SDValue FalseBB = User->getOperand(1);
9429 SDNode *NewBR =
9430 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9431 assert(NewBR == User);
9432 (void)NewBR;
9433 Dest = FalseBB;
9434
9435 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9436 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009437 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009438 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9439 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9440 Chain, Dest, CC, Cmp);
9441 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9442 Cond = Cmp;
9443 addTest = false;
9444 }
9445 }
9446 } else if (Cond.getOpcode() == ISD::SETCC &&
9447 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9448 // For FCMP_UNE, we can emit
9449 // two branches instead of an explicit AND instruction with a
9450 // separate test. However, we only do this if this block doesn't
9451 // have a fall-through edge, because this requires an explicit
9452 // jmp when the condition is false.
9453 if (Op.getNode()->hasOneUse()) {
9454 SDNode *User = *Op.getNode()->use_begin();
9455 // Look for an unconditional branch following this conditional branch.
9456 // We need this because we need to reverse the successors in order
9457 // to implement FCMP_UNE.
9458 if (User->getOpcode() == ISD::BR) {
9459 SDValue FalseBB = User->getOperand(1);
9460 SDNode *NewBR =
9461 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9462 assert(NewBR == User);
9463 (void)NewBR;
9464
9465 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9466 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009467 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009468 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9469 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9470 Chain, Dest, CC, Cmp);
9471 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9472 Cond = Cmp;
9473 addTest = false;
9474 Dest = FalseBB;
9475 }
9476 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009477 }
Evan Cheng0488db92007-09-25 01:57:46 +00009478 }
9479
9480 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009481 // Look pass the truncate if the high bits are known zero.
9482 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9483 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009484
9485 // We know the result of AND is compared against zero. Try to match
9486 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009487 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009488 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9489 if (NewSetCC.getNode()) {
9490 CC = NewSetCC.getOperand(0);
9491 Cond = NewSetCC.getOperand(1);
9492 addTest = false;
9493 }
9494 }
9495 }
9496
9497 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009498 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009499 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009500 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009501 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009502 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009503 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009504}
9505
Anton Korobeynikove060b532007-04-17 19:34:00 +00009506
9507// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9508// Calls to _alloca is needed to probe the stack when allocating more than 4k
9509// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9510// that the guard pages used by the OS virtual memory manager are allocated in
9511// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009512SDValue
9513X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009514 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009515 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009516 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009517 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009518 "are being used");
9519 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009520 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009521
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009522 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009523 SDValue Chain = Op.getOperand(0);
9524 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009525 // FIXME: Ensure alignment here
9526
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009527 bool Is64Bit = Subtarget->is64Bit();
9528 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009529
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009530 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009531 MachineFunction &MF = DAG.getMachineFunction();
9532 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009533
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009534 if (Is64Bit) {
9535 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009536 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009537 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009538
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009539 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009540 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009541 if (I->hasNestAttr())
9542 report_fatal_error("Cannot use segmented stacks with functions that "
9543 "have nested arguments.");
9544 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009545
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009546 const TargetRegisterClass *AddrRegClass =
9547 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9548 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9549 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9550 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9551 DAG.getRegister(Vreg, SPTy));
9552 SDValue Ops1[2] = { Value, Chain };
9553 return DAG.getMergeValues(Ops1, 2, dl);
9554 } else {
9555 SDValue Flag;
9556 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009557
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009558 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9559 Flag = Chain.getValue(1);
9560 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009561
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009562 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9563 Flag = Chain.getValue(1);
9564
9565 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9566
9567 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9568 return DAG.getMergeValues(Ops1, 2, dl);
9569 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009570}
9571
Dan Gohmand858e902010-04-17 15:26:15 +00009572SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009573 MachineFunction &MF = DAG.getMachineFunction();
9574 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9575
Dan Gohman69de1932008-02-06 22:27:42 +00009576 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009577 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009578
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009579 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009580 // vastart just stores the address of the VarArgsFrameIndex slot into the
9581 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009582 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9583 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009584 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9585 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009586 }
9587
9588 // __va_list_tag:
9589 // gp_offset (0 - 6 * 8)
9590 // fp_offset (48 - 48 + 8 * 16)
9591 // overflow_arg_area (point to parameters coming in memory).
9592 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009593 SmallVector<SDValue, 8> MemOps;
9594 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009595 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009596 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009597 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9598 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009599 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009600 MemOps.push_back(Store);
9601
9602 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009603 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009604 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009605 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009606 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9607 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009608 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009609 MemOps.push_back(Store);
9610
9611 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009612 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009613 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009614 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9615 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009616 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9617 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009618 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009619 MemOps.push_back(Store);
9620
9621 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009622 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009623 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009624 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9625 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009626 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9627 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009628 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009629 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009630 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009631}
9632
Dan Gohmand858e902010-04-17 15:26:15 +00009633SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009634 assert(Subtarget->is64Bit() &&
9635 "LowerVAARG only handles 64-bit va_arg!");
9636 assert((Subtarget->isTargetLinux() ||
9637 Subtarget->isTargetDarwin()) &&
9638 "Unhandled target in LowerVAARG");
9639 assert(Op.getNode()->getNumOperands() == 4);
9640 SDValue Chain = Op.getOperand(0);
9641 SDValue SrcPtr = Op.getOperand(1);
9642 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9643 unsigned Align = Op.getConstantOperandVal(3);
9644 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009645
Dan Gohman320afb82010-10-12 18:00:49 +00009646 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009647 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009648 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9649 uint8_t ArgMode;
9650
9651 // Decide which area this value should be read from.
9652 // TODO: Implement the AMD64 ABI in its entirety. This simple
9653 // selection mechanism works only for the basic types.
9654 if (ArgVT == MVT::f80) {
9655 llvm_unreachable("va_arg for f80 not yet implemented");
9656 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9657 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9658 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9659 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9660 } else {
9661 llvm_unreachable("Unhandled argument type in LowerVAARG");
9662 }
9663
9664 if (ArgMode == 2) {
9665 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009666 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009667 !(DAG.getMachineFunction()
9668 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009669 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009670 }
9671
9672 // Insert VAARG_64 node into the DAG
9673 // VAARG_64 returns two values: Variable Argument Address, Chain
9674 SmallVector<SDValue, 11> InstOps;
9675 InstOps.push_back(Chain);
9676 InstOps.push_back(SrcPtr);
9677 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9678 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9679 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9680 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9681 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9682 VTs, &InstOps[0], InstOps.size(),
9683 MVT::i64,
9684 MachinePointerInfo(SV),
9685 /*Align=*/0,
9686 /*Volatile=*/false,
9687 /*ReadMem=*/true,
9688 /*WriteMem=*/true);
9689 Chain = VAARG.getValue(1);
9690
9691 // Load the next argument and return it
9692 return DAG.getLoad(ArgVT, dl,
9693 Chain,
9694 VAARG,
9695 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009696 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009697}
9698
Craig Topper55b24052012-09-11 06:15:32 +00009699static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9700 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009701 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009702 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009703 SDValue Chain = Op.getOperand(0);
9704 SDValue DstPtr = Op.getOperand(1);
9705 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009706 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9707 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009708 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009709
Chris Lattnere72f2022010-09-21 05:40:29 +00009710 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009711 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009712 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009713 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009714}
9715
Craig Topper80e46362012-01-23 06:16:53 +00009716// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9717// may or may not be a constant. Takes immediate version of shift as input.
9718static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9719 SDValue SrcOp, SDValue ShAmt,
9720 SelectionDAG &DAG) {
9721 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9722
9723 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009724 // Constant may be a TargetConstant. Use a regular constant.
9725 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009726 switch (Opc) {
9727 default: llvm_unreachable("Unknown target vector shift node");
9728 case X86ISD::VSHLI:
9729 case X86ISD::VSRLI:
9730 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009731 return DAG.getNode(Opc, dl, VT, SrcOp,
9732 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009733 }
9734 }
9735
9736 // Change opcode to non-immediate version
9737 switch (Opc) {
9738 default: llvm_unreachable("Unknown target vector shift node");
9739 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9740 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9741 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9742 }
9743
9744 // Need to build a vector containing shift amount
9745 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9746 SDValue ShOps[4];
9747 ShOps[0] = ShAmt;
9748 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009749 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009750 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009751
9752 // The return type has to be a 128-bit type with the same element
9753 // type as the input type.
9754 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9755 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9756
9757 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009758 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9759}
9760
Craig Topper55b24052012-09-11 06:15:32 +00009761static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009762 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009763 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009764 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009765 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009766 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009767 case Intrinsic::x86_sse_comieq_ss:
9768 case Intrinsic::x86_sse_comilt_ss:
9769 case Intrinsic::x86_sse_comile_ss:
9770 case Intrinsic::x86_sse_comigt_ss:
9771 case Intrinsic::x86_sse_comige_ss:
9772 case Intrinsic::x86_sse_comineq_ss:
9773 case Intrinsic::x86_sse_ucomieq_ss:
9774 case Intrinsic::x86_sse_ucomilt_ss:
9775 case Intrinsic::x86_sse_ucomile_ss:
9776 case Intrinsic::x86_sse_ucomigt_ss:
9777 case Intrinsic::x86_sse_ucomige_ss:
9778 case Intrinsic::x86_sse_ucomineq_ss:
9779 case Intrinsic::x86_sse2_comieq_sd:
9780 case Intrinsic::x86_sse2_comilt_sd:
9781 case Intrinsic::x86_sse2_comile_sd:
9782 case Intrinsic::x86_sse2_comigt_sd:
9783 case Intrinsic::x86_sse2_comige_sd:
9784 case Intrinsic::x86_sse2_comineq_sd:
9785 case Intrinsic::x86_sse2_ucomieq_sd:
9786 case Intrinsic::x86_sse2_ucomilt_sd:
9787 case Intrinsic::x86_sse2_ucomile_sd:
9788 case Intrinsic::x86_sse2_ucomigt_sd:
9789 case Intrinsic::x86_sse2_ucomige_sd:
9790 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009791 unsigned Opc;
9792 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009793 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009794 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009795 case Intrinsic::x86_sse_comieq_ss:
9796 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009797 Opc = X86ISD::COMI;
9798 CC = ISD::SETEQ;
9799 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009800 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009801 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009802 Opc = X86ISD::COMI;
9803 CC = ISD::SETLT;
9804 break;
9805 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009806 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009807 Opc = X86ISD::COMI;
9808 CC = ISD::SETLE;
9809 break;
9810 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009811 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009812 Opc = X86ISD::COMI;
9813 CC = ISD::SETGT;
9814 break;
9815 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009816 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009817 Opc = X86ISD::COMI;
9818 CC = ISD::SETGE;
9819 break;
9820 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009821 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009822 Opc = X86ISD::COMI;
9823 CC = ISD::SETNE;
9824 break;
9825 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009826 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009827 Opc = X86ISD::UCOMI;
9828 CC = ISD::SETEQ;
9829 break;
9830 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009831 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009832 Opc = X86ISD::UCOMI;
9833 CC = ISD::SETLT;
9834 break;
9835 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009836 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009837 Opc = X86ISD::UCOMI;
9838 CC = ISD::SETLE;
9839 break;
9840 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009841 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009842 Opc = X86ISD::UCOMI;
9843 CC = ISD::SETGT;
9844 break;
9845 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009846 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009847 Opc = X86ISD::UCOMI;
9848 CC = ISD::SETGE;
9849 break;
9850 case Intrinsic::x86_sse_ucomineq_ss:
9851 case Intrinsic::x86_sse2_ucomineq_sd:
9852 Opc = X86ISD::UCOMI;
9853 CC = ISD::SETNE;
9854 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009855 }
Evan Cheng734503b2006-09-11 02:19:56 +00009856
Dan Gohman475871a2008-07-27 21:46:04 +00009857 SDValue LHS = Op.getOperand(1);
9858 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009859 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009860 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009861 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9862 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9863 DAG.getConstant(X86CC, MVT::i8), Cond);
9864 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009865 }
Craig Topper6d688152012-08-14 07:43:25 +00009866
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009867 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009868 case Intrinsic::x86_sse2_pmulu_dq:
9869 case Intrinsic::x86_avx2_pmulu_dq:
9870 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9871 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009872
9873 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009874 case Intrinsic::x86_sse3_hadd_ps:
9875 case Intrinsic::x86_sse3_hadd_pd:
9876 case Intrinsic::x86_avx_hadd_ps_256:
9877 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009878 case Intrinsic::x86_sse3_hsub_ps:
9879 case Intrinsic::x86_sse3_hsub_pd:
9880 case Intrinsic::x86_avx_hsub_ps_256:
9881 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009882 case Intrinsic::x86_ssse3_phadd_w_128:
9883 case Intrinsic::x86_ssse3_phadd_d_128:
9884 case Intrinsic::x86_avx2_phadd_w:
9885 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009886 case Intrinsic::x86_ssse3_phsub_w_128:
9887 case Intrinsic::x86_ssse3_phsub_d_128:
9888 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009889 case Intrinsic::x86_avx2_phsub_d: {
9890 unsigned Opcode;
9891 switch (IntNo) {
9892 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9893 case Intrinsic::x86_sse3_hadd_ps:
9894 case Intrinsic::x86_sse3_hadd_pd:
9895 case Intrinsic::x86_avx_hadd_ps_256:
9896 case Intrinsic::x86_avx_hadd_pd_256:
9897 Opcode = X86ISD::FHADD;
9898 break;
9899 case Intrinsic::x86_sse3_hsub_ps:
9900 case Intrinsic::x86_sse3_hsub_pd:
9901 case Intrinsic::x86_avx_hsub_ps_256:
9902 case Intrinsic::x86_avx_hsub_pd_256:
9903 Opcode = X86ISD::FHSUB;
9904 break;
9905 case Intrinsic::x86_ssse3_phadd_w_128:
9906 case Intrinsic::x86_ssse3_phadd_d_128:
9907 case Intrinsic::x86_avx2_phadd_w:
9908 case Intrinsic::x86_avx2_phadd_d:
9909 Opcode = X86ISD::HADD;
9910 break;
9911 case Intrinsic::x86_ssse3_phsub_w_128:
9912 case Intrinsic::x86_ssse3_phsub_d_128:
9913 case Intrinsic::x86_avx2_phsub_w:
9914 case Intrinsic::x86_avx2_phsub_d:
9915 Opcode = X86ISD::HSUB;
9916 break;
9917 }
9918 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009919 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009920 }
9921
9922 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009923 case Intrinsic::x86_avx2_psllv_d:
9924 case Intrinsic::x86_avx2_psllv_q:
9925 case Intrinsic::x86_avx2_psllv_d_256:
9926 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009927 case Intrinsic::x86_avx2_psrlv_d:
9928 case Intrinsic::x86_avx2_psrlv_q:
9929 case Intrinsic::x86_avx2_psrlv_d_256:
9930 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009931 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009932 case Intrinsic::x86_avx2_psrav_d_256: {
9933 unsigned Opcode;
9934 switch (IntNo) {
9935 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9936 case Intrinsic::x86_avx2_psllv_d:
9937 case Intrinsic::x86_avx2_psllv_q:
9938 case Intrinsic::x86_avx2_psllv_d_256:
9939 case Intrinsic::x86_avx2_psllv_q_256:
9940 Opcode = ISD::SHL;
9941 break;
9942 case Intrinsic::x86_avx2_psrlv_d:
9943 case Intrinsic::x86_avx2_psrlv_q:
9944 case Intrinsic::x86_avx2_psrlv_d_256:
9945 case Intrinsic::x86_avx2_psrlv_q_256:
9946 Opcode = ISD::SRL;
9947 break;
9948 case Intrinsic::x86_avx2_psrav_d:
9949 case Intrinsic::x86_avx2_psrav_d_256:
9950 Opcode = ISD::SRA;
9951 break;
9952 }
9953 return DAG.getNode(Opcode, dl, Op.getValueType(),
9954 Op.getOperand(1), Op.getOperand(2));
9955 }
9956
Craig Topper969ba282012-01-25 06:43:11 +00009957 case Intrinsic::x86_ssse3_pshuf_b_128:
9958 case Intrinsic::x86_avx2_pshuf_b:
9959 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9960 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009961
Craig Topper969ba282012-01-25 06:43:11 +00009962 case Intrinsic::x86_ssse3_psign_b_128:
9963 case Intrinsic::x86_ssse3_psign_w_128:
9964 case Intrinsic::x86_ssse3_psign_d_128:
9965 case Intrinsic::x86_avx2_psign_b:
9966 case Intrinsic::x86_avx2_psign_w:
9967 case Intrinsic::x86_avx2_psign_d:
9968 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9969 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009970
Craig Toppere566cd02012-01-26 07:18:03 +00009971 case Intrinsic::x86_sse41_insertps:
9972 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9973 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009974
Craig Toppere566cd02012-01-26 07:18:03 +00009975 case Intrinsic::x86_avx_vperm2f128_ps_256:
9976 case Intrinsic::x86_avx_vperm2f128_pd_256:
9977 case Intrinsic::x86_avx_vperm2f128_si_256:
9978 case Intrinsic::x86_avx2_vperm2i128:
9979 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9980 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009981
Craig Topperffa6c402012-04-16 07:13:00 +00009982 case Intrinsic::x86_avx2_permd:
9983 case Intrinsic::x86_avx2_permps:
9984 // Operands intentionally swapped. Mask is last operand to intrinsic,
9985 // but second operand for node/intruction.
9986 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9987 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009988
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009989 // ptest and testp intrinsics. The intrinsic these come from are designed to
9990 // return an integer value, not just an instruction so lower it to the ptest
9991 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009992 case Intrinsic::x86_sse41_ptestz:
9993 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009994 case Intrinsic::x86_sse41_ptestnzc:
9995 case Intrinsic::x86_avx_ptestz_256:
9996 case Intrinsic::x86_avx_ptestc_256:
9997 case Intrinsic::x86_avx_ptestnzc_256:
9998 case Intrinsic::x86_avx_vtestz_ps:
9999 case Intrinsic::x86_avx_vtestc_ps:
10000 case Intrinsic::x86_avx_vtestnzc_ps:
10001 case Intrinsic::x86_avx_vtestz_pd:
10002 case Intrinsic::x86_avx_vtestc_pd:
10003 case Intrinsic::x86_avx_vtestnzc_pd:
10004 case Intrinsic::x86_avx_vtestz_ps_256:
10005 case Intrinsic::x86_avx_vtestc_ps_256:
10006 case Intrinsic::x86_avx_vtestnzc_ps_256:
10007 case Intrinsic::x86_avx_vtestz_pd_256:
10008 case Intrinsic::x86_avx_vtestc_pd_256:
10009 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10010 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010011 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010012 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010013 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010014 case Intrinsic::x86_avx_vtestz_ps:
10015 case Intrinsic::x86_avx_vtestz_pd:
10016 case Intrinsic::x86_avx_vtestz_ps_256:
10017 case Intrinsic::x86_avx_vtestz_pd_256:
10018 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010019 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010020 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010021 // ZF = 1
10022 X86CC = X86::COND_E;
10023 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010024 case Intrinsic::x86_avx_vtestc_ps:
10025 case Intrinsic::x86_avx_vtestc_pd:
10026 case Intrinsic::x86_avx_vtestc_ps_256:
10027 case Intrinsic::x86_avx_vtestc_pd_256:
10028 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010029 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010030 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010031 // CF = 1
10032 X86CC = X86::COND_B;
10033 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010034 case Intrinsic::x86_avx_vtestnzc_ps:
10035 case Intrinsic::x86_avx_vtestnzc_pd:
10036 case Intrinsic::x86_avx_vtestnzc_ps_256:
10037 case Intrinsic::x86_avx_vtestnzc_pd_256:
10038 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010039 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010040 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010041 // ZF and CF = 0
10042 X86CC = X86::COND_A;
10043 break;
10044 }
Eric Christopherfd179292009-08-27 18:07:15 +000010045
Eric Christopher71c67532009-07-29 00:28:05 +000010046 SDValue LHS = Op.getOperand(1);
10047 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010048 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10049 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010050 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10051 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10052 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010053 }
Evan Cheng5759f972008-05-04 09:15:50 +000010054
Craig Topper80e46362012-01-23 06:16:53 +000010055 // SSE/AVX shift intrinsics
10056 case Intrinsic::x86_sse2_psll_w:
10057 case Intrinsic::x86_sse2_psll_d:
10058 case Intrinsic::x86_sse2_psll_q:
10059 case Intrinsic::x86_avx2_psll_w:
10060 case Intrinsic::x86_avx2_psll_d:
10061 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010062 case Intrinsic::x86_sse2_psrl_w:
10063 case Intrinsic::x86_sse2_psrl_d:
10064 case Intrinsic::x86_sse2_psrl_q:
10065 case Intrinsic::x86_avx2_psrl_w:
10066 case Intrinsic::x86_avx2_psrl_d:
10067 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010068 case Intrinsic::x86_sse2_psra_w:
10069 case Intrinsic::x86_sse2_psra_d:
10070 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010071 case Intrinsic::x86_avx2_psra_d: {
10072 unsigned Opcode;
10073 switch (IntNo) {
10074 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10075 case Intrinsic::x86_sse2_psll_w:
10076 case Intrinsic::x86_sse2_psll_d:
10077 case Intrinsic::x86_sse2_psll_q:
10078 case Intrinsic::x86_avx2_psll_w:
10079 case Intrinsic::x86_avx2_psll_d:
10080 case Intrinsic::x86_avx2_psll_q:
10081 Opcode = X86ISD::VSHL;
10082 break;
10083 case Intrinsic::x86_sse2_psrl_w:
10084 case Intrinsic::x86_sse2_psrl_d:
10085 case Intrinsic::x86_sse2_psrl_q:
10086 case Intrinsic::x86_avx2_psrl_w:
10087 case Intrinsic::x86_avx2_psrl_d:
10088 case Intrinsic::x86_avx2_psrl_q:
10089 Opcode = X86ISD::VSRL;
10090 break;
10091 case Intrinsic::x86_sse2_psra_w:
10092 case Intrinsic::x86_sse2_psra_d:
10093 case Intrinsic::x86_avx2_psra_w:
10094 case Intrinsic::x86_avx2_psra_d:
10095 Opcode = X86ISD::VSRA;
10096 break;
10097 }
10098 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010099 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010100 }
10101
10102 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010103 case Intrinsic::x86_sse2_pslli_w:
10104 case Intrinsic::x86_sse2_pslli_d:
10105 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010106 case Intrinsic::x86_avx2_pslli_w:
10107 case Intrinsic::x86_avx2_pslli_d:
10108 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010109 case Intrinsic::x86_sse2_psrli_w:
10110 case Intrinsic::x86_sse2_psrli_d:
10111 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010112 case Intrinsic::x86_avx2_psrli_w:
10113 case Intrinsic::x86_avx2_psrli_d:
10114 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010115 case Intrinsic::x86_sse2_psrai_w:
10116 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010117 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010118 case Intrinsic::x86_avx2_psrai_d: {
10119 unsigned Opcode;
10120 switch (IntNo) {
10121 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10122 case Intrinsic::x86_sse2_pslli_w:
10123 case Intrinsic::x86_sse2_pslli_d:
10124 case Intrinsic::x86_sse2_pslli_q:
10125 case Intrinsic::x86_avx2_pslli_w:
10126 case Intrinsic::x86_avx2_pslli_d:
10127 case Intrinsic::x86_avx2_pslli_q:
10128 Opcode = X86ISD::VSHLI;
10129 break;
10130 case Intrinsic::x86_sse2_psrli_w:
10131 case Intrinsic::x86_sse2_psrli_d:
10132 case Intrinsic::x86_sse2_psrli_q:
10133 case Intrinsic::x86_avx2_psrli_w:
10134 case Intrinsic::x86_avx2_psrli_d:
10135 case Intrinsic::x86_avx2_psrli_q:
10136 Opcode = X86ISD::VSRLI;
10137 break;
10138 case Intrinsic::x86_sse2_psrai_w:
10139 case Intrinsic::x86_sse2_psrai_d:
10140 case Intrinsic::x86_avx2_psrai_w:
10141 case Intrinsic::x86_avx2_psrai_d:
10142 Opcode = X86ISD::VSRAI;
10143 break;
10144 }
10145 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010146 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010147 }
10148
Craig Topper4feb6472012-08-06 06:22:36 +000010149 case Intrinsic::x86_sse42_pcmpistria128:
10150 case Intrinsic::x86_sse42_pcmpestria128:
10151 case Intrinsic::x86_sse42_pcmpistric128:
10152 case Intrinsic::x86_sse42_pcmpestric128:
10153 case Intrinsic::x86_sse42_pcmpistrio128:
10154 case Intrinsic::x86_sse42_pcmpestrio128:
10155 case Intrinsic::x86_sse42_pcmpistris128:
10156 case Intrinsic::x86_sse42_pcmpestris128:
10157 case Intrinsic::x86_sse42_pcmpistriz128:
10158 case Intrinsic::x86_sse42_pcmpestriz128: {
10159 unsigned Opcode;
10160 unsigned X86CC;
10161 switch (IntNo) {
10162 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10163 case Intrinsic::x86_sse42_pcmpistria128:
10164 Opcode = X86ISD::PCMPISTRI;
10165 X86CC = X86::COND_A;
10166 break;
10167 case Intrinsic::x86_sse42_pcmpestria128:
10168 Opcode = X86ISD::PCMPESTRI;
10169 X86CC = X86::COND_A;
10170 break;
10171 case Intrinsic::x86_sse42_pcmpistric128:
10172 Opcode = X86ISD::PCMPISTRI;
10173 X86CC = X86::COND_B;
10174 break;
10175 case Intrinsic::x86_sse42_pcmpestric128:
10176 Opcode = X86ISD::PCMPESTRI;
10177 X86CC = X86::COND_B;
10178 break;
10179 case Intrinsic::x86_sse42_pcmpistrio128:
10180 Opcode = X86ISD::PCMPISTRI;
10181 X86CC = X86::COND_O;
10182 break;
10183 case Intrinsic::x86_sse42_pcmpestrio128:
10184 Opcode = X86ISD::PCMPESTRI;
10185 X86CC = X86::COND_O;
10186 break;
10187 case Intrinsic::x86_sse42_pcmpistris128:
10188 Opcode = X86ISD::PCMPISTRI;
10189 X86CC = X86::COND_S;
10190 break;
10191 case Intrinsic::x86_sse42_pcmpestris128:
10192 Opcode = X86ISD::PCMPESTRI;
10193 X86CC = X86::COND_S;
10194 break;
10195 case Intrinsic::x86_sse42_pcmpistriz128:
10196 Opcode = X86ISD::PCMPISTRI;
10197 X86CC = X86::COND_E;
10198 break;
10199 case Intrinsic::x86_sse42_pcmpestriz128:
10200 Opcode = X86ISD::PCMPESTRI;
10201 X86CC = X86::COND_E;
10202 break;
10203 }
10204 SmallVector<SDValue, 5> NewOps;
10205 NewOps.append(Op->op_begin()+1, Op->op_end());
10206 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10207 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10208 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10209 DAG.getConstant(X86CC, MVT::i8),
10210 SDValue(PCMP.getNode(), 1));
10211 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10212 }
Craig Topper6d688152012-08-14 07:43:25 +000010213
Craig Topper4feb6472012-08-06 06:22:36 +000010214 case Intrinsic::x86_sse42_pcmpistri128:
10215 case Intrinsic::x86_sse42_pcmpestri128: {
10216 unsigned Opcode;
10217 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10218 Opcode = X86ISD::PCMPISTRI;
10219 else
10220 Opcode = X86ISD::PCMPESTRI;
10221
10222 SmallVector<SDValue, 5> NewOps;
10223 NewOps.append(Op->op_begin()+1, Op->op_end());
10224 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10225 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10226 }
Craig Topper0e292372012-08-24 04:03:22 +000010227 case Intrinsic::x86_fma_vfmadd_ps:
10228 case Intrinsic::x86_fma_vfmadd_pd:
10229 case Intrinsic::x86_fma_vfmsub_ps:
10230 case Intrinsic::x86_fma_vfmsub_pd:
10231 case Intrinsic::x86_fma_vfnmadd_ps:
10232 case Intrinsic::x86_fma_vfnmadd_pd:
10233 case Intrinsic::x86_fma_vfnmsub_ps:
10234 case Intrinsic::x86_fma_vfnmsub_pd:
10235 case Intrinsic::x86_fma_vfmaddsub_ps:
10236 case Intrinsic::x86_fma_vfmaddsub_pd:
10237 case Intrinsic::x86_fma_vfmsubadd_ps:
10238 case Intrinsic::x86_fma_vfmsubadd_pd:
10239 case Intrinsic::x86_fma_vfmadd_ps_256:
10240 case Intrinsic::x86_fma_vfmadd_pd_256:
10241 case Intrinsic::x86_fma_vfmsub_ps_256:
10242 case Intrinsic::x86_fma_vfmsub_pd_256:
10243 case Intrinsic::x86_fma_vfnmadd_ps_256:
10244 case Intrinsic::x86_fma_vfnmadd_pd_256:
10245 case Intrinsic::x86_fma_vfnmsub_ps_256:
10246 case Intrinsic::x86_fma_vfnmsub_pd_256:
10247 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10248 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10249 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10250 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010251 unsigned Opc;
10252 switch (IntNo) {
10253 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10254 case Intrinsic::x86_fma_vfmadd_ps:
10255 case Intrinsic::x86_fma_vfmadd_pd:
10256 case Intrinsic::x86_fma_vfmadd_ps_256:
10257 case Intrinsic::x86_fma_vfmadd_pd_256:
10258 Opc = X86ISD::FMADD;
10259 break;
10260 case Intrinsic::x86_fma_vfmsub_ps:
10261 case Intrinsic::x86_fma_vfmsub_pd:
10262 case Intrinsic::x86_fma_vfmsub_ps_256:
10263 case Intrinsic::x86_fma_vfmsub_pd_256:
10264 Opc = X86ISD::FMSUB;
10265 break;
10266 case Intrinsic::x86_fma_vfnmadd_ps:
10267 case Intrinsic::x86_fma_vfnmadd_pd:
10268 case Intrinsic::x86_fma_vfnmadd_ps_256:
10269 case Intrinsic::x86_fma_vfnmadd_pd_256:
10270 Opc = X86ISD::FNMADD;
10271 break;
10272 case Intrinsic::x86_fma_vfnmsub_ps:
10273 case Intrinsic::x86_fma_vfnmsub_pd:
10274 case Intrinsic::x86_fma_vfnmsub_ps_256:
10275 case Intrinsic::x86_fma_vfnmsub_pd_256:
10276 Opc = X86ISD::FNMSUB;
10277 break;
10278 case Intrinsic::x86_fma_vfmaddsub_ps:
10279 case Intrinsic::x86_fma_vfmaddsub_pd:
10280 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10281 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10282 Opc = X86ISD::FMADDSUB;
10283 break;
10284 case Intrinsic::x86_fma_vfmsubadd_ps:
10285 case Intrinsic::x86_fma_vfmsubadd_pd:
10286 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10287 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10288 Opc = X86ISD::FMSUBADD;
10289 break;
10290 }
10291
10292 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10293 Op.getOperand(2), Op.getOperand(3));
10294 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010295 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010296}
Evan Cheng72261582005-12-20 06:22:03 +000010297
Craig Topper55b24052012-09-11 06:15:32 +000010298static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010299 DebugLoc dl = Op.getDebugLoc();
10300 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10301 switch (IntNo) {
10302 default: return SDValue(); // Don't custom lower most intrinsics.
10303
10304 // RDRAND intrinsics.
10305 case Intrinsic::x86_rdrand_16:
10306 case Intrinsic::x86_rdrand_32:
10307 case Intrinsic::x86_rdrand_64: {
10308 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010309 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10310 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010311
10312 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10313 // return the value from Rand, which is always 0, casted to i32.
10314 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10315 DAG.getConstant(1, Op->getValueType(1)),
10316 DAG.getConstant(X86::COND_B, MVT::i32),
10317 SDValue(Result.getNode(), 1) };
10318 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10319 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10320 Ops, 4);
10321
10322 // Return { result, isValid, chain }.
10323 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010324 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010325 }
10326 }
10327}
10328
Dan Gohmand858e902010-04-17 15:26:15 +000010329SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10330 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010331 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10332 MFI->setReturnAddressIsTaken(true);
10333
Bill Wendling64e87322009-01-16 19:25:27 +000010334 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010335 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010336
10337 if (Depth > 0) {
10338 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10339 SDValue Offset =
10340 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010341 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010342 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010343 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010344 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010345 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010346 }
10347
10348 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010349 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010350 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010351 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010352}
10353
Dan Gohmand858e902010-04-17 15:26:15 +000010354SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010355 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10356 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010357
Owen Andersone50ed302009-08-10 22:56:29 +000010358 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010359 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010360 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10361 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010362 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010363 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010364 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10365 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010366 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010367 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010368}
10369
Dan Gohman475871a2008-07-27 21:46:04 +000010370SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010371 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010372 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010373}
10374
Dan Gohmand858e902010-04-17 15:26:15 +000010375SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010376 SDValue Chain = Op.getOperand(0);
10377 SDValue Offset = Op.getOperand(1);
10378 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010379 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010380
Dan Gohmand8816272010-08-11 18:14:00 +000010381 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10382 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10383 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010384 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010385
Dan Gohmand8816272010-08-11 18:14:00 +000010386 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10387 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010388 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010389 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10390 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010391 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010392
Dale Johannesene4d209d2009-02-03 20:21:25 +000010393 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010394 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010395 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010396}
10397
Craig Topper55b24052012-09-11 06:15:32 +000010398static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010399 return Op.getOperand(0);
10400}
10401
10402SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10403 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010404 SDValue Root = Op.getOperand(0);
10405 SDValue Trmp = Op.getOperand(1); // trampoline
10406 SDValue FPtr = Op.getOperand(2); // nested function
10407 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010408 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010409
Dan Gohman69de1932008-02-06 22:27:42 +000010410 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010411
10412 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010413 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010414
10415 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010416 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10417 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010418
Evan Cheng0e6a0522011-07-18 20:57:22 +000010419 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10420 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010421
10422 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10423
10424 // Load the pointer to the nested function into R11.
10425 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010426 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010427 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010428 Addr, MachinePointerInfo(TrmpAddr),
10429 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010430
Owen Anderson825b72b2009-08-11 20:47:22 +000010431 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10432 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010433 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10434 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010435 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010436
10437 // Load the 'nest' parameter value into R10.
10438 // R10 is specified in X86CallingConv.td
10439 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010440 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10441 DAG.getConstant(10, MVT::i64));
10442 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010443 Addr, MachinePointerInfo(TrmpAddr, 10),
10444 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010445
Owen Anderson825b72b2009-08-11 20:47:22 +000010446 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10447 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010448 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10449 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010450 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010451
10452 // Jump to the nested function.
10453 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010454 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10455 DAG.getConstant(20, MVT::i64));
10456 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010457 Addr, MachinePointerInfo(TrmpAddr, 20),
10458 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010459
10460 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010461 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10462 DAG.getConstant(22, MVT::i64));
10463 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010464 MachinePointerInfo(TrmpAddr, 22),
10465 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010466
Duncan Sands4a544a72011-09-06 13:37:06 +000010467 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010468 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010469 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010470 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010471 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010472 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010473
10474 switch (CC) {
10475 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010476 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010477 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010478 case CallingConv::X86_StdCall: {
10479 // Pass 'nest' parameter in ECX.
10480 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010481 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010482
10483 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010484 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010485 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010486
Chris Lattner58d74912008-03-12 17:45:29 +000010487 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010488 unsigned InRegCount = 0;
10489 unsigned Idx = 1;
10490
10491 for (FunctionType::param_iterator I = FTy->param_begin(),
10492 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010493 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010494 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010495 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010496
10497 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010498 report_fatal_error("Nest register in use - reduce number of inreg"
10499 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010500 }
10501 }
10502 break;
10503 }
10504 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010505 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010506 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010507 // Pass 'nest' parameter in EAX.
10508 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010509 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010510 break;
10511 }
10512
Dan Gohman475871a2008-07-27 21:46:04 +000010513 SDValue OutChains[4];
10514 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010515
Owen Anderson825b72b2009-08-11 20:47:22 +000010516 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10517 DAG.getConstant(10, MVT::i32));
10518 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010519
Chris Lattnera62fe662010-02-05 19:20:30 +000010520 // This is storing the opcode for MOV32ri.
10521 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010522 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010523 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010524 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010525 Trmp, MachinePointerInfo(TrmpAddr),
10526 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010527
Owen Anderson825b72b2009-08-11 20:47:22 +000010528 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10529 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010530 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10531 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010532 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010533
Chris Lattnera62fe662010-02-05 19:20:30 +000010534 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010535 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10536 DAG.getConstant(5, MVT::i32));
10537 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010538 MachinePointerInfo(TrmpAddr, 5),
10539 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010540
Owen Anderson825b72b2009-08-11 20:47:22 +000010541 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10542 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010543 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10544 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010545 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010546
Duncan Sands4a544a72011-09-06 13:37:06 +000010547 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010548 }
10549}
10550
Dan Gohmand858e902010-04-17 15:26:15 +000010551SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10552 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010553 /*
10554 The rounding mode is in bits 11:10 of FPSR, and has the following
10555 settings:
10556 00 Round to nearest
10557 01 Round to -inf
10558 10 Round to +inf
10559 11 Round to 0
10560
10561 FLT_ROUNDS, on the other hand, expects the following:
10562 -1 Undefined
10563 0 Round to 0
10564 1 Round to nearest
10565 2 Round to +inf
10566 3 Round to -inf
10567
10568 To perform the conversion, we do:
10569 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10570 */
10571
10572 MachineFunction &MF = DAG.getMachineFunction();
10573 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010574 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010575 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010576 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010577 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010578
10579 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010580 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010581 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010582
Michael J. Spencerec38de22010-10-10 22:04:20 +000010583
Chris Lattner2156b792010-09-22 01:11:26 +000010584 MachineMemOperand *MMO =
10585 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10586 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010587
Chris Lattner2156b792010-09-22 01:11:26 +000010588 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10589 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10590 DAG.getVTList(MVT::Other),
10591 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010592
10593 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010594 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010595 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010596
10597 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010598 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010599 DAG.getNode(ISD::SRL, DL, MVT::i16,
10600 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010601 CWD, DAG.getConstant(0x800, MVT::i16)),
10602 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010603 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010604 DAG.getNode(ISD::SRL, DL, MVT::i16,
10605 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010606 CWD, DAG.getConstant(0x400, MVT::i16)),
10607 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010608
Dan Gohman475871a2008-07-27 21:46:04 +000010609 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010610 DAG.getNode(ISD::AND, DL, MVT::i16,
10611 DAG.getNode(ISD::ADD, DL, MVT::i16,
10612 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010613 DAG.getConstant(1, MVT::i16)),
10614 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010615
10616
Duncan Sands83ec4b62008-06-06 12:08:01 +000010617 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010618 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010619}
10620
Craig Topper55b24052012-09-11 06:15:32 +000010621static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010622 EVT VT = Op.getValueType();
10623 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010624 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010625 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010626
10627 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010628 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010629 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010630 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010631 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010632 }
Evan Cheng18efe262007-12-14 02:13:44 +000010633
Evan Cheng152804e2007-12-14 08:30:15 +000010634 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010635 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010636 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010637
10638 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010639 SDValue Ops[] = {
10640 Op,
10641 DAG.getConstant(NumBits+NumBits-1, OpVT),
10642 DAG.getConstant(X86::COND_E, MVT::i8),
10643 Op.getValue(1)
10644 };
10645 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010646
10647 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010648 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010649
Owen Anderson825b72b2009-08-11 20:47:22 +000010650 if (VT == MVT::i8)
10651 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010652 return Op;
10653}
10654
Craig Topper55b24052012-09-11 06:15:32 +000010655static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010656 EVT VT = Op.getValueType();
10657 EVT OpVT = VT;
10658 unsigned NumBits = VT.getSizeInBits();
10659 DebugLoc dl = Op.getDebugLoc();
10660
10661 Op = Op.getOperand(0);
10662 if (VT == MVT::i8) {
10663 // Zero extend to i32 since there is not an i8 bsr.
10664 OpVT = MVT::i32;
10665 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10666 }
10667
10668 // Issue a bsr (scan bits in reverse).
10669 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10670 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10671
10672 // And xor with NumBits-1.
10673 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10674
10675 if (VT == MVT::i8)
10676 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10677 return Op;
10678}
10679
Craig Topper55b24052012-09-11 06:15:32 +000010680static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010681 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010682 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010683 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010684 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010685
10686 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010687 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010688 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010689
10690 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010691 SDValue Ops[] = {
10692 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010693 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010694 DAG.getConstant(X86::COND_E, MVT::i8),
10695 Op.getValue(1)
10696 };
Chandler Carruth77821022011-12-24 12:12:34 +000010697 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010698}
10699
Craig Topper13894fa2011-08-24 06:14:18 +000010700// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10701// ones, and then concatenate the result back.
10702static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010703 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010704
Craig Topper7a9a28b2012-08-12 02:23:29 +000010705 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010706 "Unsupported value type for operation");
10707
Craig Topper66ddd152012-04-27 22:54:43 +000010708 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010709 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010710
10711 // Extract the LHS vectors
10712 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010713 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10714 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010715
10716 // Extract the RHS vectors
10717 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010718 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10719 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010720
10721 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10722 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10723
10724 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10725 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10726 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10727}
10728
Craig Topper55b24052012-09-11 06:15:32 +000010729static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010730 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010731 Op.getValueType().isInteger() &&
10732 "Only handle AVX 256-bit vector integer operation");
10733 return Lower256IntArith(Op, DAG);
10734}
10735
Craig Topper55b24052012-09-11 06:15:32 +000010736static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010737 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010738 Op.getValueType().isInteger() &&
10739 "Only handle AVX 256-bit vector integer operation");
10740 return Lower256IntArith(Op, DAG);
10741}
10742
Craig Topper55b24052012-09-11 06:15:32 +000010743static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10744 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010745 EVT VT = Op.getValueType();
10746
10747 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010748 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010749 return Lower256IntArith(Op, DAG);
10750
Craig Topper5b209e82012-02-05 03:14:49 +000010751 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10752 "Only know how to lower V2I64/V4I64 multiply");
10753
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010754 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010755
Craig Topper5b209e82012-02-05 03:14:49 +000010756 // Ahi = psrlqi(a, 32);
10757 // Bhi = psrlqi(b, 32);
10758 //
10759 // AloBlo = pmuludq(a, b);
10760 // AloBhi = pmuludq(a, Bhi);
10761 // AhiBlo = pmuludq(Ahi, b);
10762
10763 // AloBhi = psllqi(AloBhi, 32);
10764 // AhiBlo = psllqi(AhiBlo, 32);
10765 // return AloBlo + AloBhi + AhiBlo;
10766
Craig Topperaaa643c2011-11-09 07:28:55 +000010767 SDValue A = Op.getOperand(0);
10768 SDValue B = Op.getOperand(1);
10769
Craig Topper5b209e82012-02-05 03:14:49 +000010770 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010771
Craig Topper5b209e82012-02-05 03:14:49 +000010772 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10773 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010774
Craig Topper5b209e82012-02-05 03:14:49 +000010775 // Bit cast to 32-bit vectors for MULUDQ
10776 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10777 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10778 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10779 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10780 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010781
Craig Topper5b209e82012-02-05 03:14:49 +000010782 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10783 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10784 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010785
Craig Topper5b209e82012-02-05 03:14:49 +000010786 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10787 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010788
Dale Johannesene4d209d2009-02-03 20:21:25 +000010789 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010790 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010791}
10792
Nadav Rotem43012222011-05-11 08:12:09 +000010793SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10794
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010795 EVT VT = Op.getValueType();
10796 DebugLoc dl = Op.getDebugLoc();
10797 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010798 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010799 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010800
Craig Topper1accb7e2012-01-10 06:54:16 +000010801 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010802 return SDValue();
10803
Nadav Rotem43012222011-05-11 08:12:09 +000010804 // Optimize shl/srl/sra with constant shift amount.
10805 if (isSplatVector(Amt.getNode())) {
10806 SDValue SclrAmt = Amt->getOperand(0);
10807 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10808 uint64_t ShiftAmt = C->getZExtValue();
10809
Craig Toppered2e13d2012-01-22 19:15:14 +000010810 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10811 (Subtarget->hasAVX2() &&
10812 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10813 if (Op.getOpcode() == ISD::SHL)
10814 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10815 DAG.getConstant(ShiftAmt, MVT::i32));
10816 if (Op.getOpcode() == ISD::SRL)
10817 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10818 DAG.getConstant(ShiftAmt, MVT::i32));
10819 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10820 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10821 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010822 }
10823
Craig Toppered2e13d2012-01-22 19:15:14 +000010824 if (VT == MVT::v16i8) {
10825 if (Op.getOpcode() == ISD::SHL) {
10826 // Make a large shift.
10827 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10828 DAG.getConstant(ShiftAmt, MVT::i32));
10829 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10830 // Zero out the rightmost bits.
10831 SmallVector<SDValue, 16> V(16,
10832 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10833 MVT::i8));
10834 return DAG.getNode(ISD::AND, dl, VT, SHL,
10835 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010836 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010837 if (Op.getOpcode() == ISD::SRL) {
10838 // Make a large shift.
10839 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10840 DAG.getConstant(ShiftAmt, MVT::i32));
10841 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10842 // Zero out the leftmost bits.
10843 SmallVector<SDValue, 16> V(16,
10844 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10845 MVT::i8));
10846 return DAG.getNode(ISD::AND, dl, VT, SRL,
10847 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10848 }
10849 if (Op.getOpcode() == ISD::SRA) {
10850 if (ShiftAmt == 7) {
10851 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010852 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010853 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010854 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010855
Craig Toppered2e13d2012-01-22 19:15:14 +000010856 // R s>> a === ((R u>> a) ^ m) - m
10857 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10858 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10859 MVT::i8));
10860 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10861 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10862 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10863 return Res;
10864 }
Craig Topper731dfd02012-04-23 03:42:40 +000010865 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010866 }
Craig Topper46154eb2011-11-11 07:39:23 +000010867
Craig Topper0d86d462011-11-20 00:12:05 +000010868 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10869 if (Op.getOpcode() == ISD::SHL) {
10870 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010871 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10872 DAG.getConstant(ShiftAmt, MVT::i32));
10873 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010874 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010875 SmallVector<SDValue, 32> V(32,
10876 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10877 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010878 return DAG.getNode(ISD::AND, dl, VT, SHL,
10879 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010880 }
Craig Topper0d86d462011-11-20 00:12:05 +000010881 if (Op.getOpcode() == ISD::SRL) {
10882 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010883 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10884 DAG.getConstant(ShiftAmt, MVT::i32));
10885 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010886 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010887 SmallVector<SDValue, 32> V(32,
10888 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10889 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010890 return DAG.getNode(ISD::AND, dl, VT, SRL,
10891 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10892 }
10893 if (Op.getOpcode() == ISD::SRA) {
10894 if (ShiftAmt == 7) {
10895 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010896 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010897 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010898 }
10899
10900 // R s>> a === ((R u>> a) ^ m) - m
10901 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10902 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10903 MVT::i8));
10904 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10905 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10906 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10907 return Res;
10908 }
Craig Topper731dfd02012-04-23 03:42:40 +000010909 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010910 }
Nadav Rotem43012222011-05-11 08:12:09 +000010911 }
10912 }
10913
10914 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010915 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010916 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10917 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010918
Chris Lattner7302d802012-02-06 21:56:39 +000010919 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10920 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010921 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10922 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010923 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010924 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010925
10926 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010927 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010928 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10929 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10930 }
Nadav Rotem43012222011-05-11 08:12:09 +000010931 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010932 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010933
Nate Begeman51409212010-07-28 00:21:48 +000010934 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010935 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10936 DAG.getConstant(5, MVT::i32));
10937 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010938
Lang Hames8b99c1e2011-12-17 01:08:46 +000010939 // Turn 'a' into a mask suitable for VSELECT
10940 SDValue VSelM = DAG.getConstant(0x80, VT);
10941 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010942 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010943
Lang Hames8b99c1e2011-12-17 01:08:46 +000010944 SDValue CM1 = DAG.getConstant(0x0f, VT);
10945 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010946
Lang Hames8b99c1e2011-12-17 01:08:46 +000010947 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10948 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010949 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10950 DAG.getConstant(4, MVT::i32), DAG);
10951 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010952 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10953
Nate Begeman51409212010-07-28 00:21:48 +000010954 // a += a
10955 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010956 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010957 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010958
Lang Hames8b99c1e2011-12-17 01:08:46 +000010959 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10960 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010961 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10962 DAG.getConstant(2, MVT::i32), DAG);
10963 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010964 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10965
Nate Begeman51409212010-07-28 00:21:48 +000010966 // a += a
10967 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010968 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010969 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010970
Lang Hames8b99c1e2011-12-17 01:08:46 +000010971 // return VSELECT(r, r+r, a);
10972 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010973 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010974 return R;
10975 }
Craig Topper46154eb2011-11-11 07:39:23 +000010976
10977 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010978 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010979 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010980 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10981 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10982
10983 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010984 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10985 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010986
10987 // Recreate the shift amount vectors
10988 SDValue Amt1, Amt2;
10989 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10990 // Constant shift amount
10991 SmallVector<SDValue, 4> Amt1Csts;
10992 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010993 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010994 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010995 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010996 Amt2Csts.push_back(Amt->getOperand(i));
10997
10998 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10999 &Amt1Csts[0], NumElems/2);
11000 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11001 &Amt2Csts[0], NumElems/2);
11002 } else {
11003 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011004 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11005 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011006 }
11007
11008 // Issue new vector shifts for the smaller types
11009 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11010 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11011
11012 // Concatenate the result back
11013 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11014 }
11015
Nate Begeman51409212010-07-28 00:21:48 +000011016 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011017}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011018
Craig Topper55b24052012-09-11 06:15:32 +000011019static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011020 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11021 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011022 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11023 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011024 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011025 SDValue LHS = N->getOperand(0);
11026 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011027 unsigned BaseOp = 0;
11028 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011029 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011030 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011031 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011032 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011033 // A subtract of one will be selected as a INC. Note that INC doesn't
11034 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011035 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11036 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011037 BaseOp = X86ISD::INC;
11038 Cond = X86::COND_O;
11039 break;
11040 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011041 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011042 Cond = X86::COND_O;
11043 break;
11044 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011045 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011046 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011047 break;
11048 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011049 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11050 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011051 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11052 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011053 BaseOp = X86ISD::DEC;
11054 Cond = X86::COND_O;
11055 break;
11056 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011057 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011058 Cond = X86::COND_O;
11059 break;
11060 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011061 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011062 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011063 break;
11064 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011065 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011066 Cond = X86::COND_O;
11067 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011068 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11069 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11070 MVT::i32);
11071 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011072
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011073 SDValue SetCC =
11074 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11075 DAG.getConstant(X86::COND_O, MVT::i32),
11076 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011077
Dan Gohman6e5fda22011-07-22 18:45:15 +000011078 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011079 }
Bill Wendling74c37652008-12-09 22:08:41 +000011080 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011081
Bill Wendling61edeb52008-12-02 01:06:39 +000011082 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011083 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011084 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011085
Bill Wendling61edeb52008-12-02 01:06:39 +000011086 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011087 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11088 DAG.getConstant(Cond, MVT::i32),
11089 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011090
Dan Gohman6e5fda22011-07-22 18:45:15 +000011091 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011092}
11093
Chad Rosier30450e82011-12-22 22:35:21 +000011094SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11095 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011096 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011097 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11098 EVT VT = Op.getValueType();
11099
Craig Toppered2e13d2012-01-22 19:15:14 +000011100 if (!Subtarget->hasSSE2() || !VT.isVector())
11101 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011102
Craig Toppered2e13d2012-01-22 19:15:14 +000011103 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11104 ExtraVT.getScalarType().getSizeInBits();
11105 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11106
11107 switch (VT.getSimpleVT().SimpleTy) {
11108 default: return SDValue();
11109 case MVT::v8i32:
11110 case MVT::v16i16:
11111 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011112 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011113 if (!Subtarget->hasAVX2()) {
11114 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011115 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011116
Craig Toppered2e13d2012-01-22 19:15:14 +000011117 // Extract the LHS vectors
11118 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011119 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11120 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011121
Craig Toppered2e13d2012-01-22 19:15:14 +000011122 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11123 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011124
Craig Toppered2e13d2012-01-22 19:15:14 +000011125 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011126 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011127 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11128 ExtraNumElems/2);
11129 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011130
Craig Toppered2e13d2012-01-22 19:15:14 +000011131 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11132 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011133
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011134 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011135 }
11136 // fall through
11137 case MVT::v4i32:
11138 case MVT::v8i16: {
11139 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11140 Op.getOperand(0), ShAmt, DAG);
11141 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011142 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011143 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011144}
11145
11146
Craig Topper55b24052012-09-11 06:15:32 +000011147static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11148 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011149 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011150
Eric Christopher77ed1352011-07-08 00:04:56 +000011151 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11152 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011153 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011154 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011155 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011156 SDValue Ops[] = {
11157 DAG.getRegister(X86::ESP, MVT::i32), // Base
11158 DAG.getTargetConstant(1, MVT::i8), // Scale
11159 DAG.getRegister(0, MVT::i32), // Index
11160 DAG.getTargetConstant(0, MVT::i32), // Disp
11161 DAG.getRegister(0, MVT::i32), // Segment.
11162 Zero,
11163 Chain
11164 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011165 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011166 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11167 array_lengthof(Ops));
11168 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011169 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011170
Eric Christopher9a9d2752010-07-22 02:48:34 +000011171 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011172 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011173 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011174
Chris Lattner132929a2010-08-14 17:26:09 +000011175 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11176 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11177 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11178 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011179
Chris Lattner132929a2010-08-14 17:26:09 +000011180 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11181 if (!Op1 && !Op2 && !Op3 && Op4)
11182 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011183
Chris Lattner132929a2010-08-14 17:26:09 +000011184 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11185 if (Op1 && !Op2 && !Op3 && !Op4)
11186 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011187
11188 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011189 // (MFENCE)>;
11190 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011191}
11192
Craig Topper55b24052012-09-11 06:15:32 +000011193static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11194 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011195 DebugLoc dl = Op.getDebugLoc();
11196 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11197 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11198 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11199 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11200
11201 // The only fence that needs an instruction is a sequentially-consistent
11202 // cross-thread fence.
11203 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11204 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11205 // no-sse2). There isn't any reason to disable it if the target processor
11206 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011207 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011208 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11209
11210 SDValue Chain = Op.getOperand(0);
11211 SDValue Zero = DAG.getConstant(0, MVT::i32);
11212 SDValue Ops[] = {
11213 DAG.getRegister(X86::ESP, MVT::i32), // Base
11214 DAG.getTargetConstant(1, MVT::i8), // Scale
11215 DAG.getRegister(0, MVT::i32), // Index
11216 DAG.getTargetConstant(0, MVT::i32), // Disp
11217 DAG.getRegister(0, MVT::i32), // Segment.
11218 Zero,
11219 Chain
11220 };
11221 SDNode *Res =
11222 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11223 array_lengthof(Ops));
11224 return SDValue(Res, 0);
11225 }
11226
11227 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11228 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11229}
11230
11231
Craig Topper55b24052012-09-11 06:15:32 +000011232static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11233 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011234 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011235 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011236 unsigned Reg = 0;
11237 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011238 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011239 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011240 case MVT::i8: Reg = X86::AL; size = 1; break;
11241 case MVT::i16: Reg = X86::AX; size = 2; break;
11242 case MVT::i32: Reg = X86::EAX; size = 4; break;
11243 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011244 assert(Subtarget->is64Bit() && "Node not type legal!");
11245 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011246 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011247 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011248 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011249 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011250 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011251 Op.getOperand(1),
11252 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011253 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011254 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011255 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011256 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11257 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11258 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011259 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011260 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011261 return cpOut;
11262}
11263
Craig Topper55b24052012-09-11 06:15:32 +000011264static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11265 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011266 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011267 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011268 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011269 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011270 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011271 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11272 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011273 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011274 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11275 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011276 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011277 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011278 rdx.getValue(1)
11279 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011280 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011281}
11282
Craig Topper55b24052012-09-11 06:15:32 +000011283SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011284 EVT SrcVT = Op.getOperand(0).getValueType();
11285 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011286 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011287 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011288 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011289 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011290 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011291 // i64 <=> MMX conversions are Legal.
11292 if (SrcVT==MVT::i64 && DstVT.isVector())
11293 return Op;
11294 if (DstVT==MVT::i64 && SrcVT.isVector())
11295 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011296 // MMX <=> MMX conversions are Legal.
11297 if (SrcVT.isVector() && DstVT.isVector())
11298 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011299 // All other conversions need to be expanded.
11300 return SDValue();
11301}
Chris Lattner5b856542010-12-20 00:59:46 +000011302
Craig Topper55b24052012-09-11 06:15:32 +000011303static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011304 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011305 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011306 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011307 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011308 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011309 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011310 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011311 Node->getOperand(0),
11312 Node->getOperand(1), negOp,
11313 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011314 cast<AtomicSDNode>(Node)->getAlignment(),
11315 cast<AtomicSDNode>(Node)->getOrdering(),
11316 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011317}
11318
Eli Friedman327236c2011-08-24 20:50:09 +000011319static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11320 SDNode *Node = Op.getNode();
11321 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011322 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011323
11324 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011325 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11326 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11327 // (The only way to get a 16-byte store is cmpxchg16b)
11328 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11329 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11330 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011331 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11332 cast<AtomicSDNode>(Node)->getMemoryVT(),
11333 Node->getOperand(0),
11334 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011335 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011336 cast<AtomicSDNode>(Node)->getOrdering(),
11337 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011338 return Swap.getValue(1);
11339 }
11340 // Other atomic stores have a simple pattern.
11341 return Op;
11342}
11343
Chris Lattner5b856542010-12-20 00:59:46 +000011344static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11345 EVT VT = Op.getNode()->getValueType(0);
11346
11347 // Let legalize expand this if it isn't a legal type yet.
11348 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11349 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011350
Chris Lattner5b856542010-12-20 00:59:46 +000011351 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011352
Chris Lattner5b856542010-12-20 00:59:46 +000011353 unsigned Opc;
11354 bool ExtraOp = false;
11355 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011356 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011357 case ISD::ADDC: Opc = X86ISD::ADD; break;
11358 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11359 case ISD::SUBC: Opc = X86ISD::SUB; break;
11360 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11361 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011362
Chris Lattner5b856542010-12-20 00:59:46 +000011363 if (!ExtraOp)
11364 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11365 Op.getOperand(1));
11366 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11367 Op.getOperand(1), Op.getOperand(2));
11368}
11369
Evan Cheng0db9fe62006-04-25 20:13:52 +000011370/// LowerOperation - Provide custom lowering hooks for some operations.
11371///
Dan Gohmand858e902010-04-17 15:26:15 +000011372SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011373 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011374 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011375 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011376 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11377 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11378 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011379 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011380 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011381 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011382 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011383 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11384 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11385 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011386 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11387 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011388 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11389 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11390 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011391 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011392 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011393 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011394 case ISD::SHL_PARTS:
11395 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011396 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011397 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011398 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011399 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011400 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011401 case ISD::FABS: return LowerFABS(Op, DAG);
11402 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011403 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011404 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011405 case ISD::SETCC: return LowerSETCC(Op, DAG);
11406 case ISD::SELECT: return LowerSELECT(Op, DAG);
11407 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011408 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011409 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011410 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011411 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011412 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011413 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011414 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11415 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011416 case ISD::FRAME_TO_ARGS_OFFSET:
11417 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011418 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011419 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011420 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11421 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011422 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011423 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011424 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011425 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011426 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011427 case ISD::SRA:
11428 case ISD::SRL:
11429 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011430 case ISD::SADDO:
11431 case ISD::UADDO:
11432 case ISD::SSUBO:
11433 case ISD::USUBO:
11434 case ISD::SMULO:
11435 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011436 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011437 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011438 case ISD::ADDC:
11439 case ISD::ADDE:
11440 case ISD::SUBC:
11441 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011442 case ISD::ADD: return LowerADD(Op, DAG);
11443 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011444 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011445}
11446
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011447static void ReplaceATOMIC_LOAD(SDNode *Node,
11448 SmallVectorImpl<SDValue> &Results,
11449 SelectionDAG &DAG) {
11450 DebugLoc dl = Node->getDebugLoc();
11451 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11452
11453 // Convert wide load -> cmpxchg8b/cmpxchg16b
11454 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11455 // (The only way to get a 16-byte load is cmpxchg16b)
11456 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011457 SDValue Zero = DAG.getConstant(0, VT);
11458 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011459 Node->getOperand(0),
11460 Node->getOperand(1), Zero, Zero,
11461 cast<AtomicSDNode>(Node)->getMemOperand(),
11462 cast<AtomicSDNode>(Node)->getOrdering(),
11463 cast<AtomicSDNode>(Node)->getSynchScope());
11464 Results.push_back(Swap.getValue(0));
11465 Results.push_back(Swap.getValue(1));
11466}
11467
Craig Topperc0878702012-08-17 06:55:11 +000011468static void
Duncan Sands1607f052008-12-01 11:39:25 +000011469ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011470 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011471 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011472 assert (Node->getValueType(0) == MVT::i64 &&
11473 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011474
11475 SDValue Chain = Node->getOperand(0);
11476 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011477 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011478 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011479 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011480 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011481 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011482 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011483 SDValue Result =
11484 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11485 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011486 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011487 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011488 Results.push_back(Result.getValue(2));
11489}
11490
Duncan Sands126d9072008-07-04 11:47:58 +000011491/// ReplaceNodeResults - Replace a node with an illegal result type
11492/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011493void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11494 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011495 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011496 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011497 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011498 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011499 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011500 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011501 case ISD::ADDC:
11502 case ISD::ADDE:
11503 case ISD::SUBC:
11504 case ISD::SUBE:
11505 // We don't want to expand or promote these.
11506 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011507 case ISD::FP_TO_SINT:
11508 case ISD::FP_TO_UINT: {
11509 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11510
11511 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11512 return;
11513
Eli Friedman948e95a2009-05-23 09:59:16 +000011514 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011515 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011516 SDValue FIST = Vals.first, StackSlot = Vals.second;
11517 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011518 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011519 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011520 if (StackSlot.getNode() != 0)
11521 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11522 MachinePointerInfo(),
11523 false, false, false, 0));
11524 else
11525 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011526 }
11527 return;
11528 }
11529 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011530 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011531 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011532 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011533 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011534 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011535 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011536 eax.getValue(2));
11537 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11538 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011539 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011540 Results.push_back(edx.getValue(1));
11541 return;
11542 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011543 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011544 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011545 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011546 bool Regs64bit = T == MVT::i128;
11547 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011548 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011549 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11550 DAG.getConstant(0, HalfT));
11551 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11552 DAG.getConstant(1, HalfT));
11553 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11554 Regs64bit ? X86::RAX : X86::EAX,
11555 cpInL, SDValue());
11556 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11557 Regs64bit ? X86::RDX : X86::EDX,
11558 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011559 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011560 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11561 DAG.getConstant(0, HalfT));
11562 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11563 DAG.getConstant(1, HalfT));
11564 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11565 Regs64bit ? X86::RBX : X86::EBX,
11566 swapInL, cpInH.getValue(1));
11567 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011568 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011569 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011570 SDValue Ops[] = { swapInH.getValue(0),
11571 N->getOperand(1),
11572 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011573 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011574 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011575 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11576 X86ISD::LCMPXCHG8_DAG;
11577 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011578 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011579 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11580 Regs64bit ? X86::RAX : X86::EAX,
11581 HalfT, Result.getValue(1));
11582 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11583 Regs64bit ? X86::RDX : X86::EDX,
11584 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011585 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011586 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011587 Results.push_back(cpOutH.getValue(1));
11588 return;
11589 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011590 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011591 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011592 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011593 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011594 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011595 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011596 case ISD::ATOMIC_SWAP: {
11597 unsigned Opc;
11598 switch (N->getOpcode()) {
11599 default: llvm_unreachable("Unexpected opcode");
11600 case ISD::ATOMIC_LOAD_ADD:
11601 Opc = X86ISD::ATOMADD64_DAG;
11602 break;
11603 case ISD::ATOMIC_LOAD_AND:
11604 Opc = X86ISD::ATOMAND64_DAG;
11605 break;
11606 case ISD::ATOMIC_LOAD_NAND:
11607 Opc = X86ISD::ATOMNAND64_DAG;
11608 break;
11609 case ISD::ATOMIC_LOAD_OR:
11610 Opc = X86ISD::ATOMOR64_DAG;
11611 break;
11612 case ISD::ATOMIC_LOAD_SUB:
11613 Opc = X86ISD::ATOMSUB64_DAG;
11614 break;
11615 case ISD::ATOMIC_LOAD_XOR:
11616 Opc = X86ISD::ATOMXOR64_DAG;
11617 break;
11618 case ISD::ATOMIC_SWAP:
11619 Opc = X86ISD::ATOMSWAP64_DAG;
11620 break;
11621 }
11622 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011623 return;
Craig Topperc0878702012-08-17 06:55:11 +000011624 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011625 case ISD::ATOMIC_LOAD:
11626 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011627 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011628}
11629
Evan Cheng72261582005-12-20 06:22:03 +000011630const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11631 switch (Opcode) {
11632 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011633 case X86ISD::BSF: return "X86ISD::BSF";
11634 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011635 case X86ISD::SHLD: return "X86ISD::SHLD";
11636 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011637 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011638 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011639 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011640 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011641 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011642 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011643 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11644 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11645 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011646 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011647 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011648 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011649 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011650 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011651 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011652 case X86ISD::COMI: return "X86ISD::COMI";
11653 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011654 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011655 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011656 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11657 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011658 case X86ISD::CMOV: return "X86ISD::CMOV";
11659 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011660 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011661 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11662 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011663 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011664 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011665 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011666 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011667 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011668 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11669 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011670 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011671 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011672 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011673 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011674 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011675 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11676 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11677 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011678 case X86ISD::HADD: return "X86ISD::HADD";
11679 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011680 case X86ISD::FHADD: return "X86ISD::FHADD";
11681 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011682 case X86ISD::FMAX: return "X86ISD::FMAX";
11683 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011684 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11685 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011686 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11687 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011688 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011689 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011690 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011691 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011692 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011693 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011694 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011695 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11696 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011697 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11698 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11699 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11700 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11701 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11702 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011703 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011704 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011705 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011706 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011707 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11708 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011709 case X86ISD::VSHL: return "X86ISD::VSHL";
11710 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011711 case X86ISD::VSRA: return "X86ISD::VSRA";
11712 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11713 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11714 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011715 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011716 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11717 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011718 case X86ISD::ADD: return "X86ISD::ADD";
11719 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011720 case X86ISD::ADC: return "X86ISD::ADC";
11721 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011722 case X86ISD::SMUL: return "X86ISD::SMUL";
11723 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011724 case X86ISD::INC: return "X86ISD::INC";
11725 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011726 case X86ISD::OR: return "X86ISD::OR";
11727 case X86ISD::XOR: return "X86ISD::XOR";
11728 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011729 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011730 case X86ISD::BLSI: return "X86ISD::BLSI";
11731 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11732 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011733 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011734 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011735 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011736 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11737 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11738 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011739 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011740 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011741 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011742 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011743 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011744 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11745 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011746 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11747 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11748 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011749 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11750 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011751 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11752 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011753 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011754 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011755 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011756 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11757 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011758 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011759 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011760 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011761 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011762 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011763 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011764 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011765 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011766 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011767 case X86ISD::FMADD: return "X86ISD::FMADD";
11768 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11769 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11770 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11771 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11772 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011773 }
11774}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011775
Chris Lattnerc9addb72007-03-30 23:15:24 +000011776// isLegalAddressingMode - Return true if the addressing mode represented
11777// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011778bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011779 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011780 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011781 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011782 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011783
Chris Lattnerc9addb72007-03-30 23:15:24 +000011784 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011785 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011786 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011787
Chris Lattnerc9addb72007-03-30 23:15:24 +000011788 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011789 unsigned GVFlags =
11790 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011791
Chris Lattnerdfed4132009-07-10 07:38:24 +000011792 // If a reference to this global requires an extra load, we can't fold it.
11793 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011794 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011795
Chris Lattnerdfed4132009-07-10 07:38:24 +000011796 // If BaseGV requires a register for the PIC base, we cannot also have a
11797 // BaseReg specified.
11798 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011799 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011800
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011801 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011802 if ((M != CodeModel::Small || R != Reloc::Static) &&
11803 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011804 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011805 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011806
Chris Lattnerc9addb72007-03-30 23:15:24 +000011807 switch (AM.Scale) {
11808 case 0:
11809 case 1:
11810 case 2:
11811 case 4:
11812 case 8:
11813 // These scales always work.
11814 break;
11815 case 3:
11816 case 5:
11817 case 9:
11818 // These scales are formed with basereg+scalereg. Only accept if there is
11819 // no basereg yet.
11820 if (AM.HasBaseReg)
11821 return false;
11822 break;
11823 default: // Other stuff never works.
11824 return false;
11825 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011826
Chris Lattnerc9addb72007-03-30 23:15:24 +000011827 return true;
11828}
11829
11830
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011831bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011832 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011833 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011834 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11835 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011836 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011837 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011838 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011839}
11840
Evan Cheng70e10d32012-07-17 06:53:39 +000011841bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11842 return Imm == (int32_t)Imm;
11843}
11844
11845bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011846 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011847 return Imm == (int32_t)Imm;
11848}
11849
Owen Andersone50ed302009-08-10 22:56:29 +000011850bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011851 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011852 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011853 unsigned NumBits1 = VT1.getSizeInBits();
11854 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011855 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011856 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011857 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011858}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011859
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011860bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011861 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011862 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011863}
11864
Owen Andersone50ed302009-08-10 22:56:29 +000011865bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011866 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011867 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011868}
11869
Owen Andersone50ed302009-08-10 22:56:29 +000011870bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011871 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011872 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011873}
11874
Evan Cheng60c07e12006-07-05 22:17:51 +000011875/// isShuffleMaskLegal - Targets can use this to indicate that they only
11876/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11877/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11878/// are assumed to be legal.
11879bool
Eric Christopherfd179292009-08-27 18:07:15 +000011880X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011881 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011882 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011883 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011884 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011885
Nate Begemana09008b2009-10-19 02:17:23 +000011886 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011887 return (VT.getVectorNumElements() == 2 ||
11888 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11889 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011890 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011891 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011892 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11893 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011894 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011895 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11896 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011897 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11898 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011899}
11900
Dan Gohman7d8143f2008-04-09 20:09:42 +000011901bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011902X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011903 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011904 unsigned NumElts = VT.getVectorNumElements();
11905 // FIXME: This collection of masks seems suspect.
11906 if (NumElts == 2)
11907 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011908 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011909 return (isMOVLMask(Mask, VT) ||
11910 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011911 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11912 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011913 }
11914 return false;
11915}
11916
11917//===----------------------------------------------------------------------===//
11918// X86 Scheduler Hooks
11919//===----------------------------------------------------------------------===//
11920
Mon P Wang63307c32008-05-05 19:05:59 +000011921// private utility function
Scott Michelfdc40a02009-02-17 22:15:04 +000011922
Michael Liaob118a072012-09-20 03:06:15 +000011923// Get CMPXCHG opcode for the specified data type.
11924static unsigned getCmpXChgOpcode(EVT VT) {
11925 switch (VT.getSimpleVT().SimpleTy) {
11926 case MVT::i8: return X86::LCMPXCHG8;
11927 case MVT::i16: return X86::LCMPXCHG16;
11928 case MVT::i32: return X86::LCMPXCHG32;
11929 case MVT::i64: return X86::LCMPXCHG64;
11930 default:
11931 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000011932 }
Michael Liaob118a072012-09-20 03:06:15 +000011933 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000011934}
11935
Michael Liaob118a072012-09-20 03:06:15 +000011936// Get LOAD opcode for the specified data type.
11937static unsigned getLoadOpcode(EVT VT) {
11938 switch (VT.getSimpleVT().SimpleTy) {
11939 case MVT::i8: return X86::MOV8rm;
11940 case MVT::i16: return X86::MOV16rm;
11941 case MVT::i32: return X86::MOV32rm;
11942 case MVT::i64: return X86::MOV64rm;
11943 default:
11944 break;
11945 }
11946 llvm_unreachable("Invalid operand size!");
11947}
11948
11949// Get opcode of the non-atomic one from the specified atomic instruction.
11950static unsigned getNonAtomicOpcode(unsigned Opc) {
11951 switch (Opc) {
11952 case X86::ATOMAND8: return X86::AND8rr;
11953 case X86::ATOMAND16: return X86::AND16rr;
11954 case X86::ATOMAND32: return X86::AND32rr;
11955 case X86::ATOMAND64: return X86::AND64rr;
11956 case X86::ATOMOR8: return X86::OR8rr;
11957 case X86::ATOMOR16: return X86::OR16rr;
11958 case X86::ATOMOR32: return X86::OR32rr;
11959 case X86::ATOMOR64: return X86::OR64rr;
11960 case X86::ATOMXOR8: return X86::XOR8rr;
11961 case X86::ATOMXOR16: return X86::XOR16rr;
11962 case X86::ATOMXOR32: return X86::XOR32rr;
11963 case X86::ATOMXOR64: return X86::XOR64rr;
11964 }
11965 llvm_unreachable("Unhandled atomic-load-op opcode!");
11966}
11967
11968// Get opcode of the non-atomic one from the specified atomic instruction with
11969// extra opcode.
11970static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
11971 unsigned &ExtraOpc) {
11972 switch (Opc) {
11973 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
11974 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
11975 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
11976 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011977 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011978 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
11979 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
11980 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011981 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011982 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
11983 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
11984 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011985 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011986 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
11987 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
11988 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000011989 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000011990 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
11991 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
11992 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
11993 }
11994 llvm_unreachable("Unhandled atomic-load-op opcode!");
11995}
11996
11997// Get opcode of the non-atomic one from the specified atomic instruction for
11998// 64-bit data type on 32-bit target.
11999static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12000 switch (Opc) {
12001 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12002 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12003 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12004 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12005 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12006 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
12007 }
12008 llvm_unreachable("Unhandled atomic-load-op opcode!");
12009}
12010
12011// Get opcode of the non-atomic one from the specified atomic instruction for
12012// 64-bit data type on 32-bit target with extra opcode.
12013static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12014 unsigned &HiOpc,
12015 unsigned &ExtraOpc) {
12016 switch (Opc) {
12017 case X86::ATOMNAND6432:
12018 ExtraOpc = X86::NOT32r;
12019 HiOpc = X86::AND32rr;
12020 return X86::AND32rr;
12021 }
12022 llvm_unreachable("Unhandled atomic-load-op opcode!");
12023}
12024
12025// Get pseudo CMOV opcode from the specified data type.
12026static unsigned getPseudoCMOVOpc(EVT VT) {
12027 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012028 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012029 case MVT::i16: return X86::CMOV_GR16;
12030 case MVT::i32: return X86::CMOV_GR32;
12031 default:
12032 break;
12033 }
12034 llvm_unreachable("Unknown CMOV opcode!");
12035}
12036
12037// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12038// They will be translated into a spin-loop or compare-exchange loop from
12039//
12040// ...
12041// dst = atomic-fetch-op MI.addr, MI.val
12042// ...
12043//
12044// to
12045//
12046// ...
12047// EAX = LOAD MI.addr
12048// loop:
12049// t1 = OP MI.val, EAX
12050// LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12051// JNE loop
12052// sink:
12053// dst = EAX
12054// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012055MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012056X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12057 MachineBasicBlock *MBB) const {
12058 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12059 DebugLoc DL = MI->getDebugLoc();
12060
12061 MachineFunction *MF = MBB->getParent();
12062 MachineRegisterInfo &MRI = MF->getRegInfo();
12063
12064 const BasicBlock *BB = MBB->getBasicBlock();
12065 MachineFunction::iterator I = MBB;
12066 ++I;
12067
12068 assert(MI->getNumOperands() <= X86::AddrNumOperands + 2 &&
12069 "Unexpected number of operands");
12070
12071 assert(MI->hasOneMemOperand() &&
12072 "Expected atomic-load-op to have one memoperand");
12073
12074 // Memory Reference
12075 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12076 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12077
12078 unsigned DstReg, SrcReg;
12079 unsigned MemOpndSlot;
12080
12081 unsigned CurOp = 0;
12082
12083 DstReg = MI->getOperand(CurOp++).getReg();
12084 MemOpndSlot = CurOp;
12085 CurOp += X86::AddrNumOperands;
12086 SrcReg = MI->getOperand(CurOp++).getReg();
12087
12088 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
12089 EVT VT = *RC->vt_begin();
12090 unsigned AccPhyReg = getX86SubSuperRegister(X86::EAX, VT);
12091
12092 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12093 unsigned LOADOpc = getLoadOpcode(VT);
12094
12095 // For the atomic load-arith operator, we generate
12096 //
12097 // thisMBB:
12098 // EAX = LOAD [MI.addr]
12099 // mainMBB:
12100 // t1 = OP MI.val, EAX
12101 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
12102 // JNE mainMBB
12103 // sinkMBB:
12104
12105 MachineBasicBlock *thisMBB = MBB;
12106 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12107 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12108 MF->insert(I, mainMBB);
12109 MF->insert(I, sinkMBB);
12110
12111 MachineInstrBuilder MIB;
12112
12113 // Transfer the remainder of BB and its successor edges to sinkMBB.
12114 sinkMBB->splice(sinkMBB->begin(), MBB,
12115 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12116 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12117
12118 // thisMBB:
12119 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), AccPhyReg);
12120 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12121 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12122 MIB.setMemRefs(MMOBegin, MMOEnd);
12123
12124 thisMBB->addSuccessor(mainMBB);
12125
12126 // mainMBB:
12127 MachineBasicBlock *origMainMBB = mainMBB;
12128 mainMBB->addLiveIn(AccPhyReg);
12129
12130 // Copy AccPhyReg as it is used more than once.
12131 unsigned AccReg = MRI.createVirtualRegister(RC);
12132 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccReg)
12133 .addReg(AccPhyReg);
12134
12135 unsigned t1 = MRI.createVirtualRegister(RC);
12136 unsigned Opc = MI->getOpcode();
12137 switch (Opc) {
12138 default:
12139 llvm_unreachable("Unhandled atomic-load-op opcode!");
12140 case X86::ATOMAND8:
12141 case X86::ATOMAND16:
12142 case X86::ATOMAND32:
12143 case X86::ATOMAND64:
12144 case X86::ATOMOR8:
12145 case X86::ATOMOR16:
12146 case X86::ATOMOR32:
12147 case X86::ATOMOR64:
12148 case X86::ATOMXOR8:
12149 case X86::ATOMXOR16:
12150 case X86::ATOMXOR32:
12151 case X86::ATOMXOR64: {
12152 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
12153 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t1).addReg(SrcReg)
12154 .addReg(AccReg);
12155 break;
12156 }
12157 case X86::ATOMNAND8:
12158 case X86::ATOMNAND16:
12159 case X86::ATOMNAND32:
12160 case X86::ATOMNAND64: {
12161 unsigned t2 = MRI.createVirtualRegister(RC);
12162 unsigned NOTOpc;
12163 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
12164 BuildMI(mainMBB, DL, TII->get(ANDOpc), t2).addReg(SrcReg)
12165 .addReg(AccReg);
12166 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1).addReg(t2);
12167 break;
12168 }
Michael Liao08382492012-09-21 03:00:17 +000012169 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012170 case X86::ATOMMAX16:
12171 case X86::ATOMMAX32:
12172 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012173 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012174 case X86::ATOMMIN16:
12175 case X86::ATOMMIN32:
12176 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000012177 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000012178 case X86::ATOMUMAX16:
12179 case X86::ATOMUMAX32:
12180 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000012181 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000012182 case X86::ATOMUMIN16:
12183 case X86::ATOMUMIN32:
12184 case X86::ATOMUMIN64: {
12185 unsigned CMPOpc;
12186 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
12187
12188 BuildMI(mainMBB, DL, TII->get(CMPOpc))
12189 .addReg(SrcReg)
12190 .addReg(AccReg);
12191
12192 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000012193 if (VT != MVT::i8) {
12194 // Native support
12195 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t1)
12196 .addReg(SrcReg)
12197 .addReg(AccReg);
12198 } else {
12199 // Promote i8 to i32 to use CMOV32
12200 const TargetRegisterClass *RC32 = getRegClassFor(MVT::i32);
12201 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
12202 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
12203 unsigned t2 = MRI.createVirtualRegister(RC32);
12204
12205 unsigned Undef = MRI.createVirtualRegister(RC32);
12206 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
12207
12208 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
12209 .addReg(Undef)
12210 .addReg(SrcReg)
12211 .addImm(X86::sub_8bit);
12212 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
12213 .addReg(Undef)
12214 .addReg(AccReg)
12215 .addImm(X86::sub_8bit);
12216
12217 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
12218 .addReg(SrcReg32)
12219 .addReg(AccReg32);
12220
12221 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t1)
12222 .addReg(t2, 0, X86::sub_8bit);
12223 }
Michael Liaob118a072012-09-20 03:06:15 +000012224 } else {
12225 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000012226 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000012227 "Invalid atomic-load-op transformation!");
12228 unsigned SelOpc = getPseudoCMOVOpc(VT);
12229 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
12230 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
12231 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t1)
12232 .addReg(SrcReg).addReg(AccReg)
12233 .addImm(CC);
12234 mainMBB = EmitLoweredSelect(MIB, mainMBB);
12235 }
12236 break;
12237 }
12238 }
12239
12240 // Copy AccPhyReg back from virtual register.
12241 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), AccPhyReg)
12242 .addReg(AccReg);
12243
12244 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12245 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12246 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12247 MIB.addReg(t1);
12248 MIB.setMemRefs(MMOBegin, MMOEnd);
12249
12250 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
12251
12252 mainMBB->addSuccessor(origMainMBB);
12253 mainMBB->addSuccessor(sinkMBB);
12254
12255 // sinkMBB:
12256 sinkMBB->addLiveIn(AccPhyReg);
12257
12258 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12259 TII->get(TargetOpcode::COPY), DstReg)
12260 .addReg(AccPhyReg);
12261
12262 MI->eraseFromParent();
12263 return sinkMBB;
12264}
12265
12266// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
12267// instructions. They will be translated into a spin-loop or compare-exchange
12268// loop from
12269//
12270// ...
12271// dst = atomic-fetch-op MI.addr, MI.val
12272// ...
12273//
12274// to
12275//
12276// ...
12277// EAX = LOAD [MI.addr + 0]
12278// EDX = LOAD [MI.addr + 4]
12279// loop:
12280// EBX = OP MI.val.lo, EAX
12281// ECX = OP MI.val.hi, EDX
12282// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12283// JNE loop
12284// sink:
12285// dst = EDX:EAX
12286// ...
12287MachineBasicBlock *
12288X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
12289 MachineBasicBlock *MBB) const {
12290 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12291 DebugLoc DL = MI->getDebugLoc();
12292
12293 MachineFunction *MF = MBB->getParent();
12294 MachineRegisterInfo &MRI = MF->getRegInfo();
12295
12296 const BasicBlock *BB = MBB->getBasicBlock();
12297 MachineFunction::iterator I = MBB;
12298 ++I;
12299
12300 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
12301 "Unexpected number of operands");
12302
12303 assert(MI->hasOneMemOperand() &&
12304 "Expected atomic-load-op32 to have one memoperand");
12305
12306 // Memory Reference
12307 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12308 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12309
12310 unsigned DstLoReg, DstHiReg;
12311 unsigned SrcLoReg, SrcHiReg;
12312 unsigned MemOpndSlot;
12313
12314 unsigned CurOp = 0;
12315
12316 DstLoReg = MI->getOperand(CurOp++).getReg();
12317 DstHiReg = MI->getOperand(CurOp++).getReg();
12318 MemOpndSlot = CurOp;
12319 CurOp += X86::AddrNumOperands;
12320 SrcLoReg = MI->getOperand(CurOp++).getReg();
12321 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012322
Craig Topperc9099502012-04-20 06:31:50 +000012323 const TargetRegisterClass *RC = &X86::GR32RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000012324
Michael Liaob118a072012-09-20 03:06:15 +000012325 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
12326 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000012327
Michael Liaob118a072012-09-20 03:06:15 +000012328 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000012329 //
Michael Liaob118a072012-09-20 03:06:15 +000012330 // thisMBB:
12331 // EAX = LOAD [MI.addr + 0]
12332 // EDX = LOAD [MI.addr + 4]
12333 // mainMBB:
12334 // EBX = OP MI.vallo, EAX
12335 // ECX = OP MI.valhi, EDX
12336 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
12337 // JNE mainMBB
12338 // sinkMBB:
Scott Michelfdc40a02009-02-17 22:15:04 +000012339
Mon P Wang63307c32008-05-05 19:05:59 +000012340 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000012341 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12342 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12343 MF->insert(I, mainMBB);
12344 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012345
Michael Liaob118a072012-09-20 03:06:15 +000012346 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000012347
Michael Liaob118a072012-09-20 03:06:15 +000012348 // Transfer the remainder of BB and its successor edges to sinkMBB.
12349 sinkMBB->splice(sinkMBB->begin(), MBB,
12350 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12351 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012352
Michael Liaob118a072012-09-20 03:06:15 +000012353 // thisMBB:
12354 // Lo
12355 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EAX);
12356 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12357 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12358 MIB.setMemRefs(MMOBegin, MMOEnd);
12359 // Hi
12360 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), X86::EDX);
12361 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
12362 if (i == X86::AddrDisp)
12363 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
12364 else
12365 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12366 }
12367 MIB.setMemRefs(MMOBegin, MMOEnd);
Scott Michelfdc40a02009-02-17 22:15:04 +000012368
Michael Liaob118a072012-09-20 03:06:15 +000012369 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012370
Michael Liaob118a072012-09-20 03:06:15 +000012371 // mainMBB:
12372 MachineBasicBlock *origMainMBB = mainMBB;
12373 mainMBB->addLiveIn(X86::EAX);
12374 mainMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012375
Michael Liaob118a072012-09-20 03:06:15 +000012376 // Copy EDX:EAX as they are used more than once.
12377 unsigned LoReg = MRI.createVirtualRegister(RC);
12378 unsigned HiReg = MRI.createVirtualRegister(RC);
12379 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), LoReg).addReg(X86::EAX);
12380 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), HiReg).addReg(X86::EDX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012381
Michael Liaob118a072012-09-20 03:06:15 +000012382 unsigned t1L = MRI.createVirtualRegister(RC);
12383 unsigned t1H = MRI.createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +000012384
Michael Liaob118a072012-09-20 03:06:15 +000012385 unsigned Opc = MI->getOpcode();
12386 switch (Opc) {
12387 default:
12388 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
12389 case X86::ATOMAND6432:
12390 case X86::ATOMOR6432:
12391 case X86::ATOMXOR6432:
12392 case X86::ATOMADD6432:
12393 case X86::ATOMSUB6432: {
12394 unsigned HiOpc;
12395 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12396 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg).addReg(LoReg);
12397 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg).addReg(HiReg);
12398 break;
12399 }
12400 case X86::ATOMNAND6432: {
12401 unsigned HiOpc, NOTOpc;
12402 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
12403 unsigned t2L = MRI.createVirtualRegister(RC);
12404 unsigned t2H = MRI.createVirtualRegister(RC);
12405 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg).addReg(LoReg);
12406 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg).addReg(HiReg);
12407 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1L).addReg(t2L);
12408 BuildMI(mainMBB, DL, TII->get(NOTOpc), t1H).addReg(t2H);
12409 break;
12410 }
12411 case X86::ATOMSWAP6432: {
12412 unsigned HiOpc;
12413 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
12414 BuildMI(mainMBB, DL, TII->get(LoOpc), t1L).addReg(SrcLoReg);
12415 BuildMI(mainMBB, DL, TII->get(HiOpc), t1H).addReg(SrcHiReg);
12416 break;
12417 }
12418 }
Mon P Wang63307c32008-05-05 19:05:59 +000012419
Michael Liaob118a072012-09-20 03:06:15 +000012420 // Copy EDX:EAX back from HiReg:LoReg
12421 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(LoReg);
12422 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(HiReg);
12423 // Copy ECX:EBX from t1H:t1L
12424 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t1L);
12425 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t1H);
Mon P Wangab3e7472008-05-05 22:56:23 +000012426
Michael Liaob118a072012-09-20 03:06:15 +000012427 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
12428 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
12429 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
12430 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000012431
Michael Liaob118a072012-09-20 03:06:15 +000012432 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012433
Michael Liaob118a072012-09-20 03:06:15 +000012434 mainMBB->addSuccessor(origMainMBB);
12435 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012436
Michael Liaob118a072012-09-20 03:06:15 +000012437 // sinkMBB:
12438 sinkMBB->addLiveIn(X86::EAX);
12439 sinkMBB->addLiveIn(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012440
Michael Liaob118a072012-09-20 03:06:15 +000012441 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12442 TII->get(TargetOpcode::COPY), DstLoReg)
12443 .addReg(X86::EAX);
12444 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12445 TII->get(TargetOpcode::COPY), DstHiReg)
12446 .addReg(X86::EDX);
Mon P Wang63307c32008-05-05 19:05:59 +000012447
Michael Liaob118a072012-09-20 03:06:15 +000012448 MI->eraseFromParent();
12449 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012450}
12451
Eric Christopherf83a5de2009-08-27 18:08:16 +000012452// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012453// or XMM0_V32I8 in AVX all of this code can be replaced with that
12454// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012455MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012456X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012457 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012458 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012459 "Target must have SSE4.2 or AVX features enabled");
12460
Eric Christopherb120ab42009-08-18 22:50:32 +000012461 DebugLoc dl = MI->getDebugLoc();
12462 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012463 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012464 if (!Subtarget->hasAVX()) {
12465 if (memArg)
12466 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12467 else
12468 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12469 } else {
12470 if (memArg)
12471 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12472 else
12473 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12474 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012475
Eric Christopher41c902f2010-11-30 08:20:21 +000012476 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012477 for (unsigned i = 0; i < numArgs; ++i) {
12478 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012479 if (!(Op.isReg() && Op.isImplicit()))
12480 MIB.addOperand(Op);
12481 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012482 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012483 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012484 .addReg(X86::XMM0);
12485
Dan Gohman14152b42010-07-06 20:24:04 +000012486 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012487 return BB;
12488}
12489
12490MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012491X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012492 DebugLoc dl = MI->getDebugLoc();
12493 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012494
Eric Christopher228232b2010-11-30 07:20:12 +000012495 // Address into RAX/EAX, other two args into ECX, EDX.
12496 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12497 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12498 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12499 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012500 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012501
Eric Christopher228232b2010-11-30 07:20:12 +000012502 unsigned ValOps = X86::AddrNumOperands;
12503 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12504 .addReg(MI->getOperand(ValOps).getReg());
12505 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12506 .addReg(MI->getOperand(ValOps+1).getReg());
12507
12508 // The instruction doesn't actually take any operands though.
12509 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012510
Eric Christopher228232b2010-11-30 07:20:12 +000012511 MI->eraseFromParent(); // The pseudo is gone now.
12512 return BB;
12513}
12514
12515MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012516X86TargetLowering::EmitVAARG64WithCustomInserter(
12517 MachineInstr *MI,
12518 MachineBasicBlock *MBB) const {
12519 // Emit va_arg instruction on X86-64.
12520
12521 // Operands to this pseudo-instruction:
12522 // 0 ) Output : destination address (reg)
12523 // 1-5) Input : va_list address (addr, i64mem)
12524 // 6 ) ArgSize : Size (in bytes) of vararg type
12525 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12526 // 8 ) Align : Alignment of type
12527 // 9 ) EFLAGS (implicit-def)
12528
12529 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12530 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12531
12532 unsigned DestReg = MI->getOperand(0).getReg();
12533 MachineOperand &Base = MI->getOperand(1);
12534 MachineOperand &Scale = MI->getOperand(2);
12535 MachineOperand &Index = MI->getOperand(3);
12536 MachineOperand &Disp = MI->getOperand(4);
12537 MachineOperand &Segment = MI->getOperand(5);
12538 unsigned ArgSize = MI->getOperand(6).getImm();
12539 unsigned ArgMode = MI->getOperand(7).getImm();
12540 unsigned Align = MI->getOperand(8).getImm();
12541
12542 // Memory Reference
12543 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12544 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12545 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12546
12547 // Machine Information
12548 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12549 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12550 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12551 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12552 DebugLoc DL = MI->getDebugLoc();
12553
12554 // struct va_list {
12555 // i32 gp_offset
12556 // i32 fp_offset
12557 // i64 overflow_area (address)
12558 // i64 reg_save_area (address)
12559 // }
12560 // sizeof(va_list) = 24
12561 // alignment(va_list) = 8
12562
12563 unsigned TotalNumIntRegs = 6;
12564 unsigned TotalNumXMMRegs = 8;
12565 bool UseGPOffset = (ArgMode == 1);
12566 bool UseFPOffset = (ArgMode == 2);
12567 unsigned MaxOffset = TotalNumIntRegs * 8 +
12568 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12569
12570 /* Align ArgSize to a multiple of 8 */
12571 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12572 bool NeedsAlign = (Align > 8);
12573
12574 MachineBasicBlock *thisMBB = MBB;
12575 MachineBasicBlock *overflowMBB;
12576 MachineBasicBlock *offsetMBB;
12577 MachineBasicBlock *endMBB;
12578
12579 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12580 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12581 unsigned OffsetReg = 0;
12582
12583 if (!UseGPOffset && !UseFPOffset) {
12584 // If we only pull from the overflow region, we don't create a branch.
12585 // We don't need to alter control flow.
12586 OffsetDestReg = 0; // unused
12587 OverflowDestReg = DestReg;
12588
12589 offsetMBB = NULL;
12590 overflowMBB = thisMBB;
12591 endMBB = thisMBB;
12592 } else {
12593 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12594 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12595 // If not, pull from overflow_area. (branch to overflowMBB)
12596 //
12597 // thisMBB
12598 // | .
12599 // | .
12600 // offsetMBB overflowMBB
12601 // | .
12602 // | .
12603 // endMBB
12604
12605 // Registers for the PHI in endMBB
12606 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12607 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12608
12609 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12610 MachineFunction *MF = MBB->getParent();
12611 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12612 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12613 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12614
12615 MachineFunction::iterator MBBIter = MBB;
12616 ++MBBIter;
12617
12618 // Insert the new basic blocks
12619 MF->insert(MBBIter, offsetMBB);
12620 MF->insert(MBBIter, overflowMBB);
12621 MF->insert(MBBIter, endMBB);
12622
12623 // Transfer the remainder of MBB and its successor edges to endMBB.
12624 endMBB->splice(endMBB->begin(), thisMBB,
12625 llvm::next(MachineBasicBlock::iterator(MI)),
12626 thisMBB->end());
12627 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12628
12629 // Make offsetMBB and overflowMBB successors of thisMBB
12630 thisMBB->addSuccessor(offsetMBB);
12631 thisMBB->addSuccessor(overflowMBB);
12632
12633 // endMBB is a successor of both offsetMBB and overflowMBB
12634 offsetMBB->addSuccessor(endMBB);
12635 overflowMBB->addSuccessor(endMBB);
12636
12637 // Load the offset value into a register
12638 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12639 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12640 .addOperand(Base)
12641 .addOperand(Scale)
12642 .addOperand(Index)
12643 .addDisp(Disp, UseFPOffset ? 4 : 0)
12644 .addOperand(Segment)
12645 .setMemRefs(MMOBegin, MMOEnd);
12646
12647 // Check if there is enough room left to pull this argument.
12648 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12649 .addReg(OffsetReg)
12650 .addImm(MaxOffset + 8 - ArgSizeA8);
12651
12652 // Branch to "overflowMBB" if offset >= max
12653 // Fall through to "offsetMBB" otherwise
12654 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12655 .addMBB(overflowMBB);
12656 }
12657
12658 // In offsetMBB, emit code to use the reg_save_area.
12659 if (offsetMBB) {
12660 assert(OffsetReg != 0);
12661
12662 // Read the reg_save_area address.
12663 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12664 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12665 .addOperand(Base)
12666 .addOperand(Scale)
12667 .addOperand(Index)
12668 .addDisp(Disp, 16)
12669 .addOperand(Segment)
12670 .setMemRefs(MMOBegin, MMOEnd);
12671
12672 // Zero-extend the offset
12673 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12674 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12675 .addImm(0)
12676 .addReg(OffsetReg)
12677 .addImm(X86::sub_32bit);
12678
12679 // Add the offset to the reg_save_area to get the final address.
12680 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12681 .addReg(OffsetReg64)
12682 .addReg(RegSaveReg);
12683
12684 // Compute the offset for the next argument
12685 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12686 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12687 .addReg(OffsetReg)
12688 .addImm(UseFPOffset ? 16 : 8);
12689
12690 // Store it back into the va_list.
12691 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12692 .addOperand(Base)
12693 .addOperand(Scale)
12694 .addOperand(Index)
12695 .addDisp(Disp, UseFPOffset ? 4 : 0)
12696 .addOperand(Segment)
12697 .addReg(NextOffsetReg)
12698 .setMemRefs(MMOBegin, MMOEnd);
12699
12700 // Jump to endMBB
12701 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12702 .addMBB(endMBB);
12703 }
12704
12705 //
12706 // Emit code to use overflow area
12707 //
12708
12709 // Load the overflow_area address into a register.
12710 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12711 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12712 .addOperand(Base)
12713 .addOperand(Scale)
12714 .addOperand(Index)
12715 .addDisp(Disp, 8)
12716 .addOperand(Segment)
12717 .setMemRefs(MMOBegin, MMOEnd);
12718
12719 // If we need to align it, do so. Otherwise, just copy the address
12720 // to OverflowDestReg.
12721 if (NeedsAlign) {
12722 // Align the overflow address
12723 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12724 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12725
12726 // aligned_addr = (addr + (align-1)) & ~(align-1)
12727 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12728 .addReg(OverflowAddrReg)
12729 .addImm(Align-1);
12730
12731 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12732 .addReg(TmpReg)
12733 .addImm(~(uint64_t)(Align-1));
12734 } else {
12735 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12736 .addReg(OverflowAddrReg);
12737 }
12738
12739 // Compute the next overflow address after this argument.
12740 // (the overflow address should be kept 8-byte aligned)
12741 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12742 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12743 .addReg(OverflowDestReg)
12744 .addImm(ArgSizeA8);
12745
12746 // Store the new overflow address.
12747 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12748 .addOperand(Base)
12749 .addOperand(Scale)
12750 .addOperand(Index)
12751 .addDisp(Disp, 8)
12752 .addOperand(Segment)
12753 .addReg(NextAddrReg)
12754 .setMemRefs(MMOBegin, MMOEnd);
12755
12756 // If we branched, emit the PHI to the front of endMBB.
12757 if (offsetMBB) {
12758 BuildMI(*endMBB, endMBB->begin(), DL,
12759 TII->get(X86::PHI), DestReg)
12760 .addReg(OffsetDestReg).addMBB(offsetMBB)
12761 .addReg(OverflowDestReg).addMBB(overflowMBB);
12762 }
12763
12764 // Erase the pseudo instruction
12765 MI->eraseFromParent();
12766
12767 return endMBB;
12768}
12769
12770MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012771X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12772 MachineInstr *MI,
12773 MachineBasicBlock *MBB) const {
12774 // Emit code to save XMM registers to the stack. The ABI says that the
12775 // number of registers to save is given in %al, so it's theoretically
12776 // possible to do an indirect jump trick to avoid saving all of them,
12777 // however this code takes a simpler approach and just executes all
12778 // of the stores if %al is non-zero. It's less code, and it's probably
12779 // easier on the hardware branch predictor, and stores aren't all that
12780 // expensive anyway.
12781
12782 // Create the new basic blocks. One block contains all the XMM stores,
12783 // and one block is the final destination regardless of whether any
12784 // stores were performed.
12785 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12786 MachineFunction *F = MBB->getParent();
12787 MachineFunction::iterator MBBIter = MBB;
12788 ++MBBIter;
12789 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12790 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12791 F->insert(MBBIter, XMMSaveMBB);
12792 F->insert(MBBIter, EndMBB);
12793
Dan Gohman14152b42010-07-06 20:24:04 +000012794 // Transfer the remainder of MBB and its successor edges to EndMBB.
12795 EndMBB->splice(EndMBB->begin(), MBB,
12796 llvm::next(MachineBasicBlock::iterator(MI)),
12797 MBB->end());
12798 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12799
Dan Gohmand6708ea2009-08-15 01:38:56 +000012800 // The original block will now fall through to the XMM save block.
12801 MBB->addSuccessor(XMMSaveMBB);
12802 // The XMMSaveMBB will fall through to the end block.
12803 XMMSaveMBB->addSuccessor(EndMBB);
12804
12805 // Now add the instructions.
12806 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12807 DebugLoc DL = MI->getDebugLoc();
12808
12809 unsigned CountReg = MI->getOperand(0).getReg();
12810 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12811 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12812
12813 if (!Subtarget->isTargetWin64()) {
12814 // If %al is 0, branch around the XMM save block.
12815 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012816 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012817 MBB->addSuccessor(EndMBB);
12818 }
12819
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012820 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012821 // In the XMM save block, save all the XMM argument registers.
12822 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12823 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012824 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012825 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012826 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012827 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012828 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012829 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012830 .addFrameIndex(RegSaveFrameIndex)
12831 .addImm(/*Scale=*/1)
12832 .addReg(/*IndexReg=*/0)
12833 .addImm(/*Disp=*/Offset)
12834 .addReg(/*Segment=*/0)
12835 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012836 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012837 }
12838
Dan Gohman14152b42010-07-06 20:24:04 +000012839 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012840
12841 return EndMBB;
12842}
Mon P Wang63307c32008-05-05 19:05:59 +000012843
Lang Hames6e3f7e42012-02-03 01:13:49 +000012844// The EFLAGS operand of SelectItr might be missing a kill marker
12845// because there were multiple uses of EFLAGS, and ISel didn't know
12846// which to mark. Figure out whether SelectItr should have had a
12847// kill marker, and set it if it should. Returns the correct kill
12848// marker value.
12849static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12850 MachineBasicBlock* BB,
12851 const TargetRegisterInfo* TRI) {
12852 // Scan forward through BB for a use/def of EFLAGS.
12853 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12854 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012855 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012856 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012857 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012858 if (mi.definesRegister(X86::EFLAGS))
12859 break; // Should have kill-flag - update below.
12860 }
12861
12862 // If we hit the end of the block, check whether EFLAGS is live into a
12863 // successor.
12864 if (miI == BB->end()) {
12865 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12866 sEnd = BB->succ_end();
12867 sItr != sEnd; ++sItr) {
12868 MachineBasicBlock* succ = *sItr;
12869 if (succ->isLiveIn(X86::EFLAGS))
12870 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012871 }
12872 }
12873
Lang Hames6e3f7e42012-02-03 01:13:49 +000012874 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12875 // out. SelectMI should have a kill flag on EFLAGS.
12876 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012877 return true;
12878}
12879
Evan Cheng60c07e12006-07-05 22:17:51 +000012880MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012881X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012882 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012883 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12884 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012885
Chris Lattner52600972009-09-02 05:57:00 +000012886 // To "insert" a SELECT_CC instruction, we actually have to insert the
12887 // diamond control-flow pattern. The incoming instruction knows the
12888 // destination vreg to set, the condition code register to branch on, the
12889 // true/false values to select between, and a branch opcode to use.
12890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12891 MachineFunction::iterator It = BB;
12892 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012893
Chris Lattner52600972009-09-02 05:57:00 +000012894 // thisMBB:
12895 // ...
12896 // TrueVal = ...
12897 // cmpTY ccX, r1, r2
12898 // bCC copy1MBB
12899 // fallthrough --> copy0MBB
12900 MachineBasicBlock *thisMBB = BB;
12901 MachineFunction *F = BB->getParent();
12902 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12903 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012904 F->insert(It, copy0MBB);
12905 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012906
Bill Wendling730c07e2010-06-25 20:48:10 +000012907 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12908 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012909 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12910 if (!MI->killsRegister(X86::EFLAGS) &&
12911 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12912 copy0MBB->addLiveIn(X86::EFLAGS);
12913 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012914 }
12915
Dan Gohman14152b42010-07-06 20:24:04 +000012916 // Transfer the remainder of BB and its successor edges to sinkMBB.
12917 sinkMBB->splice(sinkMBB->begin(), BB,
12918 llvm::next(MachineBasicBlock::iterator(MI)),
12919 BB->end());
12920 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12921
12922 // Add the true and fallthrough blocks as its successors.
12923 BB->addSuccessor(copy0MBB);
12924 BB->addSuccessor(sinkMBB);
12925
12926 // Create the conditional branch instruction.
12927 unsigned Opc =
12928 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12929 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12930
Chris Lattner52600972009-09-02 05:57:00 +000012931 // copy0MBB:
12932 // %FalseValue = ...
12933 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012934 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012935
Chris Lattner52600972009-09-02 05:57:00 +000012936 // sinkMBB:
12937 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12938 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012939 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12940 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012941 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12942 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12943
Dan Gohman14152b42010-07-06 20:24:04 +000012944 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012945 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012946}
12947
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012948MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012949X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12950 bool Is64Bit) const {
12951 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12952 DebugLoc DL = MI->getDebugLoc();
12953 MachineFunction *MF = BB->getParent();
12954 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12955
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012956 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012957
12958 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12959 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12960
12961 // BB:
12962 // ... [Till the alloca]
12963 // If stacklet is not large enough, jump to mallocMBB
12964 //
12965 // bumpMBB:
12966 // Allocate by subtracting from RSP
12967 // Jump to continueMBB
12968 //
12969 // mallocMBB:
12970 // Allocate by call to runtime
12971 //
12972 // continueMBB:
12973 // ...
12974 // [rest of original BB]
12975 //
12976
12977 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12978 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12979 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12980
12981 MachineRegisterInfo &MRI = MF->getRegInfo();
12982 const TargetRegisterClass *AddrRegClass =
12983 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12984
12985 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12986 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12987 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012988 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012989 sizeVReg = MI->getOperand(1).getReg(),
12990 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12991
12992 MachineFunction::iterator MBBIter = BB;
12993 ++MBBIter;
12994
12995 MF->insert(MBBIter, bumpMBB);
12996 MF->insert(MBBIter, mallocMBB);
12997 MF->insert(MBBIter, continueMBB);
12998
12999 continueMBB->splice(continueMBB->begin(), BB, llvm::next
13000 (MachineBasicBlock::iterator(MI)), BB->end());
13001 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
13002
13003 // Add code to the main basic block to check if the stack limit has been hit,
13004 // and if so, jump to mallocMBB otherwise to bumpMBB.
13005 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000013006 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013007 .addReg(tmpSPVReg).addReg(sizeVReg);
13008 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000013009 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013010 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013011 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
13012
13013 // bumpMBB simply decreases the stack pointer, since we know the current
13014 // stacklet has enough space.
13015 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013016 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013017 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000013018 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013019 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13020
13021 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013022 const uint32_t *RegMask =
13023 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013024 if (Is64Bit) {
13025 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
13026 .addReg(sizeVReg);
13027 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013028 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013029 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000013030 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013031 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013032 } else {
13033 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
13034 .addImm(12);
13035 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
13036 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013037 .addExternalSymbol("__morestack_allocate_stack_space")
13038 .addRegMask(RegMask)
13039 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013040 }
13041
13042 if (!Is64Bit)
13043 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
13044 .addImm(16);
13045
13046 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
13047 .addReg(Is64Bit ? X86::RAX : X86::EAX);
13048 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
13049
13050 // Set up the CFG correctly.
13051 BB->addSuccessor(bumpMBB);
13052 BB->addSuccessor(mallocMBB);
13053 mallocMBB->addSuccessor(continueMBB);
13054 bumpMBB->addSuccessor(continueMBB);
13055
13056 // Take care of the PHI nodes.
13057 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
13058 MI->getOperand(0).getReg())
13059 .addReg(mallocPtrVReg).addMBB(mallocMBB)
13060 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
13061
13062 // Delete the original pseudo instruction.
13063 MI->eraseFromParent();
13064
13065 // And we're done.
13066 return continueMBB;
13067}
13068
13069MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013070X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013071 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13073 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013074
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013075 assert(!Subtarget->isTargetEnvMacho());
13076
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013077 // The lowering is pretty easy: we're just emitting the call to _alloca. The
13078 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013079
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013080 if (Subtarget->isTargetWin64()) {
13081 if (Subtarget->isTargetCygMing()) {
13082 // ___chkstk(Mingw64):
13083 // Clobbers R10, R11, RAX and EFLAGS.
13084 // Updates RSP.
13085 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13086 .addExternalSymbol("___chkstk")
13087 .addReg(X86::RAX, RegState::Implicit)
13088 .addReg(X86::RSP, RegState::Implicit)
13089 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
13090 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
13091 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13092 } else {
13093 // __chkstk(MSVCRT): does not update stack pointer.
13094 // Clobbers R10, R11 and EFLAGS.
13095 // FIXME: RAX(allocated size) might be reused and not killed.
13096 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
13097 .addExternalSymbol("__chkstk")
13098 .addReg(X86::RAX, RegState::Implicit)
13099 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13100 // RAX has the offset to subtracted from RSP.
13101 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
13102 .addReg(X86::RSP)
13103 .addReg(X86::RAX);
13104 }
13105 } else {
13106 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013107 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
13108
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000013109 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
13110 .addExternalSymbol(StackProbeSymbol)
13111 .addReg(X86::EAX, RegState::Implicit)
13112 .addReg(X86::ESP, RegState::Implicit)
13113 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
13114 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
13115 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
13116 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013117
Dan Gohman14152b42010-07-06 20:24:04 +000013118 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000013119 return BB;
13120}
Chris Lattner52600972009-09-02 05:57:00 +000013121
13122MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000013123X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
13124 MachineBasicBlock *BB) const {
13125 // This is pretty easy. We're taking the value that we received from
13126 // our load from the relocation, sticking it in either RDI (x86-64)
13127 // or EAX and doing an indirect call. The return value will then
13128 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000013129 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000013130 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000013131 DebugLoc DL = MI->getDebugLoc();
13132 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000013133
13134 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000013135 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000013136
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013137 // Get a register mask for the lowered call.
13138 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
13139 // proper register mask.
13140 const uint32_t *RegMask =
13141 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013142 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000013143 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13144 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000013145 .addReg(X86::RIP)
13146 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013147 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013148 MI->getOperand(3).getTargetFlags())
13149 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000013150 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000013151 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013152 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000013153 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000013154 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13155 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000013156 .addReg(0)
13157 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013158 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013159 MI->getOperand(3).getTargetFlags())
13160 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013161 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013162 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013163 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013164 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013165 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13166 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013167 .addReg(TII->getGlobalBaseReg(F))
13168 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013169 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013170 MI->getOperand(3).getTargetFlags())
13171 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013172 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013173 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013174 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013175 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013176
Dan Gohman14152b42010-07-06 20:24:04 +000013177 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013178 return BB;
13179}
13180
13181MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013182X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013183 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013184 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013185 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013186 case X86::TAILJMPd64:
13187 case X86::TAILJMPr64:
13188 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013189 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013190 case X86::TCRETURNdi64:
13191 case X86::TCRETURNri64:
13192 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013193 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013194 case X86::WIN_ALLOCA:
13195 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013196 case X86::SEG_ALLOCA_32:
13197 return EmitLoweredSegAlloca(MI, BB, false);
13198 case X86::SEG_ALLOCA_64:
13199 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013200 case X86::TLSCall_32:
13201 case X86::TLSCall_64:
13202 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013203 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013204 case X86::CMOV_FR32:
13205 case X86::CMOV_FR64:
13206 case X86::CMOV_V4F32:
13207 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013208 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013209 case X86::CMOV_V8F32:
13210 case X86::CMOV_V4F64:
13211 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013212 case X86::CMOV_GR16:
13213 case X86::CMOV_GR32:
13214 case X86::CMOV_RFP32:
13215 case X86::CMOV_RFP64:
13216 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013217 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013218
Dale Johannesen849f2142007-07-03 00:53:03 +000013219 case X86::FP32_TO_INT16_IN_MEM:
13220 case X86::FP32_TO_INT32_IN_MEM:
13221 case X86::FP32_TO_INT64_IN_MEM:
13222 case X86::FP64_TO_INT16_IN_MEM:
13223 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013224 case X86::FP64_TO_INT64_IN_MEM:
13225 case X86::FP80_TO_INT16_IN_MEM:
13226 case X86::FP80_TO_INT32_IN_MEM:
13227 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13229 DebugLoc DL = MI->getDebugLoc();
13230
Evan Cheng60c07e12006-07-05 22:17:51 +000013231 // Change the floating point control register to use "round towards zero"
13232 // mode when truncating to an integer value.
13233 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013234 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013235 addFrameReference(BuildMI(*BB, MI, DL,
13236 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013237
13238 // Load the old value of the high byte of the control word...
13239 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013240 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013241 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013242 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013243
13244 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013245 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013246 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013247
13248 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013249 addFrameReference(BuildMI(*BB, MI, DL,
13250 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013251
13252 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013253 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013254 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013255
13256 // Get the X86 opcode to use.
13257 unsigned Opc;
13258 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013259 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013260 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13261 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13262 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13263 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13264 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13265 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013266 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13267 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13268 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013269 }
13270
13271 X86AddressMode AM;
13272 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013273 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013274 AM.BaseType = X86AddressMode::RegBase;
13275 AM.Base.Reg = Op.getReg();
13276 } else {
13277 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013278 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013279 }
13280 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013281 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013282 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013283 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013284 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013285 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013286 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013287 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013288 AM.GV = Op.getGlobal();
13289 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013290 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013291 }
Dan Gohman14152b42010-07-06 20:24:04 +000013292 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013293 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013294
13295 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013296 addFrameReference(BuildMI(*BB, MI, DL,
13297 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013298
Dan Gohman14152b42010-07-06 20:24:04 +000013299 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013300 return BB;
13301 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013302 // String/text processing lowering.
13303 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013304 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013305 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013306 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013307 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013308 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013309 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013310 case X86::VPCMPESTRM128MEM: {
13311 unsigned NumArgs;
13312 bool MemArg;
13313 switch (MI->getOpcode()) {
13314 default: llvm_unreachable("illegal opcode!");
13315 case X86::PCMPISTRM128REG:
13316 case X86::VPCMPISTRM128REG:
13317 NumArgs = 3; MemArg = false; break;
13318 case X86::PCMPISTRM128MEM:
13319 case X86::VPCMPISTRM128MEM:
13320 NumArgs = 3; MemArg = true; break;
13321 case X86::PCMPESTRM128REG:
13322 case X86::VPCMPESTRM128REG:
13323 NumArgs = 5; MemArg = false; break;
13324 case X86::PCMPESTRM128MEM:
13325 case X86::VPCMPESTRM128MEM:
13326 NumArgs = 5; MemArg = true; break;
13327 }
13328 return EmitPCMP(MI, BB, NumArgs, MemArg);
13329 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013330
Eric Christopher228232b2010-11-30 07:20:12 +000013331 // Thread synchronization.
13332 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013333 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013334
Eric Christopherb120ab42009-08-18 22:50:32 +000013335 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000013336 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000013337 case X86::ATOMAND16:
13338 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013339 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000013340 // Fall through
13341 case X86::ATOMOR8:
13342 case X86::ATOMOR16:
13343 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013344 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013345 // Fall through
13346 case X86::ATOMXOR16:
13347 case X86::ATOMXOR8:
13348 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000013349 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000013350 // Fall through
13351 case X86::ATOMNAND8:
13352 case X86::ATOMNAND16:
13353 case X86::ATOMNAND32:
13354 case X86::ATOMNAND64:
13355 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013356 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013357 case X86::ATOMMAX16:
13358 case X86::ATOMMAX32:
13359 case X86::ATOMMAX64:
13360 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013361 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013362 case X86::ATOMMIN16:
13363 case X86::ATOMMIN32:
13364 case X86::ATOMMIN64:
13365 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013366 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013367 case X86::ATOMUMAX16:
13368 case X86::ATOMUMAX32:
13369 case X86::ATOMUMAX64:
13370 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000013371 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013372 case X86::ATOMUMIN16:
13373 case X86::ATOMUMIN32:
13374 case X86::ATOMUMIN64:
13375 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013376
13377 // This group does 64-bit operations on a 32-bit host.
13378 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013379 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013380 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013381 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013382 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013383 case X86::ATOMSUB6432:
Michael Liaob118a072012-09-20 03:06:15 +000013384 case X86::ATOMSWAP6432:
13385 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000013386
Dan Gohmand6708ea2009-08-15 01:38:56 +000013387 case X86::VASTART_SAVE_XMM_REGS:
13388 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013389
13390 case X86::VAARG_64:
13391 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013392 }
13393}
13394
13395//===----------------------------------------------------------------------===//
13396// X86 Optimization Hooks
13397//===----------------------------------------------------------------------===//
13398
Dan Gohman475871a2008-07-27 21:46:04 +000013399void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013400 APInt &KnownZero,
13401 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013402 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013403 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013404 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013405 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013406 assert((Opc >= ISD::BUILTIN_OP_END ||
13407 Opc == ISD::INTRINSIC_WO_CHAIN ||
13408 Opc == ISD::INTRINSIC_W_CHAIN ||
13409 Opc == ISD::INTRINSIC_VOID) &&
13410 "Should use MaskedValueIsZero if you don't know whether Op"
13411 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013412
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013413 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013414 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013415 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013416 case X86ISD::ADD:
13417 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013418 case X86ISD::ADC:
13419 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013420 case X86ISD::SMUL:
13421 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013422 case X86ISD::INC:
13423 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013424 case X86ISD::OR:
13425 case X86ISD::XOR:
13426 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013427 // These nodes' second result is a boolean.
13428 if (Op.getResNo() == 0)
13429 break;
13430 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013431 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013432 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013433 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013434 case ISD::INTRINSIC_WO_CHAIN: {
13435 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13436 unsigned NumLoBits = 0;
13437 switch (IntId) {
13438 default: break;
13439 case Intrinsic::x86_sse_movmsk_ps:
13440 case Intrinsic::x86_avx_movmsk_ps_256:
13441 case Intrinsic::x86_sse2_movmsk_pd:
13442 case Intrinsic::x86_avx_movmsk_pd_256:
13443 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013444 case Intrinsic::x86_sse2_pmovmskb_128:
13445 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013446 // High bits of movmskp{s|d}, pmovmskb are known zero.
13447 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013448 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013449 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13450 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13451 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13452 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13453 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13454 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013455 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013456 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013457 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013458 break;
13459 }
13460 }
13461 break;
13462 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013463 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013464}
Chris Lattner259e97c2006-01-31 19:43:35 +000013465
Owen Andersonbc146b02010-09-21 20:42:50 +000013466unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13467 unsigned Depth) const {
13468 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13469 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13470 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013471
Owen Andersonbc146b02010-09-21 20:42:50 +000013472 // Fallback case.
13473 return 1;
13474}
13475
Evan Cheng206ee9d2006-07-07 08:33:52 +000013476/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013477/// node is a GlobalAddress + offset.
13478bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013479 const GlobalValue* &GA,
13480 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013481 if (N->getOpcode() == X86ISD::Wrapper) {
13482 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013483 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013484 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013485 return true;
13486 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013487 }
Evan Chengad4196b2008-05-12 19:56:52 +000013488 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013489}
13490
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013491/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13492/// same as extracting the high 128-bit part of 256-bit vector and then
13493/// inserting the result into the low part of a new 256-bit vector
13494static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13495 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013496 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013497
13498 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013499 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013500 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13501 SVOp->getMaskElt(j) >= 0)
13502 return false;
13503
13504 return true;
13505}
13506
13507/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13508/// same as extracting the low 128-bit part of 256-bit vector and then
13509/// inserting the result into the high part of a new 256-bit vector
13510static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13511 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013512 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013513
13514 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013515 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013516 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13517 SVOp->getMaskElt(j) >= 0)
13518 return false;
13519
13520 return true;
13521}
13522
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013523/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13524static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013525 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013526 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013527 DebugLoc dl = N->getDebugLoc();
13528 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13529 SDValue V1 = SVOp->getOperand(0);
13530 SDValue V2 = SVOp->getOperand(1);
13531 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013532 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013533
13534 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13535 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13536 //
13537 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013538 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013539 // V UNDEF BUILD_VECTOR UNDEF
13540 // \ / \ /
13541 // CONCAT_VECTOR CONCAT_VECTOR
13542 // \ /
13543 // \ /
13544 // RESULT: V + zero extended
13545 //
13546 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13547 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13548 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13549 return SDValue();
13550
13551 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13552 return SDValue();
13553
13554 // To match the shuffle mask, the first half of the mask should
13555 // be exactly the first vector, and all the rest a splat with the
13556 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013557 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013558 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13559 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13560 return SDValue();
13561
Chad Rosier3d1161e2012-01-03 21:05:52 +000013562 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13563 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013564 if (Ld->hasNUsesOfValue(1, 0)) {
13565 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13566 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13567 SDValue ResNode =
13568 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13569 Ld->getMemoryVT(),
13570 Ld->getPointerInfo(),
13571 Ld->getAlignment(),
13572 false/*isVolatile*/, true/*ReadMem*/,
13573 false/*WriteMem*/);
13574 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13575 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013576 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013577
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013578 // Emit a zeroed vector and insert the desired subvector on its
13579 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013580 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013581 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013582 return DCI.CombineTo(N, InsV);
13583 }
13584
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013585 //===--------------------------------------------------------------------===//
13586 // Combine some shuffles into subvector extracts and inserts:
13587 //
13588
13589 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13590 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013591 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13592 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013593 return DCI.CombineTo(N, InsV);
13594 }
13595
13596 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13597 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013598 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13599 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013600 return DCI.CombineTo(N, InsV);
13601 }
13602
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013603 return SDValue();
13604}
13605
13606/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013607static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013608 TargetLowering::DAGCombinerInfo &DCI,
13609 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013610 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013611 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013612
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013613 // Don't create instructions with illegal types after legalize types has run.
13614 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13615 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13616 return SDValue();
13617
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013618 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013619 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013620 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013621 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013622
13623 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013624 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013625 return SDValue();
13626
13627 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13628 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13629 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013630 SmallVector<SDValue, 16> Elts;
13631 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013632 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013633
Nate Begemanfdea31a2010-03-24 20:49:50 +000013634 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013635}
Evan Chengd880b972008-05-09 21:53:03 +000013636
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013637
Craig Topper55b24052012-09-11 06:15:32 +000013638/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013639/// a sequence of vector shuffle operations.
13640/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000013641static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
13642 TargetLowering::DAGCombinerInfo &DCI,
13643 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013644 if (!DCI.isBeforeLegalizeOps())
13645 return SDValue();
13646
Craig Topper3ef43cf2012-04-24 06:36:35 +000013647 if (!Subtarget->hasAVX())
13648 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013649
13650 EVT VT = N->getValueType(0);
13651 SDValue Op = N->getOperand(0);
13652 EVT OpVT = Op.getValueType();
13653 DebugLoc dl = N->getDebugLoc();
13654
13655 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13656
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013657 if (Subtarget->hasAVX2()) {
13658 // AVX2: v4i64 -> v4i32
13659
13660 // VPERMD
13661 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13662
13663 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13664 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13665 ShufMask);
13666
Craig Topperd63fa652012-04-22 18:51:37 +000013667 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13668 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013669 }
13670
13671 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013672 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013673 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013674
13675 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013676 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013677
13678 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13679 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13680
13681 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013682 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013683
Craig Toppercacafd42012-08-14 08:18:43 +000013684 SDValue Undef = DAG.getUNDEF(VT);
13685 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13686 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013687
13688 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013689 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013690
Elena Demikhovsky73252572012-02-01 10:33:05 +000013691 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013692 }
Craig Topperd63fa652012-04-22 18:51:37 +000013693
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013694 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13695
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013696 if (Subtarget->hasAVX2()) {
13697 // AVX2: v8i32 -> v8i16
13698
13699 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013700
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013701 // PSHUFB
13702 SmallVector<SDValue,32> pshufbMask;
13703 for (unsigned i = 0; i < 2; ++i) {
13704 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13705 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13706 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13707 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13708 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13709 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13710 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13711 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13712 for (unsigned j = 0; j < 8; ++j)
13713 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13714 }
Craig Topperd63fa652012-04-22 18:51:37 +000013715 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13716 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013717 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13718
13719 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13720
13721 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013722 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013723 &ShufMask[0]);
13724
Craig Topperd63fa652012-04-22 18:51:37 +000013725 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13726 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013727
13728 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13729 }
13730
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013731 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013732 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013733
13734 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013735 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013736
13737 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13738 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13739
13740 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013741 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13742 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013743
Craig Toppercacafd42012-08-14 08:18:43 +000013744 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13745 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13746 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013747
13748 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13749 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13750
13751 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013752 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013753
Elena Demikhovsky73252572012-02-01 10:33:05 +000013754 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013755 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013756 }
13757
13758 return SDValue();
13759}
13760
Craig Topper89f4e662012-03-20 07:17:59 +000013761/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13762/// specific shuffle of a load can be folded into a single element load.
13763/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13764/// shuffles have been customed lowered so we need to handle those here.
13765static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13766 TargetLowering::DAGCombinerInfo &DCI) {
13767 if (DCI.isBeforeLegalizeOps())
13768 return SDValue();
13769
13770 SDValue InVec = N->getOperand(0);
13771 SDValue EltNo = N->getOperand(1);
13772
13773 if (!isa<ConstantSDNode>(EltNo))
13774 return SDValue();
13775
13776 EVT VT = InVec.getValueType();
13777
13778 bool HasShuffleIntoBitcast = false;
13779 if (InVec.getOpcode() == ISD::BITCAST) {
13780 // Don't duplicate a load with other uses.
13781 if (!InVec.hasOneUse())
13782 return SDValue();
13783 EVT BCVT = InVec.getOperand(0).getValueType();
13784 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13785 return SDValue();
13786 InVec = InVec.getOperand(0);
13787 HasShuffleIntoBitcast = true;
13788 }
13789
13790 if (!isTargetShuffle(InVec.getOpcode()))
13791 return SDValue();
13792
13793 // Don't duplicate a load with other uses.
13794 if (!InVec.hasOneUse())
13795 return SDValue();
13796
13797 SmallVector<int, 16> ShuffleMask;
13798 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013799 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13800 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013801 return SDValue();
13802
13803 // Select the input vector, guarding against out of range extract vector.
13804 unsigned NumElems = VT.getVectorNumElements();
13805 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13806 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13807 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13808 : InVec.getOperand(1);
13809
13810 // If inputs to shuffle are the same for both ops, then allow 2 uses
13811 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13812
13813 if (LdNode.getOpcode() == ISD::BITCAST) {
13814 // Don't duplicate a load with other uses.
13815 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13816 return SDValue();
13817
13818 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13819 LdNode = LdNode.getOperand(0);
13820 }
13821
13822 if (!ISD::isNormalLoad(LdNode.getNode()))
13823 return SDValue();
13824
13825 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13826
13827 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13828 return SDValue();
13829
13830 if (HasShuffleIntoBitcast) {
13831 // If there's a bitcast before the shuffle, check if the load type and
13832 // alignment is valid.
13833 unsigned Align = LN0->getAlignment();
13834 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13835 unsigned NewAlign = TLI.getTargetData()->
13836 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13837
13838 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13839 return SDValue();
13840 }
13841
13842 // All checks match so transform back to vector_shuffle so that DAG combiner
13843 // can finish the job
13844 DebugLoc dl = N->getDebugLoc();
13845
13846 // Create shuffle node taking into account the case that its a unary shuffle
13847 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13848 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13849 InVec.getOperand(0), Shuffle,
13850 &ShuffleMask[0]);
13851 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13852 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13853 EltNo);
13854}
13855
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013856/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13857/// generation and convert it from being a bunch of shuffles and extracts
13858/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013859static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013860 TargetLowering::DAGCombinerInfo &DCI) {
13861 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13862 if (NewOp.getNode())
13863 return NewOp;
13864
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013865 SDValue InputVector = N->getOperand(0);
13866
13867 // Only operate on vectors of 4 elements, where the alternative shuffling
13868 // gets to be more expensive.
13869 if (InputVector.getValueType() != MVT::v4i32)
13870 return SDValue();
13871
13872 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13873 // single use which is a sign-extend or zero-extend, and all elements are
13874 // used.
13875 SmallVector<SDNode *, 4> Uses;
13876 unsigned ExtractedElements = 0;
13877 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13878 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13879 if (UI.getUse().getResNo() != InputVector.getResNo())
13880 return SDValue();
13881
13882 SDNode *Extract = *UI;
13883 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13884 return SDValue();
13885
13886 if (Extract->getValueType(0) != MVT::i32)
13887 return SDValue();
13888 if (!Extract->hasOneUse())
13889 return SDValue();
13890 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13891 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13892 return SDValue();
13893 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13894 return SDValue();
13895
13896 // Record which element was extracted.
13897 ExtractedElements |=
13898 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13899
13900 Uses.push_back(Extract);
13901 }
13902
13903 // If not all the elements were used, this may not be worthwhile.
13904 if (ExtractedElements != 15)
13905 return SDValue();
13906
13907 // Ok, we've now decided to do the transformation.
13908 DebugLoc dl = InputVector.getDebugLoc();
13909
13910 // Store the value to a temporary stack slot.
13911 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013912 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13913 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013914
13915 // Replace each use (extract) with a load of the appropriate element.
13916 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13917 UE = Uses.end(); UI != UE; ++UI) {
13918 SDNode *Extract = *UI;
13919
Nadav Rotem86694292011-05-17 08:31:57 +000013920 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013921 SDValue Idx = Extract->getOperand(1);
13922 unsigned EltSize =
13923 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13924 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013925 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013926 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13927
Nadav Rotem86694292011-05-17 08:31:57 +000013928 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013929 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013930
13931 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013932 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013933 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013934 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013935
13936 // Replace the exact with the load.
13937 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13938 }
13939
13940 // The replacement was made in place; don't return anything.
13941 return SDValue();
13942}
13943
Duncan Sands6bcd2192011-09-17 16:49:39 +000013944/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13945/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013946static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013947 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013948 const X86Subtarget *Subtarget) {
13949 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013950 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013951 // Get the LHS/RHS of the select.
13952 SDValue LHS = N->getOperand(1);
13953 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013954 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013955
Dan Gohman670e5392009-09-21 18:03:22 +000013956 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013957 // instructions match the semantics of the common C idiom x<y?x:y but not
13958 // x<=y?x:y, because of how they handle negative zero (which can be
13959 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013960 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13961 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013962 (Subtarget->hasSSE2() ||
13963 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013964 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013965
Chris Lattner47b4ce82009-03-11 05:48:52 +000013966 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013967 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013968 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13969 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013970 switch (CC) {
13971 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013972 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013973 // Converting this to a min would handle NaNs incorrectly, and swapping
13974 // the operands would cause it to handle comparisons between positive
13975 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013976 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013977 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013978 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13979 break;
13980 std::swap(LHS, RHS);
13981 }
Dan Gohman670e5392009-09-21 18:03:22 +000013982 Opcode = X86ISD::FMIN;
13983 break;
13984 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013985 // Converting this to a min would handle comparisons between positive
13986 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013987 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013988 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13989 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013990 Opcode = X86ISD::FMIN;
13991 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013992 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013993 // Converting this to a min would handle both negative zeros and NaNs
13994 // incorrectly, but we can swap the operands to fix both.
13995 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013996 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013997 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013998 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013999 Opcode = X86ISD::FMIN;
14000 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014001
Dan Gohman670e5392009-09-21 18:03:22 +000014002 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014003 // Converting this to a max would handle comparisons between positive
14004 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014005 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000014006 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014007 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014008 Opcode = X86ISD::FMAX;
14009 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000014010 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014011 // Converting this to a max would handle NaNs incorrectly, and swapping
14012 // the operands would cause it to handle comparisons between positive
14013 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014014 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014015 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014016 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
14017 break;
14018 std::swap(LHS, RHS);
14019 }
Dan Gohman670e5392009-09-21 18:03:22 +000014020 Opcode = X86ISD::FMAX;
14021 break;
14022 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014023 // Converting this to a max would handle both negative zeros and NaNs
14024 // incorrectly, but we can swap the operands to fix both.
14025 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014026 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014027 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014028 case ISD::SETGE:
14029 Opcode = X86ISD::FMAX;
14030 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000014031 }
Dan Gohman670e5392009-09-21 18:03:22 +000014032 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000014033 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
14034 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000014035 switch (CC) {
14036 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014037 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014038 // Converting this to a min would handle comparisons between positive
14039 // and negative zero incorrectly, and swapping the operands would
14040 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014041 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014042 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014043 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014044 break;
14045 std::swap(LHS, RHS);
14046 }
Dan Gohman670e5392009-09-21 18:03:22 +000014047 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014048 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014049 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014050 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014051 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014052 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14053 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014054 Opcode = X86ISD::FMIN;
14055 break;
14056 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014057 // Converting this to a min would handle both negative zeros and NaNs
14058 // incorrectly, but we can swap the operands to fix both.
14059 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014060 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014061 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014062 case ISD::SETGE:
14063 Opcode = X86ISD::FMIN;
14064 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014065
Dan Gohman670e5392009-09-21 18:03:22 +000014066 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014067 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014068 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014069 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014070 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014071 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014072 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014073 // Converting this to a max would handle comparisons between positive
14074 // and negative zero incorrectly, and swapping the operands would
14075 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014076 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014077 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014078 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014079 break;
14080 std::swap(LHS, RHS);
14081 }
Dan Gohman670e5392009-09-21 18:03:22 +000014082 Opcode = X86ISD::FMAX;
14083 break;
14084 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014085 // Converting this to a max would handle both negative zeros and NaNs
14086 // incorrectly, but we can swap the operands to fix both.
14087 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014088 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014089 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014090 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014091 Opcode = X86ISD::FMAX;
14092 break;
14093 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014094 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014095
Chris Lattner47b4ce82009-03-11 05:48:52 +000014096 if (Opcode)
14097 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014098 }
Eric Christopherfd179292009-08-27 18:07:15 +000014099
Chris Lattnerd1980a52009-03-12 06:52:53 +000014100 // If this is a select between two integer constants, try to do some
14101 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014102 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14103 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014104 // Don't do this for crazy integer types.
14105 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14106 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014107 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014108 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014109
Chris Lattnercee56e72009-03-13 05:53:31 +000014110 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014111 // Efficiently invertible.
14112 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14113 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14114 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14115 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014116 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014117 }
Eric Christopherfd179292009-08-27 18:07:15 +000014118
Chris Lattnerd1980a52009-03-12 06:52:53 +000014119 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014120 if (FalseC->getAPIntValue() == 0 &&
14121 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014122 if (NeedsCondInvert) // Invert the condition if needed.
14123 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14124 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014125
Chris Lattnerd1980a52009-03-12 06:52:53 +000014126 // Zero extend the condition if needed.
14127 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014128
Chris Lattnercee56e72009-03-13 05:53:31 +000014129 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014130 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014131 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014132 }
Eric Christopherfd179292009-08-27 18:07:15 +000014133
Chris Lattner97a29a52009-03-13 05:22:11 +000014134 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014135 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014136 if (NeedsCondInvert) // Invert the condition if needed.
14137 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14138 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014139
Chris Lattner97a29a52009-03-13 05:22:11 +000014140 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014141 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14142 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014143 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014144 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014145 }
Eric Christopherfd179292009-08-27 18:07:15 +000014146
Chris Lattnercee56e72009-03-13 05:53:31 +000014147 // Optimize cases that will turn into an LEA instruction. This requires
14148 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014149 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014150 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014151 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014152
Chris Lattnercee56e72009-03-13 05:53:31 +000014153 bool isFastMultiplier = false;
14154 if (Diff < 10) {
14155 switch ((unsigned char)Diff) {
14156 default: break;
14157 case 1: // result = add base, cond
14158 case 2: // result = lea base( , cond*2)
14159 case 3: // result = lea base(cond, cond*2)
14160 case 4: // result = lea base( , cond*4)
14161 case 5: // result = lea base(cond, cond*4)
14162 case 8: // result = lea base( , cond*8)
14163 case 9: // result = lea base(cond, cond*8)
14164 isFastMultiplier = true;
14165 break;
14166 }
14167 }
Eric Christopherfd179292009-08-27 18:07:15 +000014168
Chris Lattnercee56e72009-03-13 05:53:31 +000014169 if (isFastMultiplier) {
14170 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14171 if (NeedsCondInvert) // Invert the condition if needed.
14172 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14173 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014174
Chris Lattnercee56e72009-03-13 05:53:31 +000014175 // Zero extend the condition if needed.
14176 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14177 Cond);
14178 // Scale the condition by the difference.
14179 if (Diff != 1)
14180 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14181 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014182
Chris Lattnercee56e72009-03-13 05:53:31 +000014183 // Add the base if non-zero.
14184 if (FalseC->getAPIntValue() != 0)
14185 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14186 SDValue(FalseC, 0));
14187 return Cond;
14188 }
Eric Christopherfd179292009-08-27 18:07:15 +000014189 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014190 }
14191 }
Eric Christopherfd179292009-08-27 18:07:15 +000014192
Evan Cheng56f582d2012-01-04 01:41:39 +000014193 // Canonicalize max and min:
14194 // (x > y) ? x : y -> (x >= y) ? x : y
14195 // (x < y) ? x : y -> (x <= y) ? x : y
14196 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14197 // the need for an extra compare
14198 // against zero. e.g.
14199 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14200 // subl %esi, %edi
14201 // testl %edi, %edi
14202 // movl $0, %eax
14203 // cmovgl %edi, %eax
14204 // =>
14205 // xorl %eax, %eax
14206 // subl %esi, $edi
14207 // cmovsl %eax, %edi
14208 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14209 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14210 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14211 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14212 switch (CC) {
14213 default: break;
14214 case ISD::SETLT:
14215 case ISD::SETGT: {
14216 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14217 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14218 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14219 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14220 }
14221 }
14222 }
14223
Nadav Rotemcc616562012-01-15 19:27:55 +000014224 // If we know that this node is legal then we know that it is going to be
14225 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14226 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14227 // to simplify previous instructions.
14228 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14229 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014230 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014231 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014232
14233 // Don't optimize vector selects that map to mask-registers.
14234 if (BitWidth == 1)
14235 return SDValue();
14236
Nadav Rotemcc616562012-01-15 19:27:55 +000014237 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14238 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14239
14240 APInt KnownZero, KnownOne;
14241 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14242 DCI.isBeforeLegalizeOps());
14243 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14244 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14245 DCI.CommitTargetLoweringOpt(TLO);
14246 }
14247
Dan Gohman475871a2008-07-27 21:46:04 +000014248 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014249}
14250
Michael Liao2a33cec2012-08-10 19:58:13 +000014251// Check whether a boolean test is testing a boolean value generated by
14252// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14253// code.
14254//
14255// Simplify the following patterns:
14256// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14257// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14258// to (Op EFLAGS Cond)
14259//
14260// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14261// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14262// to (Op EFLAGS !Cond)
14263//
14264// where Op could be BRCOND or CMOV.
14265//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014266static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014267 // Quit if not CMP and SUB with its value result used.
14268 if (Cmp.getOpcode() != X86ISD::CMP &&
14269 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14270 return SDValue();
14271
14272 // Quit if not used as a boolean value.
14273 if (CC != X86::COND_E && CC != X86::COND_NE)
14274 return SDValue();
14275
14276 // Check CMP operands. One of them should be 0 or 1 and the other should be
14277 // an SetCC or extended from it.
14278 SDValue Op1 = Cmp.getOperand(0);
14279 SDValue Op2 = Cmp.getOperand(1);
14280
14281 SDValue SetCC;
14282 const ConstantSDNode* C = 0;
14283 bool needOppositeCond = (CC == X86::COND_E);
14284
14285 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14286 SetCC = Op2;
14287 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14288 SetCC = Op1;
14289 else // Quit if all operands are not constants.
14290 return SDValue();
14291
14292 if (C->getZExtValue() == 1)
14293 needOppositeCond = !needOppositeCond;
14294 else if (C->getZExtValue() != 0)
14295 // Quit if the constant is neither 0 or 1.
14296 return SDValue();
14297
14298 // Skip 'zext' node.
14299 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14300 SetCC = SetCC.getOperand(0);
14301
Michael Liao7fdc66b2012-09-10 16:36:16 +000014302 switch (SetCC.getOpcode()) {
14303 case X86ISD::SETCC:
14304 // Set the condition code or opposite one if necessary.
14305 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14306 if (needOppositeCond)
14307 CC = X86::GetOppositeBranchCondition(CC);
14308 return SetCC.getOperand(1);
14309 case X86ISD::CMOV: {
14310 // Check whether false/true value has canonical one, i.e. 0 or 1.
14311 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14312 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14313 // Quit if true value is not a constant.
14314 if (!TVal)
14315 return SDValue();
14316 // Quit if false value is not a constant.
14317 if (!FVal) {
14318 // A special case for rdrand, where 0 is set if false cond is found.
14319 SDValue Op = SetCC.getOperand(0);
14320 if (Op.getOpcode() != X86ISD::RDRAND)
14321 return SDValue();
14322 }
14323 // Quit if false value is not the constant 0 or 1.
14324 bool FValIsFalse = true;
14325 if (FVal && FVal->getZExtValue() != 0) {
14326 if (FVal->getZExtValue() != 1)
14327 return SDValue();
14328 // If FVal is 1, opposite cond is needed.
14329 needOppositeCond = !needOppositeCond;
14330 FValIsFalse = false;
14331 }
14332 // Quit if TVal is not the constant opposite of FVal.
14333 if (FValIsFalse && TVal->getZExtValue() != 1)
14334 return SDValue();
14335 if (!FValIsFalse && TVal->getZExtValue() != 0)
14336 return SDValue();
14337 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14338 if (needOppositeCond)
14339 CC = X86::GetOppositeBranchCondition(CC);
14340 return SetCC.getOperand(3);
14341 }
14342 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014343
Michael Liao7fdc66b2012-09-10 16:36:16 +000014344 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014345}
14346
Chris Lattnerd1980a52009-03-12 06:52:53 +000014347/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14348static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014349 TargetLowering::DAGCombinerInfo &DCI,
14350 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014351 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014352
Chris Lattnerd1980a52009-03-12 06:52:53 +000014353 // If the flag operand isn't dead, don't touch this CMOV.
14354 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14355 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014356
Evan Chengb5a55d92011-05-24 01:48:22 +000014357 SDValue FalseOp = N->getOperand(0);
14358 SDValue TrueOp = N->getOperand(1);
14359 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14360 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014361
Evan Chengb5a55d92011-05-24 01:48:22 +000014362 if (CC == X86::COND_E || CC == X86::COND_NE) {
14363 switch (Cond.getOpcode()) {
14364 default: break;
14365 case X86ISD::BSR:
14366 case X86ISD::BSF:
14367 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14368 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14369 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14370 }
14371 }
14372
Michael Liao2a33cec2012-08-10 19:58:13 +000014373 SDValue Flags;
14374
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014375 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014376 if (Flags.getNode() &&
14377 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014378 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014379 SDValue Ops[] = { FalseOp, TrueOp,
14380 DAG.getConstant(CC, MVT::i8), Flags };
14381 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14382 Ops, array_lengthof(Ops));
14383 }
14384
Chris Lattnerd1980a52009-03-12 06:52:53 +000014385 // If this is a select between two integer constants, try to do some
14386 // optimizations. Note that the operands are ordered the opposite of SELECT
14387 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014388 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14389 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014390 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14391 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014392 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14393 CC = X86::GetOppositeBranchCondition(CC);
14394 std::swap(TrueC, FalseC);
14395 }
Eric Christopherfd179292009-08-27 18:07:15 +000014396
Chris Lattnerd1980a52009-03-12 06:52:53 +000014397 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014398 // This is efficient for any integer data type (including i8/i16) and
14399 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014400 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014401 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14402 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014403
Chris Lattnerd1980a52009-03-12 06:52:53 +000014404 // Zero extend the condition if needed.
14405 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014406
Chris Lattnerd1980a52009-03-12 06:52:53 +000014407 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14408 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014409 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014410 if (N->getNumValues() == 2) // Dead flag value?
14411 return DCI.CombineTo(N, Cond, SDValue());
14412 return Cond;
14413 }
Eric Christopherfd179292009-08-27 18:07:15 +000014414
Chris Lattnercee56e72009-03-13 05:53:31 +000014415 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14416 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014417 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014418 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14419 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014420
Chris Lattner97a29a52009-03-13 05:22:11 +000014421 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014422 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14423 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014424 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14425 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014426
Chris Lattner97a29a52009-03-13 05:22:11 +000014427 if (N->getNumValues() == 2) // Dead flag value?
14428 return DCI.CombineTo(N, Cond, SDValue());
14429 return Cond;
14430 }
Eric Christopherfd179292009-08-27 18:07:15 +000014431
Chris Lattnercee56e72009-03-13 05:53:31 +000014432 // Optimize cases that will turn into an LEA instruction. This requires
14433 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014434 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014435 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014436 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014437
Chris Lattnercee56e72009-03-13 05:53:31 +000014438 bool isFastMultiplier = false;
14439 if (Diff < 10) {
14440 switch ((unsigned char)Diff) {
14441 default: break;
14442 case 1: // result = add base, cond
14443 case 2: // result = lea base( , cond*2)
14444 case 3: // result = lea base(cond, cond*2)
14445 case 4: // result = lea base( , cond*4)
14446 case 5: // result = lea base(cond, cond*4)
14447 case 8: // result = lea base( , cond*8)
14448 case 9: // result = lea base(cond, cond*8)
14449 isFastMultiplier = true;
14450 break;
14451 }
14452 }
Eric Christopherfd179292009-08-27 18:07:15 +000014453
Chris Lattnercee56e72009-03-13 05:53:31 +000014454 if (isFastMultiplier) {
14455 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014456 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14457 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014458 // Zero extend the condition if needed.
14459 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14460 Cond);
14461 // Scale the condition by the difference.
14462 if (Diff != 1)
14463 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14464 DAG.getConstant(Diff, Cond.getValueType()));
14465
14466 // Add the base if non-zero.
14467 if (FalseC->getAPIntValue() != 0)
14468 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14469 SDValue(FalseC, 0));
14470 if (N->getNumValues() == 2) // Dead flag value?
14471 return DCI.CombineTo(N, Cond, SDValue());
14472 return Cond;
14473 }
Eric Christopherfd179292009-08-27 18:07:15 +000014474 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014475 }
14476 }
14477 return SDValue();
14478}
14479
14480
Evan Cheng0b0cd912009-03-28 05:57:29 +000014481/// PerformMulCombine - Optimize a single multiply with constant into two
14482/// in order to implement it with two cheaper instructions, e.g.
14483/// LEA + SHL, LEA + LEA.
14484static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14485 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014486 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14487 return SDValue();
14488
Owen Andersone50ed302009-08-10 22:56:29 +000014489 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014490 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014491 return SDValue();
14492
14493 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14494 if (!C)
14495 return SDValue();
14496 uint64_t MulAmt = C->getZExtValue();
14497 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14498 return SDValue();
14499
14500 uint64_t MulAmt1 = 0;
14501 uint64_t MulAmt2 = 0;
14502 if ((MulAmt % 9) == 0) {
14503 MulAmt1 = 9;
14504 MulAmt2 = MulAmt / 9;
14505 } else if ((MulAmt % 5) == 0) {
14506 MulAmt1 = 5;
14507 MulAmt2 = MulAmt / 5;
14508 } else if ((MulAmt % 3) == 0) {
14509 MulAmt1 = 3;
14510 MulAmt2 = MulAmt / 3;
14511 }
14512 if (MulAmt2 &&
14513 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14514 DebugLoc DL = N->getDebugLoc();
14515
14516 if (isPowerOf2_64(MulAmt2) &&
14517 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14518 // If second multiplifer is pow2, issue it first. We want the multiply by
14519 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14520 // is an add.
14521 std::swap(MulAmt1, MulAmt2);
14522
14523 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014524 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014525 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014526 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014527 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014528 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014529 DAG.getConstant(MulAmt1, VT));
14530
Eric Christopherfd179292009-08-27 18:07:15 +000014531 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014532 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014533 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014534 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014535 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014536 DAG.getConstant(MulAmt2, VT));
14537
14538 // Do not add new nodes to DAG combiner worklist.
14539 DCI.CombineTo(N, NewMul, false);
14540 }
14541 return SDValue();
14542}
14543
Evan Chengad9c0a32009-12-15 00:53:42 +000014544static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14545 SDValue N0 = N->getOperand(0);
14546 SDValue N1 = N->getOperand(1);
14547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14548 EVT VT = N0.getValueType();
14549
14550 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14551 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014552 if (VT.isInteger() && !VT.isVector() &&
14553 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014554 N0.getOperand(1).getOpcode() == ISD::Constant) {
14555 SDValue N00 = N0.getOperand(0);
14556 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14557 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14558 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14559 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14560 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14561 APInt ShAmt = N1C->getAPIntValue();
14562 Mask = Mask.shl(ShAmt);
14563 if (Mask != 0)
14564 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14565 N00, DAG.getConstant(Mask, VT));
14566 }
14567 }
14568
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014569
14570 // Hardware support for vector shifts is sparse which makes us scalarize the
14571 // vector operations in many cases. Also, on sandybridge ADD is faster than
14572 // shl.
14573 // (shl V, 1) -> add V,V
14574 if (isSplatVector(N1.getNode())) {
14575 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14576 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14577 // We shift all of the values by one. In many cases we do not have
14578 // hardware support for this operation. This is better expressed as an ADD
14579 // of two values.
14580 if (N1C && (1 == N1C->getZExtValue())) {
14581 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14582 }
14583 }
14584
Evan Chengad9c0a32009-12-15 00:53:42 +000014585 return SDValue();
14586}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014587
Nate Begeman740ab032009-01-26 00:52:55 +000014588/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14589/// when possible.
14590static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014591 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014592 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014593 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014594 if (N->getOpcode() == ISD::SHL) {
14595 SDValue V = PerformSHLCombine(N, DAG);
14596 if (V.getNode()) return V;
14597 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014598
Nate Begeman740ab032009-01-26 00:52:55 +000014599 // On X86 with SSE2 support, we can transform this to a vector shift if
14600 // all elements are shifted by the same amount. We can't do this in legalize
14601 // because the a constant vector is typically transformed to a constant pool
14602 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014603 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014604 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014605
Craig Topper7be5dfd2011-11-12 09:58:49 +000014606 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14607 (!Subtarget->hasAVX2() ||
14608 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014609 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014610
Mon P Wang3becd092009-01-28 08:12:05 +000014611 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014612 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014613 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014614 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014615 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14616 unsigned NumElts = VT.getVectorNumElements();
14617 unsigned i = 0;
14618 for (; i != NumElts; ++i) {
14619 SDValue Arg = ShAmtOp.getOperand(i);
14620 if (Arg.getOpcode() == ISD::UNDEF) continue;
14621 BaseShAmt = Arg;
14622 break;
14623 }
Craig Topper37c26772012-01-17 04:44:50 +000014624 // Handle the case where the build_vector is all undef
14625 // FIXME: Should DAG allow this?
14626 if (i == NumElts)
14627 return SDValue();
14628
Mon P Wang3becd092009-01-28 08:12:05 +000014629 for (; i != NumElts; ++i) {
14630 SDValue Arg = ShAmtOp.getOperand(i);
14631 if (Arg.getOpcode() == ISD::UNDEF) continue;
14632 if (Arg != BaseShAmt) {
14633 return SDValue();
14634 }
14635 }
14636 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014637 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014638 SDValue InVec = ShAmtOp.getOperand(0);
14639 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14640 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14641 unsigned i = 0;
14642 for (; i != NumElts; ++i) {
14643 SDValue Arg = InVec.getOperand(i);
14644 if (Arg.getOpcode() == ISD::UNDEF) continue;
14645 BaseShAmt = Arg;
14646 break;
14647 }
14648 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14649 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014650 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014651 if (C->getZExtValue() == SplatIdx)
14652 BaseShAmt = InVec.getOperand(1);
14653 }
14654 }
Mon P Wang845b1892012-02-01 22:15:20 +000014655 if (BaseShAmt.getNode() == 0) {
14656 // Don't create instructions with illegal types after legalize
14657 // types has run.
14658 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14659 !DCI.isBeforeLegalize())
14660 return SDValue();
14661
Mon P Wangefa42202009-09-03 19:56:25 +000014662 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14663 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014664 }
Mon P Wang3becd092009-01-28 08:12:05 +000014665 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014666 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014667
Mon P Wangefa42202009-09-03 19:56:25 +000014668 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014669 if (EltVT.bitsGT(MVT::i32))
14670 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14671 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014672 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014673
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014674 // The shift amount is identical so we can do a vector shift.
14675 SDValue ValOp = N->getOperand(0);
14676 switch (N->getOpcode()) {
14677 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014678 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014679 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014680 switch (VT.getSimpleVT().SimpleTy) {
14681 default: return SDValue();
14682 case MVT::v2i64:
14683 case MVT::v4i32:
14684 case MVT::v8i16:
14685 case MVT::v4i64:
14686 case MVT::v8i32:
14687 case MVT::v16i16:
14688 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14689 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014690 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014691 switch (VT.getSimpleVT().SimpleTy) {
14692 default: return SDValue();
14693 case MVT::v4i32:
14694 case MVT::v8i16:
14695 case MVT::v8i32:
14696 case MVT::v16i16:
14697 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14698 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014699 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014700 switch (VT.getSimpleVT().SimpleTy) {
14701 default: return SDValue();
14702 case MVT::v2i64:
14703 case MVT::v4i32:
14704 case MVT::v8i16:
14705 case MVT::v4i64:
14706 case MVT::v8i32:
14707 case MVT::v16i16:
14708 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14709 }
Nate Begeman740ab032009-01-26 00:52:55 +000014710 }
Nate Begeman740ab032009-01-26 00:52:55 +000014711}
14712
Nate Begemanb65c1752010-12-17 22:55:37 +000014713
Stuart Hastings865f0932011-06-03 23:53:54 +000014714// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14715// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14716// and friends. Likewise for OR -> CMPNEQSS.
14717static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14718 TargetLowering::DAGCombinerInfo &DCI,
14719 const X86Subtarget *Subtarget) {
14720 unsigned opcode;
14721
14722 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14723 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014724 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014725 SDValue N0 = N->getOperand(0);
14726 SDValue N1 = N->getOperand(1);
14727 SDValue CMP0 = N0->getOperand(1);
14728 SDValue CMP1 = N1->getOperand(1);
14729 DebugLoc DL = N->getDebugLoc();
14730
14731 // The SETCCs should both refer to the same CMP.
14732 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14733 return SDValue();
14734
14735 SDValue CMP00 = CMP0->getOperand(0);
14736 SDValue CMP01 = CMP0->getOperand(1);
14737 EVT VT = CMP00.getValueType();
14738
14739 if (VT == MVT::f32 || VT == MVT::f64) {
14740 bool ExpectingFlags = false;
14741 // Check for any users that want flags:
14742 for (SDNode::use_iterator UI = N->use_begin(),
14743 UE = N->use_end();
14744 !ExpectingFlags && UI != UE; ++UI)
14745 switch (UI->getOpcode()) {
14746 default:
14747 case ISD::BR_CC:
14748 case ISD::BRCOND:
14749 case ISD::SELECT:
14750 ExpectingFlags = true;
14751 break;
14752 case ISD::CopyToReg:
14753 case ISD::SIGN_EXTEND:
14754 case ISD::ZERO_EXTEND:
14755 case ISD::ANY_EXTEND:
14756 break;
14757 }
14758
14759 if (!ExpectingFlags) {
14760 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14761 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14762
14763 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14764 X86::CondCode tmp = cc0;
14765 cc0 = cc1;
14766 cc1 = tmp;
14767 }
14768
14769 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14770 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14771 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14772 X86ISD::NodeType NTOperator = is64BitFP ?
14773 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14774 // FIXME: need symbolic constants for these magic numbers.
14775 // See X86ATTInstPrinter.cpp:printSSECC().
14776 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14777 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14778 DAG.getConstant(x86cc, MVT::i8));
14779 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14780 OnesOrZeroesF);
14781 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14782 DAG.getConstant(1, MVT::i32));
14783 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14784 return OneBitOfTruth;
14785 }
14786 }
14787 }
14788 }
14789 return SDValue();
14790}
14791
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014792/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14793/// so it can be folded inside ANDNP.
14794static bool CanFoldXORWithAllOnes(const SDNode *N) {
14795 EVT VT = N->getValueType(0);
14796
14797 // Match direct AllOnes for 128 and 256-bit vectors
14798 if (ISD::isBuildVectorAllOnes(N))
14799 return true;
14800
14801 // Look through a bit convert.
14802 if (N->getOpcode() == ISD::BITCAST)
14803 N = N->getOperand(0).getNode();
14804
14805 // Sometimes the operand may come from a insert_subvector building a 256-bit
14806 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014807 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014808 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14809 SDValue V1 = N->getOperand(0);
14810 SDValue V2 = N->getOperand(1);
14811
14812 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14813 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14814 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14815 ISD::isBuildVectorAllOnes(V2.getNode()))
14816 return true;
14817 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014818
14819 return false;
14820}
14821
Nate Begemanb65c1752010-12-17 22:55:37 +000014822static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14823 TargetLowering::DAGCombinerInfo &DCI,
14824 const X86Subtarget *Subtarget) {
14825 if (DCI.isBeforeLegalizeOps())
14826 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014827
Stuart Hastings865f0932011-06-03 23:53:54 +000014828 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14829 if (R.getNode())
14830 return R;
14831
Craig Topper54a11172011-10-14 07:06:56 +000014832 EVT VT = N->getValueType(0);
14833
Craig Topperb4c94572011-10-21 06:55:01 +000014834 // Create ANDN, BLSI, and BLSR instructions
14835 // BLSI is X & (-X)
14836 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014837 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14838 SDValue N0 = N->getOperand(0);
14839 SDValue N1 = N->getOperand(1);
14840 DebugLoc DL = N->getDebugLoc();
14841
14842 // Check LHS for not
14843 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14844 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14845 // Check RHS for not
14846 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14847 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14848
Craig Topperb4c94572011-10-21 06:55:01 +000014849 // Check LHS for neg
14850 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14851 isZero(N0.getOperand(0)))
14852 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14853
14854 // Check RHS for neg
14855 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14856 isZero(N1.getOperand(0)))
14857 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14858
14859 // Check LHS for X-1
14860 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14861 isAllOnes(N0.getOperand(1)))
14862 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14863
14864 // Check RHS for X-1
14865 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14866 isAllOnes(N1.getOperand(1)))
14867 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14868
Craig Topper54a11172011-10-14 07:06:56 +000014869 return SDValue();
14870 }
14871
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014872 // Want to form ANDNP nodes:
14873 // 1) In the hopes of then easily combining them with OR and AND nodes
14874 // to form PBLEND/PSIGN.
14875 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014876 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014877 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014878
Nate Begemanb65c1752010-12-17 22:55:37 +000014879 SDValue N0 = N->getOperand(0);
14880 SDValue N1 = N->getOperand(1);
14881 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014882
Nate Begemanb65c1752010-12-17 22:55:37 +000014883 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014884 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014885 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14886 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014887 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014888
14889 // Check RHS for vnot
14890 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014891 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14892 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014893 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014894
Nate Begemanb65c1752010-12-17 22:55:37 +000014895 return SDValue();
14896}
14897
Evan Cheng760d1942010-01-04 21:22:48 +000014898static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014899 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014900 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014901 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014902 return SDValue();
14903
Stuart Hastings865f0932011-06-03 23:53:54 +000014904 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14905 if (R.getNode())
14906 return R;
14907
Evan Cheng760d1942010-01-04 21:22:48 +000014908 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014909
Evan Cheng760d1942010-01-04 21:22:48 +000014910 SDValue N0 = N->getOperand(0);
14911 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014912
Nate Begemanb65c1752010-12-17 22:55:37 +000014913 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014914 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014915 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014916 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14917 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014918
Craig Topper1666cb62011-11-19 07:07:26 +000014919 // Canonicalize pandn to RHS
14920 if (N0.getOpcode() == X86ISD::ANDNP)
14921 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014922 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014923 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14924 SDValue Mask = N1.getOperand(0);
14925 SDValue X = N1.getOperand(1);
14926 SDValue Y;
14927 if (N0.getOperand(0) == Mask)
14928 Y = N0.getOperand(1);
14929 if (N0.getOperand(1) == Mask)
14930 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014931
Craig Topper1666cb62011-11-19 07:07:26 +000014932 // Check to see if the mask appeared in both the AND and ANDNP and
14933 if (!Y.getNode())
14934 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014935
Craig Topper1666cb62011-11-19 07:07:26 +000014936 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014937 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014938 if (Mask.getOpcode() == ISD::BITCAST)
14939 Mask = Mask.getOperand(0);
14940 if (X.getOpcode() == ISD::BITCAST)
14941 X = X.getOperand(0);
14942 if (Y.getOpcode() == ISD::BITCAST)
14943 Y = Y.getOperand(0);
14944
Craig Topper1666cb62011-11-19 07:07:26 +000014945 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014946
Craig Toppered2e13d2012-01-22 19:15:14 +000014947 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014948 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14949 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014950 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014951 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014952
14953 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014954 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014955 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14956 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14957 if ((SraAmt + 1) != EltBits)
14958 return SDValue();
14959
14960 DebugLoc DL = N->getDebugLoc();
14961
14962 // Now we know we at least have a plendvb with the mask val. See if
14963 // we can form a psignb/w/d.
14964 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014965 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14966 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014967 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14968 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14969 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014970 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014971 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014972 }
14973 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014974 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014975 return SDValue();
14976
14977 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14978
14979 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14980 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14981 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014982 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014983 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014984 }
14985 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014986
Craig Topper1666cb62011-11-19 07:07:26 +000014987 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14988 return SDValue();
14989
Nate Begemanb65c1752010-12-17 22:55:37 +000014990 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014991 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14992 std::swap(N0, N1);
14993 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14994 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014995 if (!N0.hasOneUse() || !N1.hasOneUse())
14996 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014997
14998 SDValue ShAmt0 = N0.getOperand(1);
14999 if (ShAmt0.getValueType() != MVT::i8)
15000 return SDValue();
15001 SDValue ShAmt1 = N1.getOperand(1);
15002 if (ShAmt1.getValueType() != MVT::i8)
15003 return SDValue();
15004 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
15005 ShAmt0 = ShAmt0.getOperand(0);
15006 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
15007 ShAmt1 = ShAmt1.getOperand(0);
15008
15009 DebugLoc DL = N->getDebugLoc();
15010 unsigned Opc = X86ISD::SHLD;
15011 SDValue Op0 = N0.getOperand(0);
15012 SDValue Op1 = N1.getOperand(0);
15013 if (ShAmt0.getOpcode() == ISD::SUB) {
15014 Opc = X86ISD::SHRD;
15015 std::swap(Op0, Op1);
15016 std::swap(ShAmt0, ShAmt1);
15017 }
15018
Evan Cheng8b1190a2010-04-28 01:18:01 +000015019 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000015020 if (ShAmt1.getOpcode() == ISD::SUB) {
15021 SDValue Sum = ShAmt1.getOperand(0);
15022 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000015023 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
15024 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
15025 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
15026 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000015027 return DAG.getNode(Opc, DL, VT,
15028 Op0, Op1,
15029 DAG.getNode(ISD::TRUNCATE, DL,
15030 MVT::i8, ShAmt0));
15031 }
15032 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
15033 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
15034 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000015035 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000015036 return DAG.getNode(Opc, DL, VT,
15037 N0.getOperand(0), N1.getOperand(0),
15038 DAG.getNode(ISD::TRUNCATE, DL,
15039 MVT::i8, ShAmt0));
15040 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015041
Evan Cheng760d1942010-01-04 21:22:48 +000015042 return SDValue();
15043}
15044
Manman Ren92363622012-06-07 22:39:10 +000015045// Generate NEG and CMOV for integer abs.
15046static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15047 EVT VT = N->getValueType(0);
15048
15049 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15050 // 8-bit integer abs to NEG and CMOV.
15051 if (VT.isInteger() && VT.getSizeInBits() == 8)
15052 return SDValue();
15053
15054 SDValue N0 = N->getOperand(0);
15055 SDValue N1 = N->getOperand(1);
15056 DebugLoc DL = N->getDebugLoc();
15057
15058 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15059 // and change it to SUB and CMOV.
15060 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15061 N0.getOpcode() == ISD::ADD &&
15062 N0.getOperand(1) == N1 &&
15063 N1.getOpcode() == ISD::SRA &&
15064 N1.getOperand(0) == N0.getOperand(0))
15065 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15066 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15067 // Generate SUB & CMOV.
15068 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15069 DAG.getConstant(0, VT), N0.getOperand(0));
15070
15071 SDValue Ops[] = { N0.getOperand(0), Neg,
15072 DAG.getConstant(X86::COND_GE, MVT::i8),
15073 SDValue(Neg.getNode(), 1) };
15074 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15075 Ops, array_lengthof(Ops));
15076 }
15077 return SDValue();
15078}
15079
Craig Topper3738ccd2011-12-27 06:27:23 +000015080// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015081static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15082 TargetLowering::DAGCombinerInfo &DCI,
15083 const X86Subtarget *Subtarget) {
15084 if (DCI.isBeforeLegalizeOps())
15085 return SDValue();
15086
Manman Ren45d53b82012-06-08 18:58:26 +000015087 if (Subtarget->hasCMov()) {
15088 SDValue RV = performIntegerAbsCombine(N, DAG);
15089 if (RV.getNode())
15090 return RV;
15091 }
Manman Ren92363622012-06-07 22:39:10 +000015092
15093 // Try forming BMI if it is available.
15094 if (!Subtarget->hasBMI())
15095 return SDValue();
15096
Craig Topperb4c94572011-10-21 06:55:01 +000015097 EVT VT = N->getValueType(0);
15098
15099 if (VT != MVT::i32 && VT != MVT::i64)
15100 return SDValue();
15101
Craig Topper3738ccd2011-12-27 06:27:23 +000015102 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15103
Craig Topperb4c94572011-10-21 06:55:01 +000015104 // Create BLSMSK instructions by finding X ^ (X-1)
15105 SDValue N0 = N->getOperand(0);
15106 SDValue N1 = N->getOperand(1);
15107 DebugLoc DL = N->getDebugLoc();
15108
15109 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15110 isAllOnes(N0.getOperand(1)))
15111 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15112
15113 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15114 isAllOnes(N1.getOperand(1)))
15115 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15116
15117 return SDValue();
15118}
15119
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015120/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15121static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015122 TargetLowering::DAGCombinerInfo &DCI,
15123 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015124 LoadSDNode *Ld = cast<LoadSDNode>(N);
15125 EVT RegVT = Ld->getValueType(0);
15126 EVT MemVT = Ld->getMemoryVT();
15127 DebugLoc dl = Ld->getDebugLoc();
15128 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15129
15130 ISD::LoadExtType Ext = Ld->getExtensionType();
15131
Nadav Rotemca6f2962011-09-18 19:00:23 +000015132 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015133 // shuffle. We need SSE4 for the shuffles.
15134 // TODO: It is possible to support ZExt by zeroing the undef values
15135 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015136 if (RegVT.isVector() && RegVT.isInteger() &&
15137 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015138 assert(MemVT != RegVT && "Cannot extend to the same type");
15139 assert(MemVT.isVector() && "Must load a vector from memory");
15140
15141 unsigned NumElems = RegVT.getVectorNumElements();
15142 unsigned RegSz = RegVT.getSizeInBits();
15143 unsigned MemSz = MemVT.getSizeInBits();
15144 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015145
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015146 // All sizes must be a power of two.
15147 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15148 return SDValue();
15149
15150 // Attempt to load the original value using scalar loads.
15151 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015152 MVT SclrLoadTy = MVT::i8;
15153 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15154 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15155 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015156 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015157 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015158 }
15159 }
15160
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015161 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15162 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15163 (64 <= MemSz))
15164 SclrLoadTy = MVT::f64;
15165
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015166 // Calculate the number of scalar loads that we need to perform
15167 // in order to load our vector from memory.
15168 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015169
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015170 // Represent our vector as a sequence of elements which are the
15171 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015172 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15173 RegSz/SclrLoadTy.getSizeInBits());
15174
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015175 // Represent the data using the same element type that is stored in
15176 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015177 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15178 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015179
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015180 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15181 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015182
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015183 // We can't shuffle using an illegal type.
15184 if (!TLI.isTypeLegal(WideVecVT))
15185 return SDValue();
15186
15187 SmallVector<SDValue, 8> Chains;
15188 SDValue Ptr = Ld->getBasePtr();
15189 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15190 TLI.getPointerTy());
15191 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15192
15193 for (unsigned i = 0; i < NumLoads; ++i) {
15194 // Perform a single load.
15195 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15196 Ptr, Ld->getPointerInfo(),
15197 Ld->isVolatile(), Ld->isNonTemporal(),
15198 Ld->isInvariant(), Ld->getAlignment());
15199 Chains.push_back(ScalarLoad.getValue(1));
15200 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15201 // another round of DAGCombining.
15202 if (i == 0)
15203 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15204 else
15205 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15206 ScalarLoad, DAG.getIntPtrConstant(i));
15207
15208 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15209 }
15210
15211 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15212 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015213
15214 // Bitcast the loaded value to a vector of the original element type, in
15215 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015216 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015217 unsigned SizeRatio = RegSz/MemSz;
15218
15219 // Redistribute the loaded elements into the different locations.
15220 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015221 for (unsigned i = 0; i != NumElems; ++i)
15222 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015223
15224 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015225 DAG.getUNDEF(WideVecVT),
15226 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015227
15228 // Bitcast to the requested type.
15229 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15230 // Replace the original load with the new sequence
15231 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015232 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015233 }
15234
15235 return SDValue();
15236}
15237
Chris Lattner149a4e52008-02-22 02:09:43 +000015238/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015239static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015240 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015241 StoreSDNode *St = cast<StoreSDNode>(N);
15242 EVT VT = St->getValue().getValueType();
15243 EVT StVT = St->getMemoryVT();
15244 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015245 SDValue StoredVal = St->getOperand(1);
15246 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15247
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015248 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015249 // On Sandy Bridge, 256-bit memory operations are executed by two
15250 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15251 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015252 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015253 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15254 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015255 SDValue Value0 = StoredVal.getOperand(0);
15256 SDValue Value1 = StoredVal.getOperand(1);
15257
15258 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15259 SDValue Ptr0 = St->getBasePtr();
15260 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15261
15262 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15263 St->getPointerInfo(), St->isVolatile(),
15264 St->isNonTemporal(), St->getAlignment());
15265 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15266 St->getPointerInfo(), St->isVolatile(),
15267 St->isNonTemporal(), St->getAlignment());
15268 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15269 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015270
15271 // Optimize trunc store (of multiple scalars) to shuffle and store.
15272 // First, pack all of the elements in one place. Next, store to memory
15273 // in fewer chunks.
15274 if (St->isTruncatingStore() && VT.isVector()) {
15275 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15276 unsigned NumElems = VT.getVectorNumElements();
15277 assert(StVT != VT && "Cannot truncate to the same type");
15278 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15279 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15280
15281 // From, To sizes and ElemCount must be pow of two
15282 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015283 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015284 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015285 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015286
Nadav Rotem614061b2011-08-10 19:30:14 +000015287 unsigned SizeRatio = FromSz / ToSz;
15288
15289 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15290
15291 // Create a type on which we perform the shuffle
15292 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15293 StVT.getScalarType(), NumElems*SizeRatio);
15294
15295 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15296
15297 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15298 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015299 for (unsigned i = 0; i != NumElems; ++i)
15300 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015301
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015302 // Can't shuffle using an illegal type.
15303 if (!TLI.isTypeLegal(WideVecVT))
15304 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015305
15306 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015307 DAG.getUNDEF(WideVecVT),
15308 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015309 // At this point all of the data is stored at the bottom of the
15310 // register. We now need to save it to mem.
15311
15312 // Find the largest store unit
15313 MVT StoreType = MVT::i8;
15314 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15315 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15316 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015317 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015318 StoreType = Tp;
15319 }
15320
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015321 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15322 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15323 (64 <= NumElems * ToSz))
15324 StoreType = MVT::f64;
15325
Nadav Rotem614061b2011-08-10 19:30:14 +000015326 // Bitcast the original vector into a vector of store-size units
15327 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015328 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015329 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15330 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15331 SmallVector<SDValue, 8> Chains;
15332 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15333 TLI.getPointerTy());
15334 SDValue Ptr = St->getBasePtr();
15335
15336 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015337 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015338 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15339 StoreType, ShuffWide,
15340 DAG.getIntPtrConstant(i));
15341 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15342 St->getPointerInfo(), St->isVolatile(),
15343 St->isNonTemporal(), St->getAlignment());
15344 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15345 Chains.push_back(Ch);
15346 }
15347
15348 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15349 Chains.size());
15350 }
15351
15352
Chris Lattner149a4e52008-02-22 02:09:43 +000015353 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15354 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015355 // A preferable solution to the general problem is to figure out the right
15356 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015357
15358 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015359 if (VT.getSizeInBits() != 64)
15360 return SDValue();
15361
Devang Patel578efa92009-06-05 21:57:13 +000015362 const Function *F = DAG.getMachineFunction().getFunction();
15363 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015364 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015365 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015366 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015367 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015368 isa<LoadSDNode>(St->getValue()) &&
15369 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15370 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015371 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015372 LoadSDNode *Ld = 0;
15373 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015374 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015375 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015376 // Must be a store of a load. We currently handle two cases: the load
15377 // is a direct child, and it's under an intervening TokenFactor. It is
15378 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015379 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015380 Ld = cast<LoadSDNode>(St->getChain());
15381 else if (St->getValue().hasOneUse() &&
15382 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015383 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015384 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015385 TokenFactorIndex = i;
15386 Ld = cast<LoadSDNode>(St->getValue());
15387 } else
15388 Ops.push_back(ChainVal->getOperand(i));
15389 }
15390 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015391
Evan Cheng536e6672009-03-12 05:59:15 +000015392 if (!Ld || !ISD::isNormalLoad(Ld))
15393 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015394
Evan Cheng536e6672009-03-12 05:59:15 +000015395 // If this is not the MMX case, i.e. we are just turning i64 load/store
15396 // into f64 load/store, avoid the transformation if there are multiple
15397 // uses of the loaded value.
15398 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15399 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015400
Evan Cheng536e6672009-03-12 05:59:15 +000015401 DebugLoc LdDL = Ld->getDebugLoc();
15402 DebugLoc StDL = N->getDebugLoc();
15403 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15404 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15405 // pair instead.
15406 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015407 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015408 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15409 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015410 Ld->isNonTemporal(), Ld->isInvariant(),
15411 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015412 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015413 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015414 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015415 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015416 Ops.size());
15417 }
Evan Cheng536e6672009-03-12 05:59:15 +000015418 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015419 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015420 St->isVolatile(), St->isNonTemporal(),
15421 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015422 }
Evan Cheng536e6672009-03-12 05:59:15 +000015423
15424 // Otherwise, lower to two pairs of 32-bit loads / stores.
15425 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015426 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15427 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015428
Owen Anderson825b72b2009-08-11 20:47:22 +000015429 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015430 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015431 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015432 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015433 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015434 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015435 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015436 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015437 MinAlign(Ld->getAlignment(), 4));
15438
15439 SDValue NewChain = LoLd.getValue(1);
15440 if (TokenFactorIndex != -1) {
15441 Ops.push_back(LoLd);
15442 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015443 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015444 Ops.size());
15445 }
15446
15447 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015448 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15449 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015450
15451 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015452 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015453 St->isVolatile(), St->isNonTemporal(),
15454 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015455 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015456 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015457 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015458 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015459 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015460 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015461 }
Dan Gohman475871a2008-07-27 21:46:04 +000015462 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015463}
15464
Duncan Sands17470be2011-09-22 20:15:48 +000015465/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15466/// and return the operands for the horizontal operation in LHS and RHS. A
15467/// horizontal operation performs the binary operation on successive elements
15468/// of its first operand, then on successive elements of its second operand,
15469/// returning the resulting values in a vector. For example, if
15470/// A = < float a0, float a1, float a2, float a3 >
15471/// and
15472/// B = < float b0, float b1, float b2, float b3 >
15473/// then the result of doing a horizontal operation on A and B is
15474/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15475/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15476/// A horizontal-op B, for some already available A and B, and if so then LHS is
15477/// set to A, RHS to B, and the routine returns 'true'.
15478/// Note that the binary operation should have the property that if one of the
15479/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015480static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015481 // Look for the following pattern: if
15482 // A = < float a0, float a1, float a2, float a3 >
15483 // B = < float b0, float b1, float b2, float b3 >
15484 // and
15485 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15486 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15487 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15488 // which is A horizontal-op B.
15489
15490 // At least one of the operands should be a vector shuffle.
15491 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15492 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15493 return false;
15494
15495 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015496
15497 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15498 "Unsupported vector type for horizontal add/sub");
15499
15500 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15501 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015502 unsigned NumElts = VT.getVectorNumElements();
15503 unsigned NumLanes = VT.getSizeInBits()/128;
15504 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015505 assert((NumLaneElts % 2 == 0) &&
15506 "Vector type should have an even number of elements in each lane");
15507 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015508
15509 // View LHS in the form
15510 // LHS = VECTOR_SHUFFLE A, B, LMask
15511 // If LHS is not a shuffle then pretend it is the shuffle
15512 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15513 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15514 // type VT.
15515 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015516 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015517 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15518 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15519 A = LHS.getOperand(0);
15520 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15521 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015522 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15523 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015524 } else {
15525 if (LHS.getOpcode() != ISD::UNDEF)
15526 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015527 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015528 LMask[i] = i;
15529 }
15530
15531 // Likewise, view RHS in the form
15532 // RHS = VECTOR_SHUFFLE C, D, RMask
15533 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015534 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015535 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15536 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15537 C = RHS.getOperand(0);
15538 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15539 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015540 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15541 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015542 } else {
15543 if (RHS.getOpcode() != ISD::UNDEF)
15544 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015545 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015546 RMask[i] = i;
15547 }
15548
15549 // Check that the shuffles are both shuffling the same vectors.
15550 if (!(A == C && B == D) && !(A == D && B == C))
15551 return false;
15552
15553 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15554 if (!A.getNode() && !B.getNode())
15555 return false;
15556
15557 // If A and B occur in reverse order in RHS, then "swap" them (which means
15558 // rewriting the mask).
15559 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015560 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015561
15562 // At this point LHS and RHS are equivalent to
15563 // LHS = VECTOR_SHUFFLE A, B, LMask
15564 // RHS = VECTOR_SHUFFLE A, B, RMask
15565 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015566 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015567 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015568
Craig Topperf8363302011-12-02 08:18:41 +000015569 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015570 if (LIdx < 0 || RIdx < 0 ||
15571 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15572 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015573 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015574
Craig Topperf8363302011-12-02 08:18:41 +000015575 // Check that successive elements are being operated on. If not, this is
15576 // not a horizontal operation.
15577 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15578 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015579 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015580 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015581 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015582 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015583 }
15584
15585 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15586 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15587 return true;
15588}
15589
15590/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15591static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15592 const X86Subtarget *Subtarget) {
15593 EVT VT = N->getValueType(0);
15594 SDValue LHS = N->getOperand(0);
15595 SDValue RHS = N->getOperand(1);
15596
15597 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015598 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015599 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015600 isHorizontalBinOp(LHS, RHS, true))
15601 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15602 return SDValue();
15603}
15604
15605/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15606static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15607 const X86Subtarget *Subtarget) {
15608 EVT VT = N->getValueType(0);
15609 SDValue LHS = N->getOperand(0);
15610 SDValue RHS = N->getOperand(1);
15611
15612 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015613 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015614 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015615 isHorizontalBinOp(LHS, RHS, false))
15616 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15617 return SDValue();
15618}
15619
Chris Lattner6cf73262008-01-25 06:14:17 +000015620/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15621/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015622static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015623 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15624 // F[X]OR(0.0, x) -> x
15625 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015626 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15627 if (C->getValueAPF().isPosZero())
15628 return N->getOperand(1);
15629 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15630 if (C->getValueAPF().isPosZero())
15631 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015632 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015633}
15634
Nadav Rotemd60cb112012-08-19 13:06:16 +000015635/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15636/// X86ISD::FMAX nodes.
15637static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15638 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15639
15640 // Only perform optimizations if UnsafeMath is used.
15641 if (!DAG.getTarget().Options.UnsafeFPMath)
15642 return SDValue();
15643
15644 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015645 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015646 unsigned NewOp = 0;
15647 switch (N->getOpcode()) {
15648 default: llvm_unreachable("unknown opcode");
15649 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15650 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15651 }
15652
15653 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15654 N->getOperand(0), N->getOperand(1));
15655}
15656
15657
Chris Lattneraf723b92008-01-25 05:46:26 +000015658/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015659static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015660 // FAND(0.0, x) -> 0.0
15661 // FAND(x, 0.0) -> 0.0
15662 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15663 if (C->getValueAPF().isPosZero())
15664 return N->getOperand(0);
15665 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15666 if (C->getValueAPF().isPosZero())
15667 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015668 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015669}
15670
Dan Gohmane5af2d32009-01-29 01:59:02 +000015671static SDValue PerformBTCombine(SDNode *N,
15672 SelectionDAG &DAG,
15673 TargetLowering::DAGCombinerInfo &DCI) {
15674 // BT ignores high bits in the bit index operand.
15675 SDValue Op1 = N->getOperand(1);
15676 if (Op1.hasOneUse()) {
15677 unsigned BitWidth = Op1.getValueSizeInBits();
15678 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15679 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015680 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15681 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015683 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15684 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15685 DCI.CommitTargetLoweringOpt(TLO);
15686 }
15687 return SDValue();
15688}
Chris Lattner83e6c992006-10-04 06:57:07 +000015689
Eli Friedman7a5e5552009-06-07 06:52:44 +000015690static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15691 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015692 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015693 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015694 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015695 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015696 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015697 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015698 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015699 }
15700 return SDValue();
15701}
15702
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015703static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15704 TargetLowering::DAGCombinerInfo &DCI,
15705 const X86Subtarget *Subtarget) {
15706 if (!DCI.isBeforeLegalizeOps())
15707 return SDValue();
15708
Craig Topper3ef43cf2012-04-24 06:36:35 +000015709 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015710 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015711
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015712 EVT VT = N->getValueType(0);
15713 SDValue Op = N->getOperand(0);
15714 EVT OpVT = Op.getValueType();
15715 DebugLoc dl = N->getDebugLoc();
15716
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015717 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15718 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015719
Craig Topper3ef43cf2012-04-24 06:36:35 +000015720 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015721 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015722
15723 // Optimize vectors in AVX mode
15724 // Sign extend v8i16 to v8i32 and
15725 // v4i32 to v4i64
15726 //
15727 // Divide input vector into two parts
15728 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15729 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15730 // concat the vectors to original VT
15731
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015732 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015733 SDValue Undef = DAG.getUNDEF(OpVT);
15734
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015735 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015736 for (unsigned i = 0; i != NumElems/2; ++i)
15737 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015738
Craig Toppercacafd42012-08-14 08:18:43 +000015739 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015740
15741 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015742 for (unsigned i = 0; i != NumElems/2; ++i)
15743 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015744
Craig Toppercacafd42012-08-14 08:18:43 +000015745 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015746
Craig Topper3ef43cf2012-04-24 06:36:35 +000015747 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015748 VT.getVectorNumElements()/2);
15749
Craig Topper3ef43cf2012-04-24 06:36:35 +000015750 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015751 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15752
15753 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15754 }
15755 return SDValue();
15756}
15757
Michael Liaof6c24ee2012-08-10 14:39:24 +000015758static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015759 const X86Subtarget* Subtarget) {
15760 DebugLoc dl = N->getDebugLoc();
15761 EVT VT = N->getValueType(0);
15762
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015763 // Let legalize expand this if it isn't a legal type yet.
15764 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15765 return SDValue();
15766
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015767 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015768 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15769 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015770 return SDValue();
15771
15772 SDValue A = N->getOperand(0);
15773 SDValue B = N->getOperand(1);
15774 SDValue C = N->getOperand(2);
15775
15776 bool NegA = (A.getOpcode() == ISD::FNEG);
15777 bool NegB = (B.getOpcode() == ISD::FNEG);
15778 bool NegC = (C.getOpcode() == ISD::FNEG);
15779
Michael Liaof6c24ee2012-08-10 14:39:24 +000015780 // Negative multiplication when NegA xor NegB
15781 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015782 if (NegA)
15783 A = A.getOperand(0);
15784 if (NegB)
15785 B = B.getOperand(0);
15786 if (NegC)
15787 C = C.getOperand(0);
15788
15789 unsigned Opcode;
15790 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015791 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015792 else
Craig Topperbf404372012-08-31 15:40:30 +000015793 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15794
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015795 return DAG.getNode(Opcode, dl, VT, A, B, C);
15796}
15797
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015798static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015799 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015800 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015801 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15802 // (and (i32 x86isd::setcc_carry), 1)
15803 // This eliminates the zext. This transformation is necessary because
15804 // ISD::SETCC is always legalized to i8.
15805 DebugLoc dl = N->getDebugLoc();
15806 SDValue N0 = N->getOperand(0);
15807 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015808 EVT OpVT = N0.getValueType();
15809
Evan Cheng2e489c42009-12-16 00:53:11 +000015810 if (N0.getOpcode() == ISD::AND &&
15811 N0.hasOneUse() &&
15812 N0.getOperand(0).hasOneUse()) {
15813 SDValue N00 = N0.getOperand(0);
15814 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15815 return SDValue();
15816 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15817 if (!C || C->getZExtValue() != 1)
15818 return SDValue();
15819 return DAG.getNode(ISD::AND, dl, VT,
15820 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15821 N00.getOperand(0), N00.getOperand(1)),
15822 DAG.getConstant(1, VT));
15823 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015824
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015825 // Optimize vectors in AVX mode:
15826 //
15827 // v8i16 -> v8i32
15828 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15829 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15830 // Concat upper and lower parts.
15831 //
15832 // v4i32 -> v4i64
15833 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15834 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15835 // Concat upper and lower parts.
15836 //
Craig Topperc16f8512012-04-25 06:39:39 +000015837 if (!DCI.isBeforeLegalizeOps())
15838 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015839
Craig Topperc16f8512012-04-25 06:39:39 +000015840 if (!Subtarget->hasAVX())
15841 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015842
Craig Topperc16f8512012-04-25 06:39:39 +000015843 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15844 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015845
Craig Topperc16f8512012-04-25 06:39:39 +000015846 if (Subtarget->hasAVX2())
15847 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015848
Craig Topperc16f8512012-04-25 06:39:39 +000015849 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15850 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15851 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015852
Craig Topperc16f8512012-04-25 06:39:39 +000015853 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15854 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015855
Craig Topperc16f8512012-04-25 06:39:39 +000015856 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15857 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15858
15859 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015860 }
15861
Evan Cheng2e489c42009-12-16 00:53:11 +000015862 return SDValue();
15863}
15864
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015865// Optimize x == -y --> x+y == 0
15866// x != -y --> x+y != 0
15867static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15868 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15869 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015870 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015871
15872 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15873 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15874 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15875 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15876 LHS.getValueType(), RHS, LHS.getOperand(1));
15877 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15878 addV, DAG.getConstant(0, addV.getValueType()), CC);
15879 }
15880 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15881 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15882 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15883 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15884 RHS.getValueType(), LHS, RHS.getOperand(1));
15885 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15886 addV, DAG.getConstant(0, addV.getValueType()), CC);
15887 }
15888 return SDValue();
15889}
15890
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015891// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015892static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15893 TargetLowering::DAGCombinerInfo &DCI,
15894 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015895 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015896 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15897 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015898
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015899 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15900 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15901 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015902 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015903 return DAG.getNode(ISD::AND, DL, MVT::i8,
15904 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015905 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015906 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015907
Michael Liao2a33cec2012-08-10 19:58:13 +000015908 SDValue Flags;
15909
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015910 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15911 if (Flags.getNode()) {
15912 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15913 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15914 }
15915
Michael Liao2a33cec2012-08-10 19:58:13 +000015916 return SDValue();
15917}
15918
15919// Optimize branch condition evaluation.
15920//
15921static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15922 TargetLowering::DAGCombinerInfo &DCI,
15923 const X86Subtarget *Subtarget) {
15924 DebugLoc DL = N->getDebugLoc();
15925 SDValue Chain = N->getOperand(0);
15926 SDValue Dest = N->getOperand(1);
15927 SDValue EFLAGS = N->getOperand(3);
15928 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15929
15930 SDValue Flags;
15931
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015932 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15933 if (Flags.getNode()) {
15934 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15935 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15936 Flags);
15937 }
15938
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015939 return SDValue();
15940}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015941
Craig Topper7fd5e162012-04-24 06:02:29 +000015942static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015943 SDValue Op0 = N->getOperand(0);
15944 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015945
15946 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015947 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015948 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015949 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015950 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15951 // Notice that we use SINT_TO_FP because we know that the high bits
15952 // are zero and SINT_TO_FP is better supported by the hardware.
15953 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15954 }
15955
15956 return SDValue();
15957}
15958
Benjamin Kramer1396c402011-06-18 11:09:41 +000015959static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15960 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015961 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015962 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015963
15964 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015965 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015966 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015967 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015968 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15969 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15970 }
15971
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015972 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15973 // a 32-bit target where SSE doesn't support i64->FP operations.
15974 if (Op0.getOpcode() == ISD::LOAD) {
15975 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15976 EVT VT = Ld->getValueType(0);
15977 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15978 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15979 !XTLI->getSubtarget()->is64Bit() &&
15980 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015981 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15982 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015983 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15984 return FILDChain;
15985 }
15986 }
15987 return SDValue();
15988}
15989
Craig Topper7fd5e162012-04-24 06:02:29 +000015990static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15991 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015992
15993 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015994 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15995 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015996 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015997 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15998 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15999 }
16000
16001 return SDValue();
16002}
16003
Chris Lattner23a01992010-12-20 01:37:09 +000016004// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
16005static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
16006 X86TargetLowering::DAGCombinerInfo &DCI) {
16007 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
16008 // the result is either zero or one (depending on the input carry bit).
16009 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
16010 if (X86::isZeroNode(N->getOperand(0)) &&
16011 X86::isZeroNode(N->getOperand(1)) &&
16012 // We don't have a good way to replace an EFLAGS use, so only do this when
16013 // dead right now.
16014 SDValue(N, 1).use_empty()) {
16015 DebugLoc DL = N->getDebugLoc();
16016 EVT VT = N->getValueType(0);
16017 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
16018 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
16019 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
16020 DAG.getConstant(X86::COND_B,MVT::i8),
16021 N->getOperand(2)),
16022 DAG.getConstant(1, VT));
16023 return DCI.CombineTo(N, Res1, CarryOut);
16024 }
16025
16026 return SDValue();
16027}
16028
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016029// fold (add Y, (sete X, 0)) -> adc 0, Y
16030// (add Y, (setne X, 0)) -> sbb -1, Y
16031// (sub (sete X, 0), Y) -> sbb 0, Y
16032// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016033static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016034 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016035
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016036 // Look through ZExts.
16037 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16038 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16039 return SDValue();
16040
16041 SDValue SetCC = Ext.getOperand(0);
16042 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16043 return SDValue();
16044
16045 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16046 if (CC != X86::COND_E && CC != X86::COND_NE)
16047 return SDValue();
16048
16049 SDValue Cmp = SetCC.getOperand(1);
16050 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016051 !X86::isZeroNode(Cmp.getOperand(1)) ||
16052 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016053 return SDValue();
16054
16055 SDValue CmpOp0 = Cmp.getOperand(0);
16056 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16057 DAG.getConstant(1, CmpOp0.getValueType()));
16058
16059 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16060 if (CC == X86::COND_NE)
16061 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16062 DL, OtherVal.getValueType(), OtherVal,
16063 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16064 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16065 DL, OtherVal.getValueType(), OtherVal,
16066 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16067}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016068
Craig Topper54f952a2011-11-19 09:02:40 +000016069/// PerformADDCombine - Do target-specific dag combines on integer adds.
16070static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16071 const X86Subtarget *Subtarget) {
16072 EVT VT = N->getValueType(0);
16073 SDValue Op0 = N->getOperand(0);
16074 SDValue Op1 = N->getOperand(1);
16075
16076 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016077 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016078 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016079 isHorizontalBinOp(Op0, Op1, true))
16080 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16081
16082 return OptimizeConditionalInDecrement(N, DAG);
16083}
16084
16085static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16086 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016087 SDValue Op0 = N->getOperand(0);
16088 SDValue Op1 = N->getOperand(1);
16089
16090 // X86 can't encode an immediate LHS of a sub. See if we can push the
16091 // negation into a preceding instruction.
16092 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016093 // If the RHS of the sub is a XOR with one use and a constant, invert the
16094 // immediate. Then add one to the LHS of the sub so we can turn
16095 // X-Y -> X+~Y+1, saving one register.
16096 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16097 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016098 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016099 EVT VT = Op0.getValueType();
16100 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16101 Op1.getOperand(0),
16102 DAG.getConstant(~XorC, VT));
16103 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016104 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016105 }
16106 }
16107
Craig Topper54f952a2011-11-19 09:02:40 +000016108 // Try to synthesize horizontal adds from adds of shuffles.
16109 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016110 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016111 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16112 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016113 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16114
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016115 return OptimizeConditionalInDecrement(N, DAG);
16116}
16117
Dan Gohman475871a2008-07-27 21:46:04 +000016118SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016119 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016120 SelectionDAG &DAG = DCI.DAG;
16121 switch (N->getOpcode()) {
16122 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016123 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016124 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016125 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016126 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016127 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016128 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16129 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016130 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016131 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016132 case ISD::SHL:
16133 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016134 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016135 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016136 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016137 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016138 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016139 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016140 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016141 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016142 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016143 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16144 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016145 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016146 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016147 case X86ISD::FMIN:
16148 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016149 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016150 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016151 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016152 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016153 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016154 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016155 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016156 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016157 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016158 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016159 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016160 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016161 case X86ISD::UNPCKH:
16162 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016163 case X86ISD::MOVHLPS:
16164 case X86ISD::MOVLHPS:
16165 case X86ISD::PSHUFD:
16166 case X86ISD::PSHUFHW:
16167 case X86ISD::PSHUFLW:
16168 case X86ISD::MOVSS:
16169 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016170 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016171 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016172 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016173 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016174 }
16175
Dan Gohman475871a2008-07-27 21:46:04 +000016176 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016177}
16178
Evan Chenge5b51ac2010-04-17 06:13:15 +000016179/// isTypeDesirableForOp - Return true if the target has native support for
16180/// the specified value type and it is 'desirable' to use the type for the
16181/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16182/// instruction encodings are longer and some i16 instructions are slow.
16183bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16184 if (!isTypeLegal(VT))
16185 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016186 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016187 return true;
16188
16189 switch (Opc) {
16190 default:
16191 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016192 case ISD::LOAD:
16193 case ISD::SIGN_EXTEND:
16194 case ISD::ZERO_EXTEND:
16195 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016196 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016197 case ISD::SRL:
16198 case ISD::SUB:
16199 case ISD::ADD:
16200 case ISD::MUL:
16201 case ISD::AND:
16202 case ISD::OR:
16203 case ISD::XOR:
16204 return false;
16205 }
16206}
16207
16208/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016209/// beneficial for dag combiner to promote the specified node. If true, it
16210/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016211bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016212 EVT VT = Op.getValueType();
16213 if (VT != MVT::i16)
16214 return false;
16215
Evan Cheng4c26e932010-04-19 19:29:22 +000016216 bool Promote = false;
16217 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016218 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016219 default: break;
16220 case ISD::LOAD: {
16221 LoadSDNode *LD = cast<LoadSDNode>(Op);
16222 // If the non-extending load has a single use and it's not live out, then it
16223 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016224 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16225 Op.hasOneUse()*/) {
16226 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16227 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16228 // The only case where we'd want to promote LOAD (rather then it being
16229 // promoted as an operand is when it's only use is liveout.
16230 if (UI->getOpcode() != ISD::CopyToReg)
16231 return false;
16232 }
16233 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016234 Promote = true;
16235 break;
16236 }
16237 case ISD::SIGN_EXTEND:
16238 case ISD::ZERO_EXTEND:
16239 case ISD::ANY_EXTEND:
16240 Promote = true;
16241 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016242 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016243 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016244 SDValue N0 = Op.getOperand(0);
16245 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016246 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016247 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016248 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016249 break;
16250 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016251 case ISD::ADD:
16252 case ISD::MUL:
16253 case ISD::AND:
16254 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016255 case ISD::XOR:
16256 Commute = true;
16257 // fallthrough
16258 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016259 SDValue N0 = Op.getOperand(0);
16260 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016261 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016262 return false;
16263 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016264 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016265 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016266 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016267 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016268 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016269 }
16270 }
16271
16272 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016273 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016274}
16275
Evan Cheng60c07e12006-07-05 22:17:51 +000016276//===----------------------------------------------------------------------===//
16277// X86 Inline Assembly Support
16278//===----------------------------------------------------------------------===//
16279
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016280namespace {
16281 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016282 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016283 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016284
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016285 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016286 StringRef piece(*args[i]);
16287 if (!s.startswith(piece)) // Check if the piece matches.
16288 return false;
16289
16290 s = s.substr(piece.size());
16291 StringRef::size_type pos = s.find_first_not_of(" \t");
16292 if (pos == 0) // We matched a prefix.
16293 return false;
16294
16295 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016296 }
16297
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016298 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016299 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016300 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016301}
16302
Chris Lattnerb8105652009-07-20 17:51:36 +000016303bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16304 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016305
16306 std::string AsmStr = IA->getAsmString();
16307
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016308 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16309 if (!Ty || Ty->getBitWidth() % 16 != 0)
16310 return false;
16311
Chris Lattnerb8105652009-07-20 17:51:36 +000016312 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016313 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016314 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016315
16316 switch (AsmPieces.size()) {
16317 default: return false;
16318 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016319 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016320 // we will turn this bswap into something that will be lowered to logical
16321 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16322 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016323 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016324 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16325 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16326 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16327 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16328 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16329 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016330 // No need to check constraints, nothing other than the equivalent of
16331 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016332 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016333 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016334
Chris Lattnerb8105652009-07-20 17:51:36 +000016335 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016336 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016337 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016338 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16339 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016340 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016341 const std::string &ConstraintsStr = IA->getConstraintString();
16342 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016343 std::sort(AsmPieces.begin(), AsmPieces.end());
16344 if (AsmPieces.size() == 4 &&
16345 AsmPieces[0] == "~{cc}" &&
16346 AsmPieces[1] == "~{dirflag}" &&
16347 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016348 AsmPieces[3] == "~{fpsr}")
16349 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016350 }
16351 break;
16352 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016353 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016354 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016355 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16356 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16357 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016358 AsmPieces.clear();
16359 const std::string &ConstraintsStr = IA->getConstraintString();
16360 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16361 std::sort(AsmPieces.begin(), AsmPieces.end());
16362 if (AsmPieces.size() == 4 &&
16363 AsmPieces[0] == "~{cc}" &&
16364 AsmPieces[1] == "~{dirflag}" &&
16365 AsmPieces[2] == "~{flags}" &&
16366 AsmPieces[3] == "~{fpsr}")
16367 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016368 }
Evan Cheng55d42002011-01-08 01:24:27 +000016369
16370 if (CI->getType()->isIntegerTy(64)) {
16371 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16372 if (Constraints.size() >= 2 &&
16373 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16374 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16375 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016376 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16377 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16378 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016379 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016380 }
16381 }
16382 break;
16383 }
16384 return false;
16385}
16386
16387
16388
Chris Lattnerf4dff842006-07-11 02:54:03 +000016389/// getConstraintType - Given a constraint letter, return the type of
16390/// constraint it is for this target.
16391X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016392X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16393 if (Constraint.size() == 1) {
16394 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016395 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016396 case 'q':
16397 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016398 case 'f':
16399 case 't':
16400 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016401 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016402 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016403 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016404 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016405 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016406 case 'a':
16407 case 'b':
16408 case 'c':
16409 case 'd':
16410 case 'S':
16411 case 'D':
16412 case 'A':
16413 return C_Register;
16414 case 'I':
16415 case 'J':
16416 case 'K':
16417 case 'L':
16418 case 'M':
16419 case 'N':
16420 case 'G':
16421 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016422 case 'e':
16423 case 'Z':
16424 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016425 default:
16426 break;
16427 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016428 }
Chris Lattner4234f572007-03-25 02:14:49 +000016429 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016430}
16431
John Thompson44ab89e2010-10-29 17:29:13 +000016432/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016433/// This object must already have been set up with the operand type
16434/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016435TargetLowering::ConstraintWeight
16436 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016437 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016438 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016439 Value *CallOperandVal = info.CallOperandVal;
16440 // If we don't have a value, we can't do a match,
16441 // but allow it at the lowest weight.
16442 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016443 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016444 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016445 // Look at the constraint type.
16446 switch (*constraint) {
16447 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016448 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16449 case 'R':
16450 case 'q':
16451 case 'Q':
16452 case 'a':
16453 case 'b':
16454 case 'c':
16455 case 'd':
16456 case 'S':
16457 case 'D':
16458 case 'A':
16459 if (CallOperandVal->getType()->isIntegerTy())
16460 weight = CW_SpecificReg;
16461 break;
16462 case 'f':
16463 case 't':
16464 case 'u':
16465 if (type->isFloatingPointTy())
16466 weight = CW_SpecificReg;
16467 break;
16468 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016469 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016470 weight = CW_SpecificReg;
16471 break;
16472 case 'x':
16473 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016474 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016475 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016476 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016477 break;
16478 case 'I':
16479 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16480 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016481 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016482 }
16483 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016484 case 'J':
16485 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16486 if (C->getZExtValue() <= 63)
16487 weight = CW_Constant;
16488 }
16489 break;
16490 case 'K':
16491 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16492 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16493 weight = CW_Constant;
16494 }
16495 break;
16496 case 'L':
16497 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16498 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16499 weight = CW_Constant;
16500 }
16501 break;
16502 case 'M':
16503 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16504 if (C->getZExtValue() <= 3)
16505 weight = CW_Constant;
16506 }
16507 break;
16508 case 'N':
16509 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16510 if (C->getZExtValue() <= 0xff)
16511 weight = CW_Constant;
16512 }
16513 break;
16514 case 'G':
16515 case 'C':
16516 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16517 weight = CW_Constant;
16518 }
16519 break;
16520 case 'e':
16521 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16522 if ((C->getSExtValue() >= -0x80000000LL) &&
16523 (C->getSExtValue() <= 0x7fffffffLL))
16524 weight = CW_Constant;
16525 }
16526 break;
16527 case 'Z':
16528 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16529 if (C->getZExtValue() <= 0xffffffff)
16530 weight = CW_Constant;
16531 }
16532 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016533 }
16534 return weight;
16535}
16536
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016537/// LowerXConstraint - try to replace an X constraint, which matches anything,
16538/// with another that has more specific requirements based on the type of the
16539/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016540const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016541LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016542 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16543 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016544 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016545 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016546 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016547 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016548 return "x";
16549 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016550
Chris Lattner5e764232008-04-26 23:02:14 +000016551 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016552}
16553
Chris Lattner48884cd2007-08-25 00:47:38 +000016554/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16555/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016556void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016557 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016558 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016559 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016560 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016561
Eric Christopher100c8332011-06-02 23:16:42 +000016562 // Only support length 1 constraints for now.
16563 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016564
Eric Christopher100c8332011-06-02 23:16:42 +000016565 char ConstraintLetter = Constraint[0];
16566 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016567 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016568 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016569 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016570 if (C->getZExtValue() <= 31) {
16571 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016572 break;
16573 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016574 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016575 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016576 case 'J':
16577 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016578 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016579 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16580 break;
16581 }
16582 }
16583 return;
16584 case 'K':
16585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016586 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016587 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16588 break;
16589 }
16590 }
16591 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016592 case 'N':
16593 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016594 if (C->getZExtValue() <= 255) {
16595 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016596 break;
16597 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016598 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016599 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016600 case 'e': {
16601 // 32-bit signed value
16602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016603 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16604 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016605 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016606 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016607 break;
16608 }
16609 // FIXME gcc accepts some relocatable values here too, but only in certain
16610 // memory models; it's complicated.
16611 }
16612 return;
16613 }
16614 case 'Z': {
16615 // 32-bit unsigned value
16616 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016617 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16618 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016619 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16620 break;
16621 }
16622 }
16623 // FIXME gcc accepts some relocatable values here too, but only in certain
16624 // memory models; it's complicated.
16625 return;
16626 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016627 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016628 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016629 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016630 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016631 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016632 break;
16633 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016634
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016635 // In any sort of PIC mode addresses need to be computed at runtime by
16636 // adding in a register or some sort of table lookup. These can't
16637 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016638 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016639 return;
16640
Chris Lattnerdc43a882007-05-03 16:52:29 +000016641 // If we are in non-pic codegen mode, we allow the address of a global (with
16642 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016643 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016644 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016645
Chris Lattner49921962009-05-08 18:23:14 +000016646 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16647 while (1) {
16648 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16649 Offset += GA->getOffset();
16650 break;
16651 } else if (Op.getOpcode() == ISD::ADD) {
16652 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16653 Offset += C->getZExtValue();
16654 Op = Op.getOperand(0);
16655 continue;
16656 }
16657 } else if (Op.getOpcode() == ISD::SUB) {
16658 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16659 Offset += -C->getZExtValue();
16660 Op = Op.getOperand(0);
16661 continue;
16662 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016663 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016664
Chris Lattner49921962009-05-08 18:23:14 +000016665 // Otherwise, this isn't something we can handle, reject it.
16666 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016667 }
Eric Christopherfd179292009-08-27 18:07:15 +000016668
Dan Gohman46510a72010-04-15 01:51:59 +000016669 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016670 // If we require an extra load to get this address, as in PIC mode, we
16671 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016672 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16673 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016674 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016675
Devang Patel0d881da2010-07-06 22:08:15 +000016676 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16677 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016678 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016679 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016680 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016681
Gabor Greifba36cb52008-08-28 21:40:38 +000016682 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016683 Ops.push_back(Result);
16684 return;
16685 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016686 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016687}
16688
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016689std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016690X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016691 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016692 // First, see if this is a constraint that directly corresponds to an LLVM
16693 // register class.
16694 if (Constraint.size() == 1) {
16695 // GCC Constraint Letters
16696 switch (Constraint[0]) {
16697 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016698 // TODO: Slight differences here in allocation order and leaving
16699 // RIP in the class. Do they matter any more here than they do
16700 // in the normal allocation?
16701 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16702 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016703 if (VT == MVT::i32 || VT == MVT::f32)
16704 return std::make_pair(0U, &X86::GR32RegClass);
16705 if (VT == MVT::i16)
16706 return std::make_pair(0U, &X86::GR16RegClass);
16707 if (VT == MVT::i8 || VT == MVT::i1)
16708 return std::make_pair(0U, &X86::GR8RegClass);
16709 if (VT == MVT::i64 || VT == MVT::f64)
16710 return std::make_pair(0U, &X86::GR64RegClass);
16711 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016712 }
16713 // 32-bit fallthrough
16714 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016715 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016716 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16717 if (VT == MVT::i16)
16718 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16719 if (VT == MVT::i8 || VT == MVT::i1)
16720 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16721 if (VT == MVT::i64)
16722 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016723 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016724 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016725 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016726 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016727 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016728 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016729 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016730 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016731 return std::make_pair(0U, &X86::GR32RegClass);
16732 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016733 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016734 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016735 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016736 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016737 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016738 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016739 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16740 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016741 case 'f': // FP Stack registers.
16742 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16743 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016744 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016745 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016746 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016747 return std::make_pair(0U, &X86::RFP64RegClass);
16748 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016749 case 'y': // MMX_REGS if MMX allowed.
16750 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016751 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016752 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016753 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016754 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016755 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016756 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016757
Owen Anderson825b72b2009-08-11 20:47:22 +000016758 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016759 default: break;
16760 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016761 case MVT::f32:
16762 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016763 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016764 case MVT::f64:
16765 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016766 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016767 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016768 case MVT::v16i8:
16769 case MVT::v8i16:
16770 case MVT::v4i32:
16771 case MVT::v2i64:
16772 case MVT::v4f32:
16773 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016774 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016775 // AVX types.
16776 case MVT::v32i8:
16777 case MVT::v16i16:
16778 case MVT::v8i32:
16779 case MVT::v4i64:
16780 case MVT::v8f32:
16781 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016782 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016783 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016784 break;
16785 }
16786 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016787
Chris Lattnerf76d1802006-07-31 23:26:50 +000016788 // Use the default implementation in TargetLowering to convert the register
16789 // constraint into a member of a register class.
16790 std::pair<unsigned, const TargetRegisterClass*> Res;
16791 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016792
16793 // Not found as a standard register?
16794 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016795 // Map st(0) -> st(7) -> ST0
16796 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16797 tolower(Constraint[1]) == 's' &&
16798 tolower(Constraint[2]) == 't' &&
16799 Constraint[3] == '(' &&
16800 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16801 Constraint[5] == ')' &&
16802 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016803
Chris Lattner56d77c72009-09-13 22:41:48 +000016804 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016805 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016806 return Res;
16807 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016808
Chris Lattner56d77c72009-09-13 22:41:48 +000016809 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016810 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016811 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016812 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016813 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016814 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016815
16816 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016817 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016818 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016819 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016820 return Res;
16821 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016822
Dale Johannesen330169f2008-11-13 21:52:36 +000016823 // 'A' means EAX + EDX.
16824 if (Constraint == "A") {
16825 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016826 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016827 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016828 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016829 return Res;
16830 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016831
Chris Lattnerf76d1802006-07-31 23:26:50 +000016832 // Otherwise, check to see if this is a register class of the wrong value
16833 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16834 // turn into {ax},{dx}.
16835 if (Res.second->hasType(VT))
16836 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016837
Chris Lattnerf76d1802006-07-31 23:26:50 +000016838 // All of the single-register GCC register classes map their values onto
16839 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16840 // really want an 8-bit or 32-bit register, map to the appropriate register
16841 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016842 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016843 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016844 unsigned DestReg = 0;
16845 switch (Res.first) {
16846 default: break;
16847 case X86::AX: DestReg = X86::AL; break;
16848 case X86::DX: DestReg = X86::DL; break;
16849 case X86::CX: DestReg = X86::CL; break;
16850 case X86::BX: DestReg = X86::BL; break;
16851 }
16852 if (DestReg) {
16853 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016854 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016855 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016856 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016857 unsigned DestReg = 0;
16858 switch (Res.first) {
16859 default: break;
16860 case X86::AX: DestReg = X86::EAX; break;
16861 case X86::DX: DestReg = X86::EDX; break;
16862 case X86::CX: DestReg = X86::ECX; break;
16863 case X86::BX: DestReg = X86::EBX; break;
16864 case X86::SI: DestReg = X86::ESI; break;
16865 case X86::DI: DestReg = X86::EDI; break;
16866 case X86::BP: DestReg = X86::EBP; break;
16867 case X86::SP: DestReg = X86::ESP; break;
16868 }
16869 if (DestReg) {
16870 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016871 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016872 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016873 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016874 unsigned DestReg = 0;
16875 switch (Res.first) {
16876 default: break;
16877 case X86::AX: DestReg = X86::RAX; break;
16878 case X86::DX: DestReg = X86::RDX; break;
16879 case X86::CX: DestReg = X86::RCX; break;
16880 case X86::BX: DestReg = X86::RBX; break;
16881 case X86::SI: DestReg = X86::RSI; break;
16882 case X86::DI: DestReg = X86::RDI; break;
16883 case X86::BP: DestReg = X86::RBP; break;
16884 case X86::SP: DestReg = X86::RSP; break;
16885 }
16886 if (DestReg) {
16887 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016888 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016889 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016890 }
Craig Topperc9099502012-04-20 06:31:50 +000016891 } else if (Res.second == &X86::FR32RegClass ||
16892 Res.second == &X86::FR64RegClass ||
16893 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016894 // Handle references to XMM physical registers that got mapped into the
16895 // wrong class. This can happen with constraints like {xmm0} where the
16896 // target independent register mapper will just pick the first match it can
16897 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016898
16899 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016900 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016901 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016902 Res.second = &X86::FR64RegClass;
16903 else if (X86::VR128RegClass.hasType(VT))
16904 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016905 else if (X86::VR256RegClass.hasType(VT))
16906 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016907 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016908
Chris Lattnerf76d1802006-07-31 23:26:50 +000016909 return Res;
16910}