blob: efc0781260760ea2a003e672449845b672c038bf [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000019#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/VariadicFunction.h"
Evan Cheng55d42002011-01-08 01:24:27 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/GlobalAlias.h"
38#include "llvm/IR/GlobalVariable.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/LLVMContext.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000042#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000044#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/MC/MCSymbol.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.
Andrew Trickac6d9be2013-05-25 02:42:55 +000058static SDValue getMOVL(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
Elena Demikhovsky83952512013-07-31 11:35:14 +000061static SDValue ExtractSubVector(SDValue Vec, unsigned IdxVal,
62 SelectionDAG &DAG, SDLoc dl,
63 unsigned vectorWidth) {
64 assert((vectorWidth == 128 || vectorWidth == 256) &&
65 "Unsupported vector width");
David Greenea5f26012011-02-07 19:36:54 +000066 EVT VT = Vec.getValueType();
David Greenea5f26012011-02-07 19:36:54 +000067 EVT ElVT = VT.getVectorElementType();
Elena Demikhovsky83952512013-07-31 11:35:14 +000068 unsigned Factor = VT.getSizeInBits()/vectorWidth;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000069 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
70 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000071
72 // Extract from UNDEF is UNDEF.
73 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000074 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000075
Elena Demikhovsky83952512013-07-31 11:35:14 +000076 // Extract the relevant vectorWidth bits. Generate an EXTRACT_SUBVECTOR
77 unsigned ElemsPerChunk = vectorWidth / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000078
Elena Demikhovsky83952512013-07-31 11:35:14 +000079 // This is the index of the first element of the vectorWidth-bit chunk
Craig Topperb14940a2012-04-22 20:55:18 +000080 // we want.
Elena Demikhovsky83952512013-07-31 11:35:14 +000081 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / vectorWidth)
Craig Topperb14940a2012-04-22 20:55:18 +000082 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000083
Benjamin Kramer02c2ecf2013-03-07 18:48:40 +000084 // If the input is a buildvector just emit a smaller one.
85 if (Vec.getOpcode() == ISD::BUILD_VECTOR)
86 return DAG.getNode(ISD::BUILD_VECTOR, dl, ResultVT,
87 Vec->op_begin()+NormalizedIdxVal, ElemsPerChunk);
88
Craig Topperb8d9da12012-09-06 06:09:01 +000089 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000090 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
91 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000092
Craig Topperb14940a2012-04-22 20:55:18 +000093 return Result;
Elena Demikhovsky83952512013-07-31 11:35:14 +000094
95}
96/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
97/// sets things up to match to an AVX VEXTRACTF128 / VEXTRACTI128
98/// or AVX-512 VEXTRACTF32x4 / VEXTRACTI32x4
99/// instructions or a simple subregister reference. Idx is an index in the
100/// 128 bits we want. It need not be aligned to a 128-bit bounday. That makes
101/// lowering EXTRACT_VECTOR_ELT operations easier.
102static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
103 SelectionDAG &DAG, SDLoc dl) {
104 assert(Vec.getValueType().is256BitVector() && "Unexpected vector size!");
105 return ExtractSubVector(Vec, IdxVal, DAG, dl, 128);
David Greenea5f26012011-02-07 19:36:54 +0000106}
107
Elena Demikhovsky83952512013-07-31 11:35:14 +0000108/// Generate a DAG to grab 256-bits from a 512-bit vector.
109static SDValue Extract256BitVector(SDValue Vec, unsigned IdxVal,
110 SelectionDAG &DAG, SDLoc dl) {
111 assert(Vec.getValueType().is512BitVector() && "Unexpected vector size!");
112 return ExtractSubVector(Vec, IdxVal, DAG, dl, 256);
113}
114
115static SDValue InsertSubVector(SDValue Result, SDValue Vec,
116 unsigned IdxVal, SelectionDAG &DAG,
117 SDLoc dl, unsigned vectorWidth) {
118 assert((vectorWidth == 128 || vectorWidth == 256) &&
119 "Unsupported vector width");
120 // Inserting UNDEF is Result
121 if (Vec.getOpcode() == ISD::UNDEF)
122 return Result;
123 EVT VT = Vec.getValueType();
124 EVT ElVT = VT.getVectorElementType();
125 EVT ResultVT = Result.getValueType();
126
127 // Insert the relevant vectorWidth bits.
128 unsigned ElemsPerChunk = vectorWidth/ElVT.getSizeInBits();
129
130 // This is the index of the first element of the vectorWidth-bit chunk
131 // we want.
132 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/vectorWidth)
133 * ElemsPerChunk);
134
135 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
136 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
137 VecIdx);
138}
David Greenea5f26012011-02-07 19:36:54 +0000139/// Generate a DAG to put 128-bits into a vector > 128 bits. This
Elena Demikhovsky83952512013-07-31 11:35:14 +0000140/// sets things up to match to an AVX VINSERTF128/VINSERTI128 or
141/// AVX-512 VINSERTF32x4/VINSERTI32x4 instructions or a
David Greene6b381262011-02-09 15:32:06 +0000142/// simple superregister reference. Idx is an index in the 128 bits
143/// we want. It need not be aligned to a 128-bit bounday. That makes
144/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000145static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
146 unsigned IdxVal, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000147 SDLoc dl) {
Elena Demikhovsky83952512013-07-31 11:35:14 +0000148 assert(Vec.getValueType().is128BitVector() && "Unexpected vector size!");
149 return InsertSubVector(Result, Vec, IdxVal, DAG, dl, 128);
150}
Craig Topper703c38b2012-06-20 05:39:26 +0000151
Elena Demikhovsky83952512013-07-31 11:35:14 +0000152static SDValue Insert256BitVector(SDValue Result, SDValue Vec,
153 unsigned IdxVal, SelectionDAG &DAG,
154 SDLoc dl) {
155 assert(Vec.getValueType().is256BitVector() && "Unexpected vector size!");
156 return InsertSubVector(Result, Vec, IdxVal, DAG, dl, 256);
David Greenea5f26012011-02-07 19:36:54 +0000157}
158
Craig Topper4c7972d2012-04-22 18:15:59 +0000159/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
160/// instructions. This is used because creating CONCAT_VECTOR nodes of
161/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
162/// large BUILD_VECTORS.
163static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
164 unsigned NumElems, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000165 SDLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000166 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
167 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000168}
169
Elena Demikhovsky83952512013-07-31 11:35:14 +0000170static SDValue Concat256BitVectors(SDValue V1, SDValue V2, EVT VT,
171 unsigned NumElems, SelectionDAG &DAG,
172 SDLoc dl) {
173 SDValue V = Insert256BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
174 return Insert256BitVector(V, V2, NumElems/2, DAG, dl);
175}
176
Chris Lattnerf0144122009-07-28 03:13:23 +0000177static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000178 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
179 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000180
Evan Cheng2bffee22011-02-01 01:14:13 +0000181 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000182 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000183 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000184 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000185 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000186
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000187 if (Subtarget->isTargetLinux())
188 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000189 if (Subtarget->isTargetELF())
190 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000191 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000192 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000193 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000194}
195
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000196X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000197 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000198 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000199 X86ScalarSSEf64 = Subtarget->hasSSE2();
200 X86ScalarSSEf32 = Subtarget->hasSSE1();
Micah Villmow3574eca2012-10-08 16:38:25 +0000201 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000202
Bill Wendling13bbe1f2013-04-05 21:52:40 +0000203 resetOperationActions();
204}
205
206void X86TargetLowering::resetOperationActions() {
207 const TargetMachine &TM = getTargetMachine();
208 static bool FirstTimeThrough = true;
209
210 // If none of the target options have changed, then we don't need to reset the
211 // operation actions.
212 if (!FirstTimeThrough && TO == TM.Options) return;
213
214 if (!FirstTimeThrough) {
215 // Reinitialize the actions.
216 initActions();
217 FirstTimeThrough = false;
218 }
219
220 TO = TM.Options;
221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000223 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224
225 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000226 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000227 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
228 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000229
Eric Christopherde5e1012011-03-11 01:05:58 +0000230 // For 64-bit since we have so many registers use the ILP scheduler, for
231 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000232 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000233 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000234 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000235 else if (Subtarget->is64Bit())
236 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000237 else
238 setSchedulingPreference(Sched::RegPressure);
Bill Wendlinga5e5ba62013-06-07 21:00:34 +0000239 const X86RegisterInfo *RegInfo =
240 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Michael Liaoc5c970e2012-10-31 04:14:09 +0000241 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000242
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000243 // Bypass expensive divides on Atom when compiling with O2
244 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default) {
Preston Gurd8d662b52012-10-04 21:33:40 +0000245 addBypassSlowDiv(32, 8);
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000246 if (Subtarget->is64Bit())
247 addBypassSlowDiv(64, 16);
248 }
Preston Gurd2e2efd92012-09-04 18:22:17 +0000249
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000250 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000251 // Setup Windows compiler runtime calls.
252 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000253 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000254 setLibcallName(RTLIB::SREM_I64, "_allrem");
255 setLibcallName(RTLIB::UREM_I64, "_aullrem");
256 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000257 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000258 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000259 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
260 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
261 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000262
263 // The _ftol2 runtime function has an unusual calling conv, which
264 // is modeled by a special pseudo-instruction.
265 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
266 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
267 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
268 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000269 }
270
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000271 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000272 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000273 setUseUnderscoreSetJmp(false);
274 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000275 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000276 // MS runtime is weird: it exports _setjmp, but longjmp!
277 setUseUnderscoreSetJmp(true);
278 setUseUnderscoreLongJmp(false);
279 } else {
280 setUseUnderscoreSetJmp(true);
281 setUseUnderscoreLongJmp(true);
282 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000284 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000285 addRegisterClass(MVT::i8, &X86::GR8RegClass);
286 addRegisterClass(MVT::i16, &X86::GR16RegClass);
287 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000289 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000290
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000292
Scott Michelfdc40a02009-02-17 22:15:04 +0000293 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000295 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000297 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
299 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000300
301 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
303 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
304 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
305 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
306 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
307 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000308
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
310 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
312 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
313 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000314
Evan Cheng25ab6902006-09-08 06:48:29 +0000315 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000317 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000318 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000319 // We have an algorithm for SSE2->double, and we turn this into a
320 // 64-bit FILD followed by conditional FADD for other targets.
321 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // We have an algorithm for SSE2, and we turn this into a 64-bit
323 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000324 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
327 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
328 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
330 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000331
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000332 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000333 // SSE has no i16 to fp conversion, only i32
334 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000336 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000338 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
340 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000341 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000342 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
344 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000345 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000346
Dale Johannesen73328d12007-09-19 23:55:34 +0000347 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
348 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
350 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000351
Evan Cheng02568ff2006-01-30 22:13:22 +0000352 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
353 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
355 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000356
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000357 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000359 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000361 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
363 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000364 }
365
366 // Handle FP_TO_UINT by promoting the destination to a larger signed
367 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
369 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
370 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000371
Evan Cheng25ab6902006-09-08 06:48:29 +0000372 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
374 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000375 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000376 // Since AVX is a superset of SSE3, only check for SSE here.
377 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000378 // Expand FP_TO_UINT into a select.
379 // FIXME: We would like to use a Custom expander here eventually to do
380 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000382 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000383 // With SSE3 we can use fisttpll to convert to a signed i64; without
384 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000386 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000387
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000388 if (isTargetFTOL()) {
389 // Use the _ftol2 runtime function, which has a pseudo-instruction
390 // to handle its weird calling convention.
391 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
392 }
393
Chris Lattner399610a2006-12-05 18:22:22 +0000394 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000395 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000396 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
397 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000398 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000399 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000400 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000401 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000402 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000403 }
Chris Lattner21f66852005-12-23 05:15:23 +0000404
Dan Gohmanb00ee212008-02-18 19:34:53 +0000405 // Scalar integer divide and remainder are lowered to use operations that
406 // produce two results, to match the available instructions. This exposes
407 // the two-result form to trivial CSE, which is able to combine x/y and x%y
408 // into a single instruction.
409 //
410 // Scalar integer multiply-high is also lowered to use two-result
411 // operations, to match the available instructions. However, plain multiply
412 // (low) operations are left as Legal, as there are single-result
413 // instructions for this in x86. Using the two-result multiply instructions
414 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000415 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000416 MVT VT = IntVTs[i];
417 setOperationAction(ISD::MULHS, VT, Expand);
418 setOperationAction(ISD::MULHU, VT, Expand);
419 setOperationAction(ISD::SDIV, VT, Expand);
420 setOperationAction(ISD::UDIV, VT, Expand);
421 setOperationAction(ISD::SREM, VT, Expand);
422 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000423
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000424 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000425 setOperationAction(ISD::ADDC, VT, Custom);
426 setOperationAction(ISD::ADDE, VT, Custom);
427 setOperationAction(ISD::SUBC, VT, Custom);
428 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000429 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
432 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Tom Stellard3ef53832013-03-08 15:36:57 +0000433 setOperationAction(ISD::BR_CC , MVT::f32, Expand);
434 setOperationAction(ISD::BR_CC , MVT::f64, Expand);
435 setOperationAction(ISD::BR_CC , MVT::f80, Expand);
436 setOperationAction(ISD::BR_CC , MVT::i8, Expand);
437 setOperationAction(ISD::BR_CC , MVT::i16, Expand);
438 setOperationAction(ISD::BR_CC , MVT::i32, Expand);
439 setOperationAction(ISD::BR_CC , MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000441 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
443 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
444 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
445 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
446 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
447 setOperationAction(ISD::FREM , MVT::f32 , Expand);
448 setOperationAction(ISD::FREM , MVT::f64 , Expand);
449 setOperationAction(ISD::FREM , MVT::f80 , Expand);
450 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000451
Chandler Carruth77821022011-12-24 12:12:34 +0000452 // Promote the i8 variants and force them on up to i32 which has a shorter
453 // encoding.
454 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
455 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
456 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
457 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000458 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000459 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
460 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
461 if (Subtarget->is64Bit())
462 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000463 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000464 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
465 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
466 if (Subtarget->is64Bit())
467 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
468 }
Craig Topper37f21672011-10-11 06:44:02 +0000469
470 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000471 // When promoting the i8 variants, force them to i32 for a shorter
472 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000473 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000474 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
475 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
476 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000477 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
478 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
479 if (Subtarget->is64Bit())
480 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000481 } else {
482 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
483 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
484 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000485 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
486 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
487 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
488 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000489 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000490 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
491 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000492 }
493
Benjamin Kramer1292c222010-12-04 20:32:23 +0000494 if (Subtarget->hasPOPCNT()) {
495 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
496 } else {
497 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
498 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
499 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
500 if (Subtarget->is64Bit())
501 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
502 }
503
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
505 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000506
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000508 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000509 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000510 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000511 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
513 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
514 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
515 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
516 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000517 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
519 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
520 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
521 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000522 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000524 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000525 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Hal Finkele9150472013-03-27 19:10:42 +0000527 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Michael Liao6c0e04c2012-10-15 22:39:43 +0000528 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000529 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000530 // other SjLj exception interfaces are implemented and please don't build
531 // your own exception handling based on them.
532 // LLVM/Clang supports zero-cost DWARF exception handling.
533 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
534 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000535
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000536 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
538 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
539 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
540 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000541 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
543 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000544 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000545 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
547 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
548 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
549 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000550 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000551 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000552 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
554 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
555 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000556 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
558 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
559 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000560 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561
Craig Topper1accb7e2012-01-10 06:54:16 +0000562 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000564
Eli Friedman14648462011-07-27 22:21:52 +0000565 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000566
Mon P Wang63307c32008-05-05 19:05:59 +0000567 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000568 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000569 MVT VT = IntVTs[i];
570 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
571 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000572 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000573 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000574
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000575 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000576 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
578 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
579 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
580 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
581 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
582 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
583 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000584 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
585 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
586 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
587 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000588 }
589
Eli Friedman43f51ae2011-08-26 21:21:21 +0000590 if (Subtarget->hasCmpxchg16b()) {
591 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
592 }
593
Evan Cheng3c992d22006-03-07 02:02:57 +0000594 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000595 if (!Subtarget->isTargetDarwin() &&
596 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000597 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000599 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000600
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000601 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000602 setExceptionPointerRegister(X86::RAX);
603 setExceptionSelectorRegister(X86::RDX);
604 } else {
605 setExceptionPointerRegister(X86::EAX);
606 setExceptionSelectorRegister(X86::EDX);
607 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
609 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000610
Duncan Sands4a544a72011-09-06 13:37:06 +0000611 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
612 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000613
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000615 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000616
Nate Begemanacc398c2006-01-25 18:21:52 +0000617 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::VASTART , MVT::Other, Custom);
619 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Nico Rieck944061c2013-07-29 13:07:06 +0000620 if (Subtarget->is64Bit() && !Subtarget->isTargetWin64()) {
621 // TargetInfo::X86_64ABIBuiltinVaList
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::VAARG , MVT::Other, Custom);
623 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000624 } else {
Nico Rieck944061c2013-07-29 13:07:06 +0000625 // TargetInfo::CharPtrBuiltinVaList
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::VAARG , MVT::Other, Expand);
627 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000628 }
Evan Chengae642192007-03-02 23:16:35 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
631 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000632
633 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
634 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
635 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000636 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000637 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
638 MVT::i64 : MVT::i32, Custom);
639 else
640 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
641 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000642
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000643 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000644 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000645 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000646 addRegisterClass(MVT::f32, &X86::FR32RegClass);
647 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000648
Evan Cheng223547a2006-01-31 22:28:30 +0000649 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FABS , MVT::f64, Custom);
651 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000652
653 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::FNEG , MVT::f64, Custom);
655 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000656
Evan Cheng68c47cb2007-01-05 07:55:56 +0000657 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000660
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000661 // Lower this to FGETSIGNx86 plus an AND.
662 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
663 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
664
Evan Chengd25e9e82006-02-02 00:28:23 +0000665 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000666 setOperationAction(ISD::FSIN , MVT::f64, Expand);
667 setOperationAction(ISD::FCOS , MVT::f64, Expand);
668 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
669 setOperationAction(ISD::FSIN , MVT::f32, Expand);
670 setOperationAction(ISD::FCOS , MVT::f32, Expand);
671 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000672
Chris Lattnera54aa942006-01-29 06:26:08 +0000673 // Expand FP immediates into loads from the stack, except for the special
674 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000675 addLegalFPImmediate(APFloat(+0.0)); // xorpd
676 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000677 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000678 // Use SSE for f32, x87 for f64.
679 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000680 addRegisterClass(MVT::f32, &X86::FR32RegClass);
681 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000682
683 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000685
686 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000688
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000690
691 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
693 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000694
695 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000696 setOperationAction(ISD::FSIN , MVT::f32, Expand);
697 setOperationAction(ISD::FCOS , MVT::f32, Expand);
698 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000699
Nate Begemane1795842008-02-14 08:57:00 +0000700 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000701 addLegalFPImmediate(APFloat(+0.0f)); // xorps
702 addLegalFPImmediate(APFloat(+0.0)); // FLD0
703 addLegalFPImmediate(APFloat(+1.0)); // FLD1
704 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
705 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
706
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000707 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000708 setOperationAction(ISD::FSIN , MVT::f64, Expand);
709 setOperationAction(ISD::FCOS , MVT::f64, Expand);
710 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000711 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000712 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000713 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000714 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000715 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
716 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000717
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
719 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
720 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
721 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000722
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000723 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000724 setOperationAction(ISD::FSIN , MVT::f64, Expand);
725 setOperationAction(ISD::FSIN , MVT::f32, Expand);
726 setOperationAction(ISD::FCOS , MVT::f64, Expand);
727 setOperationAction(ISD::FCOS , MVT::f32, Expand);
728 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
729 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000730 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000731 addLegalFPImmediate(APFloat(+0.0)); // FLD0
732 addLegalFPImmediate(APFloat(+1.0)); // FLD1
733 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
734 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000735 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
736 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
737 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
738 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000739 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000740
Cameron Zwarich33390842011-07-08 21:39:21 +0000741 // We don't support FMA.
742 setOperationAction(ISD::FMA, MVT::f64, Expand);
743 setOperationAction(ISD::FMA, MVT::f32, Expand);
744
Dale Johannesen59a58732007-08-05 18:49:15 +0000745 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000746 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000747 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
749 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000750 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000751 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000752 addLegalFPImmediate(TmpFlt); // FLD0
753 TmpFlt.changeSign();
754 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000755
756 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000757 APFloat TmpFlt2(+1.0);
758 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
759 &ignored);
760 addLegalFPImmediate(TmpFlt2); // FLD1
761 TmpFlt2.changeSign();
762 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
763 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000764
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000766 setOperationAction(ISD::FSIN , MVT::f80, Expand);
767 setOperationAction(ISD::FCOS , MVT::f80, Expand);
768 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000769 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000770
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000771 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
772 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
773 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
774 setOperationAction(ISD::FRINT, MVT::f80, Expand);
775 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000776 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000777 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000778
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000779 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
781 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
782 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000783
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 setOperationAction(ISD::FLOG, MVT::f80, Expand);
785 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
786 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
787 setOperationAction(ISD::FEXP, MVT::f80, Expand);
788 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000789
Mon P Wangf007a8b2008-11-06 05:31:54 +0000790 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000791 // (for widening) or expand (for scalarization). Then we will selectively
792 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000793 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
794 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000795 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000796 setOperationAction(ISD::ADD , VT, Expand);
797 setOperationAction(ISD::SUB , VT, Expand);
798 setOperationAction(ISD::FADD, VT, Expand);
799 setOperationAction(ISD::FNEG, VT, Expand);
800 setOperationAction(ISD::FSUB, VT, Expand);
801 setOperationAction(ISD::MUL , VT, Expand);
802 setOperationAction(ISD::FMUL, VT, Expand);
803 setOperationAction(ISD::SDIV, VT, Expand);
804 setOperationAction(ISD::UDIV, VT, Expand);
805 setOperationAction(ISD::FDIV, VT, Expand);
806 setOperationAction(ISD::SREM, VT, Expand);
807 setOperationAction(ISD::UREM, VT, Expand);
808 setOperationAction(ISD::LOAD, VT, Expand);
809 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
810 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
811 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
812 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
813 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
814 setOperationAction(ISD::FABS, VT, Expand);
815 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000816 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000817 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000818 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000819 setOperationAction(ISD::FREM, VT, Expand);
820 setOperationAction(ISD::FMA, VT, Expand);
821 setOperationAction(ISD::FPOWI, VT, Expand);
822 setOperationAction(ISD::FSQRT, VT, Expand);
823 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
824 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000825 setOperationAction(ISD::FCEIL, VT, Expand);
826 setOperationAction(ISD::FTRUNC, VT, Expand);
827 setOperationAction(ISD::FRINT, VT, Expand);
828 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000829 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
830 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
831 setOperationAction(ISD::SDIVREM, VT, Expand);
832 setOperationAction(ISD::UDIVREM, VT, Expand);
833 setOperationAction(ISD::FPOW, VT, Expand);
834 setOperationAction(ISD::CTPOP, VT, Expand);
835 setOperationAction(ISD::CTTZ, VT, Expand);
836 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
837 setOperationAction(ISD::CTLZ, VT, Expand);
838 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
839 setOperationAction(ISD::SHL, VT, Expand);
840 setOperationAction(ISD::SRA, VT, Expand);
841 setOperationAction(ISD::SRL, VT, Expand);
842 setOperationAction(ISD::ROTL, VT, Expand);
843 setOperationAction(ISD::ROTR, VT, Expand);
844 setOperationAction(ISD::BSWAP, VT, Expand);
845 setOperationAction(ISD::SETCC, VT, Expand);
846 setOperationAction(ISD::FLOG, VT, Expand);
847 setOperationAction(ISD::FLOG2, VT, Expand);
848 setOperationAction(ISD::FLOG10, VT, Expand);
849 setOperationAction(ISD::FEXP, VT, Expand);
850 setOperationAction(ISD::FEXP2, VT, Expand);
851 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
852 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
853 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
854 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
855 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
856 setOperationAction(ISD::TRUNCATE, VT, Expand);
857 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
858 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
859 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
860 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000861 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
862 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000863 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000864 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000865 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
866 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
867 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000868 }
869
Evan Chengc7ce29b2009-02-13 22:36:38 +0000870 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
871 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000872 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000873 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000874 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000875 }
876
Dale Johannesen0488fb62010-09-30 23:57:10 +0000877 // MMX-sized vectors (other than x86mmx) are expected to be expanded
878 // into smaller operations.
879 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
880 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
881 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
882 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
883 setOperationAction(ISD::AND, MVT::v8i8, Expand);
884 setOperationAction(ISD::AND, MVT::v4i16, Expand);
885 setOperationAction(ISD::AND, MVT::v2i32, Expand);
886 setOperationAction(ISD::AND, MVT::v1i64, Expand);
887 setOperationAction(ISD::OR, MVT::v8i8, Expand);
888 setOperationAction(ISD::OR, MVT::v4i16, Expand);
889 setOperationAction(ISD::OR, MVT::v2i32, Expand);
890 setOperationAction(ISD::OR, MVT::v1i64, Expand);
891 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
892 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
893 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
894 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
895 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
896 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
897 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
898 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
900 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
901 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
902 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
903 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000904 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
905 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
906 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
907 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000908
Craig Topper1accb7e2012-01-10 06:54:16 +0000909 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000910 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
913 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
914 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
915 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
916 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
917 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000918 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
920 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
921 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
923 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000924 }
925
Craig Topper1accb7e2012-01-10 06:54:16 +0000926 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000927 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000928
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000929 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
930 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000931 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
932 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
933 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
934 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
937 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
938 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
939 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000940 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
942 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
943 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
944 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
945 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
946 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
947 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
948 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
949 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
950 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
951 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
952 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000953 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000954
Nadav Rotem354efd82011-09-18 14:57:03 +0000955 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000956 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
957 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
958 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000959
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
961 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
963 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
964 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000965
Evan Cheng2c3ae372006-04-12 21:21:57 +0000966 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000967 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000968 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000969 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000971 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000972 // Do not attempt to custom lower non-128-bit vectors
973 if (!VT.is128BitVector())
974 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000975 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
976 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
977 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000978 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000979
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
981 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
982 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
983 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
984 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
985 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000986
Nate Begemancdd1eec2008-02-12 22:51:28 +0000987 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000988 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
989 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000990 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000991
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000992 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000993 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000994 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000995
996 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000997 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000998 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000999
Craig Topper0d1f1762012-08-12 00:34:56 +00001000 setOperationAction(ISD::AND, VT, Promote);
1001 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
1002 setOperationAction(ISD::OR, VT, Promote);
1003 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
1004 setOperationAction(ISD::XOR, VT, Promote);
1005 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
1006 setOperationAction(ISD::LOAD, VT, Promote);
1007 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
1008 setOperationAction(ISD::SELECT, VT, Promote);
1009 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +00001010 }
Evan Cheng2c3ae372006-04-12 21:21:57 +00001011
Owen Anderson825b72b2009-08-11 20:47:22 +00001012 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +00001013
Evan Cheng2c3ae372006-04-12 21:21:57 +00001014 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +00001015 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
1016 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
1017 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
1018 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +00001019
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
1021 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +00001022
Michael Liaoa7554632012-10-23 17:36:08 +00001023 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
1024 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +00001025 // As there is no 64-bit GPR available, we need build a special custom
1026 // sequence to convert from v2i32 to v2f32.
1027 if (!Subtarget->is64Bit())
1028 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +00001029
Michael Liao9d796db2012-10-10 16:32:15 +00001030 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +00001031 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +00001032
Michael Liaob8150d82012-09-10 18:33:51 +00001033 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +00001034 }
Evan Chengc7ce29b2009-02-13 22:36:38 +00001035
Justin Holewinski320185f2013-07-26 13:28:29 +00001036 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +00001037 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
1038 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
1039 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
1040 setOperationAction(ISD::FRINT, MVT::f32, Legal);
1041 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
1042 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1043 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1044 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1045 setOperationAction(ISD::FRINT, MVT::f64, Legal);
1046 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1047
Craig Topper12fb5c62012-09-08 17:42:27 +00001048 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001049 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
1050 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
1051 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
1052 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001053 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001054 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
1055 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
1056 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
1057 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001058
Nate Begeman14d12ca2008-02-11 04:19:36 +00001059 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001061
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001062 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1063 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1064 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1065 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1066 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001067
Nate Begeman14d12ca2008-02-11 04:19:36 +00001068 // i8 and i16 vectors are custom , because the source register and source
1069 // source memory operand types are not the same width. f32 vectors are
1070 // custom since the immediate controlling the insert encodes additional
1071 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001072 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1073 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1074 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1075 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001076
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1078 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1079 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1080 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001081
Pete Coopera77214a2011-11-14 19:38:42 +00001082 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001083 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001084 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001085 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1086 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001087 }
1088 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001089
Craig Topper1accb7e2012-01-10 06:54:16 +00001090 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001091 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001092 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001093
Nadav Rotem43012222011-05-11 08:12:09 +00001094 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001095 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001096
Nadav Rotem43012222011-05-11 08:12:09 +00001097 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001098 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001099
Michael Liao5c5f1902013-03-20 02:28:20 +00001100 // In the customized shift lowering, the legal cases in AVX2 will be
1101 // recognized.
1102 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1103 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001104
Michael Liao5c5f1902013-03-20 02:28:20 +00001105 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1106 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001107
Michael Liao5c5f1902013-03-20 02:28:20 +00001108 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001109
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001110 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1111 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001112 }
1113
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001114 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001115 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1116 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1117 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1118 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1119 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1120 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001121
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1124 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001125
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1127 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1128 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1129 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1130 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001131 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001132 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1133 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1134 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1135 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001137 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001138
Owen Anderson825b72b2009-08-11 20:47:22 +00001139 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1140 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1141 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1142 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1143 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001144 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001145 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1146 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1147 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1148 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001150 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001151
Michael Liaobedcbd42012-10-16 18:14:11 +00001152 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001153 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001154
1155 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1156
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001157 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
Benjamin Kramerb8f0d892013-03-31 12:49:15 +00001158 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Promote);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001159 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001160 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001161
Michael Liaoa7554632012-10-23 17:36:08 +00001162 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1163 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1164 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1165
Michael Liaob8150d82012-09-10 18:33:51 +00001166 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1167
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001168 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1169 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1170
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001171 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1172 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1173
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001174 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001175 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001176
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001177 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1178
Duncan Sands28b77e92011-09-06 19:07:46 +00001179 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1180 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1181 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1182 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001183
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001184 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1185 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1186 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1187
Craig Topperaaa643c2011-11-09 07:28:55 +00001188 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1189 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1190 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1191 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001192
Nadav Rotem0509db22012-12-28 05:45:24 +00001193 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1194 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1195 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1196 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1197 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1198 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001199
Craig Topperbf404372012-08-31 15:40:30 +00001200 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001201 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1202 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1203 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1204 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1205 setOperationAction(ISD::FMA, MVT::f32, Legal);
1206 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001207 }
Craig Topper880ef452012-08-11 22:34:26 +00001208
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001209 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001210 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1211 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1212 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1213 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001214
Craig Topperaaa643c2011-11-09 07:28:55 +00001215 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1216 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1217 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1218 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001219
Craig Topperaaa643c2011-11-09 07:28:55 +00001220 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1221 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1222 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001223 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001224
1225 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001226
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001227 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001228 } else {
1229 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1230 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1231 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1232 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1233
1234 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1235 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1236 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1237 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1238
1239 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1240 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1241 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1242 // Don't lower v32i8 because there is no 128-bit byte mul
1243 }
Craig Topper13894fa2011-08-24 06:14:18 +00001244
Michael Liao5c5f1902013-03-20 02:28:20 +00001245 // In the customized shift lowering, the legal cases in AVX2 will be
1246 // recognized.
1247 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1248 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1249
1250 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1251 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1252
1253 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1254
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001255 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001256 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1257 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001258 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001259
1260 // Extract subvector is special because the value type
1261 // (result) is 128-bit but the source is 256-bit wide.
1262 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001263 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001264
1265 // Do not attempt to custom lower other non-256-bit vectors
1266 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001267 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001268
Craig Topper0d1f1762012-08-12 00:34:56 +00001269 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1270 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1271 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1272 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1273 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1274 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1275 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001276 }
1277
David Greene54d8eba2011-01-27 22:38:56 +00001278 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001279 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001280 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001281
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001282 // Do not attempt to promote non-256-bit vectors
1283 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001284 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001285
Craig Topper0d1f1762012-08-12 00:34:56 +00001286 setOperationAction(ISD::AND, VT, Promote);
1287 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1288 setOperationAction(ISD::OR, VT, Promote);
1289 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1290 setOperationAction(ISD::XOR, VT, Promote);
1291 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1292 setOperationAction(ISD::LOAD, VT, Promote);
1293 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1294 setOperationAction(ISD::SELECT, VT, Promote);
1295 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001296 }
David Greene9b9838d2009-06-29 16:47:10 +00001297 }
1298
Elena Demikhovsky83952512013-07-31 11:35:14 +00001299 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX512()) {
1300 addRegisterClass(MVT::v16i32, &X86::VR512RegClass);
1301 addRegisterClass(MVT::v16f32, &X86::VR512RegClass);
1302 addRegisterClass(MVT::v8i64, &X86::VR512RegClass);
1303 addRegisterClass(MVT::v8f64, &X86::VR512RegClass);
1304
1305 addRegisterClass(MVT::v8i1, &X86::VK8RegClass);
1306 addRegisterClass(MVT::v16i1, &X86::VK16RegClass);
1307
1308 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, Legal);
1309 setOperationAction(ISD::LOAD, MVT::v16f32, Legal);
1310 setOperationAction(ISD::LOAD, MVT::v8f64, Legal);
1311 setOperationAction(ISD::LOAD, MVT::v8i64, Legal);
1312 setOperationAction(ISD::LOAD, MVT::v16i32, Legal);
1313 setOperationAction(ISD::LOAD, MVT::v16i1, Legal);
1314
1315 setOperationAction(ISD::FADD, MVT::v16f32, Legal);
1316 setOperationAction(ISD::FSUB, MVT::v16f32, Legal);
1317 setOperationAction(ISD::FMUL, MVT::v16f32, Legal);
1318 setOperationAction(ISD::FDIV, MVT::v16f32, Legal);
1319 setOperationAction(ISD::FSQRT, MVT::v16f32, Legal);
1320 setOperationAction(ISD::FNEG, MVT::v16f32, Custom);
1321
1322 setOperationAction(ISD::FADD, MVT::v8f64, Legal);
1323 setOperationAction(ISD::FSUB, MVT::v8f64, Legal);
1324 setOperationAction(ISD::FMUL, MVT::v8f64, Legal);
1325 setOperationAction(ISD::FDIV, MVT::v8f64, Legal);
1326 setOperationAction(ISD::FSQRT, MVT::v8f64, Legal);
1327 setOperationAction(ISD::FNEG, MVT::v8f64, Custom);
1328 setOperationAction(ISD::FMA, MVT::v8f64, Legal);
1329 setOperationAction(ISD::FMA, MVT::v16f32, Legal);
1330 setOperationAction(ISD::SDIV, MVT::v16i32, Custom);
1331
1332
1333 setOperationAction(ISD::FP_TO_SINT, MVT::v16i32, Legal);
1334 setOperationAction(ISD::FP_TO_UINT, MVT::v16i32, Legal);
1335 setOperationAction(ISD::FP_TO_UINT, MVT::v8i32, Legal);
1336 setOperationAction(ISD::SINT_TO_FP, MVT::v16i32, Legal);
1337 setOperationAction(ISD::UINT_TO_FP, MVT::v16i32, Legal);
1338 setOperationAction(ISD::UINT_TO_FP, MVT::v8i32, Legal);
1339 setOperationAction(ISD::FP_ROUND, MVT::v8f32, Legal);
1340 setOperationAction(ISD::FP_EXTEND, MVT::v8f32, Legal);
1341
1342 setOperationAction(ISD::TRUNCATE, MVT::i1, Legal);
1343 setOperationAction(ISD::TRUNCATE, MVT::v16i8, Custom);
1344 setOperationAction(ISD::TRUNCATE, MVT::v8i32, Custom);
1345 setOperationAction(ISD::TRUNCATE, MVT::v8i1, Custom);
1346 setOperationAction(ISD::TRUNCATE, MVT::v16i1, Custom);
1347 setOperationAction(ISD::ZERO_EXTEND, MVT::v16i32, Custom);
1348 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i64, Custom);
1349 setOperationAction(ISD::SIGN_EXTEND, MVT::v16i32, Custom);
1350 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i64, Custom);
1351 setOperationAction(ISD::SIGN_EXTEND, MVT::v16i8, Custom);
1352 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i16, Custom);
1353 setOperationAction(ISD::SIGN_EXTEND, MVT::v16i16, Custom);
1354
1355 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f64, Custom);
1356 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i64, Custom);
1357 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16f32, Custom);
1358 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i32, Custom);
1359 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i1, Custom);
1360
1361 setOperationAction(ISD::SETCC, MVT::v16i1, Custom);
1362 setOperationAction(ISD::SETCC, MVT::v8i1, Custom);
1363
1364 setOperationAction(ISD::MUL, MVT::v8i64, Custom);
1365
1366 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i1, Custom);
1367 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i1, Custom);
1368 setOperationAction(ISD::SELECT, MVT::v8f64, Custom);
1369 setOperationAction(ISD::SELECT, MVT::v8i64, Custom);
1370 setOperationAction(ISD::SELECT, MVT::v16f32, Custom);
1371
1372 setOperationAction(ISD::ADD, MVT::v8i64, Legal);
1373 setOperationAction(ISD::ADD, MVT::v16i32, Legal);
1374
1375 setOperationAction(ISD::SUB, MVT::v8i64, Legal);
1376 setOperationAction(ISD::SUB, MVT::v16i32, Legal);
1377
1378 setOperationAction(ISD::MUL, MVT::v16i32, Legal);
1379
1380 setOperationAction(ISD::SRL, MVT::v8i64, Custom);
1381 setOperationAction(ISD::SRL, MVT::v16i32, Custom);
1382
1383 setOperationAction(ISD::SHL, MVT::v8i64, Custom);
1384 setOperationAction(ISD::SHL, MVT::v16i32, Custom);
1385
1386 setOperationAction(ISD::SRA, MVT::v8i64, Custom);
1387 setOperationAction(ISD::SRA, MVT::v16i32, Custom);
1388
1389 setOperationAction(ISD::AND, MVT::v8i64, Legal);
1390 setOperationAction(ISD::OR, MVT::v8i64, Legal);
1391 setOperationAction(ISD::XOR, MVT::v8i64, Legal);
1392
1393 // Custom lower several nodes.
1394 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1395 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
1396 MVT VT = (MVT::SimpleValueType)i;
1397
1398 // Extract subvector is special because the value type
1399 // (result) is 256/128-bit but the source is 512-bit wide.
1400 if (VT.is128BitVector() || VT.is256BitVector())
1401 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
1402
1403 if (VT.getVectorElementType() == MVT::i1)
1404 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
1405
1406 // Do not attempt to custom lower other non-512-bit vectors
1407 if (!VT.is512BitVector())
1408 continue;
1409
1410 if (VT != MVT::v8i64) {
1411 setOperationAction(ISD::XOR, VT, Promote);
1412 AddPromotedToType (ISD::XOR, VT, MVT::v8i64);
1413 setOperationAction(ISD::OR, VT, Promote);
1414 AddPromotedToType (ISD::OR, VT, MVT::v8i64);
1415 setOperationAction(ISD::AND, VT, Promote);
1416 AddPromotedToType (ISD::AND, VT, MVT::v8i64);
1417 }
1418 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1419 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1420 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1421 setOperationAction(ISD::VSELECT, VT, Legal);
1422 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1423 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1424 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1425 }
1426 for (int i = MVT::v32i8; i != MVT::v8i64; ++i) {
1427 MVT VT = (MVT::SimpleValueType)i;
1428
1429 // Do not attempt to promote non-256-bit vectors
1430 if (!VT.is512BitVector())
1431 continue;
1432
1433 setOperationAction(ISD::LOAD, VT, Promote);
1434 AddPromotedToType (ISD::LOAD, VT, MVT::v8i64);
1435 setOperationAction(ISD::SELECT, VT, Promote);
1436 AddPromotedToType (ISD::SELECT, VT, MVT::v8i64);
1437 }
1438 }// has AVX-512
1439
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001440 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1441 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001442 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1443 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001444 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1445 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001446 }
1447
Evan Cheng6be2c582006-04-05 23:38:46 +00001448 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001450 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001451
Eli Friedman962f5492010-06-02 19:35:46 +00001452 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1453 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001454 //
Eli Friedman962f5492010-06-02 19:35:46 +00001455 // FIXME: We really should do custom legalization for addition and
1456 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1457 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001458 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1459 // Add/Sub/Mul with overflow operations are custom lowered.
1460 MVT VT = IntVTs[i];
1461 setOperationAction(ISD::SADDO, VT, Custom);
1462 setOperationAction(ISD::UADDO, VT, Custom);
1463 setOperationAction(ISD::SSUBO, VT, Custom);
1464 setOperationAction(ISD::USUBO, VT, Custom);
1465 setOperationAction(ISD::SMULO, VT, Custom);
1466 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001467 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001468
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001469 // There are no 8-bit 3-address imul/mul instructions
1470 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1471 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001472
Evan Chengd54f2d52009-03-31 19:38:51 +00001473 if (!Subtarget->is64Bit()) {
1474 // These libcalls are not available in 32-bit.
1475 setLibcallName(RTLIB::SHL_I128, 0);
1476 setLibcallName(RTLIB::SRL_I128, 0);
1477 setLibcallName(RTLIB::SRA_I128, 0);
1478 }
1479
Evan Cheng8688a582013-01-29 02:32:37 +00001480 // Combine sin / cos into one node or libcall if possible.
1481 if (Subtarget->hasSinCos()) {
1482 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1483 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001484 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001485 // For MacOSX, we don't want to the normal expansion of a libcall to
1486 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1487 // traffic.
1488 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1489 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1490 }
1491 }
1492
Evan Cheng206ee9d2006-07-07 08:33:52 +00001493 // We have target-specific dag combine patterns for the following nodes:
1494 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001495 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001496 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001497 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001498 setTargetDAGCombine(ISD::SHL);
1499 setTargetDAGCombine(ISD::SRA);
1500 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001501 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001502 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001503 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001504 setTargetDAGCombine(ISD::FADD);
1505 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001506 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001507 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001508 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001509 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001510 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001511 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001512 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001513 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001514 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001515 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001516 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001517 if (Subtarget->is64Bit())
1518 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001519 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001520
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001521 computeRegisterProperties();
1522
Evan Cheng05219282011-01-06 06:52:41 +00001523 // On Darwin, -Os means optimize for size without hurting performance,
1524 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001525 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1526 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1527 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1528 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1529 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1530 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001531 setPrefLoopAlignment(4); // 2^4 bytes.
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001532
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001533 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001534 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001535
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001536 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001537}
1538
Matt Arsenault225ed702013-05-18 00:21:46 +00001539EVT X86TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00001540 if (!VT.isVector()) return MVT::i8;
1541 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001542}
1543
Evan Cheng29286502008-01-23 23:17:41 +00001544/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1545/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001546static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001547 if (MaxAlign == 16)
1548 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001549 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001550 if (VTy->getBitWidth() == 128)
1551 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001552 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001553 unsigned EltAlign = 0;
1554 getMaxByValAlign(ATy->getElementType(), EltAlign);
1555 if (EltAlign > MaxAlign)
1556 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001557 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001558 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1559 unsigned EltAlign = 0;
1560 getMaxByValAlign(STy->getElementType(i), EltAlign);
1561 if (EltAlign > MaxAlign)
1562 MaxAlign = EltAlign;
1563 if (MaxAlign == 16)
1564 break;
1565 }
1566 }
Evan Cheng29286502008-01-23 23:17:41 +00001567}
1568
1569/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1570/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001571/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1572/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001573unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001574 if (Subtarget->is64Bit()) {
1575 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001576 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001577 if (TyAlign > 8)
1578 return TyAlign;
1579 return 8;
1580 }
1581
Evan Cheng29286502008-01-23 23:17:41 +00001582 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001583 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001584 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001585 return Align;
1586}
Chris Lattner2b02a442007-02-25 08:29:00 +00001587
Evan Chengf0df0312008-05-15 08:39:06 +00001588/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001589/// and store operations as a result of memset, memcpy, and memmove
1590/// lowering. If DstAlign is zero that means it's safe to destination
1591/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1592/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001593/// probably because the source does not need to be loaded. If 'IsMemset' is
1594/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1595/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1596/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001597/// It returns EVT::Other if the type should be determined using generic
1598/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001599EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001600X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1601 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001602 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001603 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001604 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001605 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001606 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001607 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1608 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001609 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001610 (Subtarget->isUnalignedMemAccessFast() ||
1611 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001612 (SrcAlign == 0 || SrcAlign >= 16)))) {
1613 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001614 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001615 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001616 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001617 return MVT::v8f32;
1618 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001619 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001620 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001621 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001622 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001623 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001624 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001625 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001626 // Do not use f64 to lower memcpy if source is string constant. It's
1627 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001628 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001629 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001630 }
Evan Chengf0df0312008-05-15 08:39:06 +00001631 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 return MVT::i64;
1633 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001634}
1635
Evan Cheng7d342672012-12-12 01:32:07 +00001636bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001637 if (VT == MVT::f32)
1638 return X86ScalarSSEf32;
1639 else if (VT == MVT::f64)
1640 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001641 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001642}
1643
Evan Cheng376642e2012-12-10 23:21:26 +00001644bool
1645X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1646 if (Fast)
1647 *Fast = Subtarget->isUnalignedMemAccessFast();
1648 return true;
1649}
1650
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001651/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1652/// current function. The returned value is a member of the
1653/// MachineJumpTableInfo::JTEntryKind enum.
1654unsigned X86TargetLowering::getJumpTableEncoding() const {
1655 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1656 // symbol.
1657 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1658 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001659 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001660
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001661 // Otherwise, use the normal jump table encoding heuristics.
1662 return TargetLowering::getJumpTableEncoding();
1663}
1664
Chris Lattnerc64daab2010-01-26 05:02:42 +00001665const MCExpr *
1666X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1667 const MachineBasicBlock *MBB,
1668 unsigned uid,MCContext &Ctx) const{
1669 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1670 Subtarget->isPICStyleGOT());
1671 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1672 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001673 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1674 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001675}
1676
Evan Chengcc415862007-11-09 01:32:10 +00001677/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1678/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001679SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001680 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001681 if (!Subtarget->is64Bit())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001682 // This doesn't have SDLoc associated with it, but is not really the
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001683 // same as a Register.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001684 return DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001685 return Table;
1686}
1687
Chris Lattner589c6f62010-01-26 06:28:43 +00001688/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1689/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1690/// MCExpr.
1691const MCExpr *X86TargetLowering::
1692getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1693 MCContext &Ctx) const {
1694 // X86-64 uses RIP relative addressing based on the jump table label.
1695 if (Subtarget->isPICStyleRIPRel())
1696 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1697
1698 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001699 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001700}
1701
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001702// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001703std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001704X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001705 const TargetRegisterClass *RRC = 0;
1706 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001707 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001708 default:
1709 return TargetLowering::findRepresentativeClass(VT);
1710 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001711 RRC = Subtarget->is64Bit() ?
1712 (const TargetRegisterClass*)&X86::GR64RegClass :
1713 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001714 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001715 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001716 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001717 break;
1718 case MVT::f32: case MVT::f64:
1719 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1720 case MVT::v4f32: case MVT::v2f64:
1721 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1722 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001723 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001724 break;
1725 }
1726 return std::make_pair(RRC, Cost);
1727}
1728
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001729bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1730 unsigned &Offset) const {
1731 if (!Subtarget->isTargetLinux())
1732 return false;
1733
1734 if (Subtarget->is64Bit()) {
1735 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1736 Offset = 0x28;
1737 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1738 AddressSpace = 256;
1739 else
1740 AddressSpace = 257;
1741 } else {
1742 // %gs:0x14 on i386
1743 Offset = 0x14;
1744 AddressSpace = 256;
1745 }
1746 return true;
1747}
1748
Chris Lattner2b02a442007-02-25 08:29:00 +00001749//===----------------------------------------------------------------------===//
1750// Return Value Calling Convention Implementation
1751//===----------------------------------------------------------------------===//
1752
Chris Lattner59ed56b2007-02-28 04:55:35 +00001753#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001754
Michael J. Spencerec38de22010-10-10 22:04:20 +00001755bool
Eric Christopher471e4222011-06-08 23:55:35 +00001756X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001757 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001758 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001759 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001760 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001761 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001762 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001763 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001764}
1765
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766SDValue
1767X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001768 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001769 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001770 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001771 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001772 MachineFunction &MF = DAG.getMachineFunction();
1773 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001774
Chris Lattner9774c912007-02-27 05:28:59 +00001775 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001776 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001777 RVLocs, *DAG.getContext());
1778 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001779
Dan Gohman475871a2008-07-27 21:46:04 +00001780 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001781 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001782 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1783 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001784 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1785 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001786
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001787 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001788 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1789 CCValAssign &VA = RVLocs[i];
1790 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001791 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001792 EVT ValVT = ValToCopy.getValueType();
1793
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001794 // Promote values to the appropriate types
1795 if (VA.getLocInfo() == CCValAssign::SExt)
1796 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1797 else if (VA.getLocInfo() == CCValAssign::ZExt)
1798 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1799 else if (VA.getLocInfo() == CCValAssign::AExt)
1800 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1801 else if (VA.getLocInfo() == CCValAssign::BCvt)
1802 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1803
Dale Johannesenc4510512010-09-24 19:05:48 +00001804 // If this is x86-64, and we disabled SSE, we can't return FP values,
1805 // or SSE or MMX vectors.
1806 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1807 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001808 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001809 report_fatal_error("SSE register return with SSE disabled");
1810 }
1811 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1812 // llvm-gcc has never done it right and no one has noticed, so this
1813 // should be OK for now.
1814 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001815 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001816 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001817
Chris Lattner447ff682008-03-11 03:23:40 +00001818 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1819 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001820 if (VA.getLocReg() == X86::ST0 ||
1821 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001822 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1823 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001824 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001826 RetOps.push_back(ValToCopy);
1827 // Don't emit a copytoreg.
1828 continue;
1829 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001830
Evan Cheng242b38b2009-02-23 09:03:22 +00001831 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1832 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001833 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001834 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001835 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001836 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001837 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1838 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001839 // If we don't have SSE2 available, convert to v4f32 so the generated
1840 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001841 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001842 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001843 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001844 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001845 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001846
Dale Johannesendd64c412009-02-04 00:33:20 +00001847 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001848 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001849 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001850 }
Dan Gohman61a92132008-04-21 23:59:07 +00001851
Eli Benderskya5597f02013-01-25 22:07:43 +00001852 // The x86-64 ABIs require that for returning structs by value we copy
1853 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001854 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00001855 // We saved the argument into a virtual register in the entry block,
1856 // so now we copy the value out and into %rax/%eax.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001857 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr() &&
1858 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00001859 MachineFunction &MF = DAG.getMachineFunction();
1860 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1861 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001862 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001863 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001864 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001865
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001866 unsigned RetValReg
1867 = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ?
1868 X86::RAX : X86::EAX;
Eli Benderskya5597f02013-01-25 22:07:43 +00001869 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001870 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001871
Eli Benderskya5597f02013-01-25 22:07:43 +00001872 // RAX/EAX now acts like a return value.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001873 RetOps.push_back(DAG.getRegister(RetValReg, getPointerTy()));
Dan Gohman61a92132008-04-21 23:59:07 +00001874 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
Chris Lattner447ff682008-03-11 03:23:40 +00001876 RetOps[0] = Chain; // Update chain.
1877
1878 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001879 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001880 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
1882 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001884}
1885
Evan Chengbf010eb2012-04-10 01:51:00 +00001886bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001887 if (N->getNumValues() != 1)
1888 return false;
1889 if (!N->hasNUsesOfValue(1, 0))
1890 return false;
1891
Evan Chengbf010eb2012-04-10 01:51:00 +00001892 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001893 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001894 if (Copy->getOpcode() == ISD::CopyToReg) {
1895 // If the copy has a glue operand, we conservatively assume it isn't safe to
1896 // perform a tail call.
1897 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1898 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001899 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001900 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001901 return false;
1902
Evan Cheng1bf891a2010-12-01 22:59:46 +00001903 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001904 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001905 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001906 if (UI->getOpcode() != X86ISD::RET_FLAG)
1907 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001908 HasRet = true;
1909 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001910
Evan Chengbf010eb2012-04-10 01:51:00 +00001911 if (!HasRet)
1912 return false;
1913
1914 Chain = TCChain;
1915 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001916}
1917
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001918MVT
1919X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001920 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001921 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001922 // TODO: Is this also valid on 32-bit?
1923 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001924 ReturnMVT = MVT::i8;
1925 else
1926 ReturnMVT = MVT::i32;
1927
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001928 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001929 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001930}
1931
Dan Gohman98ca4f22009-08-05 01:29:28 +00001932/// LowerCallResult - Lower the result values of a call into the
1933/// appropriate copies out of appropriate physical registers.
1934///
1935SDValue
1936X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001937 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001939 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001940 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001941
Chris Lattnere32bbf62007-02-28 07:09:55 +00001942 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001943 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001944 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001945 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001946 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001948
Chris Lattner3085e152007-02-25 08:59:22 +00001949 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001950 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001951 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001952 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001953
Torok Edwin3f142c32009-02-01 18:15:56 +00001954 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001955 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001956 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001957 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001958 }
1959
Evan Cheng79fb3b42009-02-20 20:43:02 +00001960 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001961
1962 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001963 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001964 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001965 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001966 // instead.
1967 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1968 // If we prefer to use the value in xmm registers, copy it out as f80 and
1969 // use a truncate to move it from fp stack reg to xmm reg.
1970 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001971 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001972 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
Michael Liao2a8bea72013-04-19 22:22:57 +00001973 MVT::Other, MVT::Glue, Ops), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001974 Val = Chain.getValue(0);
1975
1976 // Round the f80 to the right size, which also moves it to the appropriate
1977 // xmm register.
1978 if (CopyVT != VA.getValVT())
1979 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1980 // This truncation won't change the value.
1981 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001982 } else {
1983 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1984 CopyVT, InFlag).getValue(1);
1985 Val = Chain.getValue(0);
1986 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001987 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001989 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001990
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001992}
1993
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001994//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001995// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001996//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001997// StdCall calling convention seems to be standard for many Windows' API
1998// routines and around. It differs from C calling convention just a little:
1999// callee should clean up the stack, not caller. Symbols should be also
2000// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002001// For info on fast calling convention see Fast Calling Convention (tail call)
2002// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002003
Dan Gohman98ca4f22009-08-05 01:29:28 +00002004/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00002005/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00002006enum StructReturnType {
2007 NotStructReturn,
2008 RegStructReturn,
2009 StackStructReturn
2010};
2011static StructReturnType
2012callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00002014 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00002015
Rafael Espindola1cee7102012-07-25 13:41:10 +00002016 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
2017 if (!Flags.isSRet())
2018 return NotStructReturn;
2019 if (Flags.isInReg())
2020 return RegStructReturn;
2021 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00002022}
2023
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002024/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00002025/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00002026static StructReturnType
2027argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00002029 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00002030
Rafael Espindola1cee7102012-07-25 13:41:10 +00002031 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
2032 if (!Flags.isSRet())
2033 return NotStructReturn;
2034 if (Flags.isInReg())
2035 return RegStructReturn;
2036 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00002037}
2038
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00002039/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
2040/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002041/// the specific parameter attribute. The copy will be passed as a byval
2042/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00002043static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00002044CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002045 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002046 SDLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00002047 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00002048
Dale Johannesendd64c412009-02-04 00:33:20 +00002049 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00002050 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002051 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002052}
2053
Chris Lattner29689432010-03-11 00:22:57 +00002054/// IsTailCallConvention - Return true if the calling convention is one that
2055/// supports tail call optimization.
2056static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002057 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
2058 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00002059}
2060
Charles Davisac226bb2013-07-12 06:02:35 +00002061/// \brief Return true if the calling convention is a C calling convention.
2062static bool IsCCallConvention(CallingConv::ID CC) {
2063 return (CC == CallingConv::C || CC == CallingConv::X86_64_Win64 ||
2064 CC == CallingConv::X86_64_SysV);
2065}
2066
Evan Cheng485fafc2011-03-21 01:19:09 +00002067bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00002068 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00002069 return false;
2070
2071 CallSite CS(CI);
2072 CallingConv::ID CalleeCC = CS.getCallingConv();
Charles Davisac226bb2013-07-12 06:02:35 +00002073 if (!IsTailCallConvention(CalleeCC) && !IsCCallConvention(CalleeCC))
Evan Cheng485fafc2011-03-21 01:19:09 +00002074 return false;
2075
2076 return true;
2077}
2078
Evan Cheng0c439eb2010-01-27 00:07:07 +00002079/// FuncIsMadeTailCallSafe - Return true if the function is being made into
2080/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002081static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
2082 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002083 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00002084}
2085
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086SDValue
2087X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002088 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002090 SDLoc dl, SelectionDAG &DAG,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091 const CCValAssign &VA,
2092 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00002093 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00002094 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002095 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002096 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
2097 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00002098 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00002099 EVT ValVT;
2100
2101 // If value is passed by pointer we have address passed instead of the value
2102 // itself.
2103 if (VA.getLocInfo() == CCValAssign::Indirect)
2104 ValVT = VA.getLocVT();
2105 else
2106 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00002107
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002108 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00002109 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002110 // In case of tail call optimization mark all arguments mutable. Since they
2111 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00002112 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00002113 unsigned Bytes = Flags.getByValSize();
2114 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
2115 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00002116 return DAG.getFrameIndex(FI, getPointerTy());
2117 } else {
2118 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002119 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00002120 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2121 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002122 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002123 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00002124 }
Rafael Espindola7effac52007-09-14 15:48:13 +00002125}
2126
Dan Gohman475871a2008-07-27 21:46:04 +00002127SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002128X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002129 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002130 bool isVarArg,
2131 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002132 SDLoc dl,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002134 SmallVectorImpl<SDValue> &InVals)
2135 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00002136 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00002137 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00002138
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 const Function* Fn = MF.getFunction();
2140 if (Fn->hasExternalLinkage() &&
2141 Subtarget->isTargetCygMing() &&
2142 Fn->getName() == "main")
2143 FuncInfo->setForceFramePointer(true);
2144
Evan Cheng1bc78042006-04-26 01:20:17 +00002145 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00002146 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002147 bool IsWindows = Subtarget->isTargetWindows();
Charles Davisac226bb2013-07-12 06:02:35 +00002148 bool IsWin64 = Subtarget->isCallingConvWin64(CallConv);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002149
Chris Lattner29689432010-03-11 00:22:57 +00002150 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002151 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002152
Chris Lattner638402b2007-02-28 07:00:42 +00002153 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002154 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002155 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002157
2158 // Allocate shadow area for Win64
Charles Davisac226bb2013-07-12 06:02:35 +00002159 if (IsWin64)
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002160 CCInfo.AllocateStack(32, 8);
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002161
Duncan Sands45907662010-10-31 13:21:44 +00002162 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002163
Chris Lattnerf39f7712007-02-28 05:46:49 +00002164 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002165 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00002166 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2167 CCValAssign &VA = ArgLocs[i];
2168 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
2169 // places.
2170 assert(VA.getValNo() != LastVal &&
2171 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00002172 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00002173 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00002174
Chris Lattnerf39f7712007-02-28 05:46:49 +00002175 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002176 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00002177 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002178 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00002179 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002180 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00002181 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002182 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00002183 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002184 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00002185 RC = &X86::FR64RegClass;
Elena Demikhovsky83952512013-07-31 11:35:14 +00002186 else if (RegVT.is512BitVector())
2187 RC = &X86::VR512RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002188 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002189 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002190 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002191 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00002192 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002193 RC = &X86::VR64RegClass;
Elena Demikhovsky83952512013-07-31 11:35:14 +00002194 else if (RegVT == MVT::v8i1)
2195 RC = &X86::VK8RegClass;
2196 else if (RegVT == MVT::v16i1)
2197 RC = &X86::VK16RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002198 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002199 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002200
Devang Patel68e6bee2011-02-21 23:21:26 +00002201 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002203
Chris Lattnerf39f7712007-02-28 05:46:49 +00002204 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2205 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2206 // right size.
2207 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002208 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002209 DAG.getValueType(VA.getValVT()));
2210 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002211 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002212 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002213 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002214 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002215
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002216 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002217 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002218 if (RegVT.isVector())
2219 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2220 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002221 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002222 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002223 } else {
2224 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002226 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002227
2228 // If value is passed via pointer - do a load.
2229 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002230 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002231 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002232
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002234 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002235
Eli Benderskya5597f02013-01-25 22:07:43 +00002236 // The x86-64 ABIs require that for returning structs by value we copy
2237 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002238 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00002239 // Save the argument into a virtual register so that we can access it
2240 // from the return points.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002241 if (MF.getFunction()->hasStructRetAttr() &&
2242 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00002243 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2244 unsigned Reg = FuncInfo->getSRetReturnReg();
2245 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002246 MVT PtrTy = getPointerTy();
2247 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002248 FuncInfo->setSRetReturnReg(Reg);
2249 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002250 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002251 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002252 }
2253
Chris Lattnerf39f7712007-02-28 05:46:49 +00002254 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002255 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002256 if (FuncIsMadeTailCallSafe(CallConv,
2257 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002258 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002259
Evan Cheng1bc78042006-04-26 01:20:17 +00002260 // If the function takes variable number of arguments, make a frame index for
2261 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002262 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002263 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2264 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002265 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002266 }
2267 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002268 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2269
2270 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002271 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002272 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002273 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002274 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002275 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2276 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002277 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002278 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2279 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2280 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002281 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002282 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002283
2284 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002285 // The XMM registers which might contain var arg parameters are shadowed
2286 // in their paired GPR. So we only need to save the GPR to their home
2287 // slots.
2288 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002289 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002290 } else {
2291 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2292 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002293
Chad Rosier30450e82011-12-22 22:35:21 +00002294 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2295 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002296 }
2297 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2298 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002299
Bill Wendling831737d2012-12-30 10:32:01 +00002300 bool NoImplicitFloatOps = Fn->getAttributes().
2301 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002302 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002303 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002304 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2305 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002306 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002307 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002308 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002309 // Kernel mode asks for SSE to be disabled, so don't push them
2310 // on the stack.
2311 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002312
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002313 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002314 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002315 // Get to the caller-allocated home save location. Add 8 to account
2316 // for the return address.
2317 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002318 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002319 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002320 // Fixup to set vararg frame on shadow area (4 x i64).
2321 if (NumIntRegs < 4)
2322 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002323 } else {
2324 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002325 // registers, then we must store them to their spots on the stack so
2326 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002327 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2328 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2329 FuncInfo->setRegSaveFrameIndex(
2330 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002331 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002332 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002333
Gordon Henriksen86737662008-01-05 16:56:59 +00002334 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002335 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002336 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2337 getPointerTy());
2338 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002339 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002340 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2341 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002342 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002343 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002345 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002346 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002347 MachinePointerInfo::getFixedStack(
2348 FuncInfo->getRegSaveFrameIndex(), Offset),
2349 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002350 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002351 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002352 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002353
Dan Gohmanface41a2009-08-16 21:24:25 +00002354 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2355 // Now store the XMM (fp + vector) parameter registers.
2356 SmallVector<SDValue, 11> SaveXMMOps;
2357 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002358
Craig Topperc9099502012-04-20 06:31:50 +00002359 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002360 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2361 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002362
Dan Gohman1e93df62010-04-17 14:41:14 +00002363 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2364 FuncInfo->getRegSaveFrameIndex()));
2365 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2366 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002367
Dan Gohmanface41a2009-08-16 21:24:25 +00002368 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002369 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002370 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002371 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2372 SaveXMMOps.push_back(Val);
2373 }
2374 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2375 MVT::Other,
2376 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002377 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002378
2379 if (!MemOps.empty())
2380 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2381 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002382 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002384
Gordon Henriksen86737662008-01-05 16:56:59 +00002385 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002386 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2387 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002388 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002389 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002390 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002391 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002392 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002393 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002394 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002395 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002396
Gordon Henriksen86737662008-01-05 16:56:59 +00002397 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002398 // RegSaveFrameIndex is X86-64 only.
2399 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002400 if (CallConv == CallingConv::X86_FastCall ||
2401 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002402 // fastcc functions can't have varargs.
2403 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002404 }
Evan Cheng25caf632006-05-23 21:06:34 +00002405
Rafael Espindola76927d752011-08-30 19:39:58 +00002406 FuncInfo->setArgumentStackSize(StackSize);
2407
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002409}
2410
Dan Gohman475871a2008-07-27 21:46:04 +00002411SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002412X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2413 SDValue StackPtr, SDValue Arg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002414 SDLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002415 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002416 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002417 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002418 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002419 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002420 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002421 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002422
2423 return DAG.getStore(Chain, dl, Arg, PtrOff,
2424 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002425 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002426}
2427
Bill Wendling64e87322009-01-16 19:25:27 +00002428/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002429/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002430SDValue
2431X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002432 SDValue &OutRetAddr, SDValue Chain,
2433 bool IsTailCall, bool Is64Bit,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002434 int FPDiff, SDLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002435 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002436 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002437 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002438
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002439 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002440 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002441 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002442 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002443}
2444
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002445/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002446/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002447static SDValue
2448EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002449 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002450 unsigned SlotSize, int FPDiff, SDLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002451 // Store the return address to the appropriate stack slot.
2452 if (!FPDiff) return Chain;
2453 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002454 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002455 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002456 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002457 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002458 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002459 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002460 return Chain;
2461}
2462
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002464X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002465 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002466 SelectionDAG &DAG = CLI.DAG;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002467 SDLoc &dl = CLI.DL;
2468 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2469 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2470 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002471 SDValue Chain = CLI.Chain;
2472 SDValue Callee = CLI.Callee;
2473 CallingConv::ID CallConv = CLI.CallConv;
2474 bool &isTailCall = CLI.IsTailCall;
2475 bool isVarArg = CLI.IsVarArg;
2476
Dan Gohman98ca4f22009-08-05 01:29:28 +00002477 MachineFunction &MF = DAG.getMachineFunction();
2478 bool Is64Bit = Subtarget->is64Bit();
Charles Davisac226bb2013-07-12 06:02:35 +00002479 bool IsWin64 = Subtarget->isCallingConvWin64(CallConv);
Eli Friedman9a2478a2012-01-20 00:05:46 +00002480 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002481 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002482 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002483
Nick Lewycky22de16d2012-01-19 00:34:10 +00002484 if (MF.getTarget().Options.DisableTailCalls)
2485 isTailCall = false;
2486
Evan Cheng5f941932010-02-05 02:21:12 +00002487 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002488 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002489 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002490 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002491 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002492 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002493
2494 // Sibcalls are automatically detected tailcalls which do not require
2495 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002496 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002497 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002498
2499 if (isTailCall)
2500 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002501 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002502
Chris Lattner29689432010-03-11 00:22:57 +00002503 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002504 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002505
Chris Lattner638402b2007-02-28 07:00:42 +00002506 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002507 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002508 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002509 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002510
2511 // Allocate shadow area for Win64
Charles Davisac226bb2013-07-12 06:02:35 +00002512 if (IsWin64)
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002513 CCInfo.AllocateStack(32, 8);
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002514
Duncan Sands45907662010-10-31 13:21:44 +00002515 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002516
Chris Lattner423c5f42007-02-28 05:31:48 +00002517 // Get a count of how many bytes are to be pushed on the stack.
2518 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002519 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002520 // This is a sibcall. The memory operands are available in caller's
2521 // own caller's stack.
2522 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002523 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2524 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002525 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002526
Gordon Henriksen86737662008-01-05 16:56:59 +00002527 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002528 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002529 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002530 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2531 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2532
Gordon Henriksen86737662008-01-05 16:56:59 +00002533 FPDiff = NumBytesCallerPushed - NumBytes;
2534
2535 // Set the delta of movement of the returnaddr stackslot.
2536 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002537 if (FPDiff < X86Info->getTCReturnAddrDelta())
2538 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002539 }
2540
Evan Chengf22f9b32010-02-06 03:28:46 +00002541 if (!IsSibcall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002542 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
2543 dl);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002544
Dan Gohman475871a2008-07-27 21:46:04 +00002545 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002546 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002547 if (isTailCall && FPDiff)
2548 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2549 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002550
Dan Gohman475871a2008-07-27 21:46:04 +00002551 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2552 SmallVector<SDValue, 8> MemOpChains;
2553 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002554
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002555 // Walk the register/memloc assignments, inserting copies/loads. In the case
2556 // of tail call optimization arguments are handle later.
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002557 const X86RegisterInfo *RegInfo =
2558 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Chris Lattner423c5f42007-02-28 05:31:48 +00002559 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2560 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002561 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002562 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002563 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002564 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002565
Chris Lattner423c5f42007-02-28 05:31:48 +00002566 // Promote the value if needed.
2567 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002568 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002569 case CCValAssign::Full: break;
2570 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002571 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002572 break;
2573 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002574 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002575 break;
2576 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002577 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002578 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002579 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002580 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2581 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002582 } else
2583 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2584 break;
2585 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002586 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002587 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002588 case CCValAssign::Indirect: {
2589 // Store the argument.
2590 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002591 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002592 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002593 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002594 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002595 Arg = SpillSlot;
2596 break;
2597 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002598 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002599
Chris Lattner423c5f42007-02-28 05:31:48 +00002600 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002601 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2602 if (isVarArg && IsWin64) {
2603 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2604 // shadow reg if callee is a varargs function.
2605 unsigned ShadowReg = 0;
2606 switch (VA.getLocReg()) {
2607 case X86::XMM0: ShadowReg = X86::RCX; break;
2608 case X86::XMM1: ShadowReg = X86::RDX; break;
2609 case X86::XMM2: ShadowReg = X86::R8; break;
2610 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002611 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002612 if (ShadowReg)
2613 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002614 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002615 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002616 assert(VA.isMemLoc());
2617 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002618 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2619 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002620 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2621 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002622 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002623 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002624
Evan Cheng32fe1032006-05-25 00:59:30 +00002625 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002627 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002628
Chris Lattner88e1fd52009-07-09 04:24:46 +00002629 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002630 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2631 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002632 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002633 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
Andrew Trickac6d9be2013-05-25 02:42:55 +00002634 DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002635 } else {
2636 // If we are tail calling and generating PIC/GOT style code load the
2637 // address of the callee into ECX. The value in ecx is used as target of
2638 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2639 // for tail calls on PIC/GOT architectures. Normally we would just put the
2640 // address of GOT into ebx and then call target@PLT. But for tail calls
2641 // ebx would be restored (since ebx is callee saved) before jumping to the
2642 // target@PLT.
2643
2644 // Note: The actual moving to ECX is done further down.
2645 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2646 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2647 !G->getGlobal()->hasProtectedVisibility())
2648 Callee = LowerGlobalAddress(Callee, DAG);
2649 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002650 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002651 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002652 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002653
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002654 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002655 // From AMD64 ABI document:
2656 // For calls that may call functions that use varargs or stdargs
2657 // (prototype-less calls or calls to functions containing ellipsis (...) in
2658 // the declaration) %al is used as hidden argument to specify the number
2659 // of SSE registers used. The contents of %al do not need to match exactly
2660 // the number of registers, but must be an ubound on the number of SSE
2661 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002662
Gordon Henriksen86737662008-01-05 16:56:59 +00002663 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002664 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002665 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2666 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2667 };
2668 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002669 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002670 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002671
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002672 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2673 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002674 }
2675
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002676 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002677 if (isTailCall) {
2678 // Force all the incoming stack arguments to be loaded from the stack
2679 // before any new outgoing arguments are stored to the stack, because the
2680 // outgoing stack slots may alias the incoming argument stack slots, and
2681 // the alias isn't otherwise explicit. This is slightly more conservative
2682 // than necessary, because it means that each store effectively depends
2683 // on every argument instead of just those arguments it would clobber.
2684 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2685
Dan Gohman475871a2008-07-27 21:46:04 +00002686 SmallVector<SDValue, 8> MemOpChains2;
2687 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002688 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002689 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002690 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2691 CCValAssign &VA = ArgLocs[i];
2692 if (VA.isRegLoc())
2693 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002694 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002695 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002696 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002697 // Create frame index.
2698 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002699 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002700 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002701 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002702
Duncan Sands276dcbd2008-03-21 09:14:45 +00002703 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002704 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002705 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002706 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002707 StackPtr = DAG.getCopyFromReg(Chain, dl,
2708 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002709 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002710 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002711
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2713 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002714 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002715 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002716 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002717 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002719 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002720 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002721 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002722 }
2723 }
2724
2725 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002727 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002728
2729 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002730 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2731 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002732 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002733 }
2734
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002735 // Build a sequence of copy-to-reg nodes chained together with token chain
2736 // and flag operands which copy the outgoing args into registers.
2737 SDValue InFlag;
2738 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2739 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2740 RegsToPass[i].second, InFlag);
2741 InFlag = Chain.getValue(1);
2742 }
2743
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002744 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2745 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2746 // In the 64-bit large code model, we have to make all calls
2747 // through a register, since the call instruction's 32-bit
2748 // pc-relative offset may not be large enough to hold the whole
2749 // address.
2750 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002751 // If the callee is a GlobalAddress node (quite common, every direct call
2752 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2753 // it.
2754
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002755 // We should use extra load for direct calls to dllimported functions in
2756 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002757 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002758 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002759 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002760 bool ExtraLoad = false;
2761 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002762
Chris Lattner48a7d022009-07-09 05:02:21 +00002763 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2764 // external symbols most go through the PLT in PIC mode. If the symbol
2765 // has hidden or protected visibility, or if it is static or local, then
2766 // we don't need to use the PLT - we can directly call it.
2767 if (Subtarget->isTargetELF() &&
2768 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002769 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002770 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002771 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002772 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002773 (!Subtarget->getTargetTriple().isMacOSX() ||
2774 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002775 // PC-relative references to external symbols should go through $stub,
2776 // unless we're building with the leopard linker or later, which
2777 // automatically synthesizes these stubs.
2778 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002779 } else if (Subtarget->isPICStyleRIPRel() &&
2780 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002781 cast<Function>(GV)->getAttributes().
2782 hasAttribute(AttributeSet::FunctionIndex,
2783 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002784 // If the function is marked as non-lazy, generate an indirect call
2785 // which loads from the GOT directly. This avoids runtime overhead
2786 // at the cost of eager binding (and one extra byte of encoding).
2787 OpFlags = X86II::MO_GOTPCREL;
2788 WrapperKind = X86ISD::WrapperRIP;
2789 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002790 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002791
Devang Patel0d881da2010-07-06 22:08:15 +00002792 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002793 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002794
2795 // Add a wrapper if needed.
2796 if (WrapperKind != ISD::DELETED_NODE)
2797 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2798 // Add extra indirection if needed.
2799 if (ExtraLoad)
2800 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2801 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002802 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002803 }
Bill Wendling056292f2008-09-16 21:48:12 +00002804 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002805 unsigned char OpFlags = 0;
2806
Evan Cheng1bf891a2010-12-01 22:59:46 +00002807 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2808 // external symbols should go through the PLT.
2809 if (Subtarget->isTargetELF() &&
2810 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2811 OpFlags = X86II::MO_PLT;
2812 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002813 (!Subtarget->getTargetTriple().isMacOSX() ||
2814 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002815 // PC-relative references to external symbols should go through $stub,
2816 // unless we're building with the leopard linker or later, which
2817 // automatically synthesizes these stubs.
2818 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002819 }
Eric Christopherfd179292009-08-27 18:07:15 +00002820
Chris Lattner48a7d022009-07-09 05:02:21 +00002821 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2822 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002823 }
2824
Chris Lattnerd96d0722007-02-25 06:40:16 +00002825 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002826 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002827 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002828
Evan Chengf22f9b32010-02-06 03:28:46 +00002829 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002830 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002831 DAG.getIntPtrConstant(0, true), InFlag, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002832 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002833 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002834
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002835 Ops.push_back(Chain);
2836 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002837
Dan Gohman98ca4f22009-08-05 01:29:28 +00002838 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002839 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002840
Gordon Henriksen86737662008-01-05 16:56:59 +00002841 // Add argument registers to the end of the list so that they are known live
2842 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002843 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2844 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2845 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002846
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002847 // Add a register mask operand representing the call-preserved registers.
2848 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2849 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2850 assert(Mask && "Missing call preserved mask for calling convention");
2851 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002852
Gabor Greifba36cb52008-08-28 21:40:38 +00002853 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002854 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002855
Dan Gohman98ca4f22009-08-05 01:29:28 +00002856 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002857 // We used to do:
2858 //// If this is the first return lowered for this function, add the regs
2859 //// to the liveout set for the function.
2860 // This isn't right, although it's probably harmless on x86; liveouts
2861 // should be computed from returns not tail calls. Consider a void
2862 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002863 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002864 }
2865
Dale Johannesenace16102009-02-03 19:33:06 +00002866 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002867 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002868
Chris Lattner2d297092006-05-23 18:50:38 +00002869 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002870 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002871 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2872 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002873 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002874 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002875 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002876 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002877 // pops the hidden struct pointer, so we have to push it back.
2878 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002879 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002880 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002881 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002882 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002883
Gordon Henriksenae636f82008-01-03 16:47:34 +00002884 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002885 if (!IsSibcall) {
2886 Chain = DAG.getCALLSEQ_END(Chain,
2887 DAG.getIntPtrConstant(NumBytes, true),
2888 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2889 true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002890 InFlag, dl);
Evan Chengf22f9b32010-02-06 03:28:46 +00002891 InFlag = Chain.getValue(1);
2892 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002893
Chris Lattner3085e152007-02-25 08:59:22 +00002894 // Handle result values, copying them out of physregs into vregs that we
2895 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002896 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2897 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002898}
2899
Evan Cheng25ab6902006-09-08 06:48:29 +00002900//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002901// Fast Calling Convention (tail call) implementation
2902//===----------------------------------------------------------------------===//
2903
2904// Like std call, callee cleans arguments, convention except that ECX is
2905// reserved for storing the tail called function address. Only 2 registers are
2906// free for argument passing (inreg). Tail call optimization is performed
2907// provided:
2908// * tailcallopt is enabled
2909// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002910// On X86_64 architecture with GOT-style position independent code only local
2911// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002912// To keep the stack aligned according to platform abi the function
2913// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2914// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002915// If a tail called function callee has more arguments than the caller the
2916// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002917// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002918// original REtADDR, but before the saved framepointer or the spilled registers
2919// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2920// stack layout:
2921// arg1
2922// arg2
2923// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002924// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002925// move area ]
2926// (possible EBP)
2927// ESI
2928// EDI
2929// local1 ..
2930
2931/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2932/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002933unsigned
2934X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2935 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002936 MachineFunction &MF = DAG.getMachineFunction();
2937 const TargetMachine &TM = MF.getTarget();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002938 const X86RegisterInfo *RegInfo =
2939 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002940 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002941 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002942 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002943 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002944 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002945 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2946 // Number smaller than 12 so just add the difference.
2947 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2948 } else {
2949 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002950 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002951 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002952 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002953 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002954}
2955
Evan Cheng5f941932010-02-05 02:21:12 +00002956/// MatchingStackOffset - Return true if the given stack call argument is
2957/// already available in the same position (relatively) of the caller's
2958/// incoming argument stack.
2959static
2960bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2961 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2962 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002963 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2964 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002965 if (Arg.getOpcode() == ISD::CopyFromReg) {
2966 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002967 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002968 return false;
2969 MachineInstr *Def = MRI->getVRegDef(VR);
2970 if (!Def)
2971 return false;
2972 if (!Flags.isByVal()) {
2973 if (!TII->isLoadFromStackSlot(Def, FI))
2974 return false;
2975 } else {
2976 unsigned Opcode = Def->getOpcode();
2977 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2978 Def->getOperand(1).isFI()) {
2979 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002980 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002981 } else
2982 return false;
2983 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002984 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2985 if (Flags.isByVal())
2986 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002987 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002988 // define @foo(%struct.X* %A) {
2989 // tail call @bar(%struct.X* byval %A)
2990 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002991 return false;
2992 SDValue Ptr = Ld->getBasePtr();
2993 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2994 if (!FINode)
2995 return false;
2996 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002997 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002998 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002999 FI = FINode->getIndex();
3000 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00003001 } else
3002 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00003003
Evan Cheng4cae1332010-03-05 08:38:04 +00003004 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00003005 if (!MFI->isFixedObjectIndex(FI))
3006 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00003007 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00003008}
3009
Dan Gohman98ca4f22009-08-05 01:29:28 +00003010/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3011/// for tail call optimization. Targets which want to do tail call
3012/// optimization should implement this function.
3013bool
3014X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003015 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003016 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00003017 bool isCalleeStructRet,
3018 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00003019 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00003020 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003021 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00003022 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003023 SelectionDAG &DAG) const {
Charles Davisac226bb2013-07-12 06:02:35 +00003024 if (!IsTailCallConvention(CalleeCC) && !IsCCallConvention(CalleeCC))
Evan Chengb1712452010-01-27 06:25:16 +00003025 return false;
3026
Evan Cheng7096ae42010-01-29 06:45:59 +00003027 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00003028 const MachineFunction &MF = DAG.getMachineFunction();
Charles Davisac226bb2013-07-12 06:02:35 +00003029 const Function *CallerF = MF.getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00003030
3031 // If the function return type is x86_fp80 and the callee return type is not,
3032 // then the FP_EXTEND of the call result is not a nop. It's not safe to
3033 // perform a tailcall optimization here.
3034 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
3035 return false;
3036
Evan Cheng13617962010-04-30 01:12:32 +00003037 CallingConv::ID CallerCC = CallerF->getCallingConv();
3038 bool CCMatch = CallerCC == CalleeCC;
Charles Davisac226bb2013-07-12 06:02:35 +00003039 bool IsCalleeWin64 = Subtarget->isCallingConvWin64(CalleeCC);
3040 bool IsCallerWin64 = Subtarget->isCallingConvWin64(CallerCC);
Evan Cheng13617962010-04-30 01:12:32 +00003041
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003042 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00003043 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00003044 return true;
3045 return false;
3046 }
3047
Dale Johannesen2f05cc02010-05-28 23:24:28 +00003048 // Look for obvious safe cases to perform tail call optimization that do not
3049 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00003050
Evan Cheng2c12cb42010-03-26 16:26:03 +00003051 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
3052 // emit a special epilogue.
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00003053 const X86RegisterInfo *RegInfo =
3054 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Evan Cheng2c12cb42010-03-26 16:26:03 +00003055 if (RegInfo->needsStackRealignment(MF))
3056 return false;
3057
Evan Chenga375d472010-03-15 18:54:48 +00003058 // Also avoid sibcall optimization if either caller or callee uses struct
3059 // return semantics.
3060 if (isCalleeStructRet || isCallerStructRet)
3061 return false;
3062
Chad Rosier2416da32011-06-24 21:15:36 +00003063 // An stdcall caller is expected to clean up its arguments; the callee
3064 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003065 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00003066 return false;
3067
Chad Rosier871f6642011-05-18 19:59:50 +00003068 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00003069 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00003070 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00003071
3072 // Optimizing for varargs on Win64 is unlikely to be safe without
3073 // additional testing.
Charles Davisac226bb2013-07-12 06:02:35 +00003074 if (IsCalleeWin64 || IsCallerWin64)
Chad Rosiera1660892011-05-20 00:59:28 +00003075 return false;
3076
Chad Rosier871f6642011-05-18 19:59:50 +00003077 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003078 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00003079 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00003080
Chad Rosier871f6642011-05-18 19:59:50 +00003081 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
3082 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
3083 if (!ArgLocs[i].isRegLoc())
3084 return false;
3085 }
3086
Chad Rosier30450e82011-12-22 22:35:21 +00003087 // If the call result is in ST0 / ST1, it needs to be popped off the x87
3088 // stack. Therefore, if it's not used by the call it is not safe to optimize
3089 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00003090 bool Unused = false;
3091 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
3092 if (!Ins[i].Used) {
3093 Unused = true;
3094 break;
3095 }
3096 }
3097 if (Unused) {
3098 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003099 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00003100 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00003101 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00003102 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00003103 CCValAssign &VA = RVLocs[i];
3104 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
3105 return false;
3106 }
3107 }
3108
Evan Cheng13617962010-04-30 01:12:32 +00003109 // If the calling conventions do not match, then we'd better make sure the
3110 // results are returned in the same way as what the caller expects.
3111 if (!CCMatch) {
3112 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00003113 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00003114 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00003115 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
3116
3117 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00003118 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00003119 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00003120 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
3121
3122 if (RVLocs1.size() != RVLocs2.size())
3123 return false;
3124 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
3125 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
3126 return false;
3127 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
3128 return false;
3129 if (RVLocs1[i].isRegLoc()) {
3130 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
3131 return false;
3132 } else {
3133 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
3134 return false;
3135 }
3136 }
3137 }
3138
Evan Chenga6bff982010-01-30 01:22:00 +00003139 // If the callee takes no arguments then go on to check the results of the
3140 // call.
3141 if (!Outs.empty()) {
3142 // Check if stack adjustment is needed. For now, do not do this if any
3143 // argument is passed on the stack.
3144 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003145 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00003146 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00003147
3148 // Allocate shadow area for Win64
Charles Davisac226bb2013-07-12 06:02:35 +00003149 if (IsCalleeWin64)
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00003150 CCInfo.AllocateStack(32, 8);
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00003151
Duncan Sands45907662010-10-31 13:21:44 +00003152 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00003153 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00003154 MachineFunction &MF = DAG.getMachineFunction();
3155 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
3156 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00003157
3158 // Check if the arguments are already laid out in the right way as
3159 // the caller's fixed stack objects.
3160 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00003161 const MachineRegisterInfo *MRI = &MF.getRegInfo();
3162 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00003163 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00003164 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3165 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00003166 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00003167 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00003168 if (VA.getLocInfo() == CCValAssign::Indirect)
3169 return false;
3170 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00003171 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
3172 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00003173 return false;
3174 }
3175 }
3176 }
Evan Cheng9c044672010-05-29 01:35:22 +00003177
3178 // If the tailcall address may be in a register, then make sure it's
3179 // possible to register allocate for it. In 32-bit, the call address can
3180 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00003181 // callee-saved registers are restored. These happen to be the same
3182 // registers used to pass 'inreg' arguments so watch out for those.
3183 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003184 ((!isa<GlobalAddressSDNode>(Callee) &&
3185 !isa<ExternalSymbolSDNode>(Callee)) ||
3186 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00003187 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003188 // In PIC we need an extra register to formulate the address computation
3189 // for the callee.
3190 unsigned MaxInRegs =
3191 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
3192
Evan Cheng9c044672010-05-29 01:35:22 +00003193 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3194 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00003195 if (!VA.isRegLoc())
3196 continue;
3197 unsigned Reg = VA.getLocReg();
3198 switch (Reg) {
3199 default: break;
3200 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003201 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00003202 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00003203 break;
Evan Cheng9c044672010-05-29 01:35:22 +00003204 }
3205 }
3206 }
Evan Chenga6bff982010-01-30 01:22:00 +00003207 }
Evan Chengb1712452010-01-27 06:25:16 +00003208
Evan Cheng86809cc2010-02-03 03:28:02 +00003209 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003210}
3211
Dan Gohman3df24e62008-09-03 23:12:08 +00003212FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003213X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3214 const TargetLibraryInfo *libInfo) const {
3215 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003216}
3217
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003218//===----------------------------------------------------------------------===//
3219// Other Lowering Hooks
3220//===----------------------------------------------------------------------===//
3221
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003222static bool MayFoldLoad(SDValue Op) {
3223 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3224}
3225
3226static bool MayFoldIntoStore(SDValue Op) {
3227 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3228}
3229
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003230static bool isTargetShuffle(unsigned Opcode) {
3231 switch(Opcode) {
3232 default: return false;
3233 case X86ISD::PSHUFD:
3234 case X86ISD::PSHUFHW:
3235 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003236 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003237 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003238 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003239 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003240 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003241 case X86ISD::MOVLPS:
3242 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003243 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003244 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003245 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003246 case X86ISD::MOVSS:
3247 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003248 case X86ISD::UNPCKL:
3249 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003250 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003251 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003252 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003253 return true;
3254 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003255}
3256
Andrew Trickac6d9be2013-05-25 02:42:55 +00003257static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003258 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003259 switch(Opc) {
3260 default: llvm_unreachable("Unknown x86 shuffle node");
3261 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003262 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003263 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003264 return DAG.getNode(Opc, dl, VT, V1);
3265 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003266}
3267
Andrew Trickac6d9be2013-05-25 02:42:55 +00003268static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003269 SDValue V1, unsigned TargetMask,
3270 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003271 switch(Opc) {
3272 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003273 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003274 case X86ISD::PSHUFHW:
3275 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003276 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003277 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003278 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3279 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003280}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003281
Andrew Trickac6d9be2013-05-25 02:42:55 +00003282static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003283 SDValue V1, SDValue V2, unsigned TargetMask,
3284 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003285 switch(Opc) {
3286 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003287 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003288 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003289 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003290 return DAG.getNode(Opc, dl, VT, V1, V2,
3291 DAG.getConstant(TargetMask, MVT::i8));
3292 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003293}
3294
Andrew Trickac6d9be2013-05-25 02:42:55 +00003295static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003296 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3297 switch(Opc) {
3298 default: llvm_unreachable("Unknown x86 shuffle node");
3299 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003300 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003301 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003302 case X86ISD::MOVLPS:
3303 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003304 case X86ISD::MOVSS:
3305 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003306 case X86ISD::UNPCKL:
3307 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003308 return DAG.getNode(Opc, dl, VT, V1, V2);
3309 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003310}
3311
Dan Gohmand858e902010-04-17 15:26:15 +00003312SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003313 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00003314 const X86RegisterInfo *RegInfo =
3315 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003316 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3317 int ReturnAddrIndex = FuncInfo->getRAIndex();
3318
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003319 if (ReturnAddrIndex == 0) {
3320 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003321 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003322 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003323 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003324 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003325 }
3326
Evan Cheng25ab6902006-09-08 06:48:29 +00003327 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003328}
3329
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003330bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3331 bool hasSymbolicDisplacement) {
3332 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003333 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003334 return false;
3335
3336 // If we don't have a symbolic displacement - we don't have any extra
3337 // restrictions.
3338 if (!hasSymbolicDisplacement)
3339 return true;
3340
3341 // FIXME: Some tweaks might be needed for medium code model.
3342 if (M != CodeModel::Small && M != CodeModel::Kernel)
3343 return false;
3344
3345 // For small code model we assume that latest object is 16MB before end of 31
3346 // bits boundary. We may also accept pretty large negative constants knowing
3347 // that all objects are in the positive half of address space.
3348 if (M == CodeModel::Small && Offset < 16*1024*1024)
3349 return true;
3350
3351 // For kernel code model we know that all object resist in the negative half
3352 // of 32bits address space. We may not accept negative offsets, since they may
3353 // be just off and we may accept pretty large positive ones.
3354 if (M == CodeModel::Kernel && Offset > 0)
3355 return true;
3356
3357 return false;
3358}
3359
Evan Chengef41ff62011-06-23 17:54:54 +00003360/// isCalleePop - Determines whether the callee is required to pop its
3361/// own arguments. Callee pop is necessary to support tail calls.
3362bool X86::isCalleePop(CallingConv::ID CallingConv,
3363 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3364 if (IsVarArg)
3365 return false;
3366
3367 switch (CallingConv) {
3368 default:
3369 return false;
3370 case CallingConv::X86_StdCall:
3371 return !is64Bit;
3372 case CallingConv::X86_FastCall:
3373 return !is64Bit;
3374 case CallingConv::X86_ThisCall:
3375 return !is64Bit;
3376 case CallingConv::Fast:
3377 return TailCallOpt;
3378 case CallingConv::GHC:
3379 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003380 case CallingConv::HiPE:
3381 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003382 }
3383}
3384
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003385/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3386/// specific condition code, returning the condition code and the LHS/RHS of the
3387/// comparison to make.
3388static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3389 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003390 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003391 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3392 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3393 // X > -1 -> X == 0, jump !sign.
3394 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003395 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003396 }
3397 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003398 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003399 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003400 }
3401 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003402 // X < 1 -> X <= 0
3403 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003404 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003405 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003406 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003407
Evan Chengd9558e02006-01-06 00:43:03 +00003408 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003409 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003410 case ISD::SETEQ: return X86::COND_E;
3411 case ISD::SETGT: return X86::COND_G;
3412 case ISD::SETGE: return X86::COND_GE;
3413 case ISD::SETLT: return X86::COND_L;
3414 case ISD::SETLE: return X86::COND_LE;
3415 case ISD::SETNE: return X86::COND_NE;
3416 case ISD::SETULT: return X86::COND_B;
3417 case ISD::SETUGT: return X86::COND_A;
3418 case ISD::SETULE: return X86::COND_BE;
3419 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003420 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003421 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003422
Chris Lattner4c78e022008-12-23 23:42:27 +00003423 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003424
Chris Lattner4c78e022008-12-23 23:42:27 +00003425 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003426 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3427 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003428 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3429 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003430 }
3431
Chris Lattner4c78e022008-12-23 23:42:27 +00003432 switch (SetCCOpcode) {
3433 default: break;
3434 case ISD::SETOLT:
3435 case ISD::SETOLE:
3436 case ISD::SETUGT:
3437 case ISD::SETUGE:
3438 std::swap(LHS, RHS);
3439 break;
3440 }
3441
3442 // On a floating point condition, the flags are set as follows:
3443 // ZF PF CF op
3444 // 0 | 0 | 0 | X > Y
3445 // 0 | 0 | 1 | X < Y
3446 // 1 | 0 | 0 | X == Y
3447 // 1 | 1 | 1 | unordered
3448 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003449 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003450 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003451 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003452 case ISD::SETOLT: // flipped
3453 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003454 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003455 case ISD::SETOLE: // flipped
3456 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003457 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003458 case ISD::SETUGT: // flipped
3459 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003460 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003461 case ISD::SETUGE: // flipped
3462 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003463 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003464 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003465 case ISD::SETNE: return X86::COND_NE;
3466 case ISD::SETUO: return X86::COND_P;
3467 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003468 case ISD::SETOEQ:
3469 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003470 }
Evan Chengd9558e02006-01-06 00:43:03 +00003471}
3472
Evan Cheng4a460802006-01-11 00:33:36 +00003473/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3474/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003475/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003476static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003477 switch (X86CC) {
3478 default:
3479 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003480 case X86::COND_B:
3481 case X86::COND_BE:
3482 case X86::COND_E:
3483 case X86::COND_P:
3484 case X86::COND_A:
3485 case X86::COND_AE:
3486 case X86::COND_NE:
3487 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003488 return true;
3489 }
3490}
3491
Evan Chengeb2f9692009-10-27 19:56:55 +00003492/// isFPImmLegal - Returns true if the target can instruction select the
3493/// specified FP immediate natively. If false, the legalizer will
3494/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003495bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003496 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3497 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3498 return true;
3499 }
3500 return false;
3501}
3502
Nate Begeman9008ca62009-04-27 18:41:29 +00003503/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3504/// the specified range (L, H].
3505static bool isUndefOrInRange(int Val, int Low, int Hi) {
3506 return (Val < 0) || (Val >= Low && Val < Hi);
3507}
3508
3509/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3510/// specified value.
3511static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003512 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003513}
3514
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003515/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003516/// from position Pos and ending in Pos+Size, falls within the specified
3517/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003518static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003519 unsigned Pos, unsigned Size, int Low) {
3520 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003521 if (!isUndefOrEqual(Mask[i], Low))
3522 return false;
3523 return true;
3524}
3525
Nate Begeman9008ca62009-04-27 18:41:29 +00003526/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3527/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3528/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003529static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003530 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003531 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003533 return (Mask[0] < 2 && Mask[1] < 2);
3534 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003535}
3536
Nate Begeman9008ca62009-04-27 18:41:29 +00003537/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3538/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003539static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3540 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003541 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003542
Nate Begeman9008ca62009-04-27 18:41:29 +00003543 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003544 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3545 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003546
Evan Cheng506d3df2006-03-29 23:07:14 +00003547 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003548 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003549 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003550 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003551
Craig Toppera9a568a2012-05-02 08:03:44 +00003552 if (VT == MVT::v16i16) {
3553 // Lower quadword copied in order or undef.
3554 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3555 return false;
3556
3557 // Upper quadword shuffled.
3558 for (unsigned i = 12; i != 16; ++i)
3559 if (!isUndefOrInRange(Mask[i], 12, 16))
3560 return false;
3561 }
3562
Evan Cheng506d3df2006-03-29 23:07:14 +00003563 return true;
3564}
3565
Nate Begeman9008ca62009-04-27 18:41:29 +00003566/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3567/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003568static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3569 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003570 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003571
Rafael Espindola15684b22009-04-24 12:40:33 +00003572 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003573 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3574 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003575
Rafael Espindola15684b22009-04-24 12:40:33 +00003576 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003577 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003578 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003579 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003580
Craig Toppera9a568a2012-05-02 08:03:44 +00003581 if (VT == MVT::v16i16) {
3582 // Upper quadword copied in order.
3583 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3584 return false;
3585
3586 // Lower quadword shuffled.
3587 for (unsigned i = 8; i != 12; ++i)
3588 if (!isUndefOrInRange(Mask[i], 8, 12))
3589 return false;
3590 }
3591
Rafael Espindola15684b22009-04-24 12:40:33 +00003592 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003593}
3594
Nate Begemana09008b2009-10-19 02:17:23 +00003595/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3596/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003597static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3598 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003599 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3600 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003601 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003602
Craig Topper0e2037b2012-01-20 05:53:00 +00003603 unsigned NumElts = VT.getVectorNumElements();
3604 unsigned NumLanes = VT.getSizeInBits()/128;
3605 unsigned NumLaneElts = NumElts/NumLanes;
3606
3607 // Do not handle 64-bit element shuffles with palignr.
3608 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003609 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003610
Craig Topper0e2037b2012-01-20 05:53:00 +00003611 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3612 unsigned i;
3613 for (i = 0; i != NumLaneElts; ++i) {
3614 if (Mask[i+l] >= 0)
3615 break;
3616 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003617
Craig Topper0e2037b2012-01-20 05:53:00 +00003618 // Lane is all undef, go to next lane
3619 if (i == NumLaneElts)
3620 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003621
Craig Topper0e2037b2012-01-20 05:53:00 +00003622 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003623
Craig Topper0e2037b2012-01-20 05:53:00 +00003624 // Make sure its in this lane in one of the sources
3625 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3626 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003627 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003628
3629 // If not lane 0, then we must match lane 0
3630 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3631 return false;
3632
3633 // Correct second source to be contiguous with first source
3634 if (Start >= (int)NumElts)
3635 Start -= NumElts - NumLaneElts;
3636
3637 // Make sure we're shifting in the right direction.
3638 if (Start <= (int)(i+l))
3639 return false;
3640
3641 Start -= i;
3642
3643 // Check the rest of the elements to see if they are consecutive.
3644 for (++i; i != NumLaneElts; ++i) {
3645 int Idx = Mask[i+l];
3646
3647 // Make sure its in this lane
3648 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3649 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3650 return false;
3651
3652 // If not lane 0, then we must match lane 0
3653 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3654 return false;
3655
3656 if (Idx >= (int)NumElts)
3657 Idx -= NumElts - NumLaneElts;
3658
3659 if (!isUndefOrEqual(Idx, Start+i))
3660 return false;
3661
3662 }
Nate Begemana09008b2009-10-19 02:17:23 +00003663 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003664
Nate Begemana09008b2009-10-19 02:17:23 +00003665 return true;
3666}
3667
Craig Topper1a7700a2012-01-19 08:19:12 +00003668/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3669/// the two vector operands have swapped position.
3670static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3671 unsigned NumElems) {
3672 for (unsigned i = 0; i != NumElems; ++i) {
3673 int idx = Mask[i];
3674 if (idx < 0)
3675 continue;
3676 else if (idx < (int)NumElems)
3677 Mask[i] = idx + NumElems;
3678 else
3679 Mask[i] = idx - NumElems;
3680 }
3681}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003682
Craig Topper1a7700a2012-01-19 08:19:12 +00003683/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3684/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3685/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3686/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003687static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003688 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003689 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003690 return false;
3691
Craig Topper1a7700a2012-01-19 08:19:12 +00003692 unsigned NumElems = VT.getVectorNumElements();
3693 unsigned NumLanes = VT.getSizeInBits()/128;
3694 unsigned NumLaneElems = NumElems/NumLanes;
3695
3696 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003697 return false;
3698
3699 // VSHUFPSY divides the resulting vector into 4 chunks.
3700 // The sources are also splitted into 4 chunks, and each destination
3701 // chunk must come from a different source chunk.
3702 //
3703 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3704 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3705 //
3706 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3707 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3708 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003709 // VSHUFPDY divides the resulting vector into 4 chunks.
3710 // The sources are also splitted into 4 chunks, and each destination
3711 // chunk must come from a different source chunk.
3712 //
3713 // SRC1 => X3 X2 X1 X0
3714 // SRC2 => Y3 Y2 Y1 Y0
3715 //
3716 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3717 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003718 unsigned HalfLaneElems = NumLaneElems/2;
3719 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3720 for (unsigned i = 0; i != NumLaneElems; ++i) {
3721 int Idx = Mask[i+l];
3722 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3723 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3724 return false;
3725 // For VSHUFPSY, the mask of the second half must be the same as the
3726 // first but with the appropriate offsets. This works in the same way as
3727 // VPERMILPS works with masks.
3728 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3729 continue;
3730 if (!isUndefOrEqual(Idx, Mask[i]+l))
3731 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003732 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003733 }
3734
3735 return true;
3736}
3737
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003738/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3739/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003740static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003741 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003742 return false;
3743
Craig Topper7a9a28b2012-08-12 02:23:29 +00003744 unsigned NumElems = VT.getVectorNumElements();
3745
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003746 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003747 return false;
3748
Evan Cheng2064a2b2006-03-28 06:50:32 +00003749 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003750 return isUndefOrEqual(Mask[0], 6) &&
3751 isUndefOrEqual(Mask[1], 7) &&
3752 isUndefOrEqual(Mask[2], 2) &&
3753 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003754}
3755
Nate Begeman0b10b912009-11-07 23:17:15 +00003756/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3757/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3758/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003759static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003760 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003761 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003762
Craig Topper7a9a28b2012-08-12 02:23:29 +00003763 unsigned NumElems = VT.getVectorNumElements();
3764
Nate Begeman0b10b912009-11-07 23:17:15 +00003765 if (NumElems != 4)
3766 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003767
Craig Topperdd637ae2012-02-19 05:41:45 +00003768 return isUndefOrEqual(Mask[0], 2) &&
3769 isUndefOrEqual(Mask[1], 3) &&
3770 isUndefOrEqual(Mask[2], 2) &&
3771 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003772}
3773
Evan Cheng5ced1d82006-04-06 23:23:56 +00003774/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3775/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003776static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003777 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003778 return false;
3779
Craig Topperdd637ae2012-02-19 05:41:45 +00003780 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003781
Evan Cheng5ced1d82006-04-06 23:23:56 +00003782 if (NumElems != 2 && NumElems != 4)
3783 return false;
3784
Chad Rosier238ae312012-04-30 17:47:15 +00003785 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003786 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003787 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003788
Chad Rosier238ae312012-04-30 17:47:15 +00003789 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003790 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003791 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003792
3793 return true;
3794}
3795
Nate Begeman0b10b912009-11-07 23:17:15 +00003796/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3797/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003798static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003799 if (!VT.is128BitVector())
3800 return false;
3801
Craig Topperdd637ae2012-02-19 05:41:45 +00003802 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003803
Craig Topper7a9a28b2012-08-12 02:23:29 +00003804 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003805 return false;
3806
Chad Rosier238ae312012-04-30 17:47:15 +00003807 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003808 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003809 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003810
Chad Rosier238ae312012-04-30 17:47:15 +00003811 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3812 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003813 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003814
3815 return true;
3816}
3817
Elena Demikhovsky15963732012-06-26 08:04:10 +00003818//
3819// Some special combinations that can be optimized.
3820//
3821static
3822SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3823 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003824 MVT VT = SVOp->getValueType(0).getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003825 SDLoc dl(SVOp);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003826
3827 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3828 return SDValue();
3829
3830 ArrayRef<int> Mask = SVOp->getMask();
3831
3832 // These are the special masks that may be optimized.
3833 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3834 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3835 bool MatchEvenMask = true;
3836 bool MatchOddMask = true;
3837 for (int i=0; i<8; ++i) {
3838 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3839 MatchEvenMask = false;
3840 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3841 MatchOddMask = false;
3842 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003843
Elena Demikhovsky32510202012-09-04 12:49:02 +00003844 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003845 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003846
Elena Demikhovsky15963732012-06-26 08:04:10 +00003847 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3848
Elena Demikhovsky32510202012-09-04 12:49:02 +00003849 SDValue Op0 = SVOp->getOperand(0);
3850 SDValue Op1 = SVOp->getOperand(1);
3851
3852 if (MatchEvenMask) {
3853 // Shift the second operand right to 32 bits.
3854 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3855 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3856 } else {
3857 // Shift the first operand left to 32 bits.
3858 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3859 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3860 }
3861 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3862 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003863}
3864
Evan Cheng0038e592006-03-28 00:39:58 +00003865/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3866/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003867static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003868 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003869 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003870
3871 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3872 "Unsupported vector type for unpckh");
3873
Craig Topper5a529e42013-01-18 06:44:29 +00003874 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003875 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003876 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003877
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003878 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3879 // independently on 128-bit lanes.
3880 unsigned NumLanes = VT.getSizeInBits()/128;
3881 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003882
Craig Topper94438ba2011-12-16 08:06:31 +00003883 for (unsigned l = 0; l != NumLanes; ++l) {
3884 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3885 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003886 i += 2, ++j) {
3887 int BitI = Mask[i];
3888 int BitI1 = Mask[i+1];
3889 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003890 return false;
David Greenea20244d2011-03-02 17:23:43 +00003891 if (V2IsSplat) {
3892 if (!isUndefOrEqual(BitI1, NumElts))
3893 return false;
3894 } else {
3895 if (!isUndefOrEqual(BitI1, j + NumElts))
3896 return false;
3897 }
Evan Cheng39623da2006-04-20 08:58:49 +00003898 }
Evan Cheng0038e592006-03-28 00:39:58 +00003899 }
David Greenea20244d2011-03-02 17:23:43 +00003900
Evan Cheng0038e592006-03-28 00:39:58 +00003901 return true;
3902}
3903
Evan Cheng4fcb9222006-03-28 02:43:26 +00003904/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3905/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003906static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003907 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003908 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003909
3910 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3911 "Unsupported vector type for unpckh");
3912
Craig Topper5a529e42013-01-18 06:44:29 +00003913 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003914 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003915 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003916
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003917 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3918 // independently on 128-bit lanes.
3919 unsigned NumLanes = VT.getSizeInBits()/128;
3920 unsigned NumLaneElts = NumElts/NumLanes;
3921
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003922 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003923 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3924 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003925 int BitI = Mask[i];
3926 int BitI1 = Mask[i+1];
3927 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003928 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003929 if (V2IsSplat) {
3930 if (isUndefOrEqual(BitI1, NumElts))
3931 return false;
3932 } else {
3933 if (!isUndefOrEqual(BitI1, j+NumElts))
3934 return false;
3935 }
Evan Cheng39623da2006-04-20 08:58:49 +00003936 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003937 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003938 return true;
3939}
3940
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003941/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3942/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3943/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003944static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003945 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003946 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003947
3948 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3949 "Unsupported vector type for unpckh");
3950
Craig Topper5a529e42013-01-18 06:44:29 +00003951 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003952 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003953 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003954
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003955 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3956 // FIXME: Need a better way to get rid of this, there's no latency difference
3957 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3958 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003959 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003960 return false;
3961
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003962 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3963 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003964 unsigned NumLanes = VT.getSizeInBits()/128;
3965 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003966
Craig Topper94438ba2011-12-16 08:06:31 +00003967 for (unsigned l = 0; l != NumLanes; ++l) {
3968 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3969 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003970 i += 2, ++j) {
3971 int BitI = Mask[i];
3972 int BitI1 = Mask[i+1];
3973
3974 if (!isUndefOrEqual(BitI, j))
3975 return false;
3976 if (!isUndefOrEqual(BitI1, j))
3977 return false;
3978 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003979 }
David Greenea20244d2011-03-02 17:23:43 +00003980
Rafael Espindola15684b22009-04-24 12:40:33 +00003981 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003982}
3983
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003984/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3985/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3986/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003987static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003988 unsigned NumElts = VT.getVectorNumElements();
3989
3990 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3991 "Unsupported vector type for unpckh");
3992
Craig Topper5a529e42013-01-18 06:44:29 +00003993 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003994 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003995 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003996
Craig Topper94438ba2011-12-16 08:06:31 +00003997 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3998 // independently on 128-bit lanes.
3999 unsigned NumLanes = VT.getSizeInBits()/128;
4000 unsigned NumLaneElts = NumElts/NumLanes;
4001
4002 for (unsigned l = 0; l != NumLanes; ++l) {
4003 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
4004 i != (l+1)*NumLaneElts; i += 2, ++j) {
4005 int BitI = Mask[i];
4006 int BitI1 = Mask[i+1];
4007 if (!isUndefOrEqual(BitI, j))
4008 return false;
4009 if (!isUndefOrEqual(BitI1, j))
4010 return false;
4011 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004012 }
Rafael Espindola15684b22009-04-24 12:40:33 +00004013 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00004014}
4015
Evan Cheng017dcc62006-04-21 01:05:10 +00004016/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
4017/// specifies a shuffle of elements that is suitable for input to MOVSS,
4018/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004019static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00004020 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00004021 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00004022 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00004023 return false;
Eli Friedman10415532009-06-06 06:05:10 +00004024
Craig Topperc612d792012-01-02 09:17:37 +00004025 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004026
Nate Begeman9008ca62009-04-27 18:41:29 +00004027 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00004028 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00004029
Craig Topperc612d792012-01-02 09:17:37 +00004030 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004031 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00004032 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00004033
Evan Chengd6d1cbd2006-04-11 00:19:04 +00004034 return true;
4035}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00004036
Craig Topper70b883b2011-11-28 10:14:51 +00004037/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004038/// as permutations between 128-bit chunks or halves. As an example: this
4039/// shuffle bellow:
4040/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
4041/// The first half comes from the second half of V1 and the second half from the
4042/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004043static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4044 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004045 return false;
4046
4047 // The shuffle result is divided into half A and half B. In total the two
4048 // sources have 4 halves, namely: C, D, E, F. The final values of A and
4049 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00004050 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004051 bool MatchA = false, MatchB = false;
4052
4053 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00004054 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004055 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
4056 MatchA = true;
4057 break;
4058 }
4059 }
4060
4061 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00004062 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004063 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
4064 MatchB = true;
4065 break;
4066 }
4067 }
4068
4069 return MatchA && MatchB;
4070}
4071
Craig Topper70b883b2011-11-28 10:14:51 +00004072/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
4073/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00004074static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004075 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004076
Craig Topperc612d792012-01-02 09:17:37 +00004077 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004078
Craig Topperc612d792012-01-02 09:17:37 +00004079 unsigned FstHalf = 0, SndHalf = 0;
4080 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004081 if (SVOp->getMaskElt(i) > 0) {
4082 FstHalf = SVOp->getMaskElt(i)/HalfSize;
4083 break;
4084 }
4085 }
Craig Topperc612d792012-01-02 09:17:37 +00004086 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00004087 if (SVOp->getMaskElt(i) > 0) {
4088 SndHalf = SVOp->getMaskElt(i)/HalfSize;
4089 break;
4090 }
4091 }
4092
4093 return (FstHalf | (SndHalf << 4));
4094}
4095
Craig Topper70b883b2011-11-28 10:14:51 +00004096/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004097/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
4098/// Note that VPERMIL mask matching is different depending whether theunderlying
4099/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
4100/// to the same elements of the low, but to the higher half of the source.
4101/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00004102/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004103static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4104 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004105 return false;
4106
Craig Topperc612d792012-01-02 09:17:37 +00004107 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00004108 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00004109 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004110 return false;
4111
Craig Topperc612d792012-01-02 09:17:37 +00004112 unsigned NumLanes = VT.getSizeInBits()/128;
4113 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00004114 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00004115 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00004116 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00004117 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00004118 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00004119 continue;
4120 // VPERMILPS handling
4121 if (Mask[i] < 0)
4122 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00004123 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00004124 return false;
4125 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00004126 }
4127
4128 return true;
4129}
4130
Craig Topper5aaffa82012-02-19 02:53:47 +00004131/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00004132/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00004133/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004134static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004135 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004136 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00004137 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00004138
4139 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00004140 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00004141 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00004142
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00004144 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00004145
Craig Topperc612d792012-01-02 09:17:37 +00004146 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004147 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
4148 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
4149 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00004150 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00004151
Evan Cheng39623da2006-04-20 08:58:49 +00004152 return true;
4153}
4154
Evan Chengd9539472006-04-14 21:59:03 +00004155/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
4156/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004157/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00004158static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00004159 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00004160 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00004161 return false;
4162
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004163 unsigned NumElems = VT.getVectorNumElements();
4164
Craig Topper5a529e42013-01-18 06:44:29 +00004165 if ((VT.is128BitVector() && NumElems != 4) ||
4166 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004167 return false;
4168
4169 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00004170 for (unsigned i = 0; i != NumElems; i += 2)
4171 if (!isUndefOrEqual(Mask[i], i+1) ||
4172 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004174
4175 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004176}
4177
4178/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
4179/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004180/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00004181static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00004182 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00004183 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00004184 return false;
4185
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004186 unsigned NumElems = VT.getVectorNumElements();
4187
Craig Topper5a529e42013-01-18 06:44:29 +00004188 if ((VT.is128BitVector() && NumElems != 4) ||
4189 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004190 return false;
4191
4192 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00004193 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00004194 if (!isUndefOrEqual(Mask[i], i) ||
4195 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004196 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004197
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004198 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004199}
4200
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004201/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4202/// specifies a shuffle of elements that is suitable for input to 256-bit
4203/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004204static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4205 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00004206 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004207
Craig Topper7a9a28b2012-08-12 02:23:29 +00004208 unsigned NumElts = VT.getVectorNumElements();
4209 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004210 return false;
4211
Craig Topperc612d792012-01-02 09:17:37 +00004212 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004213 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004214 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004215 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004216 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004217 return false;
4218 return true;
4219}
4220
Evan Cheng0b457f02008-09-25 20:50:48 +00004221/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004222/// specifies a shuffle of elements that is suitable for input to 128-bit
4223/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004224static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004225 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004226 return false;
4227
Craig Topperc612d792012-01-02 09:17:37 +00004228 unsigned e = VT.getVectorNumElements() / 2;
4229 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004230 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004231 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004232 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004233 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004234 return false;
4235 return true;
4236}
4237
Elena Demikhovsky83952512013-07-31 11:35:14 +00004238/// isVEXTRACTIndex - Return true if the specified
David Greenec38a03e2011-02-03 15:50:00 +00004239/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
Elena Demikhovsky83952512013-07-31 11:35:14 +00004240/// suitable for instruction that extract 128 or 256 bit vectors
4241static bool isVEXTRACTIndex(SDNode *N, unsigned vecWidth) {
4242 assert((vecWidth == 128 || vecWidth == 256) && "Unexpected vector width");
David Greenec38a03e2011-02-03 15:50:00 +00004243 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4244 return false;
4245
Elena Demikhovsky83952512013-07-31 11:35:14 +00004246 // The index should be aligned on a vecWidth-bit boundary.
David Greenec38a03e2011-02-03 15:50:00 +00004247 uint64_t Index =
4248 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4249
Craig Topper5141d972013-01-18 08:41:28 +00004250 MVT VT = N->getValueType(0).getSimpleVT();
4251 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
Elena Demikhovsky83952512013-07-31 11:35:14 +00004252 bool Result = (Index * ElSize) % vecWidth == 0;
David Greenec38a03e2011-02-03 15:50:00 +00004253
4254 return Result;
4255}
4256
Elena Demikhovsky83952512013-07-31 11:35:14 +00004257/// isVINSERTIndex - Return true if the specified INSERT_SUBVECTOR
David Greeneccacdc12011-02-04 16:08:29 +00004258/// operand specifies a subvector insert that is suitable for input to
Elena Demikhovsky83952512013-07-31 11:35:14 +00004259/// insertion of 128 or 256-bit subvectors
4260static bool isVINSERTIndex(SDNode *N, unsigned vecWidth) {
4261 assert((vecWidth == 128 || vecWidth == 256) && "Unexpected vector width");
David Greeneccacdc12011-02-04 16:08:29 +00004262 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4263 return false;
Elena Demikhovsky83952512013-07-31 11:35:14 +00004264 // The index should be aligned on a vecWidth-bit boundary.
David Greeneccacdc12011-02-04 16:08:29 +00004265 uint64_t Index =
4266 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4267
Craig Topper5141d972013-01-18 08:41:28 +00004268 MVT VT = N->getValueType(0).getSimpleVT();
4269 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
Elena Demikhovsky83952512013-07-31 11:35:14 +00004270 bool Result = (Index * ElSize) % vecWidth == 0;
David Greeneccacdc12011-02-04 16:08:29 +00004271
4272 return Result;
4273}
4274
Elena Demikhovsky83952512013-07-31 11:35:14 +00004275bool X86::isVINSERT128Index(SDNode *N) {
4276 return isVINSERTIndex(N, 128);
4277}
4278
4279bool X86::isVINSERT256Index(SDNode *N) {
4280 return isVINSERTIndex(N, 256);
4281}
4282
4283bool X86::isVEXTRACT128Index(SDNode *N) {
4284 return isVEXTRACTIndex(N, 128);
4285}
4286
4287bool X86::isVEXTRACT256Index(SDNode *N) {
4288 return isVEXTRACTIndex(N, 256);
4289}
4290
Evan Cheng63d33002006-03-22 08:01:21 +00004291/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004292/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004293/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004294static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004295 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004296
Craig Topper1a7700a2012-01-19 08:19:12 +00004297 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4298 "Unsupported vector type for PSHUF/SHUFP");
4299
4300 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4301 // independently on 128-bit lanes.
4302 unsigned NumElts = VT.getVectorNumElements();
4303 unsigned NumLanes = VT.getSizeInBits()/128;
4304 unsigned NumLaneElts = NumElts/NumLanes;
4305
4306 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4307 "Only supports 2 or 4 elements per lane");
4308
4309 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004310 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004311 for (unsigned i = 0; i != NumElts; ++i) {
4312 int Elt = N->getMaskElt(i);
4313 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004314 Elt &= NumLaneElts - 1;
4315 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004316 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004317 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004318
Evan Cheng63d33002006-03-22 08:01:21 +00004319 return Mask;
4320}
4321
Evan Cheng506d3df2006-03-29 23:07:14 +00004322/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004323/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004324static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004325 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004326
4327 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4328 "Unsupported vector type for PSHUFHW");
4329
4330 unsigned NumElts = VT.getVectorNumElements();
4331
Evan Cheng506d3df2006-03-29 23:07:14 +00004332 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004333 for (unsigned l = 0; l != NumElts; l += 8) {
4334 // 8 nodes per lane, but we only care about the last 4.
4335 for (unsigned i = 0; i < 4; ++i) {
4336 int Elt = N->getMaskElt(l+i+4);
4337 if (Elt < 0) continue;
4338 Elt &= 0x3; // only 2-bits.
4339 Mask |= Elt << (i * 2);
4340 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004341 }
Craig Topper6b28d352012-05-03 07:12:59 +00004342
Evan Cheng506d3df2006-03-29 23:07:14 +00004343 return Mask;
4344}
4345
4346/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004347/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004348static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004349 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004350
4351 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4352 "Unsupported vector type for PSHUFHW");
4353
4354 unsigned NumElts = VT.getVectorNumElements();
4355
Evan Cheng506d3df2006-03-29 23:07:14 +00004356 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004357 for (unsigned l = 0; l != NumElts; l += 8) {
4358 // 8 nodes per lane, but we only care about the first 4.
4359 for (unsigned i = 0; i < 4; ++i) {
4360 int Elt = N->getMaskElt(l+i);
4361 if (Elt < 0) continue;
4362 Elt &= 0x3; // only 2-bits
4363 Mask |= Elt << (i * 2);
4364 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004365 }
Craig Topper6b28d352012-05-03 07:12:59 +00004366
Evan Cheng506d3df2006-03-29 23:07:14 +00004367 return Mask;
4368}
4369
Nate Begemana09008b2009-10-19 02:17:23 +00004370/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4371/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004372static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004373 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004374 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004375
Craig Topper0e2037b2012-01-20 05:53:00 +00004376 unsigned NumElts = VT.getVectorNumElements();
4377 unsigned NumLanes = VT.getSizeInBits()/128;
4378 unsigned NumLaneElts = NumElts/NumLanes;
4379
4380 int Val = 0;
4381 unsigned i;
4382 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004383 Val = SVOp->getMaskElt(i);
4384 if (Val >= 0)
4385 break;
4386 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004387 if (Val >= (int)NumElts)
4388 Val -= NumElts - NumLaneElts;
4389
Eli Friedman63f8dde2011-07-25 21:36:45 +00004390 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004391 return (Val - i) * EltSize;
4392}
4393
Elena Demikhovsky83952512013-07-31 11:35:14 +00004394static unsigned getExtractVEXTRACTImmediate(SDNode *N, unsigned vecWidth) {
4395 assert((vecWidth == 128 || vecWidth == 256) && "Unsupported vector width");
David Greenec38a03e2011-02-03 15:50:00 +00004396 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
Elena Demikhovsky83952512013-07-31 11:35:14 +00004397 llvm_unreachable("Illegal extract subvector for VEXTRACT");
David Greenec38a03e2011-02-03 15:50:00 +00004398
4399 uint64_t Index =
4400 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4401
Craig Toppercfcab212013-01-19 08:27:45 +00004402 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4403 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004404
Elena Demikhovsky83952512013-07-31 11:35:14 +00004405 unsigned NumElemsPerChunk = vecWidth / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004406 return Index / NumElemsPerChunk;
4407}
4408
Elena Demikhovsky83952512013-07-31 11:35:14 +00004409static unsigned getInsertVINSERTImmediate(SDNode *N, unsigned vecWidth) {
4410 assert((vecWidth == 128 || vecWidth == 256) && "Unsupported vector width");
David Greeneccacdc12011-02-04 16:08:29 +00004411 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
Elena Demikhovsky83952512013-07-31 11:35:14 +00004412 llvm_unreachable("Illegal insert subvector for VINSERT");
David Greeneccacdc12011-02-04 16:08:29 +00004413
4414 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004415 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004416
Craig Toppercfcab212013-01-19 08:27:45 +00004417 MVT VecVT = N->getValueType(0).getSimpleVT();
4418 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004419
Elena Demikhovsky83952512013-07-31 11:35:14 +00004420 unsigned NumElemsPerChunk = vecWidth / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004421 return Index / NumElemsPerChunk;
4422}
4423
Elena Demikhovsky83952512013-07-31 11:35:14 +00004424/// getExtractVEXTRACT128Immediate - Return the appropriate immediate
4425/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4426/// and VINSERTI128 instructions.
4427unsigned X86::getExtractVEXTRACT128Immediate(SDNode *N) {
4428 return getExtractVEXTRACTImmediate(N, 128);
4429}
4430
4431/// getExtractVEXTRACT256Immediate - Return the appropriate immediate
4432/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF64x4
4433/// and VINSERTI64x4 instructions.
4434unsigned X86::getExtractVEXTRACT256Immediate(SDNode *N) {
4435 return getExtractVEXTRACTImmediate(N, 256);
4436}
4437
4438/// getInsertVINSERT128Immediate - Return the appropriate immediate
4439/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4440/// and VINSERTI128 instructions.
4441unsigned X86::getInsertVINSERT128Immediate(SDNode *N) {
4442 return getInsertVINSERTImmediate(N, 128);
4443}
4444
4445/// getInsertVINSERT256Immediate - Return the appropriate immediate
4446/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF46x4
4447/// and VINSERTI64x4 instructions.
4448unsigned X86::getInsertVINSERT256Immediate(SDNode *N) {
4449 return getInsertVINSERTImmediate(N, 256);
4450}
4451
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004452/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4453/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4454/// Handles 256-bit.
4455static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004456 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004457
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004458 unsigned NumElts = VT.getVectorNumElements();
4459
Craig Topper095c5282012-04-15 23:48:57 +00004460 assert((VT.is256BitVector() && NumElts == 4) &&
4461 "Unsupported vector type for VPERMQ/VPERMPD");
4462
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004463 unsigned Mask = 0;
4464 for (unsigned i = 0; i != NumElts; ++i) {
4465 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004466 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004467 continue;
4468 Mask |= Elt << (i*2);
4469 }
4470
4471 return Mask;
4472}
Evan Cheng37b73872009-07-30 08:33:02 +00004473/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4474/// constant +0.0.
4475bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004476 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4477 return CN->isNullValue();
4478 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4479 return CFP->getValueAPF().isPosZero();
4480 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004481}
4482
Nate Begeman9008ca62009-04-27 18:41:29 +00004483/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4484/// their permute mask.
4485static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4486 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004487 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004488 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004489 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004490
Nate Begeman5a5ca152009-04-29 05:20:52 +00004491 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004492 int Idx = SVOp->getMaskElt(i);
4493 if (Idx >= 0) {
4494 if (Idx < (int)NumElems)
4495 Idx += NumElems;
4496 else
4497 Idx -= NumElems;
4498 }
4499 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004500 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004501 return DAG.getVectorShuffle(VT, SDLoc(SVOp), SVOp->getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004503}
4504
Evan Cheng533a0aa2006-04-19 20:35:22 +00004505/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4506/// match movhlps. The lower half elements should come from upper half of
4507/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004508/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004509static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004510 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004511 return false;
4512 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004513 return false;
4514 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004515 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004516 return false;
4517 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004518 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004519 return false;
4520 return true;
4521}
4522
Evan Cheng5ced1d82006-04-06 23:23:56 +00004523/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004524/// is promoted to a vector. It also returns the LoadSDNode by reference if
4525/// required.
4526static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004527 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4528 return false;
4529 N = N->getOperand(0).getNode();
4530 if (!ISD::isNON_EXTLoad(N))
4531 return false;
4532 if (LD)
4533 *LD = cast<LoadSDNode>(N);
4534 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004535}
4536
Dan Gohman65fd6562011-11-03 21:49:52 +00004537// Test whether the given value is a vector value which will be legalized
4538// into a load.
4539static bool WillBeConstantPoolLoad(SDNode *N) {
4540 if (N->getOpcode() != ISD::BUILD_VECTOR)
4541 return false;
4542
4543 // Check for any non-constant elements.
4544 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4545 switch (N->getOperand(i).getNode()->getOpcode()) {
4546 case ISD::UNDEF:
4547 case ISD::ConstantFP:
4548 case ISD::Constant:
4549 break;
4550 default:
4551 return false;
4552 }
4553
4554 // Vectors of all-zeros and all-ones are materialized with special
4555 // instructions rather than being loaded.
4556 return !ISD::isBuildVectorAllZeros(N) &&
4557 !ISD::isBuildVectorAllOnes(N);
4558}
4559
Evan Cheng533a0aa2006-04-19 20:35:22 +00004560/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4561/// match movlp{s|d}. The lower half elements should come from lower half of
4562/// V1 (and in order), and the upper half elements should come from the upper
4563/// half of V2 (and in order). And since V1 will become the source of the
4564/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004565static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004566 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004567 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004568 return false;
4569
Evan Cheng466685d2006-10-09 20:57:25 +00004570 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004571 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004572 // Is V2 is a vector load, don't do this transformation. We will try to use
4573 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004574 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004575 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004576
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004577 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004578
Evan Cheng533a0aa2006-04-19 20:35:22 +00004579 if (NumElems != 2 && NumElems != 4)
4580 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004581 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004582 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004583 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004584 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004585 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004586 return false;
4587 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004588}
4589
Evan Cheng39623da2006-04-20 08:58:49 +00004590/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4591/// all the same.
4592static bool isSplatVector(SDNode *N) {
4593 if (N->getOpcode() != ISD::BUILD_VECTOR)
4594 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004595
Dan Gohman475871a2008-07-27 21:46:04 +00004596 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004597 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4598 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004599 return false;
4600 return true;
4601}
4602
Evan Cheng213d2cf2007-05-17 18:45:50 +00004603/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004604/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004605/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004606static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004607 SDValue V1 = N->getOperand(0);
4608 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004609 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4610 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004612 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004613 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004614 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4615 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004616 if (Opc != ISD::BUILD_VECTOR ||
4617 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 return false;
4619 } else if (Idx >= 0) {
4620 unsigned Opc = V1.getOpcode();
4621 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4622 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004623 if (Opc != ISD::BUILD_VECTOR ||
4624 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004625 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004626 }
4627 }
4628 return true;
4629}
4630
4631/// getZeroVector - Returns a vector of specified type with all zero elements.
4632///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004633static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004634 SelectionDAG &DAG, SDLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004635 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004636
Dale Johannesen0488fb62010-09-30 23:57:10 +00004637 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004638 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004639 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004640 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004641 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004642 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4643 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4644 } else { // SSE1
4645 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4646 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4647 }
Craig Topper5a529e42013-01-18 06:44:29 +00004648 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004649 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004650 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4651 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004652 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops,
4653 array_lengthof(Ops));
Craig Topper12216172012-01-13 08:12:35 +00004654 } else {
4655 // 256-bit logic and arithmetic instructions in AVX are all
4656 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4657 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4658 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004659 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops,
4660 array_lengthof(Ops));
Craig Topper12216172012-01-13 08:12:35 +00004661 }
Craig Topper9d352402012-04-23 07:24:41 +00004662 } else
4663 llvm_unreachable("Unexpected vector type");
4664
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004665 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004666}
4667
Chris Lattner8a594482007-11-25 00:24:49 +00004668/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004669/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4670/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4671/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004672static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004673 SDLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004674 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004675
Owen Anderson825b72b2009-08-11 20:47:22 +00004676 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004677 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004678 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004679 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004680 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004681 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops,
4682 array_lengthof(Ops));
Craig Topper745a86b2011-11-19 22:34:59 +00004683 } else { // AVX
4684 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004685 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004686 }
Craig Topper5a529e42013-01-18 06:44:29 +00004687 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004688 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004689 } else
4690 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004691
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004692 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004693}
4694
Evan Cheng39623da2006-04-20 08:58:49 +00004695/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4696/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004697static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004698 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004699 if (Mask[i] > (int)NumElems) {
4700 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004701 }
Evan Cheng39623da2006-04-20 08:58:49 +00004702 }
Evan Cheng39623da2006-04-20 08:58:49 +00004703}
4704
Evan Cheng017dcc62006-04-21 01:05:10 +00004705/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4706/// operation of specified width.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004707static SDValue getMOVL(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004708 SDValue V2) {
4709 unsigned NumElems = VT.getVectorNumElements();
4710 SmallVector<int, 8> Mask;
4711 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004712 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004713 Mask.push_back(i);
4714 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004715}
4716
Nate Begeman9008ca62009-04-27 18:41:29 +00004717/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004718static SDValue getUnpackl(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 SDValue V2) {
4720 unsigned NumElems = VT.getVectorNumElements();
4721 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004722 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004723 Mask.push_back(i);
4724 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004725 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004727}
4728
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004729/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004730static SDValue getUnpackh(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004731 SDValue V2) {
4732 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004733 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004734 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004735 Mask.push_back(i + Half);
4736 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004737 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004738 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004739}
4740
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004741// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004742// a generic shuffle instruction because the target has no such instructions.
4743// Generate shuffles which repeat i16 and i8 several times until they can be
4744// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004745static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004746 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004747 int NumElems = VT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004748 SDLoc dl(V);
Rafael Espindola15684b22009-04-24 12:40:33 +00004749
Nate Begeman9008ca62009-04-27 18:41:29 +00004750 while (NumElems > 4) {
4751 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004752 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004753 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004754 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004755 EltNo -= NumElems/2;
4756 }
4757 NumElems >>= 1;
4758 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004759 return V;
4760}
Eric Christopherfd179292009-08-27 18:07:15 +00004761
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004762/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4763static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4764 EVT VT = V.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004765 SDLoc dl(V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004766
Craig Topper5a529e42013-01-18 06:44:29 +00004767 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004768 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004769 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004770 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4771 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004772 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004773 // To use VPERMILPS to splat scalars, the second half of indicies must
4774 // refer to the higher part, which is a duplication of the lower one,
4775 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004776 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4777 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004778
4779 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4780 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4781 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004782 } else
4783 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004784
4785 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4786}
4787
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004788/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004789static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4790 EVT SrcVT = SV->getValueType(0);
4791 SDValue V1 = SV->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004792 SDLoc dl(SV);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004793
4794 int EltNo = SV->getSplatIndex();
4795 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004796 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004797
Craig Topper5a529e42013-01-18 06:44:29 +00004798 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4799 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004800
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004801 // Extract the 128-bit part containing the splat element and update
4802 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004803 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004804 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4805 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004806 EltNo -= NumElems/2;
4807 }
4808
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004809 // All i16 and i8 vector types can't be used directly by a generic shuffle
4810 // instruction because the target has no such instruction. Generate shuffles
4811 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004812 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004813 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004814 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004815 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004816
4817 // Recreate the 256-bit vector and place the same 128-bit vector
4818 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004819 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004820 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004821 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004822 }
4823
4824 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004825}
4826
Evan Chengba05f722006-04-21 23:03:30 +00004827/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004828/// vector of zero or undef vector. This produces a shuffle where the low
4829/// element of V2 is swizzled into the zero/undef vector, landing at element
4830/// 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 +00004831static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004832 bool IsZero,
4833 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004834 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004835 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004836 SDValue V1 = IsZero
Andrew Trickac6d9be2013-05-25 02:42:55 +00004837 ? getZeroVector(VT, Subtarget, DAG, SDLoc(V2)) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004838 unsigned NumElems = VT.getVectorNumElements();
4839 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004840 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004841 // If this is the insertion idx, put the low elt of V2 here.
4842 MaskVec.push_back(i == Idx ? NumElems : i);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004843 return DAG.getVectorShuffle(VT, SDLoc(V2), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004844}
4845
Craig Toppera1ffc682012-03-20 06:42:26 +00004846/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4847/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004848/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004849static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004850 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004851 unsigned NumElems = VT.getVectorNumElements();
4852 SDValue ImmN;
4853
Craig Topper89f4e662012-03-20 07:17:59 +00004854 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004855 switch(N->getOpcode()) {
4856 case X86ISD::SHUFP:
4857 ImmN = N->getOperand(N->getNumOperands()-1);
4858 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4859 break;
4860 case X86ISD::UNPCKH:
4861 DecodeUNPCKHMask(VT, Mask);
4862 break;
4863 case X86ISD::UNPCKL:
4864 DecodeUNPCKLMask(VT, Mask);
4865 break;
4866 case X86ISD::MOVHLPS:
4867 DecodeMOVHLPSMask(NumElems, Mask);
4868 break;
4869 case X86ISD::MOVLHPS:
4870 DecodeMOVLHPSMask(NumElems, Mask);
4871 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004872 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004873 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004874 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004875 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004876 case X86ISD::PSHUFD:
4877 case X86ISD::VPERMILP:
4878 ImmN = N->getOperand(N->getNumOperands()-1);
4879 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004880 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004881 break;
4882 case X86ISD::PSHUFHW:
4883 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004884 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004885 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004886 break;
4887 case X86ISD::PSHUFLW:
4888 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004889 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004890 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004891 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004892 case X86ISD::VPERMI:
4893 ImmN = N->getOperand(N->getNumOperands()-1);
4894 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4895 IsUnary = true;
4896 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004897 case X86ISD::MOVSS:
4898 case X86ISD::MOVSD: {
4899 // The index 0 always comes from the first element of the second source,
4900 // this is why MOVSS and MOVSD are used in the first place. The other
4901 // elements come from the other positions of the first source vector
4902 Mask.push_back(NumElems);
4903 for (unsigned i = 1; i != NumElems; ++i) {
4904 Mask.push_back(i);
4905 }
4906 break;
4907 }
4908 case X86ISD::VPERM2X128:
4909 ImmN = N->getOperand(N->getNumOperands()-1);
4910 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004911 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004912 break;
4913 case X86ISD::MOVDDUP:
4914 case X86ISD::MOVLHPD:
4915 case X86ISD::MOVLPD:
4916 case X86ISD::MOVLPS:
4917 case X86ISD::MOVSHDUP:
4918 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004919 // Not yet implemented
4920 return false;
4921 default: llvm_unreachable("unknown target shuffle node");
4922 }
4923
4924 return true;
4925}
4926
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004927/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4928/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004929static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004930 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004931 if (Depth == 6)
4932 return SDValue(); // Limit search depth.
4933
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004934 SDValue V = SDValue(N, 0);
4935 EVT VT = V.getValueType();
4936 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004937
4938 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4939 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004940 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004941
Craig Topper3d092db2012-03-21 02:14:01 +00004942 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004943 return DAG.getUNDEF(VT.getVectorElementType());
4944
Craig Topperd156dc12012-02-06 07:17:51 +00004945 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004946 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4947 : SV->getOperand(1);
4948 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004949 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004950
4951 // Recurse into target specific vector shuffles to find scalars.
4952 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004953 MVT ShufVT = V.getValueType().getSimpleVT();
4954 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004955 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004956 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004957
Craig Topperd978c542012-05-06 19:46:21 +00004958 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004959 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004960
Craig Topper3d092db2012-03-21 02:14:01 +00004961 int Elt = ShuffleMask[Index];
4962 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004963 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004964
Craig Topper3d092db2012-03-21 02:14:01 +00004965 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004966 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004967 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004968 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004969 }
4970
4971 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004972 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004973 V = V.getOperand(0);
4974 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004975 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004976
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004977 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004978 return SDValue();
4979 }
4980
4981 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4982 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004983 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004984
4985 if (V.getOpcode() == ISD::BUILD_VECTOR)
4986 return V.getOperand(Index);
4987
4988 return SDValue();
4989}
4990
4991/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4992/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004993/// search can start in two different directions, from left or right.
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004994/// We count undefs as zeros until PreferredNum is reached.
4995static unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp,
4996 unsigned NumElems, bool ZerosFromLeft,
4997 SelectionDAG &DAG,
4998 unsigned PreferredNum = -1U) {
4999 unsigned NumZeros = 0;
5000 for (unsigned i = 0; i != NumElems; ++i) {
5001 unsigned Index = ZerosFromLeft ? i : NumElems - i - 1;
Craig Topper3d092db2012-03-21 02:14:01 +00005002 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Benjamin Kramera0de26c2013-05-17 14:48:34 +00005003 if (!Elt.getNode())
5004 break;
5005
5006 if (X86::isZeroNode(Elt))
5007 ++NumZeros;
5008 else if (Elt.getOpcode() == ISD::UNDEF) // Undef as zero up to PreferredNum.
5009 NumZeros = std::min(NumZeros + 1, PreferredNum);
5010 else
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005011 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005012 }
5013
Benjamin Kramera0de26c2013-05-17 14:48:34 +00005014 return NumZeros;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005015}
5016
Craig Topper3d092db2012-03-21 02:14:01 +00005017/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
5018/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005019/// starting from its index OpIdx. Also tell OpNum which source vector operand.
5020static
Craig Topper3d092db2012-03-21 02:14:01 +00005021bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
5022 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
5023 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005024 bool SeenV1 = false;
5025 bool SeenV2 = false;
5026
Craig Topper3d092db2012-03-21 02:14:01 +00005027 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005028 int Idx = SVOp->getMaskElt(i);
5029 // Ignore undef indicies
5030 if (Idx < 0)
5031 continue;
5032
Craig Topper3d092db2012-03-21 02:14:01 +00005033 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005034 SeenV1 = true;
5035 else
5036 SeenV2 = true;
5037
5038 // Only accept consecutive elements from the same vector
5039 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
5040 return false;
5041 }
5042
5043 OpNum = SeenV1 ? 0 : 1;
5044 return true;
5045}
5046
5047/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
5048/// logical left shift of a vector.
5049static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
5050 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
5051 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Benjamin Kramera0de26c2013-05-17 14:48:34 +00005052 unsigned NumZeros = getNumOfConsecutiveZeros(
5053 SVOp, NumElems, false /* check zeros from right */, DAG,
5054 SVOp->getMaskElt(0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005055 unsigned OpSrc;
5056
5057 if (!NumZeros)
5058 return false;
5059
5060 // Considering the elements in the mask that are not consecutive zeros,
5061 // check if they consecutively come from only one of the source vectors.
5062 //
5063 // V1 = {X, A, B, C} 0
5064 // \ \ \ /
5065 // vector_shuffle V1, V2 <1, 2, 3, X>
5066 //
5067 if (!isShuffleMaskConsecutive(SVOp,
5068 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00005069 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005070 NumZeros, // Where to start looking in the src vector
5071 NumElems, // Number of elements in vector
5072 OpSrc)) // Which source operand ?
5073 return false;
5074
5075 isLeft = false;
5076 ShAmt = NumZeros;
5077 ShVal = SVOp->getOperand(OpSrc);
5078 return true;
5079}
5080
5081/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
5082/// logical left shift of a vector.
5083static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
5084 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
5085 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Benjamin Kramera0de26c2013-05-17 14:48:34 +00005086 unsigned NumZeros = getNumOfConsecutiveZeros(
5087 SVOp, NumElems, true /* check zeros from left */, DAG,
5088 NumElems - SVOp->getMaskElt(NumElems - 1) - 1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005089 unsigned OpSrc;
5090
5091 if (!NumZeros)
5092 return false;
5093
5094 // Considering the elements in the mask that are not consecutive zeros,
5095 // check if they consecutively come from only one of the source vectors.
5096 //
5097 // 0 { A, B, X, X } = V2
5098 // / \ / /
5099 // vector_shuffle V1, V2 <X, X, 4, 5>
5100 //
5101 if (!isShuffleMaskConsecutive(SVOp,
5102 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00005103 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005104 0, // Where to start looking in the src vector
5105 NumElems, // Number of elements in vector
5106 OpSrc)) // Which source operand ?
5107 return false;
5108
5109 isLeft = true;
5110 ShAmt = NumZeros;
5111 ShVal = SVOp->getOperand(OpSrc);
5112 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00005113}
5114
5115/// isVectorShift - Returns true if the shuffle can be implemented as a
5116/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00005117static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00005118 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005119 // Although the logic below support any bitwidth size, there are no
5120 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005121 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00005122 return false;
5123
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005124 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
5125 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
5126 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00005127
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00005128 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00005129}
5130
Evan Chengc78d3b42006-04-24 18:01:45 +00005131/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
5132///
Dan Gohman475871a2008-07-27 21:46:04 +00005133static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00005134 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00005135 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005136 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00005137 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00005138 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00005139 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00005140
Andrew Trickac6d9be2013-05-25 02:42:55 +00005141 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005142 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00005143 bool First = true;
5144 for (unsigned i = 0; i < 16; ++i) {
5145 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
5146 if (ThisIsNonZero && First) {
5147 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005148 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00005149 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005150 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00005151 First = false;
5152 }
5153
5154 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00005155 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00005156 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
5157 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005158 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00005160 }
5161 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005162 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
5163 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
5164 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00005165 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00005166 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00005167 } else
5168 ThisElt = LastElt;
5169
Gabor Greifba36cb52008-08-28 21:40:38 +00005170 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00005171 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00005172 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00005173 }
5174 }
5175
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005176 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00005177}
5178
Bill Wendlinga348c562007-03-22 18:42:45 +00005179/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00005180///
Dan Gohman475871a2008-07-27 21:46:04 +00005181static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00005182 unsigned NumNonZero, unsigned NumZero,
5183 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005184 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00005185 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00005186 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00005187 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00005188
Andrew Trickac6d9be2013-05-25 02:42:55 +00005189 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005190 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00005191 bool First = true;
5192 for (unsigned i = 0; i < 8; ++i) {
5193 bool isNonZero = (NonZeros & (1 << i)) != 0;
5194 if (isNonZero) {
5195 if (First) {
5196 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005197 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00005198 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005199 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00005200 First = false;
5201 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005202 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005203 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00005204 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00005205 }
5206 }
5207
5208 return V;
5209}
5210
Evan Chengf26ffe92008-05-29 08:22:04 +00005211/// getVShift - Return a vector logical shift node.
5212///
Owen Andersone50ed302009-08-10 22:56:29 +00005213static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00005214 unsigned NumBits, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005215 const TargetLowering &TLI, SDLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005216 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00005217 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00005218 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005219 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
5220 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00005221 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00005222 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00005223 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00005224}
5225
Dan Gohman475871a2008-07-27 21:46:04 +00005226SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005227X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, SDLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00005228 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00005229
Evan Chengc3630942009-12-09 21:00:30 +00005230 // Check if the scalar load can be widened into a vector load. And if
5231 // the address is "base + cst" see if the cst can be "absorbed" into
5232 // the shuffle mask.
5233 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
5234 SDValue Ptr = LD->getBasePtr();
5235 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5236 return SDValue();
5237 EVT PVT = LD->getValueType(0);
5238 if (PVT != MVT::i32 && PVT != MVT::f32)
5239 return SDValue();
5240
5241 int FI = -1;
5242 int64_t Offset = 0;
5243 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5244 FI = FINode->getIndex();
5245 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00005246 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00005247 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5248 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5249 Offset = Ptr.getConstantOperandVal(1);
5250 Ptr = Ptr.getOperand(0);
5251 } else {
5252 return SDValue();
5253 }
5254
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005255 // FIXME: 256-bit vector instructions don't require a strict alignment,
5256 // improve this code to support it better.
5257 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005258 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005259 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005260 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005261 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005262 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005263 // Can't change the alignment. FIXME: It's possible to compute
5264 // the exact stack offset and reference FI + adjust offset instead.
5265 // If someone *really* cares about this. That's the way to implement it.
5266 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005267 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005268 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005269 }
5270 }
5271
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005272 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005273 // Ptr + (Offset & ~15).
5274 if (Offset < 0)
5275 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005276 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005277 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005278 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005279 if (StartOffset)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005280 Ptr = DAG.getNode(ISD::ADD, SDLoc(Ptr), Ptr.getValueType(),
Evan Chengc3630942009-12-09 21:00:30 +00005281 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5282
5283 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005284 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005285
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005286 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5287 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005288 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005289 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005290
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005291 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005292 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005293 Mask.push_back(EltNo);
5294
Craig Toppercc3000632012-01-30 07:50:31 +00005295 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005296 }
5297
5298 return SDValue();
5299}
5300
Michael J. Spencerec38de22010-10-10 22:04:20 +00005301/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5302/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005303/// load which has the same value as a build_vector whose operands are 'elts'.
5304///
5305/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005306///
Nate Begeman1449f292010-03-24 22:19:06 +00005307/// FIXME: we'd also like to handle the case where the last elements are zero
5308/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5309/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005310static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005311 SDLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005312 EVT EltVT = VT.getVectorElementType();
5313 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005314
Nate Begemanfdea31a2010-03-24 20:49:50 +00005315 LoadSDNode *LDBase = NULL;
5316 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005317
Nate Begeman1449f292010-03-24 22:19:06 +00005318 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005319 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005320 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005321 for (unsigned i = 0; i < NumElems; ++i) {
5322 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005323
Nate Begemanfdea31a2010-03-24 20:49:50 +00005324 if (!Elt.getNode() ||
5325 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5326 return SDValue();
5327 if (!LDBase) {
5328 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5329 return SDValue();
5330 LDBase = cast<LoadSDNode>(Elt.getNode());
5331 LastLoadedElt = i;
5332 continue;
5333 }
5334 if (Elt.getOpcode() == ISD::UNDEF)
5335 continue;
5336
5337 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5338 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5339 return SDValue();
5340 LastLoadedElt = i;
5341 }
Nate Begeman1449f292010-03-24 22:19:06 +00005342
5343 // If we have found an entire vector of loads and undefs, then return a large
5344 // load of the entire vector width starting at the base pointer. If we found
5345 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005346 if (LastLoadedElt == NumElems - 1) {
Nadav Rotem23d1d5e2013-05-22 19:28:41 +00005347 SDValue NewLd = SDValue();
Nate Begemanfdea31a2010-03-24 20:49:50 +00005348 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Nadav Rotem23d1d5e2013-05-22 19:28:41 +00005349 NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
5350 LDBase->getPointerInfo(),
5351 LDBase->isVolatile(), LDBase->isNonTemporal(),
5352 LDBase->isInvariant(), 0);
5353 NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
5354 LDBase->getPointerInfo(),
5355 LDBase->isVolatile(), LDBase->isNonTemporal(),
5356 LDBase->isInvariant(), LDBase->getAlignment());
5357
5358 if (LDBase->hasAnyUseOfValue(1)) {
5359 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5360 SDValue(LDBase, 1),
5361 SDValue(NewLd.getNode(), 1));
5362 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5363 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5364 SDValue(NewLd.getNode(), 1));
5365 }
5366
5367 return NewLd;
Craig Topper69947b92012-04-23 06:57:04 +00005368 }
5369 if (NumElems == 4 && LastLoadedElt == 1 &&
5370 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005371 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5372 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005373 SDValue ResNode =
Michael Liao0ee17002013-04-19 04:03:37 +00005374 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops,
5375 array_lengthof(Ops), MVT::i64,
Eli Friedman322ea082011-09-14 23:42:45 +00005376 LDBase->getPointerInfo(),
5377 LDBase->getAlignment(),
5378 false/*isVolatile*/, true/*ReadMem*/,
5379 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005380
5381 // Make sure the newly-created LOAD is in the same position as LDBase in
5382 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5383 // update uses of LDBase's output chain to use the TokenFactor.
5384 if (LDBase->hasAnyUseOfValue(1)) {
5385 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5386 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5387 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5388 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5389 SDValue(ResNode.getNode(), 1));
5390 }
5391
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005392 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005393 }
5394 return SDValue();
5395}
5396
Nadav Rotem9d68b062012-04-08 12:54:54 +00005397/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5398/// to generate a splat value for the following cases:
5399/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005400/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005401/// a scalar load, or a constant.
5402/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005403/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005404SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005405X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005406 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005407 return SDValue();
5408
Craig Topper45e1c752013-01-20 00:38:18 +00005409 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005410 SDLoc dl(Op);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005411
Craig Topper5da8a802012-05-04 05:49:51 +00005412 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5413 "Unsupported vector type for broadcast.");
5414
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005415 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005416 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005417
Nadav Rotem9d68b062012-04-08 12:54:54 +00005418 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005419 default:
5420 // Unknown pattern found.
5421 return SDValue();
5422
5423 case ISD::BUILD_VECTOR: {
5424 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005425 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005426 return SDValue();
5427
Nadav Rotem9d68b062012-04-08 12:54:54 +00005428 Ld = Op.getOperand(0);
5429 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5430 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005431
5432 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005433 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005434 // Constants may have multiple users.
5435 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005436 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005437 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005438 }
5439
5440 case ISD::VECTOR_SHUFFLE: {
5441 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5442
5443 // Shuffles must have a splat mask where the first element is
5444 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005445 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005446 return SDValue();
5447
5448 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005449 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005450 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5451
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005452 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005453 return SDValue();
5454
5455 // Use the register form of the broadcast instruction available on AVX2.
5456 if (VT.is256BitVector())
5457 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5458 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5459 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005460
5461 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005462 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005463 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005464
5465 // The scalar_to_vector node and the suspected
5466 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005467 // Constants may have multiple users.
5468 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005469 return SDValue();
5470 break;
5471 }
5472 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005473
Craig Topper7a9a28b2012-08-12 02:23:29 +00005474 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005475
5476 // Handle the broadcasting a single constant scalar from the constant pool
5477 // into a vector. On Sandybridge it is still better to load a constant vector
5478 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005479 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005480 EVT CVT = Ld.getValueType();
5481 assert(!CVT.isVector() && "Must not broadcast a vector type");
5482 unsigned ScalarSize = CVT.getSizeInBits();
5483
Craig Topper5da8a802012-05-04 05:49:51 +00005484 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005485 const Constant *C = 0;
5486 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5487 C = CI->getConstantIntValue();
5488 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5489 C = CF->getConstantFPValue();
5490
5491 assert(C && "Invalid constant type");
5492
Nadav Rotem154819d2012-04-09 07:45:58 +00005493 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005494 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005495 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005496 MachinePointerInfo::getConstantPool(),
5497 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005498
Nadav Rotem9d68b062012-04-08 12:54:54 +00005499 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5500 }
5501 }
5502
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005503 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005504 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5505
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005506 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005507 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005508 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5509 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5510
5511 // The scalar source must be a normal load.
5512 if (!IsLoad)
5513 return SDValue();
5514
Craig Topper5da8a802012-05-04 05:49:51 +00005515 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005516 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005517
Craig Toppera9376332012-01-10 08:23:59 +00005518 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005519 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005520 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005521 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005522 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005523 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005524
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005525 // Unsupported broadcast.
5526 return SDValue();
5527}
5528
Evan Chengc3630942009-12-09 21:00:30 +00005529SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005530X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5531 EVT VT = Op.getValueType();
5532
5533 // Skip if insert_vec_elt is not supported.
5534 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5535 return SDValue();
5536
Andrew Trickac6d9be2013-05-25 02:42:55 +00005537 SDLoc DL(Op);
Michael Liaofacace82012-10-19 17:15:18 +00005538 unsigned NumElems = Op.getNumOperands();
5539
5540 SDValue VecIn1;
5541 SDValue VecIn2;
5542 SmallVector<unsigned, 4> InsertIndices;
5543 SmallVector<int, 8> Mask(NumElems, -1);
5544
5545 for (unsigned i = 0; i != NumElems; ++i) {
5546 unsigned Opc = Op.getOperand(i).getOpcode();
5547
5548 if (Opc == ISD::UNDEF)
5549 continue;
5550
5551 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5552 // Quit if more than 1 elements need inserting.
5553 if (InsertIndices.size() > 1)
5554 return SDValue();
5555
5556 InsertIndices.push_back(i);
5557 continue;
5558 }
5559
5560 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5561 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5562
5563 // Quit if extracted from vector of different type.
5564 if (ExtractedFromVec.getValueType() != VT)
5565 return SDValue();
5566
5567 // Quit if non-constant index.
5568 if (!isa<ConstantSDNode>(ExtIdx))
5569 return SDValue();
5570
5571 if (VecIn1.getNode() == 0)
5572 VecIn1 = ExtractedFromVec;
5573 else if (VecIn1 != ExtractedFromVec) {
5574 if (VecIn2.getNode() == 0)
5575 VecIn2 = ExtractedFromVec;
5576 else if (VecIn2 != ExtractedFromVec)
5577 // Quit if more than 2 vectors to shuffle
5578 return SDValue();
5579 }
5580
5581 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5582
5583 if (ExtractedFromVec == VecIn1)
5584 Mask[i] = Idx;
5585 else if (ExtractedFromVec == VecIn2)
5586 Mask[i] = Idx + NumElems;
5587 }
5588
5589 if (VecIn1.getNode() == 0)
5590 return SDValue();
5591
5592 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5593 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5594 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5595 unsigned Idx = InsertIndices[i];
5596 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5597 DAG.getIntPtrConstant(Idx));
5598 }
5599
5600 return NV;
5601}
5602
5603SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005604X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005605 SDLoc dl(Op);
David Greenea5f26012011-02-07 19:36:54 +00005606
Craig Topper45e1c752013-01-20 00:38:18 +00005607 MVT VT = Op.getValueType().getSimpleVT();
5608 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005609 unsigned NumElems = Op.getNumOperands();
5610
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005611 // Vectors containing all zeros can be matched by pxor and xorps later
5612 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5613 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5614 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005615 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005616 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005617
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005618 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005619 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005620
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005621 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005622 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5623 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005624 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005625 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005626 return Op;
5627
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005628 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005629 }
5630
Nadav Rotem154819d2012-04-09 07:45:58 +00005631 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005632 if (Broadcast.getNode())
5633 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005634
Owen Andersone50ed302009-08-10 22:56:29 +00005635 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005636
Evan Cheng0db9fe62006-04-25 20:13:52 +00005637 unsigned NumZero = 0;
5638 unsigned NumNonZero = 0;
5639 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005640 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005641 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005642 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005643 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005644 if (Elt.getOpcode() == ISD::UNDEF)
5645 continue;
5646 Values.insert(Elt);
5647 if (Elt.getOpcode() != ISD::Constant &&
5648 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005649 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005650 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005651 NumZero++;
5652 else {
5653 NonZeros |= (1 << i);
5654 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005655 }
5656 }
5657
Chris Lattner97a2a562010-08-26 05:24:29 +00005658 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5659 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005660 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005661
Chris Lattner67f453a2008-03-09 05:42:06 +00005662 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005663 if (NumNonZero == 1) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +00005664 unsigned Idx = countTrailingZeros(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005665 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005666
Chris Lattner62098042008-03-09 01:05:04 +00005667 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5668 // the value are obviously zero, truncate the value to i32 and do the
5669 // insertion that way. Only do this if the value is non-constant or if the
5670 // value is a constant being inserted into element 0. It is cheaper to do
5671 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005673 (!IsAllConstants || Idx == 0)) {
5674 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005675 // Handle SSE only.
5676 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5677 EVT VecVT = MVT::v4i32;
5678 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005679
Chris Lattner62098042008-03-09 01:05:04 +00005680 // Truncate the value (which may itself be a constant) to i32, and
5681 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005682 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005683 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005684 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005685
Chris Lattner62098042008-03-09 01:05:04 +00005686 // Now we have our 32-bit value zero extended in the low element of
5687 // a vector. If Idx != 0, swizzle it into place.
5688 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005689 SmallVector<int, 4> Mask;
5690 Mask.push_back(Idx);
5691 for (unsigned i = 1; i != VecElts; ++i)
5692 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005693 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005694 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005695 }
Craig Topper07a27622012-01-22 03:07:48 +00005696 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005697 }
5698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005699
Chris Lattner19f79692008-03-08 22:59:52 +00005700 // If we have a constant or non-constant insertion into the low element of
5701 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5702 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005703 // depending on what the source datatype is.
5704 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005705 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005706 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005707
5708 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005710 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005711 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005712 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5713 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005714 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005715 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005716 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5717 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005718 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005719 }
5720
5721 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005723 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005724 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005725 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005726 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005727 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005728 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005729 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005730 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005731 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005732 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005733 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005734
5735 // Is it a vector logical left shift?
5736 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005737 X86::isZeroNode(Op.getOperand(0)) &&
5738 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005739 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005740 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005741 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005742 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005743 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005744 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005745
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005746 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005747 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748
Chris Lattner19f79692008-03-08 22:59:52 +00005749 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5750 // is a non-constant being inserted into an element other than the low one,
5751 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5752 // movd/movss) to move this into the low element, then shuffle it into
5753 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005754 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005755 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005756
Evan Cheng0db9fe62006-04-25 20:13:52 +00005757 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005758 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005759 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005760 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005761 MaskVec.push_back(i == Idx ? 0 : 1);
5762 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005763 }
5764 }
5765
Chris Lattner67f453a2008-03-09 05:42:06 +00005766 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005767 if (Values.size() == 1) {
5768 if (EVTBits == 32) {
5769 // Instead of a shuffle like this:
5770 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5771 // Check if it's possible to issue this instead.
5772 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
Michael J. Spencerc6af2432013-05-24 22:23:49 +00005773 unsigned Idx = countTrailingZeros(NonZeros);
Evan Chengc3630942009-12-09 21:00:30 +00005774 SDValue Item = Op.getOperand(Idx);
5775 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5776 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5777 }
Dan Gohman475871a2008-07-27 21:46:04 +00005778 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005780
Dan Gohmana3941172007-07-24 22:55:08 +00005781 // A vector full of immediates; various special cases are already
5782 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005783 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005784 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005785
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005786 // For AVX-length vectors, build the individual 128-bit pieces and use
5787 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005788 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005789 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005790 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005791 V.push_back(Op.getOperand(i));
5792
5793 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5794
5795 // Build both the lower and upper subvector.
5796 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5797 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5798 NumElems/2);
5799
5800 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005801 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005802 }
5803
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005804 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005805 if (EVTBits == 64) {
5806 if (NumNonZero == 1) {
5807 // One half is zero or undef.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00005808 unsigned Idx = countTrailingZeros(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005809 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005810 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005811 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005812 }
Dan Gohman475871a2008-07-27 21:46:04 +00005813 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005814 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005815
5816 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005817 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005819 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005820 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005821 }
5822
Bill Wendling826f36f2007-03-28 00:57:11 +00005823 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005824 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005825 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005826 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005827 }
5828
5829 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005830 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005831 if (NumElems == 4 && NumZero > 0) {
5832 for (unsigned i = 0; i < 4; ++i) {
5833 bool isZero = !(NonZeros & (1 << i));
5834 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005835 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005836 else
Dale Johannesenace16102009-02-03 19:33:06 +00005837 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005838 }
5839
5840 for (unsigned i = 0; i < 2; ++i) {
5841 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5842 default: break;
5843 case 0:
5844 V[i] = V[i*2]; // Must be a zero vector.
5845 break;
5846 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005847 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005848 break;
5849 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005850 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005851 break;
5852 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005853 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005854 break;
5855 }
5856 }
5857
Benjamin Kramer9c683542012-01-30 15:16:21 +00005858 bool Reverse1 = (NonZeros & 0x3) == 2;
5859 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5860 int MaskVec[] = {
5861 Reverse1 ? 1 : 0,
5862 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005863 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5864 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005865 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005866 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005867 }
5868
Craig Topper7a9a28b2012-08-12 02:23:29 +00005869 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005870 // Check for a build vector of consecutive loads.
5871 for (unsigned i = 0; i < NumElems; ++i)
5872 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005873
Nate Begemanfdea31a2010-03-24 20:49:50 +00005874 // Check for elements which are consecutive loads.
5875 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5876 if (LD.getNode())
5877 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005878
Michael Liaofacace82012-10-19 17:15:18 +00005879 // Check for a build vector from mostly shuffle plus few inserting.
5880 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5881 if (Sh.getNode())
5882 return Sh;
5883
Michael J. Spencerec38de22010-10-10 22:04:20 +00005884 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005885 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005886 SDValue Result;
5887 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5888 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5889 else
5890 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005891
Chris Lattner24faf612010-08-28 17:59:08 +00005892 for (unsigned i = 1; i < NumElems; ++i) {
5893 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5894 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005895 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005896 }
5897 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005898 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005899
Chris Lattner6e80e442010-08-28 17:15:43 +00005900 // Otherwise, expand into a number of unpckl*, start by extending each of
5901 // our (non-undef) elements to the full vector width with the element in the
5902 // bottom slot of the vector (which generates no code for SSE).
5903 for (unsigned i = 0; i < NumElems; ++i) {
5904 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5905 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5906 else
5907 V[i] = DAG.getUNDEF(VT);
5908 }
5909
5910 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005911 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5912 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5913 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005914 unsigned EltStride = NumElems >> 1;
5915 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005916 for (unsigned i = 0; i < EltStride; ++i) {
5917 // If V[i+EltStride] is undef and this is the first round of mixing,
5918 // then it is safe to just drop this shuffle: V[i] is already in the
5919 // right place, the one element (since it's the first round) being
5920 // inserted as undef can be dropped. This isn't safe for successive
5921 // rounds because they will permute elements within both vectors.
5922 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5923 EltStride == NumElems/2)
5924 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005925
Chris Lattner6e80e442010-08-28 17:15:43 +00005926 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005927 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005928 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005929 }
5930 return V[0];
5931 }
Dan Gohman475871a2008-07-27 21:46:04 +00005932 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005933}
5934
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005935// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5936// to create 256-bit vectors from two other 128-bit ones.
5937static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005938 SDLoc dl(Op);
Craig Topper45e1c752013-01-20 00:38:18 +00005939 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005940
Elena Demikhovsky83952512013-07-31 11:35:14 +00005941 assert((ResVT.is256BitVector() ||
5942 ResVT.is512BitVector()) && "Value type must be 256-/512-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005943
5944 SDValue V1 = Op.getOperand(0);
5945 SDValue V2 = Op.getOperand(1);
5946 unsigned NumElems = ResVT.getVectorNumElements();
Elena Demikhovsky83952512013-07-31 11:35:14 +00005947 if(ResVT.is256BitVector())
5948 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005949
Elena Demikhovsky83952512013-07-31 11:35:14 +00005950 return Concat256BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005951}
5952
Craig Topper55b24052012-09-11 06:15:32 +00005953static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005954 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005955
Elena Demikhovsky83952512013-07-31 11:35:14 +00005956 // AVX/AVX-512 can use the vinsertf128 instruction to create 256-bit vectors
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005957 // from two other 128-bit ones.
5958 return LowerAVXCONCAT_VECTORS(Op, DAG);
5959}
5960
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005961// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005962static SDValue
5963LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5964 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005965 SDValue V1 = SVOp->getOperand(0);
5966 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005967 SDLoc dl(SVOp);
Craig Topper657a99c2013-01-19 23:36:09 +00005968 MVT VT = SVOp->getValueType(0).getSimpleVT();
5969 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005970 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005971
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005972 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5973 return SDValue();
5974 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005975 return SDValue();
5976
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005977 // Check the mask for BLEND and build the value.
5978 unsigned MaskValue = 0;
5979 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005980 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005981 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005982
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005983 // Blend for v16i16 should be symetric for the both lanes.
5984 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005985
Craig Topper9b33ef72013-01-21 06:57:59 +00005986 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005987 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005988 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005989
Craig Topper04f74a12013-01-21 07:25:16 +00005990 if ((EltIdx < 0 || EltIdx == (int)i) &&
5991 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005992 continue;
5993
Craig Topper9b33ef72013-01-21 06:57:59 +00005994 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005995 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005996 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5997 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005998 else
Craig Topper1842ba02012-04-23 06:38:28 +00005999 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006000 }
6001
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00006002 // Convert i32 vectors to floating point if it is not AVX2.
6003 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00006004 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00006005 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00006006 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
6007 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00006008 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
6009 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
6010 }
Craig Topper9b33ef72013-01-21 06:57:59 +00006011
Craig Topperbbf9d3e2013-01-21 07:19:54 +00006012 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
6013 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00006014 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006015}
6016
Nate Begemanb9a47b82009-02-23 08:49:38 +00006017// v8i16 shuffles - Prefer shuffles in the following order:
6018// 1. [all] pshuflw, pshufhw, optional move
6019// 2. [ssse3] 1 x pshufb
6020// 3. [ssse3] 2 x pshufb + 1 x por
6021// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00006022static SDValue
6023LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
6024 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00006025 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00006026 SDValue V1 = SVOp->getOperand(0);
6027 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006028 SDLoc dl(SVOp);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00006030
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 // Determine if more than 1 of the words in each of the low and high quadwords
6032 // of the result come from the same quadword of one of the two inputs. Undef
6033 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00006034 unsigned LoQuad[] = { 0, 0, 0, 0 };
6035 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00006036 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006037 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00006038 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00006039 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006040 MaskVals.push_back(EltIdx);
6041 if (EltIdx < 0) {
6042 ++Quad[0];
6043 ++Quad[1];
6044 ++Quad[2];
6045 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00006046 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 }
6048 ++Quad[EltIdx / 4];
6049 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00006050 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00006051
Nate Begemanb9a47b82009-02-23 08:49:38 +00006052 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00006053 unsigned MaxQuad = 1;
6054 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006055 if (LoQuad[i] > MaxQuad) {
6056 BestLoQuad = i;
6057 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00006058 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006059 }
6060
Nate Begemanb9a47b82009-02-23 08:49:38 +00006061 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00006062 MaxQuad = 1;
6063 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006064 if (HiQuad[i] > MaxQuad) {
6065 BestHiQuad = i;
6066 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00006067 }
6068 }
6069
Nate Begemanb9a47b82009-02-23 08:49:38 +00006070 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00006071 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00006072 // single pshufb instruction is necessary. If There are more than 2 input
6073 // quads, disable the next transformation since it does not help SSSE3.
6074 bool V1Used = InputQuads[0] || InputQuads[1];
6075 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00006076 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006077 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00006078 BestLoQuad = InputQuads[0] ? 0 : 1;
6079 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006080 }
6081 if (InputQuads.count() > 2) {
6082 BestLoQuad = -1;
6083 BestHiQuad = -1;
6084 }
6085 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00006086
Nate Begemanb9a47b82009-02-23 08:49:38 +00006087 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
6088 // the shuffle mask. If a quad is scored as -1, that means that it contains
6089 // words from all 4 input quadwords.
6090 SDValue NewV;
6091 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006092 int MaskV[] = {
6093 BestLoQuad < 0 ? 0 : BestLoQuad,
6094 BestHiQuad < 0 ? 1 : BestHiQuad
6095 };
Eric Christopherfd179292009-08-27 18:07:15 +00006096 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006097 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
6098 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
6099 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006100
Nate Begemanb9a47b82009-02-23 08:49:38 +00006101 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
6102 // source words for the shuffle, to aid later transformations.
6103 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00006104 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00006105 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006106 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00006107 if (idx != (int)i)
6108 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006109 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00006110 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006111 AllWordsInNewV = false;
6112 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00006113 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00006114
Nate Begemanb9a47b82009-02-23 08:49:38 +00006115 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
6116 if (AllWordsInNewV) {
6117 for (int i = 0; i != 8; ++i) {
6118 int idx = MaskVals[i];
6119 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006120 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006121 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006122 if ((idx != i) && idx < 4)
6123 pshufhw = false;
6124 if ((idx != i) && idx > 3)
6125 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00006126 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00006127 V1 = NewV;
6128 V2Used = false;
6129 BestLoQuad = 0;
6130 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006131 }
Evan Cheng14b32e12007-12-11 01:46:18 +00006132
Nate Begemanb9a47b82009-02-23 08:49:38 +00006133 // If we've eliminated the use of V2, and the new mask is a pshuflw or
6134 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00006135 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00006136 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
6137 unsigned TargetMask = 0;
6138 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00006139 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00006140 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
6141 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
6142 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00006143 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006144 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00006145 }
Evan Cheng14b32e12007-12-11 01:46:18 +00006146 }
Eric Christopherfd179292009-08-27 18:07:15 +00006147
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006148 // Promote splats to a larger type which usually leads to more efficient code.
6149 // FIXME: Is this true if pshufb is available?
6150 if (SVOp->isSplat())
6151 return PromoteSplat(SVOp, DAG);
6152
Nate Begemanb9a47b82009-02-23 08:49:38 +00006153 // If we have SSSE3, and all words of the result are from 1 input vector,
6154 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
6155 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00006156 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006157 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006158
Nate Begemanb9a47b82009-02-23 08:49:38 +00006159 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00006160 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00006161 // mask, and elements that come from V1 in the V2 mask, so that the two
6162 // results can be OR'd together.
6163 bool TwoInputs = V1Used && V2Used;
6164 for (unsigned i = 0; i != 8; ++i) {
6165 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00006166 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
6167 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00006168 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00006169 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006170 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006171 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00006172 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006173 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006174 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006175 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006176 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00006177
Nate Begemanb9a47b82009-02-23 08:49:38 +00006178 // Calculate the shuffle mask for the second input, shuffle it, and
6179 // OR it with the first shuffled input.
6180 pshufbMask.clear();
6181 for (unsigned i = 0; i != 8; ++i) {
6182 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00006183 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
6184 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
6185 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
6186 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006187 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006188 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00006189 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006190 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006191 MVT::v16i8, &pshufbMask[0], 16));
6192 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006193 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006194 }
6195
6196 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
6197 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00006198 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006199 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006200 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00006201 for (int i = 0; i != 4; ++i) {
6202 int idx = MaskVals[i];
6203 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006204 InOrder.set(i);
6205 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006206 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006207 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006208 }
6209 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006210 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00006211 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006212
Craig Topperdd637ae2012-02-19 05:41:45 +00006213 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
6214 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006215 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00006216 NewV.getOperand(0),
6217 getShufflePSHUFLWImmediate(SVOp), DAG);
6218 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00006219 }
Eric Christopherfd179292009-08-27 18:07:15 +00006220
Nate Begemanb9a47b82009-02-23 08:49:38 +00006221 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
6222 // and update MaskVals with the new element order.
6223 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006224 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00006225 for (unsigned i = 4; i != 8; ++i) {
6226 int idx = MaskVals[i];
6227 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006228 InOrder.set(i);
6229 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006230 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006231 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006232 }
6233 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006234 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00006235 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006236
Craig Topperdd637ae2012-02-19 05:41:45 +00006237 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
6238 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006239 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00006240 NewV.getOperand(0),
6241 getShufflePSHUFHWImmediate(SVOp), DAG);
6242 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00006243 }
Eric Christopherfd179292009-08-27 18:07:15 +00006244
Nate Begemanb9a47b82009-02-23 08:49:38 +00006245 // In case BestHi & BestLo were both -1, which means each quadword has a word
6246 // from each of the four input quadwords, calculate the InOrder bitvector now
6247 // before falling through to the insert/extract cleanup.
6248 if (BestLoQuad == -1 && BestHiQuad == -1) {
6249 NewV = V1;
6250 for (int i = 0; i != 8; ++i)
6251 if (MaskVals[i] < 0 || MaskVals[i] == i)
6252 InOrder.set(i);
6253 }
Eric Christopherfd179292009-08-27 18:07:15 +00006254
Nate Begemanb9a47b82009-02-23 08:49:38 +00006255 // The other elements are put in the right place using pextrw and pinsrw.
6256 for (unsigned i = 0; i != 8; ++i) {
6257 if (InOrder[i])
6258 continue;
6259 int EltIdx = MaskVals[i];
6260 if (EltIdx < 0)
6261 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00006262 SDValue ExtOp = (EltIdx < 8) ?
6263 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
6264 DAG.getIntPtrConstant(EltIdx)) :
6265 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006266 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00006267 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006268 DAG.getIntPtrConstant(i));
6269 }
6270 return NewV;
6271}
6272
6273// v16i8 shuffles - Prefer shuffles in the following order:
6274// 1. [ssse3] 1 x pshufb
6275// 2. [ssse3] 2 x pshufb + 1 x por
6276// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6277static
Nate Begeman9008ca62009-04-27 18:41:29 +00006278SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006279 SelectionDAG &DAG,
6280 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006281 SDValue V1 = SVOp->getOperand(0);
6282 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006283 SDLoc dl(SVOp);
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006284 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006285
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006286 // Promote splats to a larger type which usually leads to more efficient code.
6287 // FIXME: Is this true if pshufb is available?
6288 if (SVOp->isSplat())
6289 return PromoteSplat(SVOp, DAG);
6290
Nate Begemanb9a47b82009-02-23 08:49:38 +00006291 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006292 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006293 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006294
Nate Begemanb9a47b82009-02-23 08:49:38 +00006295 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006296 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006297 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006298
Nate Begemanb9a47b82009-02-23 08:49:38 +00006299 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006300 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006301 //
6302 // Otherwise, we have elements from both input vectors, and must zero out
6303 // elements that come from V2 in the first mask, and V1 in the second mask
6304 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006305 for (unsigned i = 0; i != 16; ++i) {
6306 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006307 if (EltIdx < 0 || EltIdx >= 16)
6308 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006309 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006310 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006311 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006312 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006313 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006314
6315 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6316 // the 2nd operand if it's undefined or zero.
6317 if (V2.getOpcode() == ISD::UNDEF ||
6318 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006319 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006320
Nate Begemanb9a47b82009-02-23 08:49:38 +00006321 // Calculate the shuffle mask for the second input, shuffle it, and
6322 // OR it with the first shuffled input.
6323 pshufbMask.clear();
6324 for (unsigned i = 0; i != 16; ++i) {
6325 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006326 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006327 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006328 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006329 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006330 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006331 MVT::v16i8, &pshufbMask[0], 16));
6332 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006333 }
Eric Christopherfd179292009-08-27 18:07:15 +00006334
Nate Begemanb9a47b82009-02-23 08:49:38 +00006335 // No SSSE3 - Calculate in place words and then fix all out of place words
6336 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6337 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006338 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6339 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006340 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006341 for (int i = 0; i != 8; ++i) {
6342 int Elt0 = MaskVals[i*2];
6343 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006344
Nate Begemanb9a47b82009-02-23 08:49:38 +00006345 // This word of the result is all undef, skip it.
6346 if (Elt0 < 0 && Elt1 < 0)
6347 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006348
Nate Begemanb9a47b82009-02-23 08:49:38 +00006349 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006350 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006351 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006352
Nate Begemanb9a47b82009-02-23 08:49:38 +00006353 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6354 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6355 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006356
6357 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6358 // using a single extract together, load it and store it.
6359 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006360 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006361 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006362 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006363 DAG.getIntPtrConstant(i));
6364 continue;
6365 }
6366
Nate Begemanb9a47b82009-02-23 08:49:38 +00006367 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006368 // source byte is not also odd, shift the extracted word left 8 bits
6369 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006370 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006371 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006372 DAG.getIntPtrConstant(Elt1 / 2));
6373 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006374 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006375 DAG.getConstant(8,
6376 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006377 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006378 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6379 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006380 }
6381 // If Elt0 is defined, extract it from the appropriate source. If the
6382 // source byte is not also even, shift the extracted word right 8 bits. If
6383 // Elt1 was also defined, OR the extracted values together before
6384 // inserting them in the result.
6385 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006386 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006387 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6388 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006389 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006390 DAG.getConstant(8,
6391 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006392 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006393 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6394 DAG.getConstant(0x00FF, MVT::i16));
6395 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006396 : InsElt0;
6397 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006398 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006399 DAG.getIntPtrConstant(i));
6400 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006401 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006402}
6403
Elena Demikhovsky41789462012-09-06 12:42:01 +00006404// v32i8 shuffles - Translate to VPSHUFB if possible.
6405static
6406SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006407 const X86Subtarget *Subtarget,
6408 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006409 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006410 SDValue V1 = SVOp->getOperand(0);
6411 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006412 SDLoc dl(SVOp);
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006413 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006414
6415 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006416 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6417 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006418
Michael Liao471b9172012-10-03 23:43:52 +00006419 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006420 // (1) one of input vector is undefined or zeroinitializer.
6421 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6422 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006423 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006424 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006425 return SDValue();
6426
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006427 if (V1IsAllZero && !V2IsAllZero) {
6428 CommuteVectorShuffleMask(MaskVals, 32);
6429 V1 = V2;
6430 }
6431 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006432 for (unsigned i = 0; i != 32; i++) {
6433 int EltIdx = MaskVals[i];
6434 if (EltIdx < 0 || EltIdx >= 32)
6435 EltIdx = 0x80;
6436 else {
6437 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6438 // Cross lane is not allowed.
6439 return SDValue();
6440 EltIdx &= 0xf;
6441 }
6442 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6443 }
6444 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6445 DAG.getNode(ISD::BUILD_VECTOR, dl,
6446 MVT::v32i8, &pshufbMask[0], 32));
6447}
6448
Evan Cheng7a831ce2007-12-15 03:00:47 +00006449/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006450/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006451/// done when every pair / quad of shuffle mask elements point to elements in
6452/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006453/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006454static
Nate Begeman9008ca62009-04-27 18:41:29 +00006455SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006456 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006457 MVT VT = SVOp->getValueType(0).getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006458 SDLoc dl(SVOp);
Nate Begeman9008ca62009-04-27 18:41:29 +00006459 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006460 MVT NewVT;
6461 unsigned Scale;
6462 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006463 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006464 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6465 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6466 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6467 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6468 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6469 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006470 }
6471
Nate Begeman9008ca62009-04-27 18:41:29 +00006472 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006473 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006474 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006475 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006476 int EltIdx = SVOp->getMaskElt(i+j);
6477 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006478 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006479 if (StartIdx < 0)
6480 StartIdx = (EltIdx / Scale);
6481 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006482 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006483 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006484 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006485 }
6486
Craig Topper11ac1f82012-05-04 04:08:44 +00006487 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6488 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006489 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006490}
6491
Evan Chengd880b972008-05-09 21:53:03 +00006492/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006493///
Craig Topperf84b7502013-01-20 00:50:58 +00006494static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006495 SDValue SrcOp, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006496 const X86Subtarget *Subtarget, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006497 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006498 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006499 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006500 LD = dyn_cast<LoadSDNode>(SrcOp);
6501 if (!LD) {
6502 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6503 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006504 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006505 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006506 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006507 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006508 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006509 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006510 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006511 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006512 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6513 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6514 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006515 SrcOp.getOperand(0)
6516 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006517 }
6518 }
6519 }
6520
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006521 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006522 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006523 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006524 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006525}
6526
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006527/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6528/// which could not be matched by any known target speficic shuffle
6529static SDValue
6530LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006531
6532 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6533 if (NewOp.getNode())
6534 return NewOp;
6535
Craig Topper657a99c2013-01-19 23:36:09 +00006536 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006537
Craig Topper8f35c132012-01-20 09:29:03 +00006538 unsigned NumElems = VT.getVectorNumElements();
6539 unsigned NumLaneElems = NumElems / 2;
6540
Andrew Trickac6d9be2013-05-25 02:42:55 +00006541 SDLoc dl(SVOp);
Craig Topper657a99c2013-01-19 23:36:09 +00006542 MVT EltVT = VT.getVectorElementType();
6543 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006544 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006545
Craig Topper9a2b6e12012-04-06 07:45:23 +00006546 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006547 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006548 // Build a shuffle mask for the output, discovering on the fly which
6549 // input vectors to use as shuffle operands (recorded in InputUsed).
6550 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006551 // out with UseBuildVector set.
6552 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006553 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006554 unsigned LaneStart = l * NumLaneElems;
6555 for (unsigned i = 0; i != NumLaneElems; ++i) {
6556 // The mask element. This indexes into the input.
6557 int Idx = SVOp->getMaskElt(i+LaneStart);
6558 if (Idx < 0) {
6559 // the mask element does not index into any input vector.
6560 Mask.push_back(-1);
6561 continue;
6562 }
Craig Topper8f35c132012-01-20 09:29:03 +00006563
Craig Topper9a2b6e12012-04-06 07:45:23 +00006564 // The input vector this mask element indexes into.
6565 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006566
Craig Topper9a2b6e12012-04-06 07:45:23 +00006567 // Turn the index into an offset from the start of the input vector.
6568 Idx -= Input * NumLaneElems;
6569
6570 // Find or create a shuffle vector operand to hold this input.
6571 unsigned OpNo;
6572 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6573 if (InputUsed[OpNo] == Input)
6574 // This input vector is already an operand.
6575 break;
6576 if (InputUsed[OpNo] < 0) {
6577 // Create a new operand for this input vector.
6578 InputUsed[OpNo] = Input;
6579 break;
6580 }
6581 }
6582
6583 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006584 // More than two input vectors used! Give up on trying to create a
6585 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6586 UseBuildVector = true;
6587 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006588 }
6589
6590 // Add the mask index for the new shuffle vector.
6591 Mask.push_back(Idx + OpNo * NumLaneElems);
6592 }
6593
Craig Topper8ae97ba2012-05-21 06:40:16 +00006594 if (UseBuildVector) {
6595 SmallVector<SDValue, 16> SVOps;
6596 for (unsigned i = 0; i != NumLaneElems; ++i) {
6597 // The mask element. This indexes into the input.
6598 int Idx = SVOp->getMaskElt(i+LaneStart);
6599 if (Idx < 0) {
6600 SVOps.push_back(DAG.getUNDEF(EltVT));
6601 continue;
6602 }
6603
6604 // The input vector this mask element indexes into.
6605 int Input = Idx / NumElems;
6606
6607 // Turn the index into an offset from the start of the input vector.
6608 Idx -= Input * NumElems;
6609
6610 // Extract the vector element by hand.
6611 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6612 SVOp->getOperand(Input),
6613 DAG.getIntPtrConstant(Idx)));
6614 }
6615
6616 // Construct the output using a BUILD_VECTOR.
6617 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6618 SVOps.size());
6619 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006620 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006621 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006622 } else {
6623 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006624 (InputUsed[0] % 2) * NumLaneElems,
6625 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006626 // If only one input was used, use an undefined vector for the other.
6627 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6628 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006629 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006630 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006631 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006632 }
6633
6634 Mask.clear();
6635 }
Craig Topper8f35c132012-01-20 09:29:03 +00006636
6637 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006638 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006639}
6640
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006641/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6642/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006643static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006644LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006645 SDValue V1 = SVOp->getOperand(0);
6646 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006647 SDLoc dl(SVOp);
Craig Topper657a99c2013-01-19 23:36:09 +00006648 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006649
Craig Topper7a9a28b2012-08-12 02:23:29 +00006650 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006651
Benjamin Kramer9c683542012-01-30 15:16:21 +00006652 std::pair<int, int> Locs[4];
6653 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006654 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006655
Evan Chengace3c172008-07-22 21:13:36 +00006656 unsigned NumHi = 0;
6657 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006658 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006659 int Idx = PermMask[i];
6660 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006661 Locs[i] = std::make_pair(-1, -1);
6662 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006663 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6664 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006665 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006666 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006667 NumLo++;
6668 } else {
6669 Locs[i] = std::make_pair(1, NumHi);
6670 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006671 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006672 NumHi++;
6673 }
6674 }
6675 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006676
Evan Chengace3c172008-07-22 21:13:36 +00006677 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006678 // If no more than two elements come from either vector. This can be
6679 // implemented with two shuffles. First shuffle gather the elements.
6680 // The second shuffle, which takes the first shuffle as both of its
6681 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006682 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006683
Benjamin Kramer9c683542012-01-30 15:16:21 +00006684 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006685
Benjamin Kramer9c683542012-01-30 15:16:21 +00006686 for (unsigned i = 0; i != 4; ++i)
6687 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006688 unsigned Idx = (i < 2) ? 0 : 4;
6689 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006690 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006691 }
Evan Chengace3c172008-07-22 21:13:36 +00006692
Nate Begeman9008ca62009-04-27 18:41:29 +00006693 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006694 }
6695
6696 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006697 // Otherwise, we must have three elements from one vector, call it X, and
6698 // one element from the other, call it Y. First, use a shufps to build an
6699 // intermediate vector with the one element from Y and the element from X
6700 // that will be in the same half in the final destination (the indexes don't
6701 // matter). Then, use a shufps to build the final vector, taking the half
6702 // containing the element from Y from the intermediate, and the other half
6703 // from X.
6704 if (NumHi == 3) {
6705 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006706 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006707 std::swap(V1, V2);
6708 }
6709
6710 // Find the element from V2.
6711 unsigned HiIndex;
6712 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006713 int Val = PermMask[HiIndex];
6714 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006715 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006716 if (Val >= 4)
6717 break;
6718 }
6719
Nate Begeman9008ca62009-04-27 18:41:29 +00006720 Mask1[0] = PermMask[HiIndex];
6721 Mask1[1] = -1;
6722 Mask1[2] = PermMask[HiIndex^1];
6723 Mask1[3] = -1;
6724 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006725
6726 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006727 Mask1[0] = PermMask[0];
6728 Mask1[1] = PermMask[1];
6729 Mask1[2] = HiIndex & 1 ? 6 : 4;
6730 Mask1[3] = HiIndex & 1 ? 4 : 6;
6731 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006732 }
Craig Topper69947b92012-04-23 06:57:04 +00006733
6734 Mask1[0] = HiIndex & 1 ? 2 : 0;
6735 Mask1[1] = HiIndex & 1 ? 0 : 2;
6736 Mask1[2] = PermMask[2];
6737 Mask1[3] = PermMask[3];
6738 if (Mask1[2] >= 0)
6739 Mask1[2] += 4;
6740 if (Mask1[3] >= 0)
6741 Mask1[3] += 4;
6742 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006743 }
6744
6745 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006746 int LoMask[] = { -1, -1, -1, -1 };
6747 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006748
Benjamin Kramer9c683542012-01-30 15:16:21 +00006749 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006750 unsigned MaskIdx = 0;
6751 unsigned LoIdx = 0;
6752 unsigned HiIdx = 2;
6753 for (unsigned i = 0; i != 4; ++i) {
6754 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006755 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006756 MaskIdx = 1;
6757 LoIdx = 0;
6758 HiIdx = 2;
6759 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006760 int Idx = PermMask[i];
6761 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006762 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006763 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006764 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006765 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006766 LoIdx++;
6767 } else {
6768 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006769 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006770 HiIdx++;
6771 }
6772 }
6773
Nate Begeman9008ca62009-04-27 18:41:29 +00006774 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6775 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006776 int MaskOps[] = { -1, -1, -1, -1 };
6777 for (unsigned i = 0; i != 4; ++i)
6778 if (Locs[i].first != -1)
6779 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006780 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006781}
6782
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006783static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006784 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006785 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006786
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006787 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6788 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006789 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6790 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6791 // BUILD_VECTOR (load), undef
6792 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006793
6794 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006795}
6796
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006797static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006798SDValue getMOVDDup(SDValue &Op, SDLoc &dl, SDValue V1, SelectionDAG &DAG) {
Evan Cheng835580f2010-10-07 20:50:20 +00006799 EVT VT = Op.getValueType();
6800
6801 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006802 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6803 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006804 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6805 V1, DAG));
6806}
6807
6808static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006809SDValue getMOVLowToHigh(SDValue &Op, SDLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006810 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006811 SDValue V1 = Op.getOperand(0);
6812 SDValue V2 = Op.getOperand(1);
6813 EVT VT = Op.getValueType();
6814
6815 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6816
Craig Topper1accb7e2012-01-10 06:54:16 +00006817 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006818 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6819
Evan Cheng0899f5c2011-08-31 02:05:24 +00006820 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6821 return DAG.getNode(ISD::BITCAST, dl, VT,
6822 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6823 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6824 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006825}
6826
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006827static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006828SDValue getMOVHighToLow(SDValue &Op, SDLoc &dl, SelectionDAG &DAG) {
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006829 SDValue V1 = Op.getOperand(0);
6830 SDValue V2 = Op.getOperand(1);
6831 EVT VT = Op.getValueType();
6832
6833 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6834 "unsupported shuffle type");
6835
6836 if (V2.getOpcode() == ISD::UNDEF)
6837 V2 = V1;
6838
6839 // v4i32 or v4f32
6840 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6841}
6842
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006843static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006844SDValue getMOVLP(SDValue &Op, SDLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006845 SDValue V1 = Op.getOperand(0);
6846 SDValue V2 = Op.getOperand(1);
6847 EVT VT = Op.getValueType();
6848 unsigned NumElems = VT.getVectorNumElements();
6849
6850 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6851 // operand of these instructions is only memory, so check if there's a
6852 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6853 // same masks.
6854 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006855
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006856 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006857 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006858 CanFoldLoad = true;
6859
6860 // When V1 is a load, it can be folded later into a store in isel, example:
6861 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6862 // turns into:
6863 // (MOVLPSmr addr:$src1, VR128:$src2)
6864 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006865 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006866 CanFoldLoad = true;
6867
Dan Gohman65fd6562011-11-03 21:49:52 +00006868 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006869 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006870 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006871 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6872
6873 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006874 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006875 if (SVOp->getMaskElt(1) != -1)
6876 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006877 }
6878
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006879 // movl and movlp will both match v2i64, but v2i64 is never matched by
6880 // movl earlier because we make it strict to avoid messing with the movlp load
6881 // folding logic (see the code above getMOVLP call). Match it here then,
6882 // this is horrible, but will stay like this until we move all shuffle
6883 // matching to x86 specific nodes. Note that for the 1st condition all
6884 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006885 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006886 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6887 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006888 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006889 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006890 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006891 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006892
6893 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6894
6895 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006896 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006897 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006898}
6899
Michael Liaod9d09602012-10-23 17:34:00 +00006900// Reduce a vector shuffle to zext.
6901SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006902X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006903 // PMOVZX is only available from SSE41.
6904 if (!Subtarget->hasSSE41())
6905 return SDValue();
6906
6907 EVT VT = Op.getValueType();
6908
6909 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006910 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006911 return SDValue();
6912
6913 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006914 SDLoc DL(Op);
Michael Liaod9d09602012-10-23 17:34:00 +00006915 SDValue V1 = Op.getOperand(0);
6916 SDValue V2 = Op.getOperand(1);
6917 unsigned NumElems = VT.getVectorNumElements();
6918
6919 // Extending is an unary operation and the element type of the source vector
6920 // won't be equal to or larger than i64.
6921 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6922 VT.getVectorElementType() == MVT::i64)
6923 return SDValue();
6924
6925 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6926 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006927 while ((1U << Shift) < NumElems) {
6928 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006929 break;
6930 Shift += 1;
6931 // The maximal ratio is 8, i.e. from i8 to i64.
6932 if (Shift > 3)
6933 return SDValue();
6934 }
6935
6936 // Check the shuffle mask.
6937 unsigned Mask = (1U << Shift) - 1;
6938 for (unsigned i = 0; i != NumElems; ++i) {
6939 int EltIdx = SVOp->getMaskElt(i);
6940 if ((i & Mask) != 0 && EltIdx != -1)
6941 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006942 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006943 return SDValue();
6944 }
6945
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006946 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006947 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006948 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6949 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006950
6951 if (!isTypeLegal(NVT))
6952 return SDValue();
6953
6954 // Simplify the operand as it's prepared to be fed into shuffle.
6955 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6956 if (V1.getOpcode() == ISD::BITCAST &&
6957 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6958 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6959 V1.getOperand(0)
6960 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6961 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6962 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006963 ConstantSDNode *CIdx =
6964 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006965 // If it's foldable, i.e. normal load with single use, we will let code
6966 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006967 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006968 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6969 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6970 // The "ext_vec_elt" node is wider than the result node.
6971 // In this case we should extract subvector from V.
6972 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6973 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6974 EVT FullVT = V.getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00006975 EVT SubVecVT = EVT::getVectorVT(*Context,
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006976 FullVT.getVectorElementType(),
6977 FullVT.getVectorNumElements()/Ratio);
Matt Arsenault225ed702013-05-18 00:21:46 +00006978 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006979 DAG.getIntPtrConstant(0));
6980 }
Michael Liaod9d09602012-10-23 17:34:00 +00006981 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006982 }
Michael Liaod9d09602012-10-23 17:34:00 +00006983 }
6984
6985 return DAG.getNode(ISD::BITCAST, DL, VT,
6986 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6987}
6988
Nadav Rotem154819d2012-04-09 07:45:58 +00006989SDValue
6990X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006991 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006992 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006993 SDLoc dl(Op);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006994 SDValue V1 = Op.getOperand(0);
6995 SDValue V2 = Op.getOperand(1);
6996
6997 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006998 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006999
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00007000 // Handle splat operations
7001 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00007002 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00007003 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00007004 if (Broadcast.getNode())
7005 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00007006 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007007
Michael Liaod9d09602012-10-23 17:34:00 +00007008 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00007009 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00007010 if (NewOp.getNode())
7011 return NewOp;
7012
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007013 // If the shuffle can be profitably rewritten as a narrower shuffle, then
7014 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00007015 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
7016 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00007017 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007018 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007019 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00007020 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00007021 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007022 // FIXME: Figure out a cleaner way to do this.
7023 // Try to make use of movq to zero out the top part.
7024 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00007025 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007026 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00007027 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00007028 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
7029 NewVT, true, false))
7030 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007031 DAG, Subtarget, dl);
7032 }
7033 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00007034 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00007035 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00007036 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00007037 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
7038 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
7039 DAG, Subtarget, dl);
7040 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007041 }
7042 }
7043 return SDValue();
7044}
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007047X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007048 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00007049 SDValue V1 = Op.getOperand(0);
7050 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00007051 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007052 SDLoc dl(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00007053 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00007054 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007055 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00007056 bool V1IsSplat = false;
7057 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00007058 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007059 bool HasFp256 = Subtarget->hasFp256();
7060 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00007061 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00007062 bool OptForSize = MF.getFunction()->getAttributes().
7063 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007064
Craig Topper3426a3e2011-11-14 06:46:21 +00007065 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00007066
Elena Demikhovsky16db7102012-01-12 20:33:10 +00007067 if (V1IsUndef && V2IsUndef)
7068 return DAG.getUNDEF(VT);
7069
7070 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00007071
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007072 // Vector shuffle lowering takes 3 steps:
7073 //
7074 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
7075 // narrowing and commutation of operands should be handled.
7076 // 2) Matching of shuffles with known shuffle masks to x86 target specific
7077 // shuffle nodes.
7078 // 3) Rewriting of unmatched masks into new generic shuffle operations,
7079 // so the shuffle can be broken into other shuffles and the legalizer can
7080 // try the lowering again.
7081 //
Craig Topper3426a3e2011-11-14 06:46:21 +00007082 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007083 // be matched during isel, all of them must be converted to a target specific
7084 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00007085
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007086 // Normalize the input vectors. Here splats, zeroed vectors, profitable
7087 // narrowing and commutation of operands should be handled. The actual code
7088 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00007089 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00007090 if (NewOp.getNode())
7091 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00007092
Craig Topper5aaffa82012-02-19 02:53:47 +00007093 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
7094
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007095 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
7096 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007097 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007098 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007099 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007100 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00007101
Craig Topperdd637ae2012-02-19 05:41:45 +00007102 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00007103 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00007104 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00007105
Craig Topperdd637ae2012-02-19 05:41:45 +00007106 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00007107 return getMOVHighToLow(Op, dl, DAG);
7108
7109 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007110 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00007111 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00007112 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00007113
Craig Topper5aaffa82012-02-19 02:53:47 +00007114 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00007115 // The actual implementation will match the mask in the if above and then
7116 // during isel it can match several different instructions, not only pshufd
7117 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00007118 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
7119 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00007120
Craig Topper5aaffa82012-02-19 02:53:47 +00007121 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00007122
Craig Topper1accb7e2012-01-10 06:54:16 +00007123 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00007124 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
7125
Nadav Roteme4ccfef2012-12-07 19:01:13 +00007126 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
7127 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
7128 DAG);
7129
Craig Topperb3982da2011-12-31 23:50:21 +00007130 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00007131 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00007132 }
Eric Christopherfd179292009-08-27 18:07:15 +00007133
Benjamin Kramera0de26c2013-05-17 14:48:34 +00007134 if (isPALIGNRMask(M, VT, Subtarget))
7135 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
7136 getShufflePALIGNRImmediate(SVOp),
7137 DAG);
7138
Evan Chengf26ffe92008-05-29 08:22:04 +00007139 // Check if this can be converted into a logical shift.
7140 bool isLeft = false;
7141 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00007142 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00007143 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00007144 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007145 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00007146 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00007147 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007148 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00007149 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00007150 }
Eric Christopherfd179292009-08-27 18:07:15 +00007151
Craig Topper5aaffa82012-02-19 02:53:47 +00007152 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007153 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00007154 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00007155 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00007156 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00007157 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
7158
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00007159 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00007160 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
7161 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00007162 }
Eric Christopherfd179292009-08-27 18:07:15 +00007163
Nate Begeman9008ca62009-04-27 18:41:29 +00007164 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007165 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00007166 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00007167
Craig Topperdd637ae2012-02-19 05:41:45 +00007168 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00007169 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00007170
Craig Topperdd637ae2012-02-19 05:41:45 +00007171 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00007172 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00007173
Craig Topperdd637ae2012-02-19 05:41:45 +00007174 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00007175 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00007176
Craig Topperdd637ae2012-02-19 05:41:45 +00007177 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00007178 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007179
Craig Topperdd637ae2012-02-19 05:41:45 +00007180 if (ShouldXformToMOVHLPS(M, VT) ||
7181 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00007182 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007183
Evan Chengf26ffe92008-05-29 08:22:04 +00007184 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00007185 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00007186 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007187 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00007188 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00007189 }
Eric Christopherfd179292009-08-27 18:07:15 +00007190
Evan Cheng9eca5e82006-10-25 21:49:50 +00007191 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00007192 // FIXME: This should also accept a bitcast of a splat? Be careful, not
7193 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00007194 V1IsSplat = isSplatVector(V1.getNode());
7195 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00007196
Chris Lattner8a594482007-11-25 00:24:49 +00007197 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00007198 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
7199 CommuteVectorShuffleMask(M, NumElems);
7200 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00007201 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00007202 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00007203 }
7204
Craig Topperbeabc6c2011-12-05 06:56:46 +00007205 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007206 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00007207 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00007208 return V1;
7209 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
7210 // the instruction selector will not match, so get a canonical MOVL with
7211 // swapped operands to undo the commute.
7212 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00007213 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007214
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007215 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007216 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00007217
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007218 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007219 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00007220
Evan Cheng9bbbb982006-10-25 20:48:19 +00007221 if (V2IsSplat) {
7222 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007223 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00007224 // new vector_shuffle with the corrected mask.p
7225 SmallVector<int, 8> NewMask(M.begin(), M.end());
7226 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007227 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00007228 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007229 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00007230 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007231 }
7232
Evan Cheng9eca5e82006-10-25 21:49:50 +00007233 if (Commuted) {
7234 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00007235 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00007236 CommuteVectorShuffleMask(M, NumElems);
7237 std::swap(V1, V2);
7238 std::swap(V1IsSplat, V2IsSplat);
7239 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00007240
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007241 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00007242 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00007243
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007244 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00007245 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00007246 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007247
Nate Begeman9008ca62009-04-27 18:41:29 +00007248 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007249 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00007250 return CommuteVectorShuffle(SVOp, DAG);
7251
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00007252 // The checks below are all present in isShuffleMaskLegal, but they are
7253 // inlined here right now to enable us to directly emit target specific
7254 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00007255
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00007256 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
7257 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00007258 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00007259 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00007260 }
7261
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007262 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007263 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007264 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007265 DAG);
7266
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007267 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007268 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007269 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007270 DAG);
7271
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007272 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00007273 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00007274 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00007275
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007276 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007277 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007278 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007279 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007280
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007281 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007282 // Generate target specific nodes for 128 or 256-bit shuffles only
7283 // supported in the AVX instruction set.
7284 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007285
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007286 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007287 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007288 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7289
Craig Topper70b883b2011-11-28 10:14:51 +00007290 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007291 if (isVPERMILPMask(M, VT, HasFp256)) {
7292 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007293 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007294 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007295 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007296 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007297 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007298
Craig Topper70b883b2011-11-28 10:14:51 +00007299 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007300 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007301 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007302 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007303
Craig Topper1842ba02012-04-23 06:38:28 +00007304 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007305 if (BlendOp.getNode())
7306 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007307
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007308 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007309 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007310 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007311 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007312 }
Craig Topper92040742012-04-16 06:43:40 +00007313 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7314 &permclMask[0], 8);
7315 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007316 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007317 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007318 }
Craig Topper095c5282012-04-15 23:48:57 +00007319
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007320 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007321 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007322 getShuffleCLImmediate(SVOp), DAG);
7323
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007324 //===--------------------------------------------------------------------===//
7325 // Since no target specific shuffle was selected for this generic one,
7326 // lower it into other known shuffles. FIXME: this isn't true yet, but
7327 // this is the plan.
7328 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007329
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007330 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7331 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007332 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007333 if (NewOp.getNode())
7334 return NewOp;
7335 }
7336
7337 if (VT == MVT::v16i8) {
7338 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7339 if (NewOp.getNode())
7340 return NewOp;
7341 }
7342
Elena Demikhovsky41789462012-09-06 12:42:01 +00007343 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007344 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007345 if (NewOp.getNode())
7346 return NewOp;
7347 }
7348
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007349 // Handle all 128-bit wide vectors with 4 elements, and match them with
7350 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007351 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007352 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7353
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007354 // Handle general 256-bit shuffles
7355 if (VT.is256BitVector())
7356 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7357
Dan Gohman475871a2008-07-27 21:46:04 +00007358 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007359}
7360
Craig Topperf84b7502013-01-20 00:50:58 +00007361static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007362 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007363 SDLoc dl(Op);
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007364
Craig Topper45e1c752013-01-20 00:38:18 +00007365 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007366 return SDValue();
7367
Duncan Sands83ec4b62008-06-06 12:08:01 +00007368 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007369 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007370 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007371 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007372 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007373 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007374 }
7375
7376 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007377 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7378 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7379 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007380 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7381 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007382 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007383 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007384 Op.getOperand(0)),
7385 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007386 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007387 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007388 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007389 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007390 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007391 }
7392
7393 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007394 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7395 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007396 // result has a single use which is a store or a bitcast to i32. And in
7397 // the case of a store, it's not worth it if the index is a constant 0,
7398 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007399 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007400 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007401 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007402 if ((User->getOpcode() != ISD::STORE ||
7403 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7404 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007405 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007407 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007408 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007409 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007410 Op.getOperand(0)),
7411 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007412 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007413 }
7414
7415 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007416 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007417 if (isa<ConstantSDNode>(Op.getOperand(1)))
7418 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007419 }
Dan Gohman475871a2008-07-27 21:46:04 +00007420 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007421}
7422
Dan Gohman475871a2008-07-27 21:46:04 +00007423SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007424X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7425 SelectionDAG &DAG) const {
Elena Demikhovsky83952512013-07-31 11:35:14 +00007426 SDLoc dl(Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007427 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007428 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007429
David Greene74a579d2011-02-10 16:57:36 +00007430 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007431 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007432
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007433 // If this is a 256-bit vector result, first extract the 128-bit vector and
7434 // then extract the element from the 128-bit vector.
Elena Demikhovsky83952512013-07-31 11:35:14 +00007435 if (VecVT.is256BitVector() || VecVT.is512BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007436 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007437 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7438
7439 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007440 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
Elena Demikhovsky83952512013-07-31 11:35:14 +00007441 EVT EltVT = VecVT.getVectorElementType();
David Greene74a579d2011-02-10 16:57:36 +00007442
Elena Demikhovsky83952512013-07-31 11:35:14 +00007443 unsigned ElemsPerChunk = 128 / EltVT.getSizeInBits();
7444
7445 //if (IdxVal >= NumElems/2)
7446 // IdxVal -= NumElems/2;
7447 IdxVal -= (IdxVal/ElemsPerChunk)*ElemsPerChunk;
David Greene74a579d2011-02-10 16:57:36 +00007448 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007449 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007450 }
7451
Craig Topper7a9a28b2012-08-12 02:23:29 +00007452 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007453
Craig Topperd0a31172012-01-10 06:37:29 +00007454 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007455 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007456 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007457 return Res;
7458 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007459
Craig Topper45e1c752013-01-20 00:38:18 +00007460 MVT VT = Op.getValueType().getSimpleVT();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007461 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007462 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007463 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007464 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007465 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007466 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7467 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007468 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007469 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007470 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007471 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007472 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007473 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007474 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007475 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007476 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007477 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007478 }
7479
7480 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007481 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007482 if (Idx == 0)
7483 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007484
Evan Cheng0db9fe62006-04-25 20:13:52 +00007485 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007486 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007487 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007488 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007489 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007490 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007491 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007492 }
7493
7494 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007495 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7496 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7497 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007498 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007499 if (Idx == 0)
7500 return Op;
7501
7502 // UNPCKHPD the element to the lowest double word, then movsd.
7503 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7504 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007505 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007506 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007507 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007508 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007509 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007510 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007511 }
7512
Dan Gohman475871a2008-07-27 21:46:04 +00007513 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007514}
7515
Craig Topperf84b7502013-01-20 00:50:58 +00007516static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007517 MVT VT = Op.getValueType().getSimpleVT();
7518 MVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007519 SDLoc dl(Op);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007520
Dan Gohman475871a2008-07-27 21:46:04 +00007521 SDValue N0 = Op.getOperand(0);
7522 SDValue N1 = Op.getOperand(1);
7523 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007524
Craig Topper7a9a28b2012-08-12 02:23:29 +00007525 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007526 return SDValue();
7527
Dan Gohman8a55ce42009-09-23 21:02:20 +00007528 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007529 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007530 unsigned Opc;
7531 if (VT == MVT::v8i16)
7532 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007533 else if (VT == MVT::v16i8)
7534 Opc = X86ISD::PINSRB;
7535 else
7536 Opc = X86ISD::PINSRB;
7537
Nate Begeman14d12ca2008-02-11 04:19:36 +00007538 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7539 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007540 if (N1.getValueType() != MVT::i32)
7541 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7542 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007543 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007544 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007545 }
7546
7547 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007548 // Bits [7:6] of the constant are the source select. This will always be
7549 // zero here. The DAG Combiner may combine an extract_elt index into these
7550 // bits. For example (insert (extract, 3), 2) could be matched by putting
7551 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007552 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007553 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007554 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007555 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007556 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007557 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007559 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007560 }
7561
7562 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007563 // PINSR* works with constant index.
7564 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007565 }
Dan Gohman475871a2008-07-27 21:46:04 +00007566 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007567}
7568
Dan Gohman475871a2008-07-27 21:46:04 +00007569SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007570X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007571 MVT VT = Op.getValueType().getSimpleVT();
7572 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007573
Andrew Trickac6d9be2013-05-25 02:42:55 +00007574 SDLoc dl(Op);
David Greene6b381262011-02-09 15:32:06 +00007575 SDValue N0 = Op.getOperand(0);
7576 SDValue N1 = Op.getOperand(1);
7577 SDValue N2 = Op.getOperand(2);
7578
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007579 // If this is a 256-bit vector result, first extract the 128-bit vector,
7580 // insert the element into the extracted half and then place it back.
Elena Demikhovsky83952512013-07-31 11:35:14 +00007581 if (VT.is256BitVector() || VT.is512BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007582 if (!isa<ConstantSDNode>(N2))
7583 return SDValue();
7584
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007585 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007586 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007587 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007588
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007589 // Insert the element into the desired half.
Elena Demikhovsky83952512013-07-31 11:35:14 +00007590 unsigned NumEltsIn128 = 128/EltVT.getSizeInBits();
7591 unsigned IdxIn128 = IdxVal - (IdxVal/NumEltsIn128) * NumEltsIn128;
7592
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007593 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
Elena Demikhovsky83952512013-07-31 11:35:14 +00007594 DAG.getConstant(IdxIn128, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007595
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007596 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007597 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007598 }
7599
Craig Topperd0a31172012-01-10 06:37:29 +00007600 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007601 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7602
Dan Gohman8a55ce42009-09-23 21:02:20 +00007603 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007604 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007605
Dan Gohman8a55ce42009-09-23 21:02:20 +00007606 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007607 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7608 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007609 if (N1.getValueType() != MVT::i32)
7610 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7611 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007612 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007613 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007614 }
Dan Gohman475871a2008-07-27 21:46:04 +00007615 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007616}
7617
Craig Topper55b24052012-09-11 06:15:32 +00007618static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007619 LLVMContext *Context = DAG.getContext();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007620 SDLoc dl(Op);
Craig Topper45e1c752013-01-20 00:38:18 +00007621 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007622
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007623 // If this is a 256-bit vector result, first insert into a 128-bit
7624 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007625 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007626 // Insert into a 128-bit vector.
Elena Demikhovsky83952512013-07-31 11:35:14 +00007627 unsigned SizeFactor = OpVT.getSizeInBits()/128;
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007628 EVT VT128 = EVT::getVectorVT(*Context,
7629 OpVT.getVectorElementType(),
Elena Demikhovsky83952512013-07-31 11:35:14 +00007630 OpVT.getVectorNumElements() / SizeFactor);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007631
7632 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7633
7634 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007635 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007636 }
7637
Craig Topperd77d2fe2012-04-29 20:22:05 +00007638 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007639 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007641
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007643 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007644 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007645 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007646}
7647
David Greene91585092011-01-26 15:38:49 +00007648// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7649// a simple subregister reference or explicit instructions to grab
7650// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007651static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7652 SelectionDAG &DAG) {
Elena Demikhovsky83952512013-07-31 11:35:14 +00007653 SDLoc dl(Op);
7654 SDValue In = Op.getOperand(0);
7655 SDValue Idx = Op.getOperand(1);
7656 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7657 EVT ResVT = Op.getValueType();
7658 EVT InVT = In.getValueType();
David Greenea5f26012011-02-07 19:36:54 +00007659
Elena Demikhovsky83952512013-07-31 11:35:14 +00007660 if (Subtarget->hasFp256()) {
7661 if (ResVT.is128BitVector() &&
7662 (InVT.is256BitVector() || InVT.is512BitVector()) &&
Craig Topperb14940a2012-04-22 20:55:18 +00007663 isa<ConstantSDNode>(Idx)) {
Elena Demikhovsky83952512013-07-31 11:35:14 +00007664 return Extract128BitVector(In, IdxVal, DAG, dl);
7665 }
7666 if (ResVT.is256BitVector() && InVT.is512BitVector() &&
7667 isa<ConstantSDNode>(Idx)) {
7668 return Extract256BitVector(In, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007669 }
David Greene91585092011-01-26 15:38:49 +00007670 }
7671 return SDValue();
7672}
7673
David Greenecfe33c42011-01-26 19:13:22 +00007674// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7675// simple superregister reference or explicit instructions to insert
7676// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007677static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7678 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007679 if (Subtarget->hasFp256()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007680 SDLoc dl(Op.getNode());
David Greenecfe33c42011-01-26 19:13:22 +00007681 SDValue Vec = Op.getNode()->getOperand(0);
7682 SDValue SubVec = Op.getNode()->getOperand(1);
7683 SDValue Idx = Op.getNode()->getOperand(2);
7684
Elena Demikhovsky83952512013-07-31 11:35:14 +00007685 if ((Op.getNode()->getValueType(0).is256BitVector() ||
7686 Op.getNode()->getValueType(0).is512BitVector()) &&
Craig Topper7a9a28b2012-08-12 02:23:29 +00007687 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007688 isa<ConstantSDNode>(Idx)) {
7689 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7690 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007691 }
Elena Demikhovsky83952512013-07-31 11:35:14 +00007692
7693 if (Op.getNode()->getValueType(0).is512BitVector() &&
7694 SubVec.getNode()->getValueType(0).is256BitVector() &&
7695 isa<ConstantSDNode>(Idx)) {
7696 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7697 return Insert256BitVector(Vec, SubVec, IdxVal, DAG, dl);
7698 }
David Greenecfe33c42011-01-26 19:13:22 +00007699 }
7700 return SDValue();
7701}
7702
Bill Wendling056292f2008-09-16 21:48:12 +00007703// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7704// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7705// one of the above mentioned nodes. It has to be wrapped because otherwise
7706// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7707// be used to form addressing mode. These wrapped nodes will be selected
7708// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007709SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007710X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007711 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007712
Chris Lattner41621a22009-06-26 19:22:52 +00007713 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7714 // global base reg.
7715 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007716 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007717 CodeModel::Model M = getTargetMachine().getCodeModel();
7718
Chris Lattner4f066492009-07-11 20:29:19 +00007719 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007720 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007721 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007722 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007723 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007724 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007725 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007726
Evan Cheng1606e8e2009-03-13 07:51:59 +00007727 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007728 CP->getAlignment(),
7729 CP->getOffset(), OpFlag);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007730 SDLoc DL(CP);
Chris Lattner18c59872009-06-27 04:16:01 +00007731 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007732 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007733 if (OpFlag) {
7734 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007735 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007736 SDLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007737 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007738 }
7739
7740 return Result;
7741}
7742
Dan Gohmand858e902010-04-17 15:26:15 +00007743SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007744 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007745
Chris Lattner18c59872009-06-27 04:16:01 +00007746 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7747 // global base reg.
7748 unsigned char OpFlag = 0;
7749 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007750 CodeModel::Model M = getTargetMachine().getCodeModel();
7751
Chris Lattner4f066492009-07-11 20:29:19 +00007752 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007753 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007754 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007755 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007756 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007757 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007758 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007759
Chris Lattner18c59872009-06-27 04:16:01 +00007760 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7761 OpFlag);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007762 SDLoc DL(JT);
Chris Lattner18c59872009-06-27 04:16:01 +00007763 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007764
Chris Lattner18c59872009-06-27 04:16:01 +00007765 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007766 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007767 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7768 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007769 SDLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007770 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007771
Chris Lattner18c59872009-06-27 04:16:01 +00007772 return Result;
7773}
7774
7775SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007776X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007777 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007778
Chris Lattner18c59872009-06-27 04:16:01 +00007779 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7780 // global base reg.
7781 unsigned char OpFlag = 0;
7782 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007783 CodeModel::Model M = getTargetMachine().getCodeModel();
7784
Chris Lattner4f066492009-07-11 20:29:19 +00007785 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007786 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7787 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7788 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007789 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007790 } else if (Subtarget->isPICStyleGOT()) {
7791 OpFlag = X86II::MO_GOT;
7792 } else if (Subtarget->isPICStyleStubPIC()) {
7793 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7794 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7795 OpFlag = X86II::MO_DARWIN_NONLAZY;
7796 }
Eric Christopherfd179292009-08-27 18:07:15 +00007797
Chris Lattner18c59872009-06-27 04:16:01 +00007798 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007799
Andrew Trickac6d9be2013-05-25 02:42:55 +00007800 SDLoc DL(Op);
Chris Lattner18c59872009-06-27 04:16:01 +00007801 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007802
Chris Lattner18c59872009-06-27 04:16:01 +00007803 // With PIC, the address is actually $g + Offset.
7804 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007805 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007806 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7807 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007808 SDLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007809 Result);
7810 }
Eric Christopherfd179292009-08-27 18:07:15 +00007811
Eli Friedman586272d2011-08-11 01:48:05 +00007812 // For symbols that require a load from a stub to get the address, emit the
7813 // load.
7814 if (isGlobalStubReference(OpFlag))
7815 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007816 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007817
Chris Lattner18c59872009-06-27 04:16:01 +00007818 return Result;
7819}
7820
Dan Gohman475871a2008-07-27 21:46:04 +00007821SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007822X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007823 // Create the TargetBlockAddressAddress node.
7824 unsigned char OpFlags =
7825 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007826 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007827 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007828 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007829 SDLoc dl(Op);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007830 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7831 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007832
Dan Gohmanf705adb2009-10-30 01:28:02 +00007833 if (Subtarget->isPICStyleRIPRel() &&
7834 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007835 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7836 else
7837 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007838
Dan Gohman29cbade2009-11-20 23:18:13 +00007839 // With PIC, the address is actually $g + Offset.
7840 if (isGlobalRelativeToPICBase(OpFlags)) {
7841 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7842 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7843 Result);
7844 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007845
7846 return Result;
7847}
7848
7849SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00007850X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, SDLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007851 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007852 // Create the TargetGlobalAddress node, folding in the constant
7853 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007854 unsigned char OpFlags =
7855 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007856 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007857 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007858 if (OpFlags == X86II::MO_NO_FLAG &&
7859 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007860 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007861 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007862 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007863 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007864 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007865 }
Eric Christopherfd179292009-08-27 18:07:15 +00007866
Chris Lattner4f066492009-07-11 20:29:19 +00007867 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007868 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007869 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7870 else
7871 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007872
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007873 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007874 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007875 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7876 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007877 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007879
Chris Lattner36c25012009-07-10 07:34:39 +00007880 // For globals that require a load from a stub to get the address, emit the
7881 // load.
7882 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007883 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007884 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007885
Dan Gohman6520e202008-10-18 02:06:02 +00007886 // If there was a non-zero offset that we didn't fold, create an explicit
7887 // addition for it.
7888 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007889 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007890 DAG.getConstant(Offset, getPointerTy()));
7891
Evan Cheng0db9fe62006-04-25 20:13:52 +00007892 return Result;
7893}
7894
Evan Chengda43bcf2008-09-24 00:05:32 +00007895SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007896X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007897 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007898 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007899 return LowerGlobalAddress(GV, SDLoc(Op), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007900}
7901
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007902static SDValue
7903GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007904 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007905 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007906 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007907 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007908 SDLoc dl(GA);
Devang Patel0d881da2010-07-06 22:08:15 +00007909 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007910 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007911 GA->getOffset(),
7912 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007913
7914 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7915 : X86ISD::TLSADDR;
7916
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007917 if (InFlag) {
7918 SDValue Ops[] = { Chain, TGA, *InFlag };
Michael Liao0ee17002013-04-19 04:03:37 +00007919 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops));
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007920 } else {
7921 SDValue Ops[] = { Chain, TGA };
Michael Liao0ee17002013-04-19 04:03:37 +00007922 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops));
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007923 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007924
7925 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007926 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007927
Rafael Espindola15f1b662009-04-24 12:59:40 +00007928 SDValue Flag = Chain.getValue(1);
7929 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007930}
7931
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007932// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007933static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007934LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007935 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007936 SDValue InFlag;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007937 SDLoc dl(GA); // ? function entry point might be better
Dale Johannesendd64c412009-02-04 00:33:20 +00007938 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007939 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007940 SDLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007941 InFlag = Chain.getValue(1);
7942
Chris Lattnerb903bed2009-06-26 21:20:29 +00007943 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007944}
7945
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007946// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007947static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007948LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007949 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007950 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7951 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007952}
7953
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007954static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7955 SelectionDAG &DAG,
7956 const EVT PtrVT,
7957 bool is64Bit) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007958 SDLoc dl(GA);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007959
7960 // Get the start address of the TLS block for this module.
7961 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7962 .getInfo<X86MachineFunctionInfo>();
7963 MFI->incNumLocalDynamicTLSAccesses();
7964
7965 SDValue Base;
7966 if (is64Bit) {
7967 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7968 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7969 } else {
7970 SDValue InFlag;
7971 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007972 DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), InFlag);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007973 InFlag = Chain.getValue(1);
7974 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7975 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7976 }
7977
7978 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7979 // of Base.
7980
7981 // Build x@dtpoff.
7982 unsigned char OperandFlags = X86II::MO_DTPOFF;
7983 unsigned WrapperKind = X86ISD::Wrapper;
7984 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7985 GA->getValueType(0),
7986 GA->getOffset(), OperandFlags);
7987 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7988
7989 // Add x@dtpoff with the base.
7990 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7991}
7992
Hans Wennborg228756c2012-05-11 10:11:01 +00007993// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007994static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007995 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007996 bool is64Bit, bool isPIC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007997 SDLoc dl(GA);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007998
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007999 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
8000 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
8001 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00008002
Michael J. Spencerec38de22010-10-10 22:04:20 +00008003 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00008004 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008005 MachinePointerInfo(Ptr),
8006 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00008007
Chris Lattnerb903bed2009-06-26 21:20:29 +00008008 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00008009 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
8010 // initialexec.
8011 unsigned WrapperKind = X86ISD::Wrapper;
8012 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00008013 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00008014 } else if (model == TLSModel::InitialExec) {
8015 if (is64Bit) {
8016 OperandFlags = X86II::MO_GOTTPOFF;
8017 WrapperKind = X86ISD::WrapperRIP;
8018 } else {
8019 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
8020 }
Chris Lattner18c59872009-06-27 04:16:01 +00008021 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00008022 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00008023 }
Eric Christopherfd179292009-08-27 18:07:15 +00008024
Hans Wennborg228756c2012-05-11 10:11:01 +00008025 // emit "addl x@ntpoff,%eax" (local exec)
8026 // or "addl x@indntpoff,%eax" (initial exec)
8027 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00008028 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00008029 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00008030 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00008031 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00008032
Hans Wennborg228756c2012-05-11 10:11:01 +00008033 if (model == TLSModel::InitialExec) {
8034 if (isPIC && !is64Bit) {
8035 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00008036 DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT),
Hans Wennborg228756c2012-05-11 10:11:01 +00008037 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00008038 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00008039
8040 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
8041 MachinePointerInfo::getGOT(), false, false, false,
8042 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00008043 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00008044
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008045 // The address of the thread local variable is the add of the thread
8046 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00008047 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008048}
8049
Dan Gohman475871a2008-07-27 21:46:04 +00008050SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00008051X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00008052
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008053 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00008054 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00008055
Eric Christopher30ef0e52010-06-03 04:07:48 +00008056 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00008057 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008058
Eric Christopher30ef0e52010-06-03 04:07:48 +00008059 switch (model) {
8060 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00008061 if (Subtarget->is64Bit())
8062 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
8063 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00008064 case TLSModel::LocalDynamic:
8065 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
8066 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00008067 case TLSModel::InitialExec:
8068 case TLSModel::LocalExec:
8069 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00008070 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00008071 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008072 }
Craig Toppere8eb1162012-04-23 03:26:18 +00008073 llvm_unreachable("Unknown TLS model.");
8074 }
8075
8076 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00008077 // Darwin only has one model of TLS. Lower to that.
8078 unsigned char OpFlag = 0;
8079 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
8080 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00008081
Eric Christopher30ef0e52010-06-03 04:07:48 +00008082 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
8083 // global base reg.
8084 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
8085 !Subtarget->is64Bit();
8086 if (PIC32)
8087 OpFlag = X86II::MO_TLVP_PIC_BASE;
8088 else
8089 OpFlag = X86II::MO_TLVP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008090 SDLoc DL(Op);
Devang Patel0d881da2010-07-06 22:08:15 +00008091 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00008092 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00008093 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00008094 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008095
Eric Christopher30ef0e52010-06-03 04:07:48 +00008096 // With PIC32, the address is actually $g + Offset.
8097 if (PIC32)
8098 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
8099 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00008100 SDLoc(), getPointerTy()),
Eric Christopher30ef0e52010-06-03 04:07:48 +00008101 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008102
Eric Christopher30ef0e52010-06-03 04:07:48 +00008103 // Lowering the machine isd will make sure everything is in the right
8104 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00008105 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008106 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00008107 SDValue Args[] = { Chain, Offset };
8108 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008109
Eric Christopher30ef0e52010-06-03 04:07:48 +00008110 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
8111 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8112 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008113
Eric Christopher30ef0e52010-06-03 04:07:48 +00008114 // And our return value (tls address) is in the standard call return value
8115 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00008116 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00008117 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
8118 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00008119 }
8120
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00008121 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00008122 // Just use the implicit TLS architecture
8123 // Need to generate someting similar to:
8124 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
8125 // ; from TEB
8126 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
8127 // mov rcx, qword [rdx+rcx*8]
8128 // mov eax, .tls$:tlsvar
8129 // [rax+rcx] contains the address
8130 // Windows 64bit: gs:0x58
8131 // Windows 32bit: fs:__tls_array
8132
8133 // If GV is an alias then use the aliasee for determining
8134 // thread-localness.
8135 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
8136 GV = GA->resolveAliasedGlobal(false);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008137 SDLoc dl(GA);
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00008138 SDValue Chain = DAG.getEntryNode();
8139
8140 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00008141 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
8142 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00008143 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
8144 ? Type::getInt8PtrTy(*DAG.getContext(),
8145 256)
8146 : Type::getInt32PtrTy(*DAG.getContext(),
8147 257));
8148
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00008149 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
8150 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
8151 DAG.getExternalSymbol("_tls_array", getPointerTy()));
8152
8153 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00008154 MachinePointerInfo(Ptr),
8155 false, false, false, 0);
8156
8157 // Load the _tls_index variable
8158 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
8159 if (Subtarget->is64Bit())
8160 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
8161 IDX, MachinePointerInfo(), MVT::i32,
8162 false, false, 0);
8163 else
8164 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
8165 false, false, false, 0);
8166
Chandler Carruth426c2bf2012-11-01 09:14:31 +00008167 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00008168 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00008169 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
8170
8171 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
8172 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
8173 false, false, false, 0);
8174
8175 // Get the offset of start of .tls section
8176 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
8177 GA->getValueType(0),
8178 GA->getOffset(), X86II::MO_SECREL);
8179 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
8180
8181 // The address of the thread local variable is the add of the thread
8182 // pointer with the offset of the variable.
8183 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00008184 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008185
David Blaikie4d6ccb52012-01-20 21:51:11 +00008186 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008187}
8188
Chad Rosierb90d2a92012-01-03 23:19:12 +00008189/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
8190/// and take a 2 x i32 value to shift plus a shift amount.
8191SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00008192 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00008193 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00008194 unsigned VTBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008195 SDLoc dl(Op);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008196 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00008197 SDValue ShOpLo = Op.getOperand(0);
8198 SDValue ShOpHi = Op.getOperand(1);
8199 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00008200 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00008201 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00008202 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00008203
Dan Gohman475871a2008-07-27 21:46:04 +00008204 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008205 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00008206 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
8207 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008208 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008209 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
8210 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008211 }
Evan Chenge3413162006-01-09 18:33:28 +00008212
Owen Anderson825b72b2009-08-11 20:47:22 +00008213 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
8214 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00008215 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00008217
Dan Gohman475871a2008-07-27 21:46:04 +00008218 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00008219 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00008220 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
8221 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00008222
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008223 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00008224 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
8225 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008226 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00008227 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
8228 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00008229 }
8230
Dan Gohman475871a2008-07-27 21:46:04 +00008231 SDValue Ops[2] = { Lo, Hi };
Michael Liao0ee17002013-04-19 04:03:37 +00008232 return DAG.getMergeValues(Ops, array_lengthof(Ops), dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008233}
Evan Chenga3195e82006-01-12 22:54:21 +00008234
Dan Gohmand858e902010-04-17 15:26:15 +00008235SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
8236 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008237 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00008238
Dale Johannesen0488fb62010-09-30 23:57:10 +00008239 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008240 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008241
Owen Anderson825b72b2009-08-11 20:47:22 +00008242 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00008243 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00008244
Eli Friedman36df4992009-05-27 00:47:34 +00008245 // These are really Legal; return the operand so the caller accepts it as
8246 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008247 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00008248 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00008250 Subtarget->is64Bit()) {
8251 return Op;
8252 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008253
Andrew Trickac6d9be2013-05-25 02:42:55 +00008254 SDLoc dl(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008255 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00008256 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00008257 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008258 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00008259 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00008260 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008261 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008262 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008263 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
8264}
Evan Cheng0db9fe62006-04-25 20:13:52 +00008265
Owen Andersone50ed302009-08-10 22:56:29 +00008266SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008267 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00008268 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008269 // Build the FILD
Andrew Trickac6d9be2013-05-25 02:42:55 +00008270 SDLoc DL(Op);
Chris Lattner5a88b832007-02-25 07:10:00 +00008271 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00008272 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008273 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008274 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00008275 else
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008277
Chris Lattner492a43e2010-09-22 01:28:21 +00008278 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00008279
Stuart Hastings84be9582011-06-02 15:57:11 +00008280 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
8281 MachineMemOperand *MMO;
8282 if (FI) {
8283 int SSFI = FI->getIndex();
8284 MMO =
8285 DAG.getMachineFunction()
8286 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8287 MachineMemOperand::MOLoad, ByteSize, ByteSize);
8288 } else {
8289 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
8290 StackSlot = StackSlot.getOperand(1);
8291 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008292 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008293 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
8294 X86ISD::FILD, DL,
8295 Tys, Ops, array_lengthof(Ops),
8296 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008297
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008298 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008299 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008300 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008301
8302 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8303 // shouldn't be necessary except that RFP cannot be live across
8304 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008305 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008306 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8307 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008308 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008309 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008310 SDValue Ops[] = {
8311 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8312 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008313 MachineMemOperand *MMO =
8314 DAG.getMachineFunction()
8315 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008316 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008317
Chris Lattner492a43e2010-09-22 01:28:21 +00008318 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8319 Ops, array_lengthof(Ops),
8320 Op.getValueType(), MMO);
8321 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008322 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008323 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008324 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008325
Evan Cheng0db9fe62006-04-25 20:13:52 +00008326 return Result;
8327}
8328
Bill Wendling8b8a6362009-01-17 03:56:04 +00008329// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008330SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8331 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008332 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008333 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008334 movq %rax, %xmm0
8335 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8336 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8337 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008338 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008339 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008340 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008341 addpd %xmm1, %xmm0
8342 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008343 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008344
Andrew Trickac6d9be2013-05-25 02:42:55 +00008345 SDLoc dl(Op);
Owen Andersona90b3dc2009-07-15 21:51:10 +00008346 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008347
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008348 // Build some magic constants.
Craig Topperda129a22013-07-15 06:54:12 +00008349 static const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
Chris Lattner7302d802012-02-06 21:56:39 +00008350 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008351 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008352
Chris Lattner97484792012-01-25 09:56:22 +00008353 SmallVector<Constant*,2> CV1;
8354 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008355 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8356 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008357 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008358 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8359 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008360 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008361 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008362
Bill Wendling397ae212012-01-05 02:13:20 +00008363 // Load the 64-bit value into an XMM register.
8364 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8365 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008366 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008367 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008368 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008369 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8370 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8371 CLod0);
8372
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008374 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008375 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008376 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008377 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008378 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008379
Craig Topperd0a31172012-01-10 06:37:29 +00008380 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008381 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8382 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8383 } else {
8384 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8385 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8386 S2F, 0x4E, DAG);
8387 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8388 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8389 Sub);
8390 }
8391
8392 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008393 DAG.getIntPtrConstant(0));
8394}
8395
Bill Wendling8b8a6362009-01-17 03:56:04 +00008396// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008397SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8398 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008399 SDLoc dl(Op);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008400 // FP constant to bias correct the final result.
8401 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008402 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008403
8404 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008405 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008406 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008407
Eli Friedmanf3704762011-08-29 21:15:46 +00008408 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008409 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008410
Owen Anderson825b72b2009-08-11 20:47:22 +00008411 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008412 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008413 DAG.getIntPtrConstant(0));
8414
8415 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008416 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008417 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008418 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008419 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008420 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008421 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008422 MVT::v2f64, Bias)));
8423 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008424 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008425 DAG.getIntPtrConstant(0));
8426
8427 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008429
8430 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008431 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008432
Craig Topper69947b92012-04-23 06:57:04 +00008433 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008434 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008435 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008436 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008437 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008438
8439 // Handle final rounding.
8440 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008441}
8442
Michael Liaoa7554632012-10-23 17:36:08 +00008443SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8444 SelectionDAG &DAG) const {
8445 SDValue N0 = Op.getOperand(0);
8446 EVT SVT = N0.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008447 SDLoc dl(Op);
Michael Liaoa7554632012-10-23 17:36:08 +00008448
8449 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8450 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8451 "Custom UINT_TO_FP is not supported!");
8452
Craig Topperb99bafe2013-01-21 06:21:54 +00008453 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8454 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008455 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8456 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8457}
8458
Dan Gohmand858e902010-04-17 15:26:15 +00008459SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8460 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008461 SDValue N0 = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008462 SDLoc dl(Op);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008463
Michael Liaoa7554632012-10-23 17:36:08 +00008464 if (Op.getValueType().isVector())
8465 return lowerUINT_TO_FP_vec(Op, DAG);
8466
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008467 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008468 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8469 // the optimization here.
8470 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008471 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008472
Owen Andersone50ed302009-08-10 22:56:29 +00008473 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008474 EVT DstVT = Op.getValueType();
8475 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008476 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008477 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008478 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008479 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008480 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008481
8482 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008483 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008484 if (SrcVT == MVT::i32) {
8485 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8486 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8487 getPointerTy(), StackSlot, WordOff);
8488 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008489 StackSlot, MachinePointerInfo(),
8490 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008491 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008492 OffsetSlot, MachinePointerInfo(),
8493 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008494 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8495 return Fild;
8496 }
8497
8498 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8499 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008500 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008501 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008502 // For i64 source, we need to add the appropriate power of 2 if the input
8503 // was negative. This is the same as the optimization in
8504 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8505 // we must be careful to do the computation in x87 extended precision, not
8506 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008507 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8508 MachineMemOperand *MMO =
8509 DAG.getMachineFunction()
8510 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8511 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008512
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008513 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8514 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Michael Liao0ee17002013-04-19 04:03:37 +00008515 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops,
8516 array_lengthof(Ops), MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008517
8518 APInt FF(32, 0x5F800000ULL);
8519
8520 // Check whether the sign bit is set.
Matt Arsenault225ed702013-05-18 00:21:46 +00008521 SDValue SignSet = DAG.getSetCC(dl,
8522 getSetCCResultType(*DAG.getContext(), MVT::i64),
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008523 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8524 ISD::SETLT);
8525
8526 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8527 SDValue FudgePtr = DAG.getConstantPool(
8528 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8529 getPointerTy());
8530
8531 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8532 SDValue Zero = DAG.getIntPtrConstant(0);
8533 SDValue Four = DAG.getIntPtrConstant(4);
8534 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8535 Zero, Four);
8536 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8537
8538 // Load the value out, extending it from f32 to f80.
8539 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008540 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008541 FudgePtr, MachinePointerInfo::getConstantPool(),
8542 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008543 // Extend everything to 80 bits to force it to be done on x87.
8544 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8545 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008546}
8547
Craig Topperb99bafe2013-01-21 06:21:54 +00008548std::pair<SDValue,SDValue>
8549X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8550 bool IsSigned, bool IsReplace) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008551 SDLoc DL(Op);
Eli Friedman948e95a2009-05-23 09:59:16 +00008552
Owen Andersone50ed302009-08-10 22:56:29 +00008553 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008554
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008555 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008556 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8557 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008558 }
8559
Owen Anderson825b72b2009-08-11 20:47:22 +00008560 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8561 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008562 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008563
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008564 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008565 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008566 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008567 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008568 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008569 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008570 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008571 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008572
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008573 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8574 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008575 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008576 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008577 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008578 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008579
Evan Cheng0db9fe62006-04-25 20:13:52 +00008580 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008581 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8582 Opc = X86ISD::WIN_FTOL;
8583 else
8584 switch (DstTy.getSimpleVT().SimpleTy) {
8585 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8586 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8587 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8588 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8589 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008590
Dan Gohman475871a2008-07-27 21:46:04 +00008591 SDValue Chain = DAG.getEntryNode();
8592 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008593 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008594 // FIXME This causes a redundant load/store if the SSE-class value is already
8595 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008596 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008598 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008599 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008600 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008601 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008602 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008603 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008604 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008605
Chris Lattner492a43e2010-09-22 01:28:21 +00008606 MachineMemOperand *MMO =
8607 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8608 MachineMemOperand::MOLoad, MemSize, MemSize);
Michael Liao0ee17002013-04-19 04:03:37 +00008609 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops,
8610 array_lengthof(Ops), DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008611 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008612 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008613 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8614 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008615
Chris Lattner07290932010-09-22 01:05:16 +00008616 MachineMemOperand *MMO =
8617 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8618 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008619
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008620 if (Opc != X86ISD::WIN_FTOL) {
8621 // Build the FP_TO_INT*_IN_MEM
8622 SDValue Ops[] = { Chain, Value, StackSlot };
8623 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
Michael Liao0ee17002013-04-19 04:03:37 +00008624 Ops, array_lengthof(Ops), DstTy,
8625 MMO);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008626 return std::make_pair(FIST, StackSlot);
8627 } else {
8628 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8629 DAG.getVTList(MVT::Other, MVT::Glue),
8630 Chain, Value);
8631 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8632 MVT::i32, ftol.getValue(1));
8633 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8634 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008635 SDValue Ops[] = { eax, edx };
8636 SDValue pair = IsReplace
Michael Liao0ee17002013-04-19 04:03:37 +00008637 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, array_lengthof(Ops))
8638 : DAG.getMergeValues(Ops, array_lengthof(Ops), DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008639 return std::make_pair(pair, SDValue());
8640 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008641}
8642
Nadav Rotem0509db22012-12-28 05:45:24 +00008643static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8644 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008645 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008646 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008647 MVT InVT = In.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008648 SDLoc dl(Op);
Nadav Rotem0509db22012-12-28 05:45:24 +00008649
8650 // Optimize vectors in AVX mode:
8651 //
8652 // v8i16 -> v8i32
8653 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8654 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8655 // Concat upper and lower parts.
8656 //
8657 // v4i32 -> v4i64
8658 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8659 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8660 // Concat upper and lower parts.
8661 //
8662
8663 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8664 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8665 return SDValue();
8666
8667 if (Subtarget->hasInt256())
8668 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8669
8670 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8671 SDValue Undef = DAG.getUNDEF(InVT);
8672 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8673 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8674 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8675
Craig Toppera080daf2013-01-20 21:50:27 +00008676 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008677 VT.getVectorNumElements()/2);
8678
8679 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8680 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8681
8682 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8683}
8684
8685SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8686 SelectionDAG &DAG) const {
8687 if (Subtarget->hasFp256()) {
8688 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8689 if (Res.getNode())
8690 return Res;
8691 }
8692
8693 return SDValue();
8694}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008695SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8696 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008697 SDLoc DL(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008698 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008699 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008700 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008701
Nadav Rotem0509db22012-12-28 05:45:24 +00008702 if (Subtarget->hasFp256()) {
8703 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8704 if (Res.getNode())
8705 return Res;
8706 }
8707
Michael Liaoa7554632012-10-23 17:36:08 +00008708 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8709 VT.getVectorNumElements() != SVT.getVectorNumElements())
8710 return SDValue();
8711
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008712 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008713
8714 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008715 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008716 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8717
8718 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8719 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8720 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008721 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8722 DAG.getUNDEF(MVT::v8i16),
8723 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008724
8725 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8726}
8727
Craig Topperd713c0f2013-01-20 21:34:37 +00008728SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008729 SDLoc DL(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008730 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008731 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008732 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008733
Nadav Rotem3c22a442012-12-27 07:45:10 +00008734 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8735 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8736 if (Subtarget->hasInt256()) {
8737 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8738 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8739 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8740 ShufMask);
8741 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8742 DAG.getIntPtrConstant(0));
8743 }
8744
8745 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8746 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8747 DAG.getIntPtrConstant(0));
8748 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8749 DAG.getIntPtrConstant(2));
8750
8751 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8752 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8753
8754 // The PSHUFD mask:
8755 static const int ShufMask1[] = {0, 2, 0, 0};
8756 SDValue Undef = DAG.getUNDEF(VT);
8757 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8758 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8759
8760 // The MOVLHPS mask:
8761 static const int ShufMask2[] = {0, 1, 4, 5};
8762 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8763 }
8764
8765 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8766 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8767 if (Subtarget->hasInt256()) {
8768 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8769
8770 SmallVector<SDValue,32> pshufbMask;
8771 for (unsigned i = 0; i < 2; ++i) {
8772 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8773 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8774 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8775 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8776 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8777 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8778 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8779 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8780 for (unsigned j = 0; j < 8; ++j)
8781 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8782 }
8783 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8784 &pshufbMask[0], 32);
8785 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8786 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8787
8788 static const int ShufMask[] = {0, 2, -1, -1};
8789 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8790 &ShufMask[0]);
8791 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8792 DAG.getIntPtrConstant(0));
8793 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8794 }
8795
8796 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8797 DAG.getIntPtrConstant(0));
8798
8799 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8800 DAG.getIntPtrConstant(4));
8801
8802 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8803 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8804
8805 // The PSHUFB mask:
8806 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8807 -1, -1, -1, -1, -1, -1, -1, -1};
8808
8809 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8810 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8811 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8812
8813 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8814 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8815
8816 // The MOVLHPS Mask:
8817 static const int ShufMask2[] = {0, 1, 4, 5};
8818 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8819 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8820 }
8821
8822 // Handle truncation of V256 to V128 using shuffles.
8823 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008824 return SDValue();
8825
Nadav Rotem3c22a442012-12-27 07:45:10 +00008826 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8827 "Invalid op");
8828 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008829
8830 unsigned NumElems = VT.getVectorNumElements();
8831 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8832 NumElems * 2);
8833
Michael Liaobedcbd42012-10-16 18:14:11 +00008834 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8835 // Prepare truncation shuffle mask
8836 for (unsigned i = 0; i != NumElems; ++i)
8837 MaskVec[i] = i * 2;
8838 SDValue V = DAG.getVectorShuffle(NVT, DL,
8839 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8840 DAG.getUNDEF(NVT), &MaskVec[0]);
8841 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8842 DAG.getIntPtrConstant(0));
8843}
8844
Dan Gohmand858e902010-04-17 15:26:15 +00008845SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8846 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008847 MVT VT = Op.getValueType().getSimpleVT();
8848 if (VT.isVector()) {
8849 if (VT == MVT::v8i16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00008850 return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT,
8851 DAG.getNode(ISD::FP_TO_SINT, SDLoc(Op),
Michael Liaobedcbd42012-10-16 18:14:11 +00008852 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008853 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008854 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008855
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008856 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8857 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008858 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008859 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8860 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008861
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008862 if (StackSlot.getNode())
8863 // Load the result.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008864 return DAG.getLoad(Op.getValueType(), SDLoc(Op),
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008865 FIST, StackSlot, MachinePointerInfo(),
8866 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008867
8868 // The node is the result.
8869 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008870}
8871
Dan Gohmand858e902010-04-17 15:26:15 +00008872SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8873 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008874 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8875 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008876 SDValue FIST = Vals.first, StackSlot = Vals.second;
8877 assert(FIST.getNode() && "Unexpected failure");
8878
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008879 if (StackSlot.getNode())
8880 // Load the result.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008881 return DAG.getLoad(Op.getValueType(), SDLoc(Op),
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008882 FIST, StackSlot, MachinePointerInfo(),
8883 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008884
8885 // The node is the result.
8886 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008887}
8888
Craig Topperb84b4232013-01-21 06:13:28 +00008889static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008890 SDLoc DL(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008891 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008892 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008893 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008894
8895 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8896
8897 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8898 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8899 In, DAG.getUNDEF(SVT)));
8900}
8901
Craig Topper43620672012-09-08 07:31:51 +00008902SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008903 LLVMContext *Context = DAG.getContext();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008904 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008905 MVT VT = Op.getValueType().getSimpleVT();
8906 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008907 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8908 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008909 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008910 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008911 }
Craig Topper43620672012-09-08 07:31:51 +00008912 Constant *C;
8913 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008914 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8915 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008916 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008917 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8918 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008919 C = ConstantVector::getSplat(NumElts, C);
8920 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8921 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008922 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008923 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008924 false, false, false, Alignment);
8925 if (VT.isVector()) {
8926 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8927 return DAG.getNode(ISD::BITCAST, dl, VT,
8928 DAG.getNode(ISD::AND, dl, ANDVT,
8929 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8930 Op.getOperand(0)),
8931 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8932 }
Dale Johannesenace16102009-02-03 19:33:06 +00008933 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008934}
8935
Dan Gohmand858e902010-04-17 15:26:15 +00008936SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008937 LLVMContext *Context = DAG.getContext();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008938 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008939 MVT VT = Op.getValueType().getSimpleVT();
8940 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008941 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8942 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008943 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008944 NumElts = VT.getVectorNumElements();
8945 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008946 Constant *C;
8947 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008948 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8949 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008950 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008951 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8952 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008953 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008954 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8955 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008956 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008957 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008958 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008959 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008960 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008961 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008962 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008963 DAG.getNode(ISD::BITCAST, dl, XORVT,
8964 Op.getOperand(0)),
8965 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008966 }
Craig Topper69947b92012-04-23 06:57:04 +00008967
8968 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008969}
8970
Dan Gohmand858e902010-04-17 15:26:15 +00008971SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008972 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008973 SDValue Op0 = Op.getOperand(0);
8974 SDValue Op1 = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008975 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008976 MVT VT = Op.getValueType().getSimpleVT();
8977 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008978
8979 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008980 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008981 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008982 SrcVT = VT;
8983 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008984 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008985 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008986 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008987 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008988 }
8989
8990 // At this point the operands and the result should have the same
8991 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008992
Evan Cheng68c47cb2007-01-05 07:55:56 +00008993 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008994 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008995 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008996 const fltSemantics &Sem = APFloat::IEEEdouble;
8997 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8998 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008999 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00009000 const fltSemantics &Sem = APFloat::IEEEsingle;
9001 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
9002 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
9003 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
9004 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00009005 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00009006 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00009007 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00009008 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009009 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009010 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00009011 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009012
9013 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00009014 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009015 // Op0 is MVT::f32, Op1 is MVT::f64.
9016 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
9017 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
9018 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009019 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00009020 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00009021 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00009022 }
9023
Evan Cheng73d6cf12007-01-05 21:37:56 +00009024 // Clear first operand sign bit.
9025 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00009027 const fltSemantics &Sem = APFloat::IEEEdouble;
9028 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
9029 APInt(64, ~(1ULL << 63)))));
9030 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00009031 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00009032 const fltSemantics &Sem = APFloat::IEEEsingle;
9033 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
9034 APInt(32, ~(1U << 31)))));
9035 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
9036 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
9037 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00009038 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00009039 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00009040 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00009041 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00009042 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009043 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00009044 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00009045
9046 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00009047 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00009048}
9049
Craig Topper55b24052012-09-11 06:15:32 +00009050static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009051 SDValue N0 = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009052 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00009053 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00009054
9055 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
9056 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
9057 DAG.getConstant(1, VT));
9058 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
9059}
9060
Michael Liaof966e4e2012-09-13 20:24:54 +00009061// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
9062//
Craig Topperb99bafe2013-01-21 06:21:54 +00009063SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
9064 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00009065 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
9066
9067 if (!Subtarget->hasSSE41())
9068 return SDValue();
9069
9070 if (!Op->hasOneUse())
9071 return SDValue();
9072
9073 SDNode *N = Op.getNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009074 SDLoc DL(N);
Michael Liaof966e4e2012-09-13 20:24:54 +00009075
9076 SmallVector<SDValue, 8> Opnds;
9077 DenseMap<SDValue, unsigned> VecInMap;
9078 EVT VT = MVT::Other;
9079
9080 // Recognize a special case where a vector is casted into wide integer to
9081 // test all 0s.
9082 Opnds.push_back(N->getOperand(0));
9083 Opnds.push_back(N->getOperand(1));
9084
9085 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
Craig Topper365ef0b2013-07-03 15:07:05 +00009086 SmallVectorImpl<SDValue>::const_iterator I = Opnds.begin() + Slot;
Michael Liaof966e4e2012-09-13 20:24:54 +00009087 // BFS traverse all OR'd operands.
9088 if (I->getOpcode() == ISD::OR) {
9089 Opnds.push_back(I->getOperand(0));
9090 Opnds.push_back(I->getOperand(1));
9091 // Re-evaluate the number of nodes to be traversed.
9092 e += 2; // 2 more nodes (LHS and RHS) are pushed.
9093 continue;
9094 }
9095
9096 // Quit if a non-EXTRACT_VECTOR_ELT
9097 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
9098 return SDValue();
9099
9100 // Quit if without a constant index.
9101 SDValue Idx = I->getOperand(1);
9102 if (!isa<ConstantSDNode>(Idx))
9103 return SDValue();
9104
9105 SDValue ExtractedFromVec = I->getOperand(0);
9106 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
9107 if (M == VecInMap.end()) {
9108 VT = ExtractedFromVec.getValueType();
9109 // Quit if not 128/256-bit vector.
9110 if (!VT.is128BitVector() && !VT.is256BitVector())
9111 return SDValue();
9112 // Quit if not the same type.
9113 if (VecInMap.begin() != VecInMap.end() &&
9114 VT != VecInMap.begin()->first.getValueType())
9115 return SDValue();
9116 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
9117 }
9118 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
9119 }
9120
9121 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00009122 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00009123
9124 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
9125 SmallVector<SDValue, 8> VecIns;
9126
9127 for (DenseMap<SDValue, unsigned>::const_iterator
9128 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
9129 // Quit if not all elements are used.
9130 if (I->second != FullMask)
9131 return SDValue();
9132 VecIns.push_back(I->first);
9133 }
9134
9135 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
9136
9137 // Cast all vectors into TestVT for PTEST.
9138 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
9139 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
9140
9141 // If more than one full vectors are evaluated, OR them first before PTEST.
9142 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
9143 // Each iteration will OR 2 nodes and append the result until there is only
9144 // 1 node left, i.e. the final OR'd value of all vectors.
9145 SDValue LHS = VecIns[Slot];
9146 SDValue RHS = VecIns[Slot + 1];
9147 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
9148 }
9149
9150 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
9151 VecIns.back(), VecIns.back());
9152}
9153
Dan Gohman076aee32009-03-04 19:44:21 +00009154/// Emit nodes that will be selected as "test Op0,Op0", or something
9155/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009156SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009157 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009158 SDLoc dl(Op);
Dan Gohman076aee32009-03-04 19:44:21 +00009159
Dan Gohman31125812009-03-07 01:58:32 +00009160 // CF and OF aren't always set the way we want. Determine which
9161 // of these we need.
9162 bool NeedCF = false;
9163 bool NeedOF = false;
9164 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009165 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00009166 case X86::COND_A: case X86::COND_AE:
9167 case X86::COND_B: case X86::COND_BE:
9168 NeedCF = true;
9169 break;
9170 case X86::COND_G: case X86::COND_GE:
9171 case X86::COND_L: case X86::COND_LE:
9172 case X86::COND_O: case X86::COND_NO:
9173 NeedOF = true;
9174 break;
Dan Gohman31125812009-03-07 01:58:32 +00009175 }
9176
Dan Gohman076aee32009-03-04 19:44:21 +00009177 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00009178 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
9179 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009180 if (Op.getResNo() != 0 || NeedOF || NeedCF)
9181 // Emit a CMP with 0, which is the TEST pattern.
9182 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9183 DAG.getConstant(0, Op.getValueType()));
9184
9185 unsigned Opcode = 0;
9186 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009187
9188 // Truncate operations may prevent the merge of the SETCC instruction
9189 // and the arithmetic intruction before it. Attempt to truncate the operands
9190 // of the arithmetic instruction and use a reduced bit-width instruction.
9191 bool NeedTruncation = false;
9192 SDValue ArithOp = Op;
9193 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
9194 SDValue Arith = Op->getOperand(0);
9195 // Both the trunc and the arithmetic op need to have one user each.
9196 if (Arith->hasOneUse())
9197 switch (Arith.getOpcode()) {
9198 default: break;
9199 case ISD::ADD:
9200 case ISD::SUB:
9201 case ISD::AND:
9202 case ISD::OR:
9203 case ISD::XOR: {
9204 NeedTruncation = true;
9205 ArithOp = Arith;
9206 }
9207 }
9208 }
9209
9210 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
9211 // which may be the result of a CAST. We use the variable 'Op', which is the
9212 // non-casted variable when we check for possible users.
9213 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009214 case ISD::ADD:
9215 // Due to an isel shortcoming, be conservative if this add is likely to be
9216 // selected as part of a load-modify-store instruction. When the root node
9217 // in a match is a store, isel doesn't know how to remap non-chain non-flag
9218 // uses of other nodes in the match, such as the ADD in this case. This
9219 // leads to the ADD being left around and reselected, with the result being
9220 // two adds in the output. Alas, even if none our users are stores, that
9221 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
9222 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
9223 // climbing the DAG back to the root, and it doesn't seem to be worth the
9224 // effort.
9225 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00009226 UE = Op.getNode()->use_end(); UI != UE; ++UI)
9227 if (UI->getOpcode() != ISD::CopyToReg &&
9228 UI->getOpcode() != ISD::SETCC &&
9229 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009230 goto default_case;
9231
9232 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009233 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009234 // An add of one will be selected as an INC.
9235 if (C->getAPIntValue() == 1) {
9236 Opcode = X86ISD::INC;
9237 NumOperands = 1;
9238 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00009239 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009240
9241 // An add of negative one (subtract of one) will be selected as a DEC.
9242 if (C->getAPIntValue().isAllOnesValue()) {
9243 Opcode = X86ISD::DEC;
9244 NumOperands = 1;
9245 break;
9246 }
Dan Gohman076aee32009-03-04 19:44:21 +00009247 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009248
9249 // Otherwise use a regular EFLAGS-setting add.
9250 Opcode = X86ISD::ADD;
9251 NumOperands = 2;
9252 break;
9253 case ISD::AND: {
9254 // If the primary and result isn't used, don't bother using X86ISD::AND,
9255 // because a TEST instruction will be better.
9256 bool NonFlagUse = false;
9257 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9258 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9259 SDNode *User = *UI;
9260 unsigned UOpNo = UI.getOperandNo();
9261 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
9262 // Look pass truncate.
9263 UOpNo = User->use_begin().getOperandNo();
9264 User = *User->use_begin();
9265 }
9266
9267 if (User->getOpcode() != ISD::BRCOND &&
9268 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009269 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009270 NonFlagUse = true;
9271 break;
9272 }
Dan Gohman076aee32009-03-04 19:44:21 +00009273 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009274
9275 if (!NonFlagUse)
9276 break;
9277 }
9278 // FALL THROUGH
9279 case ISD::SUB:
9280 case ISD::OR:
9281 case ISD::XOR:
9282 // Due to the ISEL shortcoming noted above, be conservative if this op is
9283 // likely to be selected as part of a load-modify-store instruction.
9284 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9285 UE = Op.getNode()->use_end(); UI != UE; ++UI)
9286 if (UI->getOpcode() == ISD::STORE)
9287 goto default_case;
9288
9289 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009290 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009291 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009292 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009293 case ISD::XOR: Opcode = X86ISD::XOR; break;
9294 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00009295 case ISD::OR: {
9296 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
9297 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9298 if (EFLAGS.getNode())
9299 return EFLAGS;
9300 }
9301 Opcode = X86ISD::OR;
9302 break;
9303 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009304 }
9305
9306 NumOperands = 2;
9307 break;
9308 case X86ISD::ADD:
9309 case X86ISD::SUB:
9310 case X86ISD::INC:
9311 case X86ISD::DEC:
9312 case X86ISD::OR:
9313 case X86ISD::XOR:
9314 case X86ISD::AND:
9315 return SDValue(Op.getNode(), 1);
9316 default:
9317 default_case:
9318 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009319 }
9320
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009321 // If we found that truncation is beneficial, perform the truncation and
9322 // update 'Op'.
9323 if (NeedTruncation) {
9324 EVT VT = Op.getValueType();
9325 SDValue WideVal = Op->getOperand(0);
9326 EVT WideVT = WideVal.getValueType();
9327 unsigned ConvertedOp = 0;
9328 // Use a target machine opcode to prevent further DAGCombine
9329 // optimizations that may separate the arithmetic operations
9330 // from the setcc node.
9331 switch (WideVal.getOpcode()) {
9332 default: break;
9333 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9334 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9335 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9336 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9337 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9338 }
9339
9340 if (ConvertedOp) {
9341 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9342 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9343 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9344 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9345 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9346 }
9347 }
9348 }
9349
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009350 if (Opcode == 0)
9351 // Emit a CMP with 0, which is the TEST pattern.
9352 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9353 DAG.getConstant(0, Op.getValueType()));
9354
9355 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9356 SmallVector<SDValue, 4> Ops;
9357 for (unsigned i = 0; i != NumOperands; ++i)
9358 Ops.push_back(Op.getOperand(i));
9359
9360 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9361 DAG.ReplaceAllUsesWith(Op, New);
9362 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009363}
9364
9365/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9366/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009367SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009368 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009369 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9370 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009371 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009372
Andrew Trickac6d9be2013-05-25 02:42:55 +00009373 SDLoc dl(Op0);
Manman Ren39ad5682012-08-08 00:51:41 +00009374 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9375 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9376 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9377 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9378 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9379 Op0, Op1);
9380 return SDValue(Sub.getNode(), 1);
9381 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009382 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009383}
9384
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009385/// Convert a comparison if required by the subtarget.
9386SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9387 SelectionDAG &DAG) const {
9388 // If the subtarget does not support the FUCOMI instruction, floating-point
9389 // comparisons have to be converted.
9390 if (Subtarget->hasCMov() ||
9391 Cmp.getOpcode() != X86ISD::CMP ||
9392 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9393 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9394 return Cmp;
9395
9396 // The instruction selector will select an FUCOM instruction instead of
9397 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9398 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9399 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009400 SDLoc dl(Cmp);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009401 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9402 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9403 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9404 DAG.getConstant(8, MVT::i8));
9405 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9406 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9407}
9408
Evan Cheng4e544802012-12-05 00:10:38 +00009409static bool isAllOnes(SDValue V) {
9410 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9411 return C && C->isAllOnesValue();
9412}
9413
Evan Chengd40d03e2010-01-06 19:38:29 +00009414/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9415/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009416SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
Andrew Trickac6d9be2013-05-25 02:42:55 +00009417 SDLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009418 SDValue Op0 = And.getOperand(0);
9419 SDValue Op1 = And.getOperand(1);
9420 if (Op0.getOpcode() == ISD::TRUNCATE)
9421 Op0 = Op0.getOperand(0);
9422 if (Op1.getOpcode() == ISD::TRUNCATE)
9423 Op1 = Op1.getOperand(0);
9424
Evan Chengd40d03e2010-01-06 19:38:29 +00009425 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009426 if (Op1.getOpcode() == ISD::SHL)
9427 std::swap(Op0, Op1);
9428 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009429 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9430 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009431 // If we looked past a truncate, check that it's only truncating away
9432 // known zeros.
9433 unsigned BitWidth = Op0.getValueSizeInBits();
9434 unsigned AndBitWidth = And.getValueSizeInBits();
9435 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009436 APInt Zeros, Ones;
9437 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009438 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9439 return SDValue();
9440 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009441 LHS = Op1;
9442 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009443 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009444 } else if (Op1.getOpcode() == ISD::Constant) {
9445 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009446 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009447 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009448
9449 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009450 LHS = AndLHS.getOperand(0);
9451 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009452 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009453
9454 // Use BT if the immediate can't be encoded in a TEST instruction.
9455 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9456 LHS = AndLHS;
9457 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9458 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009459 }
Evan Cheng0488db92007-09-25 01:57:46 +00009460
Evan Chengd40d03e2010-01-06 19:38:29 +00009461 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009462 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009463 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009464 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009465 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009466 // Also promote i16 to i32 for performance / code size reason.
9467 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009468 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009469 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009470
Evan Chengd40d03e2010-01-06 19:38:29 +00009471 // If the operand types disagree, extend the shift amount to match. Since
9472 // BT ignores high bits (like shifts) we can use anyextend.
9473 if (LHS.getValueType() != RHS.getValueType())
9474 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009475
Evan Chengd40d03e2010-01-06 19:38:29 +00009476 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009477 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Evan Chengd40d03e2010-01-06 19:38:29 +00009478 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9479 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009480 }
9481
Evan Cheng54de3ea2010-01-05 06:52:31 +00009482 return SDValue();
9483}
9484
Craig Topper89af15e2011-09-18 08:03:58 +00009485// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009486// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009487static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009488 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009489
Craig Topper7a9a28b2012-08-12 02:23:29 +00009490 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009491 "Unsupported value type for operation");
9492
Craig Topper66ddd152012-04-27 22:54:43 +00009493 unsigned NumElems = VT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009494 SDLoc dl(Op);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009495 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009496
9497 // Extract the LHS vectors
9498 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009499 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9500 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009501
9502 // Extract the RHS vectors
9503 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009504 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9505 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009506
9507 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009508 MVT EltVT = VT.getVectorElementType();
9509 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009510 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9511 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9512 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9513}
9514
Craig Topper26827f32013-01-20 09:02:22 +00009515static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9516 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009517 SDValue Cond;
9518 SDValue Op0 = Op.getOperand(0);
9519 SDValue Op1 = Op.getOperand(1);
9520 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009521 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009522 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009523 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009524 SDLoc dl(Op);
Nate Begeman30a0de92008-07-17 16:51:19 +00009525
9526 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009527#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009528 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009529 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9530#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009531
Craig Topper523908d2012-08-13 02:34:03 +00009532 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009533 bool Swap = false;
9534
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009535 // SSE Condition code mapping:
9536 // 0 - EQ
9537 // 1 - LT
9538 // 2 - LE
9539 // 3 - UNORD
9540 // 4 - NEQ
9541 // 5 - NLT
9542 // 6 - NLE
9543 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009544 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009545 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009546 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009547 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009548 case ISD::SETOGT:
9549 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009550 case ISD::SETLT:
9551 case ISD::SETOLT: SSECC = 1; break;
9552 case ISD::SETOGE:
9553 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009554 case ISD::SETLE:
9555 case ISD::SETOLE: SSECC = 2; break;
9556 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009557 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009558 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009559 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009560 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009561 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009562 case ISD::SETUGT: SSECC = 6; break;
9563 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009564 case ISD::SETUEQ:
9565 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009566 }
9567 if (Swap)
9568 std::swap(Op0, Op1);
9569
Nate Begemanfb8ead02008-07-25 19:05:58 +00009570 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009571 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009572 unsigned CC0, CC1;
9573 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009574 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009575 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9576 } else {
9577 assert(SetCCOpcode == ISD::SETONE);
9578 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009579 }
Craig Topper523908d2012-08-13 02:34:03 +00009580
9581 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9582 DAG.getConstant(CC0, MVT::i8));
9583 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9584 DAG.getConstant(CC1, MVT::i8));
9585 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009586 }
9587 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009588 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9589 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009590 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009591
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009592 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009593 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009594 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009595
Nate Begeman30a0de92008-07-17 16:51:19 +00009596 // We are handling one of the integer comparisons here. Since SSE only has
9597 // GT and EQ comparisons for integer, swapping operands and multiple
9598 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009599 unsigned Opc;
Juergen Ributzkab95e0f62013-07-16 18:20:45 +00009600 bool Swap = false, Invert = false, FlipSigns = false, MinMax = false;
9601
Nate Begeman30a0de92008-07-17 16:51:19 +00009602 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009603 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009604 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009605 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009606 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009607 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009608 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009609 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009610 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009611 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009612 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009613 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009614 }
Juergen Ributzkab95e0f62013-07-16 18:20:45 +00009615
9616 // Special case: Use min/max operations for SETULE/SETUGE
9617 MVT VET = VT.getVectorElementType();
9618 bool hasMinMax =
9619 (Subtarget->hasSSE41() && (VET >= MVT::i8 && VET <= MVT::i32))
9620 || (Subtarget->hasSSE2() && (VET == MVT::i8));
9621
9622 if (hasMinMax) {
9623 switch (SetCCOpcode) {
9624 default: break;
9625 case ISD::SETULE: Opc = X86ISD::UMIN; MinMax = true; break;
9626 case ISD::SETUGE: Opc = X86ISD::UMAX; MinMax = true; break;
9627 }
9628
9629 if (MinMax) { Swap = false; Invert = false; FlipSigns = false; }
9630 }
9631
Nate Begeman30a0de92008-07-17 16:51:19 +00009632 if (Swap)
9633 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009634
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009635 // Check that the operation in question is available (most are plain SSE2,
9636 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009637 if (VT == MVT::v2i64) {
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009638 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42()) {
9639 assert(Subtarget->hasSSE2() && "Don't know how to lower!");
9640
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009641 // First cast everything to the right type.
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009642 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9643 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9644
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009645 // Since SSE has no unsigned integer comparisons, we need to flip the sign
Benjamin Kramer60ef6c92013-05-22 17:01:12 +00009646 // bits of the inputs before performing those operations. The lower
9647 // compare is always unsigned.
9648 SDValue SB;
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009649 if (FlipSigns) {
Benjamin Kramer60ef6c92013-05-22 17:01:12 +00009650 SB = DAG.getConstant(0x80000000U, MVT::v4i32);
9651 } else {
9652 SDValue Sign = DAG.getConstant(0x80000000U, MVT::i32);
9653 SDValue Zero = DAG.getConstant(0x00000000U, MVT::i32);
9654 SB = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
9655 Sign, Zero, Sign, Zero);
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009656 }
Benjamin Kramer60ef6c92013-05-22 17:01:12 +00009657 Op0 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op0, SB);
9658 Op1 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op1, SB);
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009659
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009660 // Emulate PCMPGTQ with (hi1 > hi2) | ((hi1 == hi2) & (lo1 > lo2))
9661 SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1);
9662 SDValue EQ = DAG.getNode(X86ISD::PCMPEQ, dl, MVT::v4i32, Op0, Op1);
9663
9664 // Create masks for only the low parts/high parts of the 64 bit integers.
Craig Topperda129a22013-07-15 06:54:12 +00009665 static const int MaskHi[] = { 1, 1, 3, 3 };
9666 static const int MaskLo[] = { 0, 0, 2, 2 };
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009667 SDValue EQHi = DAG.getVectorShuffle(MVT::v4i32, dl, EQ, EQ, MaskHi);
9668 SDValue GTLo = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskLo);
9669 SDValue GTHi = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi);
9670
9671 SDValue Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, EQHi, GTLo);
9672 Result = DAG.getNode(ISD::OR, dl, MVT::v4i32, Result, GTHi);
9673
9674 if (Invert)
9675 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9676
9677 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9678 }
9679
Benjamin Kramer382ed782012-12-25 12:54:19 +00009680 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9681 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009682 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009683 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9684
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009685 // First cast everything to the right type.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009686 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9687 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9688
9689 // Do the compare.
9690 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9691
9692 // Make sure the lower and upper halves are both all-ones.
Craig Topperda129a22013-07-15 06:54:12 +00009693 static const int Mask[] = { 1, 0, 3, 2 };
Benjamin Kramer99f78062012-12-25 13:09:08 +00009694 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9695 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009696
9697 if (Invert)
9698 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9699
9700 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9701 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009702 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009703
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009704 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9705 // bits of the inputs before performing those operations.
9706 if (FlipSigns) {
9707 EVT EltVT = VT.getVectorElementType();
9708 SDValue SB = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), VT);
9709 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SB);
9710 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SB);
9711 }
9712
Dale Johannesenace16102009-02-03 19:33:06 +00009713 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009714
9715 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009716 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009717 Result = DAG.getNOT(dl, Result, VT);
Juergen Ributzkab95e0f62013-07-16 18:20:45 +00009718
9719 if (MinMax)
9720 Result = DAG.getNode(X86ISD::PCMPEQ, dl, VT, Op0, Result);
Bob Wilson4c245462009-01-22 17:39:32 +00009721
Nate Begeman30a0de92008-07-17 16:51:19 +00009722 return Result;
9723}
Evan Cheng0488db92007-09-25 01:57:46 +00009724
Craig Topper26827f32013-01-20 09:02:22 +00009725SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9726
9727 MVT VT = Op.getValueType().getSimpleVT();
9728
9729 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9730
9731 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9732 SDValue Op0 = Op.getOperand(0);
9733 SDValue Op1 = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009734 SDLoc dl(Op);
Craig Topper26827f32013-01-20 09:02:22 +00009735 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9736
9737 // Optimize to BT if possible.
9738 // Lower (X & (1 << N)) == 0 to BT(X, N).
9739 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9740 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9741 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9742 Op1.getOpcode() == ISD::Constant &&
9743 cast<ConstantSDNode>(Op1)->isNullValue() &&
9744 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9745 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9746 if (NewSetCC.getNode())
9747 return NewSetCC;
9748 }
9749
9750 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9751 // these.
9752 if (Op1.getOpcode() == ISD::Constant &&
9753 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9754 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9755 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9756
9757 // If the input is a setcc, then reuse the input setcc or use a new one with
9758 // the inverted condition.
9759 if (Op0.getOpcode() == X86ISD::SETCC) {
9760 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9761 bool Invert = (CC == ISD::SETNE) ^
9762 cast<ConstantSDNode>(Op1)->isNullValue();
9763 if (!Invert) return Op0;
9764
9765 CCode = X86::GetOppositeBranchCondition(CCode);
9766 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9767 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9768 }
9769 }
9770
9771 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9772 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9773 if (X86CC == X86::COND_INVALID)
9774 return SDValue();
9775
9776 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9777 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9778 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9779 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9780}
9781
Evan Cheng370e5342008-12-03 08:38:43 +00009782// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009783static bool isX86LogicalCmp(SDValue Op) {
9784 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009785 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9786 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009787 return true;
9788 if (Op.getResNo() == 1 &&
9789 (Opc == X86ISD::ADD ||
9790 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009791 Opc == X86ISD::ADC ||
9792 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009793 Opc == X86ISD::SMUL ||
9794 Opc == X86ISD::UMUL ||
9795 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009796 Opc == X86ISD::DEC ||
9797 Opc == X86ISD::OR ||
9798 Opc == X86ISD::XOR ||
9799 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009800 return true;
9801
Chris Lattner9637d5b2010-12-05 07:49:54 +00009802 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9803 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009804
Dan Gohman076aee32009-03-04 19:44:21 +00009805 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009806}
9807
Chris Lattnera2b56002010-12-05 01:23:24 +00009808static bool isZero(SDValue V) {
9809 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9810 return C && C->isNullValue();
9811}
9812
Evan Chengb64dd5f2012-08-07 22:21:00 +00009813static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9814 if (V.getOpcode() != ISD::TRUNCATE)
9815 return false;
9816
9817 SDValue VOp0 = V.getOperand(0);
9818 unsigned InBits = VOp0.getValueSizeInBits();
9819 unsigned Bits = V.getValueSizeInBits();
9820 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9821}
9822
Dan Gohmand858e902010-04-17 15:26:15 +00009823SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009824 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009825 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009826 SDValue Op1 = Op.getOperand(1);
9827 SDValue Op2 = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009828 SDLoc DL(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00009829 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009830
Dan Gohman1a492952009-10-20 16:22:37 +00009831 if (Cond.getOpcode() == ISD::SETCC) {
9832 SDValue NewCond = LowerSETCC(Cond, DAG);
9833 if (NewCond.getNode())
9834 Cond = NewCond;
9835 }
Evan Cheng734503b2006-09-11 02:19:56 +00009836
Chris Lattnera2b56002010-12-05 01:23:24 +00009837 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009838 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009839 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009840 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009841 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009842 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9843 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009844 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009845
Chris Lattnera2b56002010-12-05 01:23:24 +00009846 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009847
9848 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009849 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9850 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009851
9852 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009853 // Apply further optimizations for special cases
9854 // (select (x != 0), -1, 0) -> neg & sbb
9855 // (select (x == 0), 0, -1) -> neg & sbb
9856 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009857 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009858 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9859 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009860 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9861 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009862 CmpOp0);
9863 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9864 DAG.getConstant(X86::COND_B, MVT::i8),
9865 SDValue(Neg.getNode(), 1));
9866 return Res;
9867 }
9868
Chris Lattnera2b56002010-12-05 01:23:24 +00009869 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9870 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009871 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009872
Chris Lattner96908b12010-12-05 02:00:51 +00009873 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009874 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9875 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009876
Chris Lattner96908b12010-12-05 02:00:51 +00009877 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9878 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009879
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009880 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009881 if (N2C == 0 || !N2C->isNullValue())
9882 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9883 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009884 }
9885 }
9886
Chris Lattnera2b56002010-12-05 01:23:24 +00009887 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009888 if (Cond.getOpcode() == ISD::AND &&
9889 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9890 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009891 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009892 Cond = Cond.getOperand(0);
9893 }
9894
Evan Cheng3f41d662007-10-08 22:16:29 +00009895 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9896 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009897 unsigned CondOpcode = Cond.getOpcode();
9898 if (CondOpcode == X86ISD::SETCC ||
9899 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009900 CC = Cond.getOperand(0);
9901
Dan Gohman475871a2008-07-27 21:46:04 +00009902 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009903 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009904 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009905
Evan Cheng3f41d662007-10-08 22:16:29 +00009906 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009907 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009908 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009909 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009910
Chris Lattnerd1980a52009-03-12 06:52:53 +00009911 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9912 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009913 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009914 addTest = false;
9915 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009916 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9917 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9918 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9919 Cond.getOperand(0).getValueType() != MVT::i8)) {
9920 SDValue LHS = Cond.getOperand(0);
9921 SDValue RHS = Cond.getOperand(1);
9922 unsigned X86Opcode;
9923 unsigned X86Cond;
9924 SDVTList VTs;
9925 switch (CondOpcode) {
9926 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9927 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9928 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9929 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9930 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9931 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9932 default: llvm_unreachable("unexpected overflowing operator");
9933 }
9934 if (CondOpcode == ISD::UMULO)
9935 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9936 MVT::i32);
9937 else
9938 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9939
9940 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9941
9942 if (CondOpcode == ISD::UMULO)
9943 Cond = X86Op.getValue(2);
9944 else
9945 Cond = X86Op.getValue(1);
9946
9947 CC = DAG.getConstant(X86Cond, MVT::i8);
9948 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009949 }
9950
9951 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009952 // Look pass the truncate if the high bits are known zero.
9953 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9954 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009955
9956 // We know the result of AND is compared against zero. Try to match
9957 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009958 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009959 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009960 if (NewSetCC.getNode()) {
9961 CC = NewSetCC.getOperand(0);
9962 Cond = NewSetCC.getOperand(1);
9963 addTest = false;
9964 }
9965 }
9966 }
9967
9968 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009969 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009970 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009971 }
9972
Benjamin Kramere915ff32010-12-22 23:09:28 +00009973 // a < b ? -1 : 0 -> RES = ~setcc_carry
9974 // a < b ? 0 : -1 -> RES = setcc_carry
9975 // a >= b ? -1 : 0 -> RES = setcc_carry
9976 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009977 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009978 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009979 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9980
9981 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9982 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9983 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9984 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9985 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9986 return DAG.getNOT(DL, Res, Res.getValueType());
9987 return Res;
9988 }
9989 }
9990
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009991 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9992 // widen the cmov and push the truncate through. This avoids introducing a new
9993 // branch during isel and doesn't add any extensions.
9994 if (Op.getValueType() == MVT::i8 &&
9995 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9996 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9997 if (T1.getValueType() == T2.getValueType() &&
9998 // Blacklist CopyFromReg to avoid partial register stalls.
9999 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
10000 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +000010001 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +000010002 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
10003 }
10004 }
10005
Evan Cheng0488db92007-09-25 01:57:46 +000010006 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
10007 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000010008 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +000010009 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +000010010 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +000010011}
10012
Nadav Rotem1a330af2012-12-27 22:47:16 +000010013SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
10014 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +000010015 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +000010016 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +000010017 MVT InVT = In.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010018 SDLoc dl(Op);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010019
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010020 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
10021 (VT != MVT::v8i32 || InVT != MVT::v8i16))
10022 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +000010023
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010024 if (Subtarget->hasInt256())
10025 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010026
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010027 // Optimize vectors in AVX mode
10028 // Sign extend v8i16 to v8i32 and
10029 // v4i32 to v4i64
10030 //
10031 // Divide input vector into two parts
10032 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
10033 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
10034 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +000010035
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010036 unsigned NumElems = InVT.getVectorNumElements();
10037 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010038
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010039 SmallVector<int,8> ShufMask1(NumElems, -1);
10040 for (unsigned i = 0; i != NumElems/2; ++i)
10041 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +000010042
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010043 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010044
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010045 SmallVector<int,8> ShufMask2(NumElems, -1);
10046 for (unsigned i = 0; i != NumElems/2; ++i)
10047 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +000010048
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010049 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010050
Craig Toppera080daf2013-01-20 21:50:27 +000010051 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010052 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010053
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010054 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
10055 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010056
Nadav Rotem587fb1d2012-12-27 23:08:05 +000010057 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +000010058}
10059
Evan Cheng370e5342008-12-03 08:38:43 +000010060// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
10061// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
10062// from the AND / OR.
10063static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
10064 Opc = Op.getOpcode();
10065 if (Opc != ISD::OR && Opc != ISD::AND)
10066 return false;
10067 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
10068 Op.getOperand(0).hasOneUse() &&
10069 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
10070 Op.getOperand(1).hasOneUse());
10071}
10072
Evan Cheng961d6d42009-02-02 08:19:07 +000010073// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
10074// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +000010075static bool isXor1OfSetCC(SDValue Op) {
10076 if (Op.getOpcode() != ISD::XOR)
10077 return false;
10078 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
10079 if (N1C && N1C->getAPIntValue() == 1) {
10080 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
10081 Op.getOperand(0).hasOneUse();
10082 }
10083 return false;
10084}
10085
Dan Gohmand858e902010-04-17 15:26:15 +000010086SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +000010087 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +000010088 SDValue Chain = Op.getOperand(0);
10089 SDValue Cond = Op.getOperand(1);
10090 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010091 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +000010092 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +000010093 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +000010094
Dan Gohman1a492952009-10-20 16:22:37 +000010095 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +000010096 // Check for setcc([su]{add,sub,mul}o == 0).
10097 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
10098 isa<ConstantSDNode>(Cond.getOperand(1)) &&
10099 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
10100 Cond.getOperand(0).getResNo() == 1 &&
10101 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
10102 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
10103 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
10104 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
10105 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
10106 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
10107 Inverted = true;
10108 Cond = Cond.getOperand(0);
10109 } else {
10110 SDValue NewCond = LowerSETCC(Cond, DAG);
10111 if (NewCond.getNode())
10112 Cond = NewCond;
10113 }
Dan Gohman1a492952009-10-20 16:22:37 +000010114 }
Chris Lattnere55484e2008-12-25 05:34:37 +000010115#if 0
10116 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +000010117 else if (Cond.getOpcode() == X86ISD::ADD ||
10118 Cond.getOpcode() == X86ISD::SUB ||
10119 Cond.getOpcode() == X86ISD::SMUL ||
10120 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +000010121 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +000010122#endif
Scott Michelfdc40a02009-02-17 22:15:04 +000010123
Evan Chengad9c0a32009-12-15 00:53:42 +000010124 // Look pass (and (setcc_carry (cmp ...)), 1).
10125 if (Cond.getOpcode() == ISD::AND &&
10126 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
10127 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +000010128 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +000010129 Cond = Cond.getOperand(0);
10130 }
10131
Evan Cheng3f41d662007-10-08 22:16:29 +000010132 // If condition flag is set by a X86ISD::CMP, then use it as the condition
10133 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +000010134 unsigned CondOpcode = Cond.getOpcode();
10135 if (CondOpcode == X86ISD::SETCC ||
10136 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +000010137 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +000010138
Dan Gohman475871a2008-07-27 21:46:04 +000010139 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +000010140 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +000010141 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +000010142 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +000010143 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +000010144 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +000010145 } else {
Evan Cheng370e5342008-12-03 08:38:43 +000010146 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +000010147 default: break;
10148 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +000010149 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +000010150 // These can only come from an arithmetic instruction with overflow,
10151 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +000010152 Cond = Cond.getNode()->getOperand(1);
10153 addTest = false;
10154 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000010155 }
Evan Cheng0488db92007-09-25 01:57:46 +000010156 }
Dan Gohman65fd6562011-11-03 21:49:52 +000010157 }
10158 CondOpcode = Cond.getOpcode();
10159 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
10160 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
10161 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
10162 Cond.getOperand(0).getValueType() != MVT::i8)) {
10163 SDValue LHS = Cond.getOperand(0);
10164 SDValue RHS = Cond.getOperand(1);
10165 unsigned X86Opcode;
10166 unsigned X86Cond;
10167 SDVTList VTs;
10168 switch (CondOpcode) {
10169 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
10170 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
10171 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
10172 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
10173 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
10174 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
10175 default: llvm_unreachable("unexpected overflowing operator");
10176 }
10177 if (Inverted)
10178 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
10179 if (CondOpcode == ISD::UMULO)
10180 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
10181 MVT::i32);
10182 else
10183 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
10184
10185 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
10186
10187 if (CondOpcode == ISD::UMULO)
10188 Cond = X86Op.getValue(2);
10189 else
10190 Cond = X86Op.getValue(1);
10191
10192 CC = DAG.getConstant(X86Cond, MVT::i8);
10193 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +000010194 } else {
10195 unsigned CondOpc;
10196 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
10197 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +000010198 if (CondOpc == ISD::OR) {
10199 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
10200 // two branches instead of an explicit OR instruction with a
10201 // separate test.
10202 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +000010203 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +000010204 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010205 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +000010206 Chain, Dest, CC, Cmp);
10207 CC = Cond.getOperand(1).getOperand(0);
10208 Cond = Cmp;
10209 addTest = false;
10210 }
10211 } else { // ISD::AND
10212 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
10213 // two branches instead of an explicit AND instruction with a
10214 // separate test. However, we only do this if this block doesn't
10215 // have a fall-through edge, because this requires an explicit
10216 // jmp when the condition is false.
10217 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +000010218 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +000010219 Op.getNode()->hasOneUse()) {
10220 X86::CondCode CCode =
10221 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
10222 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +000010223 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +000010224 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +000010225 // Look for an unconditional branch following this conditional branch.
10226 // We need this because we need to reverse the successors in order
10227 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +000010228 if (User->getOpcode() == ISD::BR) {
10229 SDValue FalseBB = User->getOperand(1);
10230 SDNode *NewBR =
10231 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +000010232 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +000010233 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +000010234 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +000010235
Dale Johannesene4d209d2009-02-03 20:21:25 +000010236 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +000010237 Chain, Dest, CC, Cmp);
10238 X86::CondCode CCode =
10239 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
10240 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +000010241 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +000010242 Cond = Cmp;
10243 addTest = false;
10244 }
10245 }
Dan Gohman279c22e2008-10-21 03:29:32 +000010246 }
Evan Cheng67ad9db2009-02-02 08:07:36 +000010247 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
10248 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
10249 // It should be transformed during dag combiner except when the condition
10250 // is set by a arithmetics with overflow node.
10251 X86::CondCode CCode =
10252 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
10253 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +000010254 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +000010255 Cond = Cond.getOperand(0).getOperand(1);
10256 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +000010257 } else if (Cond.getOpcode() == ISD::SETCC &&
10258 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
10259 // For FCMP_OEQ, we can emit
10260 // two branches instead of an explicit AND instruction with a
10261 // separate test. However, we only do this if this block doesn't
10262 // have a fall-through edge, because this requires an explicit
10263 // jmp when the condition is false.
10264 if (Op.getNode()->hasOneUse()) {
10265 SDNode *User = *Op.getNode()->use_begin();
10266 // Look for an unconditional branch following this conditional branch.
10267 // We need this because we need to reverse the successors in order
10268 // to implement FCMP_OEQ.
10269 if (User->getOpcode() == ISD::BR) {
10270 SDValue FalseBB = User->getOperand(1);
10271 SDNode *NewBR =
10272 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
10273 assert(NewBR == User);
10274 (void)NewBR;
10275 Dest = FalseBB;
10276
10277 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
10278 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010279 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +000010280 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
10281 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
10282 Chain, Dest, CC, Cmp);
10283 CC = DAG.getConstant(X86::COND_P, MVT::i8);
10284 Cond = Cmp;
10285 addTest = false;
10286 }
10287 }
10288 } else if (Cond.getOpcode() == ISD::SETCC &&
10289 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
10290 // For FCMP_UNE, we can emit
10291 // two branches instead of an explicit AND instruction with a
10292 // separate test. However, we only do this if this block doesn't
10293 // have a fall-through edge, because this requires an explicit
10294 // jmp when the condition is false.
10295 if (Op.getNode()->hasOneUse()) {
10296 SDNode *User = *Op.getNode()->use_begin();
10297 // Look for an unconditional branch following this conditional branch.
10298 // We need this because we need to reverse the successors in order
10299 // to implement FCMP_UNE.
10300 if (User->getOpcode() == ISD::BR) {
10301 SDValue FalseBB = User->getOperand(1);
10302 SDNode *NewBR =
10303 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
10304 assert(NewBR == User);
10305 (void)NewBR;
10306
10307 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
10308 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010309 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +000010310 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
10311 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
10312 Chain, Dest, CC, Cmp);
10313 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
10314 Cond = Cmp;
10315 addTest = false;
10316 Dest = FalseBB;
10317 }
10318 }
Dan Gohman279c22e2008-10-21 03:29:32 +000010319 }
Evan Cheng0488db92007-09-25 01:57:46 +000010320 }
10321
10322 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +000010323 // Look pass the truncate if the high bits are known zero.
10324 if (isTruncWithZeroHighBitsInput(Cond, DAG))
10325 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +000010326
10327 // We know the result of AND is compared against zero. Try to match
10328 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010329 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +000010330 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
10331 if (NewSetCC.getNode()) {
10332 CC = NewSetCC.getOperand(0);
10333 Cond = NewSetCC.getOperand(1);
10334 addTest = false;
10335 }
10336 }
10337 }
10338
10339 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010340 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +000010341 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +000010342 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010343 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010344 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010345 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010346}
10347
Anton Korobeynikove060b532007-04-17 19:34:00 +000010348// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10349// Calls to _alloca is needed to probe the stack when allocating more than 4k
10350// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10351// that the guard pages used by the OS virtual memory manager are allocated in
10352// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010353SDValue
10354X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010355 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010356 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010357 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010358 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010359 "are being used");
10360 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Andrew Trickac6d9be2013-05-25 02:42:55 +000010361 SDLoc dl(Op);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010362
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010363 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010364 SDValue Chain = Op.getOperand(0);
10365 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010366 // FIXME: Ensure alignment here
10367
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010368 bool Is64Bit = Subtarget->is64Bit();
10369 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010370
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010371 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010372 MachineFunction &MF = DAG.getMachineFunction();
10373 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010374
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010375 if (Is64Bit) {
10376 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010377 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010378 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010379
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010380 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010381 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010382 if (I->hasNestAttr())
10383 report_fatal_error("Cannot use segmented stacks with functions that "
10384 "have nested arguments.");
10385 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010386
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010387 const TargetRegisterClass *AddrRegClass =
10388 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10389 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10390 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10391 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10392 DAG.getRegister(Vreg, SPTy));
10393 SDValue Ops1[2] = { Value, Chain };
10394 return DAG.getMergeValues(Ops1, 2, dl);
10395 } else {
10396 SDValue Flag;
10397 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010398
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010399 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10400 Flag = Chain.getValue(1);
10401 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010402
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010403 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10404 Flag = Chain.getValue(1);
10405
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000010406 const X86RegisterInfo *RegInfo =
10407 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaoc5c970e2012-10-31 04:14:09 +000010408 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10409 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010410
10411 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10412 return DAG.getMergeValues(Ops1, 2, dl);
10413 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010414}
10415
Dan Gohmand858e902010-04-17 15:26:15 +000010416SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010417 MachineFunction &MF = DAG.getMachineFunction();
10418 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10419
Dan Gohman69de1932008-02-06 22:27:42 +000010420 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010421 SDLoc DL(Op);
Evan Cheng8b2794a2006-10-13 21:14:26 +000010422
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010423 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010424 // vastart just stores the address of the VarArgsFrameIndex slot into the
10425 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010426 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10427 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010428 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10429 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010430 }
10431
10432 // __va_list_tag:
10433 // gp_offset (0 - 6 * 8)
10434 // fp_offset (48 - 48 + 8 * 16)
10435 // overflow_arg_area (point to parameters coming in memory).
10436 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010437 SmallVector<SDValue, 8> MemOps;
10438 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010439 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010440 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010441 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10442 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010443 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010444 MemOps.push_back(Store);
10445
10446 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010447 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010448 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010449 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010450 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10451 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010452 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010453 MemOps.push_back(Store);
10454
10455 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010456 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010457 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010458 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10459 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010460 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10461 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010462 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010463 MemOps.push_back(Store);
10464
10465 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010466 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010467 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010468 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10469 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010470 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10471 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010472 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010473 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010474 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010475}
10476
Dan Gohmand858e902010-04-17 15:26:15 +000010477SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010478 assert(Subtarget->is64Bit() &&
10479 "LowerVAARG only handles 64-bit va_arg!");
10480 assert((Subtarget->isTargetLinux() ||
10481 Subtarget->isTargetDarwin()) &&
10482 "Unhandled target in LowerVAARG");
10483 assert(Op.getNode()->getNumOperands() == 4);
10484 SDValue Chain = Op.getOperand(0);
10485 SDValue SrcPtr = Op.getOperand(1);
10486 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10487 unsigned Align = Op.getConstantOperandVal(3);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010488 SDLoc dl(Op);
Dan Gohman9018e832008-05-10 01:26:14 +000010489
Dan Gohman320afb82010-10-12 18:00:49 +000010490 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010491 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010492 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010493 uint8_t ArgMode;
10494
10495 // Decide which area this value should be read from.
10496 // TODO: Implement the AMD64 ABI in its entirety. This simple
10497 // selection mechanism works only for the basic types.
10498 if (ArgVT == MVT::f80) {
10499 llvm_unreachable("va_arg for f80 not yet implemented");
10500 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10501 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10502 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10503 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10504 } else {
10505 llvm_unreachable("Unhandled argument type in LowerVAARG");
10506 }
10507
10508 if (ArgMode == 2) {
10509 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010510 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010511 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010512 .getFunction()->getAttributes()
10513 .hasAttribute(AttributeSet::FunctionIndex,
10514 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010515 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010516 }
10517
10518 // Insert VAARG_64 node into the DAG
10519 // VAARG_64 returns two values: Variable Argument Address, Chain
10520 SmallVector<SDValue, 11> InstOps;
10521 InstOps.push_back(Chain);
10522 InstOps.push_back(SrcPtr);
10523 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10524 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10525 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10526 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10527 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10528 VTs, &InstOps[0], InstOps.size(),
10529 MVT::i64,
10530 MachinePointerInfo(SV),
10531 /*Align=*/0,
10532 /*Volatile=*/false,
10533 /*ReadMem=*/true,
10534 /*WriteMem=*/true);
10535 Chain = VAARG.getValue(1);
10536
10537 // Load the next argument and return it
10538 return DAG.getLoad(ArgVT, dl,
10539 Chain,
10540 VAARG,
10541 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010542 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010543}
10544
Craig Topper55b24052012-09-11 06:15:32 +000010545static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10546 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010547 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010548 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010549 SDValue Chain = Op.getOperand(0);
10550 SDValue DstPtr = Op.getOperand(1);
10551 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010552 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10553 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010554 SDLoc DL(Op);
Evan Chengae642192007-03-02 23:16:35 +000010555
Chris Lattnere72f2022010-09-21 05:40:29 +000010556 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010557 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010558 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010559 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010560}
10561
Craig Topperff3139f2013-02-19 07:43:59 +000010562// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010563// may or may not be a constant. Takes immediate version of shift as input.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010564static SDValue getTargetVShiftNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper80e46362012-01-23 06:16:53 +000010565 SDValue SrcOp, SDValue ShAmt,
10566 SelectionDAG &DAG) {
10567 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10568
10569 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010570 // Constant may be a TargetConstant. Use a regular constant.
10571 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010572 switch (Opc) {
10573 default: llvm_unreachable("Unknown target vector shift node");
10574 case X86ISD::VSHLI:
10575 case X86ISD::VSRLI:
10576 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010577 return DAG.getNode(Opc, dl, VT, SrcOp,
10578 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010579 }
10580 }
10581
10582 // Change opcode to non-immediate version
10583 switch (Opc) {
10584 default: llvm_unreachable("Unknown target vector shift node");
10585 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10586 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10587 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10588 }
10589
10590 // Need to build a vector containing shift amount
10591 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10592 SDValue ShOps[4];
10593 ShOps[0] = ShAmt;
10594 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010595 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010596 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010597
10598 // The return type has to be a 128-bit type with the same element
10599 // type as the input type.
10600 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10601 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10602
10603 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010604 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10605}
10606
Craig Topper55b24052012-09-11 06:15:32 +000010607static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010608 SDLoc dl(Op);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010609 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010610 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010611 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010612 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010613 case Intrinsic::x86_sse_comieq_ss:
10614 case Intrinsic::x86_sse_comilt_ss:
10615 case Intrinsic::x86_sse_comile_ss:
10616 case Intrinsic::x86_sse_comigt_ss:
10617 case Intrinsic::x86_sse_comige_ss:
10618 case Intrinsic::x86_sse_comineq_ss:
10619 case Intrinsic::x86_sse_ucomieq_ss:
10620 case Intrinsic::x86_sse_ucomilt_ss:
10621 case Intrinsic::x86_sse_ucomile_ss:
10622 case Intrinsic::x86_sse_ucomigt_ss:
10623 case Intrinsic::x86_sse_ucomige_ss:
10624 case Intrinsic::x86_sse_ucomineq_ss:
10625 case Intrinsic::x86_sse2_comieq_sd:
10626 case Intrinsic::x86_sse2_comilt_sd:
10627 case Intrinsic::x86_sse2_comile_sd:
10628 case Intrinsic::x86_sse2_comigt_sd:
10629 case Intrinsic::x86_sse2_comige_sd:
10630 case Intrinsic::x86_sse2_comineq_sd:
10631 case Intrinsic::x86_sse2_ucomieq_sd:
10632 case Intrinsic::x86_sse2_ucomilt_sd:
10633 case Intrinsic::x86_sse2_ucomile_sd:
10634 case Intrinsic::x86_sse2_ucomigt_sd:
10635 case Intrinsic::x86_sse2_ucomige_sd:
10636 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010637 unsigned Opc;
10638 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010639 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010640 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010641 case Intrinsic::x86_sse_comieq_ss:
10642 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010643 Opc = X86ISD::COMI;
10644 CC = ISD::SETEQ;
10645 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010646 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010647 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010648 Opc = X86ISD::COMI;
10649 CC = ISD::SETLT;
10650 break;
10651 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010652 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010653 Opc = X86ISD::COMI;
10654 CC = ISD::SETLE;
10655 break;
10656 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010657 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010658 Opc = X86ISD::COMI;
10659 CC = ISD::SETGT;
10660 break;
10661 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010662 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010663 Opc = X86ISD::COMI;
10664 CC = ISD::SETGE;
10665 break;
10666 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010667 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010668 Opc = X86ISD::COMI;
10669 CC = ISD::SETNE;
10670 break;
10671 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010672 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010673 Opc = X86ISD::UCOMI;
10674 CC = ISD::SETEQ;
10675 break;
10676 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010677 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010678 Opc = X86ISD::UCOMI;
10679 CC = ISD::SETLT;
10680 break;
10681 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010682 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010683 Opc = X86ISD::UCOMI;
10684 CC = ISD::SETLE;
10685 break;
10686 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010687 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010688 Opc = X86ISD::UCOMI;
10689 CC = ISD::SETGT;
10690 break;
10691 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010692 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010693 Opc = X86ISD::UCOMI;
10694 CC = ISD::SETGE;
10695 break;
10696 case Intrinsic::x86_sse_ucomineq_ss:
10697 case Intrinsic::x86_sse2_ucomineq_sd:
10698 Opc = X86ISD::UCOMI;
10699 CC = ISD::SETNE;
10700 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010701 }
Evan Cheng734503b2006-09-11 02:19:56 +000010702
Dan Gohman475871a2008-07-27 21:46:04 +000010703 SDValue LHS = Op.getOperand(1);
10704 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010705 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010706 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010707 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10708 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10709 DAG.getConstant(X86CC, MVT::i8), Cond);
10710 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010711 }
Craig Topper6d688152012-08-14 07:43:25 +000010712
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010713 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010714 case Intrinsic::x86_sse2_pmulu_dq:
10715 case Intrinsic::x86_avx2_pmulu_dq:
10716 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10717 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010718
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010719 // SSE2/AVX2 sub with unsigned saturation intrinsics
10720 case Intrinsic::x86_sse2_psubus_b:
10721 case Intrinsic::x86_sse2_psubus_w:
10722 case Intrinsic::x86_avx2_psubus_b:
10723 case Intrinsic::x86_avx2_psubus_w:
10724 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10725 Op.getOperand(1), Op.getOperand(2));
10726
Craig Topper6d688152012-08-14 07:43:25 +000010727 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010728 case Intrinsic::x86_sse3_hadd_ps:
10729 case Intrinsic::x86_sse3_hadd_pd:
10730 case Intrinsic::x86_avx_hadd_ps_256:
10731 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010732 case Intrinsic::x86_sse3_hsub_ps:
10733 case Intrinsic::x86_sse3_hsub_pd:
10734 case Intrinsic::x86_avx_hsub_ps_256:
10735 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010736 case Intrinsic::x86_ssse3_phadd_w_128:
10737 case Intrinsic::x86_ssse3_phadd_d_128:
10738 case Intrinsic::x86_avx2_phadd_w:
10739 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010740 case Intrinsic::x86_ssse3_phsub_w_128:
10741 case Intrinsic::x86_ssse3_phsub_d_128:
10742 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010743 case Intrinsic::x86_avx2_phsub_d: {
10744 unsigned Opcode;
10745 switch (IntNo) {
10746 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10747 case Intrinsic::x86_sse3_hadd_ps:
10748 case Intrinsic::x86_sse3_hadd_pd:
10749 case Intrinsic::x86_avx_hadd_ps_256:
10750 case Intrinsic::x86_avx_hadd_pd_256:
10751 Opcode = X86ISD::FHADD;
10752 break;
10753 case Intrinsic::x86_sse3_hsub_ps:
10754 case Intrinsic::x86_sse3_hsub_pd:
10755 case Intrinsic::x86_avx_hsub_ps_256:
10756 case Intrinsic::x86_avx_hsub_pd_256:
10757 Opcode = X86ISD::FHSUB;
10758 break;
10759 case Intrinsic::x86_ssse3_phadd_w_128:
10760 case Intrinsic::x86_ssse3_phadd_d_128:
10761 case Intrinsic::x86_avx2_phadd_w:
10762 case Intrinsic::x86_avx2_phadd_d:
10763 Opcode = X86ISD::HADD;
10764 break;
10765 case Intrinsic::x86_ssse3_phsub_w_128:
10766 case Intrinsic::x86_ssse3_phsub_d_128:
10767 case Intrinsic::x86_avx2_phsub_w:
10768 case Intrinsic::x86_avx2_phsub_d:
10769 Opcode = X86ISD::HSUB;
10770 break;
10771 }
10772 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010773 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010774 }
10775
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010776 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10777 case Intrinsic::x86_sse2_pmaxu_b:
10778 case Intrinsic::x86_sse41_pmaxuw:
10779 case Intrinsic::x86_sse41_pmaxud:
10780 case Intrinsic::x86_avx2_pmaxu_b:
10781 case Intrinsic::x86_avx2_pmaxu_w:
10782 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010783 case Intrinsic::x86_sse2_pminu_b:
10784 case Intrinsic::x86_sse41_pminuw:
10785 case Intrinsic::x86_sse41_pminud:
10786 case Intrinsic::x86_avx2_pminu_b:
10787 case Intrinsic::x86_avx2_pminu_w:
10788 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010789 case Intrinsic::x86_sse41_pmaxsb:
10790 case Intrinsic::x86_sse2_pmaxs_w:
10791 case Intrinsic::x86_sse41_pmaxsd:
10792 case Intrinsic::x86_avx2_pmaxs_b:
10793 case Intrinsic::x86_avx2_pmaxs_w:
10794 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010795 case Intrinsic::x86_sse41_pminsb:
10796 case Intrinsic::x86_sse2_pmins_w:
10797 case Intrinsic::x86_sse41_pminsd:
10798 case Intrinsic::x86_avx2_pmins_b:
10799 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010800 case Intrinsic::x86_avx2_pmins_d: {
10801 unsigned Opcode;
10802 switch (IntNo) {
10803 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10804 case Intrinsic::x86_sse2_pmaxu_b:
10805 case Intrinsic::x86_sse41_pmaxuw:
10806 case Intrinsic::x86_sse41_pmaxud:
10807 case Intrinsic::x86_avx2_pmaxu_b:
10808 case Intrinsic::x86_avx2_pmaxu_w:
10809 case Intrinsic::x86_avx2_pmaxu_d:
10810 Opcode = X86ISD::UMAX;
10811 break;
10812 case Intrinsic::x86_sse2_pminu_b:
10813 case Intrinsic::x86_sse41_pminuw:
10814 case Intrinsic::x86_sse41_pminud:
10815 case Intrinsic::x86_avx2_pminu_b:
10816 case Intrinsic::x86_avx2_pminu_w:
10817 case Intrinsic::x86_avx2_pminu_d:
10818 Opcode = X86ISD::UMIN;
10819 break;
10820 case Intrinsic::x86_sse41_pmaxsb:
10821 case Intrinsic::x86_sse2_pmaxs_w:
10822 case Intrinsic::x86_sse41_pmaxsd:
10823 case Intrinsic::x86_avx2_pmaxs_b:
10824 case Intrinsic::x86_avx2_pmaxs_w:
10825 case Intrinsic::x86_avx2_pmaxs_d:
10826 Opcode = X86ISD::SMAX;
10827 break;
10828 case Intrinsic::x86_sse41_pminsb:
10829 case Intrinsic::x86_sse2_pmins_w:
10830 case Intrinsic::x86_sse41_pminsd:
10831 case Intrinsic::x86_avx2_pmins_b:
10832 case Intrinsic::x86_avx2_pmins_w:
10833 case Intrinsic::x86_avx2_pmins_d:
10834 Opcode = X86ISD::SMIN;
10835 break;
10836 }
10837 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010838 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010839 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010840
Craig Topper6d183e42012-12-29 16:44:25 +000010841 // SSE/SSE2/AVX floating point max/min intrinsics.
10842 case Intrinsic::x86_sse_max_ps:
10843 case Intrinsic::x86_sse2_max_pd:
10844 case Intrinsic::x86_avx_max_ps_256:
10845 case Intrinsic::x86_avx_max_pd_256:
10846 case Intrinsic::x86_sse_min_ps:
10847 case Intrinsic::x86_sse2_min_pd:
10848 case Intrinsic::x86_avx_min_ps_256:
10849 case Intrinsic::x86_avx_min_pd_256: {
10850 unsigned Opcode;
10851 switch (IntNo) {
10852 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10853 case Intrinsic::x86_sse_max_ps:
10854 case Intrinsic::x86_sse2_max_pd:
10855 case Intrinsic::x86_avx_max_ps_256:
10856 case Intrinsic::x86_avx_max_pd_256:
10857 Opcode = X86ISD::FMAX;
10858 break;
10859 case Intrinsic::x86_sse_min_ps:
10860 case Intrinsic::x86_sse2_min_pd:
10861 case Intrinsic::x86_avx_min_ps_256:
10862 case Intrinsic::x86_avx_min_pd_256:
10863 Opcode = X86ISD::FMIN;
10864 break;
10865 }
10866 return DAG.getNode(Opcode, dl, Op.getValueType(),
10867 Op.getOperand(1), Op.getOperand(2));
10868 }
10869
Craig Topper6d688152012-08-14 07:43:25 +000010870 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010871 case Intrinsic::x86_avx2_psllv_d:
10872 case Intrinsic::x86_avx2_psllv_q:
10873 case Intrinsic::x86_avx2_psllv_d_256:
10874 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010875 case Intrinsic::x86_avx2_psrlv_d:
10876 case Intrinsic::x86_avx2_psrlv_q:
10877 case Intrinsic::x86_avx2_psrlv_d_256:
10878 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010879 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010880 case Intrinsic::x86_avx2_psrav_d_256: {
10881 unsigned Opcode;
10882 switch (IntNo) {
10883 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10884 case Intrinsic::x86_avx2_psllv_d:
10885 case Intrinsic::x86_avx2_psllv_q:
10886 case Intrinsic::x86_avx2_psllv_d_256:
10887 case Intrinsic::x86_avx2_psllv_q_256:
10888 Opcode = ISD::SHL;
10889 break;
10890 case Intrinsic::x86_avx2_psrlv_d:
10891 case Intrinsic::x86_avx2_psrlv_q:
10892 case Intrinsic::x86_avx2_psrlv_d_256:
10893 case Intrinsic::x86_avx2_psrlv_q_256:
10894 Opcode = ISD::SRL;
10895 break;
10896 case Intrinsic::x86_avx2_psrav_d:
10897 case Intrinsic::x86_avx2_psrav_d_256:
10898 Opcode = ISD::SRA;
10899 break;
10900 }
10901 return DAG.getNode(Opcode, dl, Op.getValueType(),
10902 Op.getOperand(1), Op.getOperand(2));
10903 }
10904
Craig Topper969ba282012-01-25 06:43:11 +000010905 case Intrinsic::x86_ssse3_pshuf_b_128:
10906 case Intrinsic::x86_avx2_pshuf_b:
10907 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10908 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010909
Craig Topper969ba282012-01-25 06:43:11 +000010910 case Intrinsic::x86_ssse3_psign_b_128:
10911 case Intrinsic::x86_ssse3_psign_w_128:
10912 case Intrinsic::x86_ssse3_psign_d_128:
10913 case Intrinsic::x86_avx2_psign_b:
10914 case Intrinsic::x86_avx2_psign_w:
10915 case Intrinsic::x86_avx2_psign_d:
10916 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10917 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010918
Craig Toppere566cd02012-01-26 07:18:03 +000010919 case Intrinsic::x86_sse41_insertps:
10920 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10921 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010922
Craig Toppere566cd02012-01-26 07:18:03 +000010923 case Intrinsic::x86_avx_vperm2f128_ps_256:
10924 case Intrinsic::x86_avx_vperm2f128_pd_256:
10925 case Intrinsic::x86_avx_vperm2f128_si_256:
10926 case Intrinsic::x86_avx2_vperm2i128:
10927 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10928 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010929
Craig Topperffa6c402012-04-16 07:13:00 +000010930 case Intrinsic::x86_avx2_permd:
10931 case Intrinsic::x86_avx2_permps:
10932 // Operands intentionally swapped. Mask is last operand to intrinsic,
10933 // but second operand for node/intruction.
10934 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10935 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010936
Craig Topper22d8f0d2012-12-29 18:18:20 +000010937 case Intrinsic::x86_sse_sqrt_ps:
10938 case Intrinsic::x86_sse2_sqrt_pd:
10939 case Intrinsic::x86_avx_sqrt_ps_256:
10940 case Intrinsic::x86_avx_sqrt_pd_256:
10941 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10942
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010943 // ptest and testp intrinsics. The intrinsic these come from are designed to
10944 // return an integer value, not just an instruction so lower it to the ptest
10945 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010946 case Intrinsic::x86_sse41_ptestz:
10947 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010948 case Intrinsic::x86_sse41_ptestnzc:
10949 case Intrinsic::x86_avx_ptestz_256:
10950 case Intrinsic::x86_avx_ptestc_256:
10951 case Intrinsic::x86_avx_ptestnzc_256:
10952 case Intrinsic::x86_avx_vtestz_ps:
10953 case Intrinsic::x86_avx_vtestc_ps:
10954 case Intrinsic::x86_avx_vtestnzc_ps:
10955 case Intrinsic::x86_avx_vtestz_pd:
10956 case Intrinsic::x86_avx_vtestc_pd:
10957 case Intrinsic::x86_avx_vtestnzc_pd:
10958 case Intrinsic::x86_avx_vtestz_ps_256:
10959 case Intrinsic::x86_avx_vtestc_ps_256:
10960 case Intrinsic::x86_avx_vtestnzc_ps_256:
10961 case Intrinsic::x86_avx_vtestz_pd_256:
10962 case Intrinsic::x86_avx_vtestc_pd_256:
10963 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10964 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010965 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010966 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010967 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010968 case Intrinsic::x86_avx_vtestz_ps:
10969 case Intrinsic::x86_avx_vtestz_pd:
10970 case Intrinsic::x86_avx_vtestz_ps_256:
10971 case Intrinsic::x86_avx_vtestz_pd_256:
10972 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010973 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010974 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010975 // ZF = 1
10976 X86CC = X86::COND_E;
10977 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010978 case Intrinsic::x86_avx_vtestc_ps:
10979 case Intrinsic::x86_avx_vtestc_pd:
10980 case Intrinsic::x86_avx_vtestc_ps_256:
10981 case Intrinsic::x86_avx_vtestc_pd_256:
10982 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010983 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010984 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010985 // CF = 1
10986 X86CC = X86::COND_B;
10987 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010988 case Intrinsic::x86_avx_vtestnzc_ps:
10989 case Intrinsic::x86_avx_vtestnzc_pd:
10990 case Intrinsic::x86_avx_vtestnzc_ps_256:
10991 case Intrinsic::x86_avx_vtestnzc_pd_256:
10992 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010993 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010994 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010995 // ZF and CF = 0
10996 X86CC = X86::COND_A;
10997 break;
10998 }
Eric Christopherfd179292009-08-27 18:07:15 +000010999
Eric Christopher71c67532009-07-29 00:28:05 +000011000 SDValue LHS = Op.getOperand(1);
11001 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011002 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
11003 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000011004 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
11005 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
11006 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000011007 }
Evan Cheng5759f972008-05-04 09:15:50 +000011008
Craig Topper80e46362012-01-23 06:16:53 +000011009 // SSE/AVX shift intrinsics
11010 case Intrinsic::x86_sse2_psll_w:
11011 case Intrinsic::x86_sse2_psll_d:
11012 case Intrinsic::x86_sse2_psll_q:
11013 case Intrinsic::x86_avx2_psll_w:
11014 case Intrinsic::x86_avx2_psll_d:
11015 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000011016 case Intrinsic::x86_sse2_psrl_w:
11017 case Intrinsic::x86_sse2_psrl_d:
11018 case Intrinsic::x86_sse2_psrl_q:
11019 case Intrinsic::x86_avx2_psrl_w:
11020 case Intrinsic::x86_avx2_psrl_d:
11021 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000011022 case Intrinsic::x86_sse2_psra_w:
11023 case Intrinsic::x86_sse2_psra_d:
11024 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000011025 case Intrinsic::x86_avx2_psra_d: {
11026 unsigned Opcode;
11027 switch (IntNo) {
11028 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
11029 case Intrinsic::x86_sse2_psll_w:
11030 case Intrinsic::x86_sse2_psll_d:
11031 case Intrinsic::x86_sse2_psll_q:
11032 case Intrinsic::x86_avx2_psll_w:
11033 case Intrinsic::x86_avx2_psll_d:
11034 case Intrinsic::x86_avx2_psll_q:
11035 Opcode = X86ISD::VSHL;
11036 break;
11037 case Intrinsic::x86_sse2_psrl_w:
11038 case Intrinsic::x86_sse2_psrl_d:
11039 case Intrinsic::x86_sse2_psrl_q:
11040 case Intrinsic::x86_avx2_psrl_w:
11041 case Intrinsic::x86_avx2_psrl_d:
11042 case Intrinsic::x86_avx2_psrl_q:
11043 Opcode = X86ISD::VSRL;
11044 break;
11045 case Intrinsic::x86_sse2_psra_w:
11046 case Intrinsic::x86_sse2_psra_d:
11047 case Intrinsic::x86_avx2_psra_w:
11048 case Intrinsic::x86_avx2_psra_d:
11049 Opcode = X86ISD::VSRA;
11050 break;
11051 }
11052 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000011053 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000011054 }
11055
11056 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000011057 case Intrinsic::x86_sse2_pslli_w:
11058 case Intrinsic::x86_sse2_pslli_d:
11059 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000011060 case Intrinsic::x86_avx2_pslli_w:
11061 case Intrinsic::x86_avx2_pslli_d:
11062 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000011063 case Intrinsic::x86_sse2_psrli_w:
11064 case Intrinsic::x86_sse2_psrli_d:
11065 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000011066 case Intrinsic::x86_avx2_psrli_w:
11067 case Intrinsic::x86_avx2_psrli_d:
11068 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000011069 case Intrinsic::x86_sse2_psrai_w:
11070 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000011071 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000011072 case Intrinsic::x86_avx2_psrai_d: {
11073 unsigned Opcode;
11074 switch (IntNo) {
11075 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
11076 case Intrinsic::x86_sse2_pslli_w:
11077 case Intrinsic::x86_sse2_pslli_d:
11078 case Intrinsic::x86_sse2_pslli_q:
11079 case Intrinsic::x86_avx2_pslli_w:
11080 case Intrinsic::x86_avx2_pslli_d:
11081 case Intrinsic::x86_avx2_pslli_q:
11082 Opcode = X86ISD::VSHLI;
11083 break;
11084 case Intrinsic::x86_sse2_psrli_w:
11085 case Intrinsic::x86_sse2_psrli_d:
11086 case Intrinsic::x86_sse2_psrli_q:
11087 case Intrinsic::x86_avx2_psrli_w:
11088 case Intrinsic::x86_avx2_psrli_d:
11089 case Intrinsic::x86_avx2_psrli_q:
11090 Opcode = X86ISD::VSRLI;
11091 break;
11092 case Intrinsic::x86_sse2_psrai_w:
11093 case Intrinsic::x86_sse2_psrai_d:
11094 case Intrinsic::x86_avx2_psrai_w:
11095 case Intrinsic::x86_avx2_psrai_d:
11096 Opcode = X86ISD::VSRAI;
11097 break;
11098 }
11099 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000011100 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000011101 }
11102
Craig Topper4feb6472012-08-06 06:22:36 +000011103 case Intrinsic::x86_sse42_pcmpistria128:
11104 case Intrinsic::x86_sse42_pcmpestria128:
11105 case Intrinsic::x86_sse42_pcmpistric128:
11106 case Intrinsic::x86_sse42_pcmpestric128:
11107 case Intrinsic::x86_sse42_pcmpistrio128:
11108 case Intrinsic::x86_sse42_pcmpestrio128:
11109 case Intrinsic::x86_sse42_pcmpistris128:
11110 case Intrinsic::x86_sse42_pcmpestris128:
11111 case Intrinsic::x86_sse42_pcmpistriz128:
11112 case Intrinsic::x86_sse42_pcmpestriz128: {
11113 unsigned Opcode;
11114 unsigned X86CC;
11115 switch (IntNo) {
11116 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
11117 case Intrinsic::x86_sse42_pcmpistria128:
11118 Opcode = X86ISD::PCMPISTRI;
11119 X86CC = X86::COND_A;
11120 break;
11121 case Intrinsic::x86_sse42_pcmpestria128:
11122 Opcode = X86ISD::PCMPESTRI;
11123 X86CC = X86::COND_A;
11124 break;
11125 case Intrinsic::x86_sse42_pcmpistric128:
11126 Opcode = X86ISD::PCMPISTRI;
11127 X86CC = X86::COND_B;
11128 break;
11129 case Intrinsic::x86_sse42_pcmpestric128:
11130 Opcode = X86ISD::PCMPESTRI;
11131 X86CC = X86::COND_B;
11132 break;
11133 case Intrinsic::x86_sse42_pcmpistrio128:
11134 Opcode = X86ISD::PCMPISTRI;
11135 X86CC = X86::COND_O;
11136 break;
11137 case Intrinsic::x86_sse42_pcmpestrio128:
11138 Opcode = X86ISD::PCMPESTRI;
11139 X86CC = X86::COND_O;
11140 break;
11141 case Intrinsic::x86_sse42_pcmpistris128:
11142 Opcode = X86ISD::PCMPISTRI;
11143 X86CC = X86::COND_S;
11144 break;
11145 case Intrinsic::x86_sse42_pcmpestris128:
11146 Opcode = X86ISD::PCMPESTRI;
11147 X86CC = X86::COND_S;
11148 break;
11149 case Intrinsic::x86_sse42_pcmpistriz128:
11150 Opcode = X86ISD::PCMPISTRI;
11151 X86CC = X86::COND_E;
11152 break;
11153 case Intrinsic::x86_sse42_pcmpestriz128:
11154 Opcode = X86ISD::PCMPESTRI;
11155 X86CC = X86::COND_E;
11156 break;
11157 }
11158 SmallVector<SDValue, 5> NewOps;
11159 NewOps.append(Op->op_begin()+1, Op->op_end());
11160 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
11161 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
11162 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
11163 DAG.getConstant(X86CC, MVT::i8),
11164 SDValue(PCMP.getNode(), 1));
11165 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
11166 }
Craig Topper6d688152012-08-14 07:43:25 +000011167
Craig Topper4feb6472012-08-06 06:22:36 +000011168 case Intrinsic::x86_sse42_pcmpistri128:
11169 case Intrinsic::x86_sse42_pcmpestri128: {
11170 unsigned Opcode;
11171 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
11172 Opcode = X86ISD::PCMPISTRI;
11173 else
11174 Opcode = X86ISD::PCMPESTRI;
11175
11176 SmallVector<SDValue, 5> NewOps;
11177 NewOps.append(Op->op_begin()+1, Op->op_end());
11178 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
11179 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
11180 }
Craig Topper0e292372012-08-24 04:03:22 +000011181 case Intrinsic::x86_fma_vfmadd_ps:
11182 case Intrinsic::x86_fma_vfmadd_pd:
11183 case Intrinsic::x86_fma_vfmsub_ps:
11184 case Intrinsic::x86_fma_vfmsub_pd:
11185 case Intrinsic::x86_fma_vfnmadd_ps:
11186 case Intrinsic::x86_fma_vfnmadd_pd:
11187 case Intrinsic::x86_fma_vfnmsub_ps:
11188 case Intrinsic::x86_fma_vfnmsub_pd:
11189 case Intrinsic::x86_fma_vfmaddsub_ps:
11190 case Intrinsic::x86_fma_vfmaddsub_pd:
11191 case Intrinsic::x86_fma_vfmsubadd_ps:
11192 case Intrinsic::x86_fma_vfmsubadd_pd:
11193 case Intrinsic::x86_fma_vfmadd_ps_256:
11194 case Intrinsic::x86_fma_vfmadd_pd_256:
11195 case Intrinsic::x86_fma_vfmsub_ps_256:
11196 case Intrinsic::x86_fma_vfmsub_pd_256:
11197 case Intrinsic::x86_fma_vfnmadd_ps_256:
11198 case Intrinsic::x86_fma_vfnmadd_pd_256:
11199 case Intrinsic::x86_fma_vfnmsub_ps_256:
11200 case Intrinsic::x86_fma_vfnmsub_pd_256:
11201 case Intrinsic::x86_fma_vfmaddsub_ps_256:
11202 case Intrinsic::x86_fma_vfmaddsub_pd_256:
11203 case Intrinsic::x86_fma_vfmsubadd_ps_256:
11204 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000011205 unsigned Opc;
11206 switch (IntNo) {
11207 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
11208 case Intrinsic::x86_fma_vfmadd_ps:
11209 case Intrinsic::x86_fma_vfmadd_pd:
11210 case Intrinsic::x86_fma_vfmadd_ps_256:
11211 case Intrinsic::x86_fma_vfmadd_pd_256:
11212 Opc = X86ISD::FMADD;
11213 break;
11214 case Intrinsic::x86_fma_vfmsub_ps:
11215 case Intrinsic::x86_fma_vfmsub_pd:
11216 case Intrinsic::x86_fma_vfmsub_ps_256:
11217 case Intrinsic::x86_fma_vfmsub_pd_256:
11218 Opc = X86ISD::FMSUB;
11219 break;
11220 case Intrinsic::x86_fma_vfnmadd_ps:
11221 case Intrinsic::x86_fma_vfnmadd_pd:
11222 case Intrinsic::x86_fma_vfnmadd_ps_256:
11223 case Intrinsic::x86_fma_vfnmadd_pd_256:
11224 Opc = X86ISD::FNMADD;
11225 break;
11226 case Intrinsic::x86_fma_vfnmsub_ps:
11227 case Intrinsic::x86_fma_vfnmsub_pd:
11228 case Intrinsic::x86_fma_vfnmsub_ps_256:
11229 case Intrinsic::x86_fma_vfnmsub_pd_256:
11230 Opc = X86ISD::FNMSUB;
11231 break;
11232 case Intrinsic::x86_fma_vfmaddsub_ps:
11233 case Intrinsic::x86_fma_vfmaddsub_pd:
11234 case Intrinsic::x86_fma_vfmaddsub_ps_256:
11235 case Intrinsic::x86_fma_vfmaddsub_pd_256:
11236 Opc = X86ISD::FMADDSUB;
11237 break;
11238 case Intrinsic::x86_fma_vfmsubadd_ps:
11239 case Intrinsic::x86_fma_vfmsubadd_pd:
11240 case Intrinsic::x86_fma_vfmsubadd_ps_256:
11241 case Intrinsic::x86_fma_vfmsubadd_pd_256:
11242 Opc = X86ISD::FMSUBADD;
11243 break;
11244 }
11245
11246 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
11247 Op.getOperand(2), Op.getOperand(3));
11248 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000011249 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000011250}
Evan Cheng72261582005-12-20 06:22:03 +000011251
Craig Topper55b24052012-09-11 06:15:32 +000011252static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011253 SDLoc dl(Op);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011254 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11255 switch (IntNo) {
11256 default: return SDValue(); // Don't custom lower most intrinsics.
11257
Michael Liaoc26392a2013-03-28 23:41:26 +000011258 // RDRAND/RDSEED intrinsics.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011259 case Intrinsic::x86_rdrand_16:
11260 case Intrinsic::x86_rdrand_32:
Michael Liaoc26392a2013-03-28 23:41:26 +000011261 case Intrinsic::x86_rdrand_64:
11262 case Intrinsic::x86_rdseed_16:
11263 case Intrinsic::x86_rdseed_32:
11264 case Intrinsic::x86_rdseed_64: {
11265 unsigned Opcode = (IntNo == Intrinsic::x86_rdseed_16 ||
11266 IntNo == Intrinsic::x86_rdseed_32 ||
11267 IntNo == Intrinsic::x86_rdseed_64) ? X86ISD::RDSEED :
11268 X86ISD::RDRAND;
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011269 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000011270 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
Michael Liaoc26392a2013-03-28 23:41:26 +000011271 SDValue Result = DAG.getNode(Opcode, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011272
Michael Liaoc26392a2013-03-28 23:41:26 +000011273 // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1.
11274 // Otherwise return the value from Rand, which is always 0, casted to i32.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011275 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
11276 DAG.getConstant(1, Op->getValueType(1)),
11277 DAG.getConstant(X86::COND_B, MVT::i32),
11278 SDValue(Result.getNode(), 1) };
11279 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
11280 DAG.getVTList(Op->getValueType(1), MVT::Glue),
Michael Liao0ee17002013-04-19 04:03:37 +000011281 Ops, array_lengthof(Ops));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011282
11283 // Return { result, isValid, chain }.
11284 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000011285 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011286 }
Michael Liaof8fd8832013-03-26 22:47:01 +000011287
11288 // XTEST intrinsics.
11289 case Intrinsic::x86_xtest: {
11290 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
11291 SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0));
11292 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
11293 DAG.getConstant(X86::COND_NE, MVT::i8),
11294 InTrans);
11295 SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
11296 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
11297 Ret, SDValue(InTrans.getNode(), 1));
11298 }
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011299 }
11300}
11301
Dan Gohmand858e902010-04-17 15:26:15 +000011302SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
11303 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000011304 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11305 MFI->setReturnAddressIsTaken(true);
11306
Bill Wendling64e87322009-01-16 19:25:27 +000011307 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011308 SDLoc dl(Op);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011309 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000011310
11311 if (Depth > 0) {
11312 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011313 const X86RegisterInfo *RegInfo =
11314 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
11315 SDValue Offset = DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011316 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
11317 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000011318 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011319 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000011320 }
11321
11322 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000011323 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011324 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011325 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011326}
11327
Dan Gohmand858e902010-04-17 15:26:15 +000011328SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000011329 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11330 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000011331
Owen Andersone50ed302009-08-10 22:56:29 +000011332 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011333 SDLoc dl(Op); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000011334 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011335 const X86RegisterInfo *RegInfo =
11336 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaob9cca132013-05-02 08:21:56 +000011337 unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
11338 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
Michael Liao299eb2e2013-05-02 09:22:04 +000011339 (FrameReg == X86::EBP && VT == MVT::i32)) &&
11340 "Invalid Frame Register!");
Dale Johannesendd64c412009-02-04 00:33:20 +000011341 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000011342 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000011343 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
11344 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011345 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000011346 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000011347}
11348
Dan Gohman475871a2008-07-27 21:46:04 +000011349SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011350 SelectionDAG &DAG) const {
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011351 const X86RegisterInfo *RegInfo =
11352 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011353 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011354}
11355
Dan Gohmand858e902010-04-17 15:26:15 +000011356SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011357 SDValue Chain = Op.getOperand(0);
11358 SDValue Offset = Op.getOperand(1);
11359 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +000011360 SDLoc dl (Op);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011361
Michael Liaodb7da202013-05-02 09:18:38 +000011362 EVT PtrVT = getPointerTy();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011363 const X86RegisterInfo *RegInfo =
11364 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaodb7da202013-05-02 09:18:38 +000011365 unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
11366 assert(((FrameReg == X86::RBP && PtrVT == MVT::i64) ||
11367 (FrameReg == X86::EBP && PtrVT == MVT::i32)) &&
11368 "Invalid Frame Register!");
11369 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, PtrVT);
11370 unsigned StoreAddrReg = (PtrVT == MVT::i64) ? X86::RCX : X86::ECX;
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011371
Michael Liaodb7da202013-05-02 09:18:38 +000011372 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, Frame,
Michael Liao299eb2e2013-05-02 09:22:04 +000011373 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Michael Liaodb7da202013-05-02 09:18:38 +000011374 StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011375 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11376 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011377 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011378
Michael Liaodb7da202013-05-02 09:18:38 +000011379 return DAG.getNode(X86ISD::EH_RETURN, dl, MVT::Other, Chain,
11380 DAG.getRegister(StoreAddrReg, PtrVT));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011381}
11382
Michael Liao6c0e04c2012-10-15 22:39:43 +000011383SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11384 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011385 SDLoc DL(Op);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011386 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11387 DAG.getVTList(MVT::i32, MVT::Other),
11388 Op.getOperand(0), Op.getOperand(1));
11389}
11390
11391SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11392 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011393 SDLoc DL(Op);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011394 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11395 Op.getOperand(0), Op.getOperand(1));
11396}
11397
Craig Topper55b24052012-09-11 06:15:32 +000011398static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011399 return Op.getOperand(0);
11400}
11401
11402SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11403 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011404 SDValue Root = Op.getOperand(0);
11405 SDValue Trmp = Op.getOperand(1); // trampoline
11406 SDValue FPtr = Op.getOperand(2); // nested function
11407 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickac6d9be2013-05-25 02:42:55 +000011408 SDLoc dl (Op);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011409
Dan Gohman69de1932008-02-06 22:27:42 +000011410 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011411 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011412
11413 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011414 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011415
11416 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011417 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11418 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011419
Michael Liao7abf67a2012-10-04 19:50:43 +000011420 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11421 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011422
11423 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11424
11425 // Load the pointer to the nested function into R11.
11426 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011427 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011428 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011429 Addr, MachinePointerInfo(TrmpAddr),
11430 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011431
Owen Anderson825b72b2009-08-11 20:47:22 +000011432 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11433 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011434 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11435 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011436 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011437
11438 // Load the 'nest' parameter value into R10.
11439 // R10 is specified in X86CallingConv.td
11440 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011441 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11442 DAG.getConstant(10, MVT::i64));
11443 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011444 Addr, MachinePointerInfo(TrmpAddr, 10),
11445 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011446
Owen Anderson825b72b2009-08-11 20:47:22 +000011447 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11448 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011449 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11450 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011451 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011452
11453 // Jump to the nested function.
11454 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011455 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11456 DAG.getConstant(20, MVT::i64));
11457 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011458 Addr, MachinePointerInfo(TrmpAddr, 20),
11459 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011460
11461 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011462 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11463 DAG.getConstant(22, MVT::i64));
11464 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011465 MachinePointerInfo(TrmpAddr, 22),
11466 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011467
Duncan Sands4a544a72011-09-06 13:37:06 +000011468 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011469 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011470 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011471 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011472 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011473 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011474
11475 switch (CC) {
11476 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011477 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011478 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011479 case CallingConv::X86_StdCall: {
11480 // Pass 'nest' parameter in ECX.
11481 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011482 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011483
11484 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011485 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011486 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011487
Chris Lattner58d74912008-03-12 17:45:29 +000011488 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011489 unsigned InRegCount = 0;
11490 unsigned Idx = 1;
11491
11492 for (FunctionType::param_iterator I = FTy->param_begin(),
11493 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011494 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011495 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011496 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011497
11498 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011499 report_fatal_error("Nest register in use - reduce number of inreg"
11500 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011501 }
11502 }
11503 break;
11504 }
11505 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011506 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011507 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011508 // Pass 'nest' parameter in EAX.
11509 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011510 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011511 break;
11512 }
11513
Dan Gohman475871a2008-07-27 21:46:04 +000011514 SDValue OutChains[4];
11515 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011516
Owen Anderson825b72b2009-08-11 20:47:22 +000011517 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11518 DAG.getConstant(10, MVT::i32));
11519 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011520
Chris Lattnera62fe662010-02-05 19:20:30 +000011521 // This is storing the opcode for MOV32ri.
11522 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011523 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011524 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011525 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011526 Trmp, MachinePointerInfo(TrmpAddr),
11527 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011528
Owen Anderson825b72b2009-08-11 20:47:22 +000011529 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11530 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011531 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11532 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011533 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011534
Chris Lattnera62fe662010-02-05 19:20:30 +000011535 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011536 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11537 DAG.getConstant(5, MVT::i32));
11538 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011539 MachinePointerInfo(TrmpAddr, 5),
11540 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011541
Owen Anderson825b72b2009-08-11 20:47:22 +000011542 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11543 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011544 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11545 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011546 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011547
Duncan Sands4a544a72011-09-06 13:37:06 +000011548 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011549 }
11550}
11551
Dan Gohmand858e902010-04-17 15:26:15 +000011552SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11553 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011554 /*
11555 The rounding mode is in bits 11:10 of FPSR, and has the following
11556 settings:
11557 00 Round to nearest
11558 01 Round to -inf
11559 10 Round to +inf
11560 11 Round to 0
11561
11562 FLT_ROUNDS, on the other hand, expects the following:
11563 -1 Undefined
11564 0 Round to 0
11565 1 Round to nearest
11566 2 Round to +inf
11567 3 Round to -inf
11568
11569 To perform the conversion, we do:
11570 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11571 */
11572
11573 MachineFunction &MF = DAG.getMachineFunction();
11574 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011575 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011576 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011577 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011578 SDLoc DL(Op);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011579
11580 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011581 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011582 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011583
Chris Lattner2156b792010-09-22 01:11:26 +000011584 MachineMemOperand *MMO =
11585 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11586 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011587
Chris Lattner2156b792010-09-22 01:11:26 +000011588 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11589 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11590 DAG.getVTList(MVT::Other),
Michael Liao0ee17002013-04-19 04:03:37 +000011591 Ops, array_lengthof(Ops), MVT::i16,
11592 MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011593
11594 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011595 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011596 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011597
11598 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011599 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011600 DAG.getNode(ISD::SRL, DL, MVT::i16,
11601 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011602 CWD, DAG.getConstant(0x800, MVT::i16)),
11603 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011604 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011605 DAG.getNode(ISD::SRL, DL, MVT::i16,
11606 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011607 CWD, DAG.getConstant(0x400, MVT::i16)),
11608 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011609
Dan Gohman475871a2008-07-27 21:46:04 +000011610 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011611 DAG.getNode(ISD::AND, DL, MVT::i16,
11612 DAG.getNode(ISD::ADD, DL, MVT::i16,
11613 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011614 DAG.getConstant(1, MVT::i16)),
11615 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011616
Duncan Sands83ec4b62008-06-06 12:08:01 +000011617 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011618 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011619}
11620
Craig Topper55b24052012-09-11 06:15:32 +000011621static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011622 EVT VT = Op.getValueType();
11623 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011624 unsigned NumBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011625 SDLoc dl(Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011626
11627 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011628 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011629 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011630 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011631 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011632 }
Evan Cheng18efe262007-12-14 02:13:44 +000011633
Evan Cheng152804e2007-12-14 08:30:15 +000011634 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011635 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011636 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011637
11638 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011639 SDValue Ops[] = {
11640 Op,
11641 DAG.getConstant(NumBits+NumBits-1, OpVT),
11642 DAG.getConstant(X86::COND_E, MVT::i8),
11643 Op.getValue(1)
11644 };
11645 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011646
11647 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011648 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011649
Owen Anderson825b72b2009-08-11 20:47:22 +000011650 if (VT == MVT::i8)
11651 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011652 return Op;
11653}
11654
Craig Topper55b24052012-09-11 06:15:32 +000011655static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011656 EVT VT = Op.getValueType();
11657 EVT OpVT = VT;
11658 unsigned NumBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011659 SDLoc dl(Op);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011660
11661 Op = Op.getOperand(0);
11662 if (VT == MVT::i8) {
11663 // Zero extend to i32 since there is not an i8 bsr.
11664 OpVT = MVT::i32;
11665 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11666 }
11667
11668 // Issue a bsr (scan bits in reverse).
11669 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11670 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11671
11672 // And xor with NumBits-1.
11673 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11674
11675 if (VT == MVT::i8)
11676 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11677 return Op;
11678}
11679
Craig Topper55b24052012-09-11 06:15:32 +000011680static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011681 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011682 unsigned NumBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011683 SDLoc dl(Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011684 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011685
11686 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011687 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011688 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011689
11690 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011691 SDValue Ops[] = {
11692 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011693 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011694 DAG.getConstant(X86::COND_E, MVT::i8),
11695 Op.getValue(1)
11696 };
Chandler Carruth77821022011-12-24 12:12:34 +000011697 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011698}
11699
Craig Topper13894fa2011-08-24 06:14:18 +000011700// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11701// ones, and then concatenate the result back.
11702static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011703 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011704
Craig Topper7a9a28b2012-08-12 02:23:29 +000011705 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011706 "Unsupported value type for operation");
11707
Craig Topper66ddd152012-04-27 22:54:43 +000011708 unsigned NumElems = VT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011709 SDLoc dl(Op);
Craig Topper13894fa2011-08-24 06:14:18 +000011710
11711 // Extract the LHS vectors
11712 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011713 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11714 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011715
11716 // Extract the RHS vectors
11717 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011718 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11719 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011720
11721 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11722 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11723
11724 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11725 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11726 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11727}
11728
Craig Topper55b24052012-09-11 06:15:32 +000011729static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011730 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011731 Op.getValueType().isInteger() &&
11732 "Only handle AVX 256-bit vector integer operation");
11733 return Lower256IntArith(Op, DAG);
11734}
11735
Craig Topper55b24052012-09-11 06:15:32 +000011736static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011737 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011738 Op.getValueType().isInteger() &&
11739 "Only handle AVX 256-bit vector integer operation");
11740 return Lower256IntArith(Op, DAG);
11741}
11742
Craig Topper55b24052012-09-11 06:15:32 +000011743static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11744 SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011745 SDLoc dl(Op);
Craig Topper13894fa2011-08-24 06:14:18 +000011746 EVT VT = Op.getValueType();
11747
11748 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011749 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011750 return Lower256IntArith(Op, DAG);
11751
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011752 SDValue A = Op.getOperand(0);
11753 SDValue B = Op.getOperand(1);
11754
11755 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11756 if (VT == MVT::v4i32) {
11757 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11758 "Should not custom lower when pmuldq is available!");
11759
11760 // Extract the odd parts.
Craig Topperda129a22013-07-15 06:54:12 +000011761 static const int UnpackMask[] = { 1, -1, 3, -1 };
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011762 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11763 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11764
11765 // Multiply the even parts.
11766 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11767 // Now multiply odd parts.
11768 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11769
11770 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11771 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11772
11773 // Merge the two vectors back together with a shuffle. This expands into 2
11774 // shuffles.
Craig Topperda129a22013-07-15 06:54:12 +000011775 static const int ShufMask[] = { 0, 4, 2, 6 };
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011776 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11777 }
11778
Craig Topper5b209e82012-02-05 03:14:49 +000011779 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11780 "Only know how to lower V2I64/V4I64 multiply");
11781
Craig Topper5b209e82012-02-05 03:14:49 +000011782 // Ahi = psrlqi(a, 32);
11783 // Bhi = psrlqi(b, 32);
11784 //
11785 // AloBlo = pmuludq(a, b);
11786 // AloBhi = pmuludq(a, Bhi);
11787 // AhiBlo = pmuludq(Ahi, b);
11788
11789 // AloBhi = psllqi(AloBhi, 32);
11790 // AhiBlo = psllqi(AhiBlo, 32);
11791 // return AloBlo + AloBhi + AhiBlo;
11792
Craig Topper5b209e82012-02-05 03:14:49 +000011793 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011794
Craig Topper5b209e82012-02-05 03:14:49 +000011795 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11796 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011797
Craig Topper5b209e82012-02-05 03:14:49 +000011798 // Bit cast to 32-bit vectors for MULUDQ
11799 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11800 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11801 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11802 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11803 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011804
Craig Topper5b209e82012-02-05 03:14:49 +000011805 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11806 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11807 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011808
Craig Topper5b209e82012-02-05 03:14:49 +000011809 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11810 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011811
Dale Johannesene4d209d2009-02-03 20:21:25 +000011812 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011813 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011814}
11815
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011816SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11817 EVT VT = Op.getValueType();
11818 EVT EltTy = VT.getVectorElementType();
11819 unsigned NumElts = VT.getVectorNumElements();
11820 SDValue N0 = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000011821 SDLoc dl(Op);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011822
11823 // Lower sdiv X, pow2-const.
11824 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11825 if (!C)
11826 return SDValue();
11827
11828 APInt SplatValue, SplatUndef;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +000011829 unsigned SplatBitSize;
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011830 bool HasAnyUndefs;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +000011831 if (!C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
11832 HasAnyUndefs) ||
11833 EltTy.getSizeInBits() < SplatBitSize)
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011834 return SDValue();
11835
11836 if ((SplatValue != 0) &&
11837 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11838 unsigned lg2 = SplatValue.countTrailingZeros();
11839 // Splat the sign bit.
11840 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11841 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11842 // Add (N0 < 0) ? abs2 - 1 : 0;
11843 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11844 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11845 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11846 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11847 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11848
11849 // If we're dividing by a positive value, we're done. Otherwise, we must
11850 // negate the result.
11851 if (SplatValue.isNonNegative())
11852 return SRA;
11853
11854 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11855 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11856 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11857 }
11858 return SDValue();
11859}
11860
Michael Liao4b7ab122013-03-20 02:20:36 +000011861static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11862 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011863 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011864 SDLoc dl(Op);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011865 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011866 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011867
Nadav Rotem43012222011-05-11 08:12:09 +000011868 // Optimize shl/srl/sra with constant shift amount.
11869 if (isSplatVector(Amt.getNode())) {
11870 SDValue SclrAmt = Amt->getOperand(0);
11871 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11872 uint64_t ShiftAmt = C->getZExtValue();
11873
Craig Toppered2e13d2012-01-22 19:15:14 +000011874 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011875 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011876 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11877 if (Op.getOpcode() == ISD::SHL)
11878 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11879 DAG.getConstant(ShiftAmt, MVT::i32));
11880 if (Op.getOpcode() == ISD::SRL)
11881 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11882 DAG.getConstant(ShiftAmt, MVT::i32));
11883 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11884 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11885 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011886 }
11887
Craig Toppered2e13d2012-01-22 19:15:14 +000011888 if (VT == MVT::v16i8) {
11889 if (Op.getOpcode() == ISD::SHL) {
11890 // Make a large shift.
11891 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11892 DAG.getConstant(ShiftAmt, MVT::i32));
11893 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11894 // Zero out the rightmost bits.
11895 SmallVector<SDValue, 16> V(16,
11896 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11897 MVT::i8));
11898 return DAG.getNode(ISD::AND, dl, VT, SHL,
11899 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011900 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011901 if (Op.getOpcode() == ISD::SRL) {
11902 // Make a large shift.
11903 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11904 DAG.getConstant(ShiftAmt, MVT::i32));
11905 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11906 // Zero out the leftmost bits.
11907 SmallVector<SDValue, 16> V(16,
11908 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11909 MVT::i8));
11910 return DAG.getNode(ISD::AND, dl, VT, SRL,
11911 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11912 }
11913 if (Op.getOpcode() == ISD::SRA) {
11914 if (ShiftAmt == 7) {
11915 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011916 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011917 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011918 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011919
Craig Toppered2e13d2012-01-22 19:15:14 +000011920 // R s>> a === ((R u>> a) ^ m) - m
11921 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11922 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11923 MVT::i8));
11924 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11925 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11926 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11927 return Res;
11928 }
Craig Topper731dfd02012-04-23 03:42:40 +000011929 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011930 }
Craig Topper46154eb2011-11-11 07:39:23 +000011931
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011932 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011933 if (Op.getOpcode() == ISD::SHL) {
11934 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011935 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11936 DAG.getConstant(ShiftAmt, MVT::i32));
11937 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011938 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011939 SmallVector<SDValue, 32> V(32,
11940 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11941 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011942 return DAG.getNode(ISD::AND, dl, VT, SHL,
11943 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011944 }
Craig Topper0d86d462011-11-20 00:12:05 +000011945 if (Op.getOpcode() == ISD::SRL) {
11946 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011947 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11948 DAG.getConstant(ShiftAmt, MVT::i32));
11949 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011950 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011951 SmallVector<SDValue, 32> V(32,
11952 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11953 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011954 return DAG.getNode(ISD::AND, dl, VT, SRL,
11955 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11956 }
11957 if (Op.getOpcode() == ISD::SRA) {
11958 if (ShiftAmt == 7) {
11959 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011960 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011961 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011962 }
11963
11964 // R s>> a === ((R u>> a) ^ m) - m
11965 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11966 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11967 MVT::i8));
11968 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11969 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11970 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11971 return Res;
11972 }
Craig Topper731dfd02012-04-23 03:42:40 +000011973 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011974 }
Nadav Rotem43012222011-05-11 08:12:09 +000011975 }
11976 }
11977
Michael Liao42317cc2013-03-20 02:33:21 +000011978 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11979 if (!Subtarget->is64Bit() &&
11980 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11981 Amt.getOpcode() == ISD::BITCAST &&
11982 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11983 Amt = Amt.getOperand(0);
11984 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11985 VT.getVectorNumElements();
11986 unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
11987 uint64_t ShiftAmt = 0;
11988 for (unsigned i = 0; i != Ratio; ++i) {
11989 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i));
11990 if (C == 0)
11991 return SDValue();
11992 // 6 == Log2(64)
11993 ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
11994 }
11995 // Check remaining shift amounts.
11996 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11997 uint64_t ShAmt = 0;
11998 for (unsigned j = 0; j != Ratio; ++j) {
11999 ConstantSDNode *C =
12000 dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
12001 if (C == 0)
12002 return SDValue();
12003 // 6 == Log2(64)
12004 ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
12005 }
12006 if (ShAmt != ShiftAmt)
12007 return SDValue();
12008 }
12009 switch (Op.getOpcode()) {
12010 default:
12011 llvm_unreachable("Unknown shift opcode!");
12012 case ISD::SHL:
12013 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
12014 DAG.getConstant(ShiftAmt, MVT::i32));
12015 case ISD::SRL:
12016 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
12017 DAG.getConstant(ShiftAmt, MVT::i32));
12018 case ISD::SRA:
12019 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
12020 DAG.getConstant(ShiftAmt, MVT::i32));
12021 }
12022 }
12023
12024 return SDValue();
12025}
12026
12027static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
12028 const X86Subtarget* Subtarget) {
12029 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012030 SDLoc dl(Op);
Michael Liao42317cc2013-03-20 02:33:21 +000012031 SDValue R = Op.getOperand(0);
12032 SDValue Amt = Op.getOperand(1);
12033
12034 if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) ||
12035 VT == MVT::v4i32 || VT == MVT::v8i16 ||
12036 (Subtarget->hasInt256() &&
12037 ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) ||
12038 VT == MVT::v8i32 || VT == MVT::v16i16))) {
12039 SDValue BaseShAmt;
12040 EVT EltVT = VT.getVectorElementType();
12041
12042 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
12043 unsigned NumElts = VT.getVectorNumElements();
12044 unsigned i, j;
12045 for (i = 0; i != NumElts; ++i) {
12046 if (Amt.getOperand(i).getOpcode() == ISD::UNDEF)
12047 continue;
12048 break;
12049 }
12050 for (j = i; j != NumElts; ++j) {
12051 SDValue Arg = Amt.getOperand(j);
12052 if (Arg.getOpcode() == ISD::UNDEF) continue;
12053 if (Arg != Amt.getOperand(i))
12054 break;
12055 }
12056 if (i != NumElts && j == NumElts)
12057 BaseShAmt = Amt.getOperand(i);
12058 } else {
12059 if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12060 Amt = Amt.getOperand(0);
12061 if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE &&
12062 cast<ShuffleVectorSDNode>(Amt)->isSplat()) {
12063 SDValue InVec = Amt.getOperand(0);
12064 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
12065 unsigned NumElts = InVec.getValueType().getVectorNumElements();
12066 unsigned i = 0;
12067 for (; i != NumElts; ++i) {
12068 SDValue Arg = InVec.getOperand(i);
12069 if (Arg.getOpcode() == ISD::UNDEF) continue;
12070 BaseShAmt = Arg;
12071 break;
12072 }
12073 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12074 if (ConstantSDNode *C =
12075 dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
12076 unsigned SplatIdx =
12077 cast<ShuffleVectorSDNode>(Amt)->getSplatIndex();
12078 if (C->getZExtValue() == SplatIdx)
12079 BaseShAmt = InVec.getOperand(1);
12080 }
12081 }
12082 if (BaseShAmt.getNode() == 0)
12083 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt,
12084 DAG.getIntPtrConstant(0));
12085 }
12086 }
12087
12088 if (BaseShAmt.getNode()) {
12089 if (EltVT.bitsGT(MVT::i32))
12090 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
12091 else if (EltVT.bitsLT(MVT::i32))
12092 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
12093
12094 switch (Op.getOpcode()) {
12095 default:
12096 llvm_unreachable("Unknown shift opcode!");
12097 case ISD::SHL:
12098 switch (VT.getSimpleVT().SimpleTy) {
12099 default: return SDValue();
12100 case MVT::v2i64:
12101 case MVT::v4i32:
12102 case MVT::v8i16:
12103 case MVT::v4i64:
12104 case MVT::v8i32:
12105 case MVT::v16i16:
12106 return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG);
12107 }
12108 case ISD::SRA:
12109 switch (VT.getSimpleVT().SimpleTy) {
12110 default: return SDValue();
12111 case MVT::v4i32:
12112 case MVT::v8i16:
12113 case MVT::v8i32:
12114 case MVT::v16i16:
12115 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG);
12116 }
12117 case ISD::SRL:
12118 switch (VT.getSimpleVT().SimpleTy) {
12119 default: return SDValue();
12120 case MVT::v2i64:
12121 case MVT::v4i32:
12122 case MVT::v8i16:
12123 case MVT::v4i64:
12124 case MVT::v8i32:
12125 case MVT::v16i16:
12126 return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG);
12127 }
12128 }
12129 }
12130 }
12131
12132 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
12133 if (!Subtarget->is64Bit() &&
12134 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
12135 Amt.getOpcode() == ISD::BITCAST &&
12136 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
12137 Amt = Amt.getOperand(0);
12138 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
12139 VT.getVectorNumElements();
12140 std::vector<SDValue> Vals(Ratio);
12141 for (unsigned i = 0; i != Ratio; ++i)
12142 Vals[i] = Amt.getOperand(i);
12143 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
12144 for (unsigned j = 0; j != Ratio; ++j)
12145 if (Vals[j] != Amt.getOperand(i + j))
12146 return SDValue();
12147 }
12148 switch (Op.getOpcode()) {
12149 default:
12150 llvm_unreachable("Unknown shift opcode!");
12151 case ISD::SHL:
12152 return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1));
12153 case ISD::SRL:
12154 return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1));
12155 case ISD::SRA:
12156 return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1));
12157 }
12158 }
12159
Michael Liao4b7ab122013-03-20 02:20:36 +000012160 return SDValue();
12161}
12162
12163SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
12164
12165 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012166 SDLoc dl(Op);
Michael Liao4b7ab122013-03-20 02:20:36 +000012167 SDValue R = Op.getOperand(0);
12168 SDValue Amt = Op.getOperand(1);
12169 SDValue V;
12170
12171 if (!Subtarget->hasSSE2())
12172 return SDValue();
12173
12174 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
12175 if (V.getNode())
12176 return V;
12177
Michael Liao42317cc2013-03-20 02:33:21 +000012178 V = LowerScalarVariableShift(Op, DAG, Subtarget);
12179 if (V.getNode())
12180 return V;
12181
Michael Liao5c5f1902013-03-20 02:28:20 +000012182 // AVX2 has VPSLLV/VPSRAV/VPSRLV.
12183 if (Subtarget->hasInt256()) {
12184 if (Op.getOpcode() == ISD::SRL &&
12185 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
12186 VT == MVT::v4i64 || VT == MVT::v8i32))
12187 return Op;
12188 if (Op.getOpcode() == ISD::SHL &&
12189 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
12190 VT == MVT::v4i64 || VT == MVT::v8i32))
12191 return Op;
12192 if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32))
12193 return Op;
12194 }
12195
Nadav Rotem43012222011-05-11 08:12:09 +000012196 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000012197 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000012198 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000012199
Benjamin Kramer9fa92512013-02-04 15:19:25 +000012200 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012201 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000012202 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
12203 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
12204 }
Nadav Rotem43012222011-05-11 08:12:09 +000012205 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000012206 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000012207
Nate Begeman51409212010-07-28 00:21:48 +000012208 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000012209 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000012210 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000012211
Lang Hames8b99c1e2011-12-17 01:08:46 +000012212 // Turn 'a' into a mask suitable for VSELECT
12213 SDValue VSelM = DAG.getConstant(0x80, VT);
12214 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000012215 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000012216
Lang Hames8b99c1e2011-12-17 01:08:46 +000012217 SDValue CM1 = DAG.getConstant(0x0f, VT);
12218 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000012219
Lang Hames8b99c1e2011-12-17 01:08:46 +000012220 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
12221 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000012222 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
12223 DAG.getConstant(4, MVT::i32), DAG);
12224 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000012225 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
12226
Nate Begeman51409212010-07-28 00:21:48 +000012227 // a += a
12228 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000012229 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000012230 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000012231
Lang Hames8b99c1e2011-12-17 01:08:46 +000012232 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
12233 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012234 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
12235 DAG.getConstant(2, MVT::i32), DAG);
12236 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000012237 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
12238
Nate Begeman51409212010-07-28 00:21:48 +000012239 // a += a
12240 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000012241 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000012242 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000012243
Lang Hames8b99c1e2011-12-17 01:08:46 +000012244 // return VSELECT(r, r+r, a);
12245 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000012246 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000012247 return R;
12248 }
Craig Topper46154eb2011-11-11 07:39:23 +000012249
12250 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000012251 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012252 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000012253 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12254 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
12255
12256 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000012257 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
12258 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000012259
12260 // Recreate the shift amount vectors
12261 SDValue Amt1, Amt2;
12262 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
12263 // Constant shift amount
12264 SmallVector<SDValue, 4> Amt1Csts;
12265 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000012266 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000012267 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000012268 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000012269 Amt2Csts.push_back(Amt->getOperand(i));
12270
12271 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
12272 &Amt1Csts[0], NumElems/2);
12273 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
12274 &Amt2Csts[0], NumElems/2);
12275 } else {
12276 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000012277 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
12278 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000012279 }
12280
12281 // Issue new vector shifts for the smaller types
12282 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
12283 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
12284
12285 // Concatenate the result back
12286 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
12287 }
12288
Nate Begeman51409212010-07-28 00:21:48 +000012289 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000012290}
Mon P Wangaf9b9522008-12-18 21:42:19 +000012291
Craig Topper55b24052012-09-11 06:15:32 +000012292static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000012293 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
12294 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000012295 // looks for this combo and may remove the "setcc" instruction if the "setcc"
12296 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000012297 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000012298 SDValue LHS = N->getOperand(0);
12299 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000012300 unsigned BaseOp = 0;
12301 unsigned Cond = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +000012302 SDLoc DL(Op);
Bill Wendling74c37652008-12-09 22:08:41 +000012303 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012304 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000012305 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000012306 // A subtract of one will be selected as a INC. Note that INC doesn't
12307 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000012308 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
12309 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000012310 BaseOp = X86ISD::INC;
12311 Cond = X86::COND_O;
12312 break;
12313 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012314 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000012315 Cond = X86::COND_O;
12316 break;
12317 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012318 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000012319 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000012320 break;
12321 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000012322 // A subtract of one will be selected as a DEC. Note that DEC doesn't
12323 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000012324 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
12325 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000012326 BaseOp = X86ISD::DEC;
12327 Cond = X86::COND_O;
12328 break;
12329 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012330 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000012331 Cond = X86::COND_O;
12332 break;
12333 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012334 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000012335 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000012336 break;
12337 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000012338 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000012339 Cond = X86::COND_O;
12340 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012341 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
12342 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
12343 MVT::i32);
12344 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012345
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012346 SDValue SetCC =
12347 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12348 DAG.getConstant(X86::COND_O, MVT::i32),
12349 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012350
Dan Gohman6e5fda22011-07-22 18:45:15 +000012351 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012352 }
Bill Wendling74c37652008-12-09 22:08:41 +000012353 }
Bill Wendling3fafd932008-11-26 22:37:40 +000012354
Bill Wendling61edeb52008-12-02 01:06:39 +000012355 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000012356 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012357 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000012358
Bill Wendling61edeb52008-12-02 01:06:39 +000012359 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012360 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
12361 DAG.getConstant(Cond, MVT::i32),
12362 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000012363
Dan Gohman6e5fda22011-07-22 18:45:15 +000012364 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000012365}
12366
Chad Rosier30450e82011-12-22 22:35:21 +000012367SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
12368 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012369 SDLoc dl(Op);
Craig Toppera124f942011-11-21 01:12:36 +000012370 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
12371 EVT VT = Op.getValueType();
12372
Craig Toppered2e13d2012-01-22 19:15:14 +000012373 if (!Subtarget->hasSSE2() || !VT.isVector())
12374 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012375
Craig Toppered2e13d2012-01-22 19:15:14 +000012376 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
12377 ExtraVT.getScalarType().getSizeInBits();
12378 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
12379
12380 switch (VT.getSimpleVT().SimpleTy) {
12381 default: return SDValue();
12382 case MVT::v8i32:
12383 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012384 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012385 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012386 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012387 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000012388 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000012389
Craig Toppered2e13d2012-01-22 19:15:14 +000012390 // Extract the LHS vectors
12391 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000012392 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
12393 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000012394
Craig Toppered2e13d2012-01-22 19:15:14 +000012395 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12396 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000012397
Craig Toppered2e13d2012-01-22 19:15:14 +000012398 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000012399 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000012400 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
12401 ExtraNumElems/2);
12402 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000012403
Craig Toppered2e13d2012-01-22 19:15:14 +000012404 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
12405 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000012406
Dmitri Gribenko2de05722012-09-10 21:26:47 +000012407 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012408 }
12409 // fall through
12410 case MVT::v4i32:
12411 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000012412 // (sext (vzext x)) -> (vsext x)
12413 SDValue Op0 = Op.getOperand(0);
12414 SDValue Op00 = Op0.getOperand(0);
12415 SDValue Tmp1;
12416 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
12417 if (Op0.getOpcode() == ISD::BITCAST &&
12418 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
12419 Tmp1 = LowerVectorIntExtend(Op00, DAG);
12420 if (Tmp1.getNode()) {
12421 SDValue Tmp1Op0 = Tmp1.getOperand(0);
12422 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
12423 "This optimization is invalid without a VZEXT.");
12424 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
12425 }
12426
12427 // If the above didn't work, then just use Shift-Left + Shift-Right.
12428 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000012429 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012430 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012431 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012432}
12433
Craig Topper55b24052012-09-11 06:15:32 +000012434static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
12435 SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012436 SDLoc dl(Op);
Eli Friedman14648462011-07-27 22:21:52 +000012437 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
12438 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
12439 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
12440 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
12441
12442 // The only fence that needs an instruction is a sequentially-consistent
12443 // cross-thread fence.
12444 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
12445 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
12446 // no-sse2). There isn't any reason to disable it if the target processor
12447 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012448 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000012449 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
12450
12451 SDValue Chain = Op.getOperand(0);
12452 SDValue Zero = DAG.getConstant(0, MVT::i32);
12453 SDValue Ops[] = {
12454 DAG.getRegister(X86::ESP, MVT::i32), // Base
12455 DAG.getTargetConstant(1, MVT::i8), // Scale
12456 DAG.getRegister(0, MVT::i32), // Index
12457 DAG.getTargetConstant(0, MVT::i32), // Disp
12458 DAG.getRegister(0, MVT::i32), // Segment.
12459 Zero,
12460 Chain
12461 };
Michael Liao2a8bea72013-04-19 22:22:57 +000012462 SDNode *Res = DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops);
Eli Friedman14648462011-07-27 22:21:52 +000012463 return SDValue(Res, 0);
12464 }
12465
12466 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
12467 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
12468}
12469
Craig Topper55b24052012-09-11 06:15:32 +000012470static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
12471 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000012472 EVT T = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012473 SDLoc DL(Op);
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000012474 unsigned Reg = 0;
12475 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000012476 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000012477 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000012478 case MVT::i8: Reg = X86::AL; size = 1; break;
12479 case MVT::i16: Reg = X86::AX; size = 2; break;
12480 case MVT::i32: Reg = X86::EAX; size = 4; break;
12481 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000012482 assert(Subtarget->is64Bit() && "Node not type legal!");
12483 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000012484 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000012485 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012486 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000012487 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000012488 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012489 Op.getOperand(1),
12490 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000012491 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012492 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012493 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012494 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
12495 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
Michael Liao0ee17002013-04-19 04:03:37 +000012496 Ops, array_lengthof(Ops), T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000012497 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012498 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000012499 return cpOut;
12500}
12501
Craig Topper55b24052012-09-11 06:15:32 +000012502static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
12503 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000012504 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012505 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012506 SDValue TheChain = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000012507 SDLoc dl(Op);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012508 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012509 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
12510 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000012511 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000012512 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
12513 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000012514 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000012515 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000012516 rdx.getValue(1)
12517 };
Michael Liao0ee17002013-04-19 04:03:37 +000012518 return DAG.getMergeValues(Ops, array_lengthof(Ops), dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012519}
12520
Craig Topper55b24052012-09-11 06:15:32 +000012521SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012522 EVT SrcVT = Op.getOperand(0).getValueType();
12523 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012524 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012525 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012526 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012527 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012528 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012529 // i64 <=> MMX conversions are Legal.
12530 if (SrcVT==MVT::i64 && DstVT.isVector())
12531 return Op;
12532 if (DstVT==MVT::i64 && SrcVT.isVector())
12533 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012534 // MMX <=> MMX conversions are Legal.
12535 if (SrcVT.isVector() && DstVT.isVector())
12536 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012537 // All other conversions need to be expanded.
12538 return SDValue();
12539}
Chris Lattner5b856542010-12-20 00:59:46 +000012540
Craig Topper55b24052012-09-11 06:15:32 +000012541static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012542 SDNode *Node = Op.getNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012543 SDLoc dl(Node);
Owen Andersone50ed302009-08-10 22:56:29 +000012544 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012545 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012546 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012547 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012548 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012549 Node->getOperand(0),
12550 Node->getOperand(1), negOp,
12551 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012552 cast<AtomicSDNode>(Node)->getAlignment(),
12553 cast<AtomicSDNode>(Node)->getOrdering(),
12554 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012555}
12556
Eli Friedman327236c2011-08-24 20:50:09 +000012557static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12558 SDNode *Node = Op.getNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012559 SDLoc dl(Node);
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012560 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012561
12562 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012563 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12564 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12565 // (The only way to get a 16-byte store is cmpxchg16b)
12566 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12567 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12568 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012569 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12570 cast<AtomicSDNode>(Node)->getMemoryVT(),
12571 Node->getOperand(0),
12572 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012573 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012574 cast<AtomicSDNode>(Node)->getOrdering(),
12575 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012576 return Swap.getValue(1);
12577 }
12578 // Other atomic stores have a simple pattern.
12579 return Op;
12580}
12581
Chris Lattner5b856542010-12-20 00:59:46 +000012582static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12583 EVT VT = Op.getNode()->getValueType(0);
12584
12585 // Let legalize expand this if it isn't a legal type yet.
12586 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12587 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012588
Chris Lattner5b856542010-12-20 00:59:46 +000012589 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012590
Chris Lattner5b856542010-12-20 00:59:46 +000012591 unsigned Opc;
12592 bool ExtraOp = false;
12593 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012594 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012595 case ISD::ADDC: Opc = X86ISD::ADD; break;
12596 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12597 case ISD::SUBC: Opc = X86ISD::SUB; break;
12598 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12599 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012600
Chris Lattner5b856542010-12-20 00:59:46 +000012601 if (!ExtraOp)
Andrew Trickac6d9be2013-05-25 02:42:55 +000012602 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Chris Lattner5b856542010-12-20 00:59:46 +000012603 Op.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +000012604 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Chris Lattner5b856542010-12-20 00:59:46 +000012605 Op.getOperand(1), Op.getOperand(2));
12606}
12607
Evan Cheng8688a582013-01-29 02:32:37 +000012608SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012609 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012610
Evan Cheng8688a582013-01-29 02:32:37 +000012611 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012612 // which returns the values as { float, float } (in XMM0) or
12613 // { double, double } (which is returned in XMM0, XMM1).
Andrew Trickac6d9be2013-05-25 02:42:55 +000012614 SDLoc dl(Op);
Evan Cheng8688a582013-01-29 02:32:37 +000012615 SDValue Arg = Op.getOperand(0);
12616 EVT ArgVT = Arg.getValueType();
12617 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012618
Evan Cheng8688a582013-01-29 02:32:37 +000012619 ArgListTy Args;
12620 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012621
Evan Cheng8688a582013-01-29 02:32:37 +000012622 Entry.Node = Arg;
12623 Entry.Ty = ArgTy;
12624 Entry.isSExt = false;
12625 Entry.isZExt = false;
12626 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012627
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012628 bool isF64 = ArgVT == MVT::f64;
Evan Chenga66f40a2013-01-30 22:56:35 +000012629 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12630 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12631 // the results are returned via SRet in memory.
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012632 const char *LibcallName = isF64 ? "__sincos_stret" : "__sincosf_stret";
Evan Cheng8688a582013-01-29 02:32:37 +000012633 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012634
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012635 Type *RetTy = isF64
12636 ? (Type*)StructType::get(ArgTy, ArgTy, NULL)
12637 : (Type*)VectorType::get(ArgTy, 4);
Evan Cheng8688a582013-01-29 02:32:37 +000012638 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012639 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12640 false, false, false, false, 0,
12641 CallingConv::C, /*isTaillCall=*/false,
12642 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12643 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012644 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012645
12646 if (isF64)
12647 // Returned in xmm0 and xmm1.
12648 return CallResult.first;
12649
12650 // Returned in bits 0:31 and 32:64 xmm0.
12651 SDValue SinVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
12652 CallResult.first, DAG.getIntPtrConstant(0));
12653 SDValue CosVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
12654 CallResult.first, DAG.getIntPtrConstant(1));
12655 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
12656 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal);
Evan Cheng8688a582013-01-29 02:32:37 +000012657}
12658
Evan Cheng0db9fe62006-04-25 20:13:52 +000012659/// LowerOperation - Provide custom lowering hooks for some operations.
12660///
Dan Gohmand858e902010-04-17 15:26:15 +000012661SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012662 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012663 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012664 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012665 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12666 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012667 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012668 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012669 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012670 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012671 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12672 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12673 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012674 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12675 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012676 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12677 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12678 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012679 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012680 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012681 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012682 case ISD::SHL_PARTS:
12683 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012684 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012685 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012686 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012687 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012688 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12689 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12690 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012691 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012692 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012693 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012694 case ISD::FABS: return LowerFABS(Op, DAG);
12695 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012696 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012697 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012698 case ISD::SETCC: return LowerSETCC(Op, DAG);
12699 case ISD::SELECT: return LowerSELECT(Op, DAG);
12700 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012701 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012702 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012703 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012704 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012705 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012706 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012707 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12708 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012709 case ISD::FRAME_TO_ARGS_OFFSET:
12710 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012711 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012712 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012713 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12714 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012715 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12716 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012717 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012718 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012719 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012720 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012721 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012722 case ISD::SRA:
12723 case ISD::SRL:
12724 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012725 case ISD::SADDO:
12726 case ISD::UADDO:
12727 case ISD::SSUBO:
12728 case ISD::USUBO:
12729 case ISD::SMULO:
12730 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012731 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012732 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012733 case ISD::ADDC:
12734 case ISD::ADDE:
12735 case ISD::SUBC:
12736 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012737 case ISD::ADD: return LowerADD(Op, DAG);
12738 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012739 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012740 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012741 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012742}
12743
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012744static void ReplaceATOMIC_LOAD(SDNode *Node,
12745 SmallVectorImpl<SDValue> &Results,
12746 SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012747 SDLoc dl(Node);
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012748 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12749
12750 // Convert wide load -> cmpxchg8b/cmpxchg16b
12751 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12752 // (The only way to get a 16-byte load is cmpxchg16b)
12753 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012754 SDValue Zero = DAG.getConstant(0, VT);
12755 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012756 Node->getOperand(0),
12757 Node->getOperand(1), Zero, Zero,
12758 cast<AtomicSDNode>(Node)->getMemOperand(),
12759 cast<AtomicSDNode>(Node)->getOrdering(),
12760 cast<AtomicSDNode>(Node)->getSynchScope());
12761 Results.push_back(Swap.getValue(0));
12762 Results.push_back(Swap.getValue(1));
12763}
12764
Craig Topperc0878702012-08-17 06:55:11 +000012765static void
Duncan Sands1607f052008-12-01 11:39:25 +000012766ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012767 SelectionDAG &DAG, unsigned NewOp) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012768 SDLoc dl(Node);
Duncan Sands17001ce2011-10-18 12:44:00 +000012769 assert (Node->getValueType(0) == MVT::i64 &&
12770 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012771
12772 SDValue Chain = Node->getOperand(0);
12773 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012774 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012775 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012776 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012777 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012778 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012779 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012780 SDValue Result =
Michael Liao0ee17002013-04-19 04:03:37 +000012781 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, array_lengthof(Ops), MVT::i64,
Dan Gohmanc76909a2009-09-25 20:36:54 +000012782 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012783 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012784 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012785 Results.push_back(Result.getValue(2));
12786}
12787
Duncan Sands126d9072008-07-04 11:47:58 +000012788/// ReplaceNodeResults - Replace a node with an illegal result type
12789/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012790void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12791 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012792 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012793 SDLoc dl(N);
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012794 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012795 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012796 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012797 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012798 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012799 case ISD::ADDC:
12800 case ISD::ADDE:
12801 case ISD::SUBC:
12802 case ISD::SUBE:
12803 // We don't want to expand or promote these.
12804 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012805 case ISD::FP_TO_SINT:
12806 case ISD::FP_TO_UINT: {
12807 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12808
12809 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12810 return;
12811
Eli Friedman948e95a2009-05-23 09:59:16 +000012812 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012813 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012814 SDValue FIST = Vals.first, StackSlot = Vals.second;
12815 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012816 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012817 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012818 if (StackSlot.getNode() != 0)
12819 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12820 MachinePointerInfo(),
12821 false, false, false, 0));
12822 else
12823 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012824 }
12825 return;
12826 }
Michael Liao991b6a22012-10-24 04:09:32 +000012827 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012828 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12829 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012830 N->getValueType(0) != MVT::v2f32)
12831 return;
12832 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12833 N->getOperand(0));
12834 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12835 MVT::f64);
12836 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12837 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12838 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12839 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12840 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12841 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12842 return;
12843 }
Michael Liao44c2d612012-10-10 16:53:28 +000012844 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012845 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12846 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012847 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12848 Results.push_back(V);
12849 return;
12850 }
Duncan Sands1607f052008-12-01 11:39:25 +000012851 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012852 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012853 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012854 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012855 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012856 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012857 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012858 eax.getValue(2));
12859 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12860 SDValue Ops[] = { eax, edx };
Michael Liao0ee17002013-04-19 04:03:37 +000012861 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops,
12862 array_lengthof(Ops)));
Duncan Sands1607f052008-12-01 11:39:25 +000012863 Results.push_back(edx.getValue(1));
12864 return;
12865 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012866 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012867 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012868 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012869 bool Regs64bit = T == MVT::i128;
12870 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012871 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012872 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12873 DAG.getConstant(0, HalfT));
12874 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12875 DAG.getConstant(1, HalfT));
12876 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12877 Regs64bit ? X86::RAX : X86::EAX,
12878 cpInL, SDValue());
12879 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12880 Regs64bit ? X86::RDX : X86::EDX,
12881 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012882 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012883 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12884 DAG.getConstant(0, HalfT));
12885 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12886 DAG.getConstant(1, HalfT));
12887 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12888 Regs64bit ? X86::RBX : X86::EBX,
12889 swapInL, cpInH.getValue(1));
12890 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012891 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012892 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012893 SDValue Ops[] = { swapInH.getValue(0),
12894 N->getOperand(1),
12895 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012896 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012897 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012898 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12899 X86ISD::LCMPXCHG8_DAG;
12900 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Michael Liao0ee17002013-04-19 04:03:37 +000012901 Ops, array_lengthof(Ops), T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012902 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12903 Regs64bit ? X86::RAX : X86::EAX,
12904 HalfT, Result.getValue(1));
12905 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12906 Regs64bit ? X86::RDX : X86::EDX,
12907 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012908 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012909 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012910 Results.push_back(cpOutH.getValue(1));
12911 return;
12912 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012913 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012914 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012915 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012916 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012917 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012918 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012919 case ISD::ATOMIC_LOAD_MAX:
12920 case ISD::ATOMIC_LOAD_MIN:
12921 case ISD::ATOMIC_LOAD_UMAX:
12922 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012923 case ISD::ATOMIC_SWAP: {
12924 unsigned Opc;
12925 switch (N->getOpcode()) {
12926 default: llvm_unreachable("Unexpected opcode");
12927 case ISD::ATOMIC_LOAD_ADD:
12928 Opc = X86ISD::ATOMADD64_DAG;
12929 break;
12930 case ISD::ATOMIC_LOAD_AND:
12931 Opc = X86ISD::ATOMAND64_DAG;
12932 break;
12933 case ISD::ATOMIC_LOAD_NAND:
12934 Opc = X86ISD::ATOMNAND64_DAG;
12935 break;
12936 case ISD::ATOMIC_LOAD_OR:
12937 Opc = X86ISD::ATOMOR64_DAG;
12938 break;
12939 case ISD::ATOMIC_LOAD_SUB:
12940 Opc = X86ISD::ATOMSUB64_DAG;
12941 break;
12942 case ISD::ATOMIC_LOAD_XOR:
12943 Opc = X86ISD::ATOMXOR64_DAG;
12944 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012945 case ISD::ATOMIC_LOAD_MAX:
12946 Opc = X86ISD::ATOMMAX64_DAG;
12947 break;
12948 case ISD::ATOMIC_LOAD_MIN:
12949 Opc = X86ISD::ATOMMIN64_DAG;
12950 break;
12951 case ISD::ATOMIC_LOAD_UMAX:
12952 Opc = X86ISD::ATOMUMAX64_DAG;
12953 break;
12954 case ISD::ATOMIC_LOAD_UMIN:
12955 Opc = X86ISD::ATOMUMIN64_DAG;
12956 break;
Craig Topperc0878702012-08-17 06:55:11 +000012957 case ISD::ATOMIC_SWAP:
12958 Opc = X86ISD::ATOMSWAP64_DAG;
12959 break;
12960 }
12961 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012962 return;
Craig Topperc0878702012-08-17 06:55:11 +000012963 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012964 case ISD::ATOMIC_LOAD:
12965 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012966 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012967}
12968
Evan Cheng72261582005-12-20 06:22:03 +000012969const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12970 switch (Opcode) {
12971 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012972 case X86ISD::BSF: return "X86ISD::BSF";
12973 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012974 case X86ISD::SHLD: return "X86ISD::SHLD";
12975 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012976 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012977 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012978 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012979 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012980 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012981 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012982 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12983 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12984 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012985 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012986 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012987 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012988 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012989 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012990 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012991 case X86ISD::COMI: return "X86ISD::COMI";
12992 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012993 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012994 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012995 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12996 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012997 case X86ISD::CMOV: return "X86ISD::CMOV";
12998 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012999 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000013000 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
13001 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000013002 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000013003 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000013004 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000013005 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000013006 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000013007 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
13008 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000013009 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000013010 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000013011 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000013012 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000013013 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000013014 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000013015 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000013016 case X86ISD::HADD: return "X86ISD::HADD";
13017 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000013018 case X86ISD::FHADD: return "X86ISD::FHADD";
13019 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000013020 case X86ISD::UMAX: return "X86ISD::UMAX";
13021 case X86ISD::UMIN: return "X86ISD::UMIN";
13022 case X86ISD::SMAX: return "X86ISD::SMAX";
13023 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000013024 case X86ISD::FMAX: return "X86ISD::FMAX";
13025 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000013026 case X86ISD::FMAXC: return "X86ISD::FMAXC";
13027 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000013028 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
13029 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000013030 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000013031 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000013032 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000013033 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
13034 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000013035 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000013036 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000013037 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000013038 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000013039 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
13040 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013041 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
13042 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
13043 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
13044 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
13045 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
13046 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000013047 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000013048 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000013049 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000013050 case X86ISD::VZEXT: return "X86ISD::VZEXT";
13051 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000013052 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000013053 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000013054 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
13055 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000013056 case X86ISD::VSHL: return "X86ISD::VSHL";
13057 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000013058 case X86ISD::VSRA: return "X86ISD::VSRA";
13059 case X86ISD::VSHLI: return "X86ISD::VSHLI";
13060 case X86ISD::VSRLI: return "X86ISD::VSRLI";
13061 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000013062 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000013063 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
13064 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000013065 case X86ISD::ADD: return "X86ISD::ADD";
13066 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000013067 case X86ISD::ADC: return "X86ISD::ADC";
13068 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000013069 case X86ISD::SMUL: return "X86ISD::SMUL";
13070 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000013071 case X86ISD::INC: return "X86ISD::INC";
13072 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000013073 case X86ISD::OR: return "X86ISD::OR";
13074 case X86ISD::XOR: return "X86ISD::XOR";
13075 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000013076 case X86ISD::BLSI: return "X86ISD::BLSI";
13077 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
13078 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000013079 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000013080 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000013081 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000013082 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000013083 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
13084 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000013085 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000013086 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000013087 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000013088 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000013089 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000013090 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
13091 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000013092 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
13093 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
13094 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000013095 case X86ISD::MOVSD: return "X86ISD::MOVSD";
13096 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000013097 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
13098 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000013099 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000013100 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000013101 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000013102 case X86ISD::VPERMV: return "X86ISD::VPERMV";
13103 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000013104 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000013105 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000013106 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013107 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000013108 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000013109 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000013110 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000013111 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000013112 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Michael Liaoc26392a2013-03-28 23:41:26 +000013113 case X86ISD::RDSEED: return "X86ISD::RDSEED";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000013114 case X86ISD::FMADD: return "X86ISD::FMADD";
13115 case X86ISD::FMSUB: return "X86ISD::FMSUB";
13116 case X86ISD::FNMADD: return "X86ISD::FNMADD";
13117 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
13118 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
13119 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000013120 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
13121 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Michael Liaof8fd8832013-03-26 22:47:01 +000013122 case X86ISD::XTEST: return "X86ISD::XTEST";
Evan Cheng72261582005-12-20 06:22:03 +000013123 }
13124}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013125
Chris Lattnerc9addb72007-03-30 23:15:24 +000013126// isLegalAddressingMode - Return true if the addressing mode represented
13127// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000013128bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013129 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000013130 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000013131 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000013132 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000013133
Chris Lattnerc9addb72007-03-30 23:15:24 +000013134 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000013135 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000013136 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000013137
Chris Lattnerc9addb72007-03-30 23:15:24 +000013138 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000013139 unsigned GVFlags =
13140 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000013141
Chris Lattnerdfed4132009-07-10 07:38:24 +000013142 // If a reference to this global requires an extra load, we can't fold it.
13143 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000013144 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000013145
Chris Lattnerdfed4132009-07-10 07:38:24 +000013146 // If BaseGV requires a register for the PIC base, we cannot also have a
13147 // BaseReg specified.
13148 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000013149 return false;
Evan Cheng52787842007-08-01 23:46:47 +000013150
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000013151 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000013152 if ((M != CodeModel::Small || R != Reloc::Static) &&
13153 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000013154 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000013155 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013156
Chris Lattnerc9addb72007-03-30 23:15:24 +000013157 switch (AM.Scale) {
13158 case 0:
13159 case 1:
13160 case 2:
13161 case 4:
13162 case 8:
13163 // These scales always work.
13164 break;
13165 case 3:
13166 case 5:
13167 case 9:
13168 // These scales are formed with basereg+scalereg. Only accept if there is
13169 // no basereg yet.
13170 if (AM.HasBaseReg)
13171 return false;
13172 break;
13173 default: // Other stuff never works.
13174 return false;
13175 }
Scott Michelfdc40a02009-02-17 22:15:04 +000013176
Chris Lattnerc9addb72007-03-30 23:15:24 +000013177 return true;
13178}
13179
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013180bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000013181 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000013182 return false;
Evan Chenge127a732007-10-29 07:57:50 +000013183 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
13184 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000013185 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000013186}
13187
Evan Cheng70e10d32012-07-17 06:53:39 +000013188bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000013189 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000013190}
13191
13192bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000013193 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000013194 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000013195}
13196
Owen Andersone50ed302009-08-10 22:56:29 +000013197bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000013198 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000013199 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000013200 unsigned NumBits1 = VT1.getSizeInBits();
13201 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000013202 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000013203}
Evan Cheng2bd122c2007-10-26 01:56:11 +000013204
Chris Lattnerdb125cf2011-07-18 04:54:35 +000013205bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000013206 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000013207 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000013208}
13209
Owen Andersone50ed302009-08-10 22:56:29 +000013210bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000013211 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000013212 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000013213}
13214
Evan Cheng2766a472012-12-06 19:13:27 +000013215bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
13216 EVT VT1 = Val.getValueType();
13217 if (isZExtFree(VT1, VT2))
13218 return true;
13219
13220 if (Val.getOpcode() != ISD::LOAD)
13221 return false;
13222
13223 if (!VT1.isSimple() || !VT1.isInteger() ||
13224 !VT2.isSimple() || !VT2.isInteger())
13225 return false;
13226
13227 switch (VT1.getSimpleVT().SimpleTy) {
13228 default: break;
13229 case MVT::i8:
13230 case MVT::i16:
13231 case MVT::i32:
13232 // X86 has 8, 16, and 32-bit zero-extending loads.
13233 return true;
13234 }
13235
13236 return false;
13237}
13238
Stephen Line54885a2013-07-09 18:16:56 +000013239bool
13240X86TargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
13241 if (!(Subtarget->hasFMA() || Subtarget->hasFMA4()))
13242 return false;
13243
13244 VT = VT.getScalarType();
13245
13246 if (!VT.isSimple())
13247 return false;
13248
13249 switch (VT.getSimpleVT().SimpleTy) {
13250 case MVT::f32:
13251 case MVT::f64:
13252 return true;
13253 default:
13254 break;
13255 }
13256
13257 return false;
13258}
13259
Owen Andersone50ed302009-08-10 22:56:29 +000013260bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000013261 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000013262 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000013263}
13264
Evan Cheng60c07e12006-07-05 22:17:51 +000013265/// isShuffleMaskLegal - Targets can use this to indicate that they only
13266/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
13267/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
13268/// are assumed to be legal.
13269bool
Eric Christopherfd179292009-08-27 18:07:15 +000013270X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000013271 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000013272 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000013273 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000013274 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000013275
Nate Begemana09008b2009-10-19 02:17:23 +000013276 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000013277 return (VT.getVectorNumElements() == 2 ||
13278 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
13279 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013280 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000013281 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013282 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
13283 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000013284 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013285 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
13286 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
13287 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
13288 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000013289}
13290
Dan Gohman7d8143f2008-04-09 20:09:42 +000013291bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000013292X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000013293 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000013294 unsigned NumElts = VT.getVectorNumElements();
13295 // FIXME: This collection of masks seems suspect.
13296 if (NumElts == 2)
13297 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000013298 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000013299 return (isMOVLMask(Mask, VT) ||
13300 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013301 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
13302 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000013303 }
13304 return false;
13305}
13306
13307//===----------------------------------------------------------------------===//
13308// X86 Scheduler Hooks
13309//===----------------------------------------------------------------------===//
13310
Michael Liaobe02a902012-11-08 07:28:54 +000013311/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000013312static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
13313 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000013314 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000013315
13316 const BasicBlock *BB = MBB->getBasicBlock();
13317 MachineFunction::iterator I = MBB;
13318 ++I;
13319
13320 // For the v = xbegin(), we generate
13321 //
13322 // thisMBB:
13323 // xbegin sinkMBB
13324 //
13325 // mainMBB:
13326 // eax = -1
13327 //
13328 // sinkMBB:
13329 // v = eax
13330
13331 MachineBasicBlock *thisMBB = MBB;
13332 MachineFunction *MF = MBB->getParent();
13333 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13334 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13335 MF->insert(I, mainMBB);
13336 MF->insert(I, sinkMBB);
13337
13338 // Transfer the remainder of BB and its successor edges to sinkMBB.
13339 sinkMBB->splice(sinkMBB->begin(), MBB,
13340 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13341 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13342
13343 // thisMBB:
13344 // xbegin sinkMBB
13345 // # fallthrough to mainMBB
13346 // # abortion to sinkMBB
13347 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
13348 thisMBB->addSuccessor(mainMBB);
13349 thisMBB->addSuccessor(sinkMBB);
13350
13351 // mainMBB:
13352 // EAX = -1
13353 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
13354 mainMBB->addSuccessor(sinkMBB);
13355
13356 // sinkMBB:
13357 // EAX is live into the sinkMBB
13358 sinkMBB->addLiveIn(X86::EAX);
13359 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13360 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13361 .addReg(X86::EAX);
13362
13363 MI->eraseFromParent();
13364 return sinkMBB;
13365}
13366
Michael Liaob118a072012-09-20 03:06:15 +000013367// Get CMPXCHG opcode for the specified data type.
13368static unsigned getCmpXChgOpcode(EVT VT) {
13369 switch (VT.getSimpleVT().SimpleTy) {
13370 case MVT::i8: return X86::LCMPXCHG8;
13371 case MVT::i16: return X86::LCMPXCHG16;
13372 case MVT::i32: return X86::LCMPXCHG32;
13373 case MVT::i64: return X86::LCMPXCHG64;
13374 default:
13375 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000013376 }
Michael Liaob118a072012-09-20 03:06:15 +000013377 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000013378}
13379
Michael Liaob118a072012-09-20 03:06:15 +000013380// Get LOAD opcode for the specified data type.
13381static unsigned getLoadOpcode(EVT VT) {
13382 switch (VT.getSimpleVT().SimpleTy) {
13383 case MVT::i8: return X86::MOV8rm;
13384 case MVT::i16: return X86::MOV16rm;
13385 case MVT::i32: return X86::MOV32rm;
13386 case MVT::i64: return X86::MOV64rm;
13387 default:
13388 break;
13389 }
13390 llvm_unreachable("Invalid operand size!");
13391}
13392
13393// Get opcode of the non-atomic one from the specified atomic instruction.
13394static unsigned getNonAtomicOpcode(unsigned Opc) {
13395 switch (Opc) {
13396 case X86::ATOMAND8: return X86::AND8rr;
13397 case X86::ATOMAND16: return X86::AND16rr;
13398 case X86::ATOMAND32: return X86::AND32rr;
13399 case X86::ATOMAND64: return X86::AND64rr;
13400 case X86::ATOMOR8: return X86::OR8rr;
13401 case X86::ATOMOR16: return X86::OR16rr;
13402 case X86::ATOMOR32: return X86::OR32rr;
13403 case X86::ATOMOR64: return X86::OR64rr;
13404 case X86::ATOMXOR8: return X86::XOR8rr;
13405 case X86::ATOMXOR16: return X86::XOR16rr;
13406 case X86::ATOMXOR32: return X86::XOR32rr;
13407 case X86::ATOMXOR64: return X86::XOR64rr;
13408 }
13409 llvm_unreachable("Unhandled atomic-load-op opcode!");
13410}
13411
13412// Get opcode of the non-atomic one from the specified atomic instruction with
13413// extra opcode.
13414static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
13415 unsigned &ExtraOpc) {
13416 switch (Opc) {
13417 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
13418 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
13419 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
13420 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013421 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013422 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
13423 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
13424 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013425 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013426 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
13427 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
13428 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013429 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013430 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
13431 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
13432 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013433 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013434 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
13435 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
13436 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
13437 }
13438 llvm_unreachable("Unhandled atomic-load-op opcode!");
13439}
13440
13441// Get opcode of the non-atomic one from the specified atomic instruction for
13442// 64-bit data type on 32-bit target.
13443static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
13444 switch (Opc) {
13445 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
13446 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
13447 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
13448 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
13449 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
13450 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013451 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
13452 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
13453 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
13454 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000013455 }
13456 llvm_unreachable("Unhandled atomic-load-op opcode!");
13457}
13458
13459// Get opcode of the non-atomic one from the specified atomic instruction for
13460// 64-bit data type on 32-bit target with extra opcode.
13461static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
13462 unsigned &HiOpc,
13463 unsigned &ExtraOpc) {
13464 switch (Opc) {
13465 case X86::ATOMNAND6432:
13466 ExtraOpc = X86::NOT32r;
13467 HiOpc = X86::AND32rr;
13468 return X86::AND32rr;
13469 }
13470 llvm_unreachable("Unhandled atomic-load-op opcode!");
13471}
13472
13473// Get pseudo CMOV opcode from the specified data type.
13474static unsigned getPseudoCMOVOpc(EVT VT) {
13475 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000013476 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000013477 case MVT::i16: return X86::CMOV_GR16;
13478 case MVT::i32: return X86::CMOV_GR32;
13479 default:
13480 break;
13481 }
13482 llvm_unreachable("Unknown CMOV opcode!");
13483}
13484
13485// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
13486// They will be translated into a spin-loop or compare-exchange loop from
13487//
13488// ...
13489// dst = atomic-fetch-op MI.addr, MI.val
13490// ...
13491//
13492// to
13493//
13494// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013495// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000013496// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013497// t4 = phi(t1, t3 / loop)
13498// t2 = OP MI.val, t4
13499// EAX = t4
13500// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
13501// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013502// JNE loop
13503// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013504// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013505// ...
Mon P Wang63307c32008-05-05 19:05:59 +000013506MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000013507X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
13508 MachineBasicBlock *MBB) const {
13509 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13510 DebugLoc DL = MI->getDebugLoc();
13511
13512 MachineFunction *MF = MBB->getParent();
13513 MachineRegisterInfo &MRI = MF->getRegInfo();
13514
13515 const BasicBlock *BB = MBB->getBasicBlock();
13516 MachineFunction::iterator I = MBB;
13517 ++I;
13518
Michael Liao13d08bf2013-01-22 21:47:38 +000013519 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000013520 "Unexpected number of operands");
13521
13522 assert(MI->hasOneMemOperand() &&
13523 "Expected atomic-load-op to have one memoperand");
13524
13525 // Memory Reference
13526 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13527 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13528
13529 unsigned DstReg, SrcReg;
13530 unsigned MemOpndSlot;
13531
13532 unsigned CurOp = 0;
13533
13534 DstReg = MI->getOperand(CurOp++).getReg();
13535 MemOpndSlot = CurOp;
13536 CurOp += X86::AddrNumOperands;
13537 SrcReg = MI->getOperand(CurOp++).getReg();
13538
13539 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000013540 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000013541 unsigned t1 = MRI.createVirtualRegister(RC);
13542 unsigned t2 = MRI.createVirtualRegister(RC);
13543 unsigned t3 = MRI.createVirtualRegister(RC);
13544 unsigned t4 = MRI.createVirtualRegister(RC);
13545 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000013546
13547 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
13548 unsigned LOADOpc = getLoadOpcode(VT);
13549
13550 // For the atomic load-arith operator, we generate
13551 //
13552 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013553 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000013554 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013555 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013556 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013557 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013558 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013559 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013560 // JNE mainMBB
13561 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013562 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013563
13564 MachineBasicBlock *thisMBB = MBB;
13565 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13566 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13567 MF->insert(I, mainMBB);
13568 MF->insert(I, sinkMBB);
13569
13570 MachineInstrBuilder MIB;
13571
13572 // Transfer the remainder of BB and its successor edges to sinkMBB.
13573 sinkMBB->splice(sinkMBB->begin(), MBB,
13574 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13575 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13576
13577 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013578 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13579 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13580 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13581 if (NewMO.isReg())
13582 NewMO.setIsKill(false);
13583 MIB.addOperand(NewMO);
13584 }
13585 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13586 unsigned flags = (*MMOI)->getFlags();
13587 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13588 MachineMemOperand *MMO =
13589 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13590 (*MMOI)->getSize(),
13591 (*MMOI)->getBaseAlignment(),
13592 (*MMOI)->getTBAAInfo(),
13593 (*MMOI)->getRanges());
13594 MIB.addMemOperand(MMO);
13595 }
Michael Liaob118a072012-09-20 03:06:15 +000013596
13597 thisMBB->addSuccessor(mainMBB);
13598
13599 // mainMBB:
13600 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013601
Michael Liaoc537f792013-03-06 00:17:04 +000013602 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013603 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13604 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013605
Michael Liaob118a072012-09-20 03:06:15 +000013606 unsigned Opc = MI->getOpcode();
13607 switch (Opc) {
13608 default:
13609 llvm_unreachable("Unhandled atomic-load-op opcode!");
13610 case X86::ATOMAND8:
13611 case X86::ATOMAND16:
13612 case X86::ATOMAND32:
13613 case X86::ATOMAND64:
13614 case X86::ATOMOR8:
13615 case X86::ATOMOR16:
13616 case X86::ATOMOR32:
13617 case X86::ATOMOR64:
13618 case X86::ATOMXOR8:
13619 case X86::ATOMXOR16:
13620 case X86::ATOMXOR32:
13621 case X86::ATOMXOR64: {
13622 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013623 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13624 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013625 break;
13626 }
13627 case X86::ATOMNAND8:
13628 case X86::ATOMNAND16:
13629 case X86::ATOMNAND32:
13630 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013631 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013632 unsigned NOTOpc;
13633 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013634 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13635 .addReg(t4);
13636 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013637 break;
13638 }
Michael Liao08382492012-09-21 03:00:17 +000013639 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013640 case X86::ATOMMAX16:
13641 case X86::ATOMMAX32:
13642 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013643 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013644 case X86::ATOMMIN16:
13645 case X86::ATOMMIN32:
13646 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013647 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013648 case X86::ATOMUMAX16:
13649 case X86::ATOMUMAX32:
13650 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013651 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013652 case X86::ATOMUMIN16:
13653 case X86::ATOMUMIN32:
13654 case X86::ATOMUMIN64: {
13655 unsigned CMPOpc;
13656 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13657
13658 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13659 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013660 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013661
13662 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013663 if (VT != MVT::i8) {
13664 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013665 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013666 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013667 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013668 } else {
13669 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013670 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13671 const TargetRegisterClass *RC32 =
13672 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013673 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13674 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013675 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013676
13677 unsigned Undef = MRI.createVirtualRegister(RC32);
13678 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13679
13680 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13681 .addReg(Undef)
13682 .addReg(SrcReg)
13683 .addImm(X86::sub_8bit);
13684 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13685 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013686 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013687 .addImm(X86::sub_8bit);
13688
Michael Liaoc537f792013-03-06 00:17:04 +000013689 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013690 .addReg(SrcReg32)
13691 .addReg(AccReg32);
13692
Michael Liaoc537f792013-03-06 00:17:04 +000013693 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13694 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013695 }
Michael Liaob118a072012-09-20 03:06:15 +000013696 } else {
13697 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013698 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013699 "Invalid atomic-load-op transformation!");
13700 unsigned SelOpc = getPseudoCMOVOpc(VT);
13701 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13702 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013703 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13704 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013705 .addImm(CC);
13706 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013707 // Replace the original PHI node as mainMBB is changed after CMOV
13708 // lowering.
13709 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13710 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13711 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013712 }
13713 break;
13714 }
13715 }
13716
Michael Liaoc537f792013-03-06 00:17:04 +000013717 // Copy PhyReg back from virtual register.
13718 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13719 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013720
13721 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013722 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13723 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13724 if (NewMO.isReg())
13725 NewMO.setIsKill(false);
13726 MIB.addOperand(NewMO);
13727 }
13728 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013729 MIB.setMemRefs(MMOBegin, MMOEnd);
13730
Michael Liaoc537f792013-03-06 00:17:04 +000013731 // Copy PhyReg back to virtual register.
13732 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13733 .addReg(PhyReg);
13734
Michael Liaob118a072012-09-20 03:06:15 +000013735 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13736
13737 mainMBB->addSuccessor(origMainMBB);
13738 mainMBB->addSuccessor(sinkMBB);
13739
13740 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013741 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13742 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013743 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013744
13745 MI->eraseFromParent();
13746 return sinkMBB;
13747}
13748
13749// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13750// instructions. They will be translated into a spin-loop or compare-exchange
13751// loop from
13752//
13753// ...
13754// dst = atomic-fetch-op MI.addr, MI.val
13755// ...
13756//
13757// to
13758//
13759// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013760// t1L = LOAD [MI.addr + 0]
13761// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013762// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013763// t4L = phi(t1L, t3L / loop)
13764// t4H = phi(t1H, t3H / loop)
13765// t2L = OP MI.val.lo, t4L
13766// t2H = OP MI.val.hi, t4H
13767// EAX = t4L
13768// EDX = t4H
13769// EBX = t2L
13770// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013771// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013772// t3L = EAX
13773// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013774// JNE loop
13775// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013776// dstL = t3L
13777// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013778// ...
13779MachineBasicBlock *
13780X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13781 MachineBasicBlock *MBB) const {
13782 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13783 DebugLoc DL = MI->getDebugLoc();
13784
13785 MachineFunction *MF = MBB->getParent();
13786 MachineRegisterInfo &MRI = MF->getRegInfo();
13787
13788 const BasicBlock *BB = MBB->getBasicBlock();
13789 MachineFunction::iterator I = MBB;
13790 ++I;
13791
Michael Liao13d08bf2013-01-22 21:47:38 +000013792 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013793 "Unexpected number of operands");
13794
13795 assert(MI->hasOneMemOperand() &&
13796 "Expected atomic-load-op32 to have one memoperand");
13797
13798 // Memory Reference
13799 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13800 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13801
13802 unsigned DstLoReg, DstHiReg;
13803 unsigned SrcLoReg, SrcHiReg;
13804 unsigned MemOpndSlot;
13805
13806 unsigned CurOp = 0;
13807
13808 DstLoReg = MI->getOperand(CurOp++).getReg();
13809 DstHiReg = MI->getOperand(CurOp++).getReg();
13810 MemOpndSlot = CurOp;
13811 CurOp += X86::AddrNumOperands;
13812 SrcLoReg = MI->getOperand(CurOp++).getReg();
13813 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013814
Craig Topperc9099502012-04-20 06:31:50 +000013815 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013816 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013817
Michael Liaoc537f792013-03-06 00:17:04 +000013818 unsigned t1L = MRI.createVirtualRegister(RC);
13819 unsigned t1H = MRI.createVirtualRegister(RC);
13820 unsigned t2L = MRI.createVirtualRegister(RC);
13821 unsigned t2H = MRI.createVirtualRegister(RC);
13822 unsigned t3L = MRI.createVirtualRegister(RC);
13823 unsigned t3H = MRI.createVirtualRegister(RC);
13824 unsigned t4L = MRI.createVirtualRegister(RC);
13825 unsigned t4H = MRI.createVirtualRegister(RC);
13826
Michael Liaob118a072012-09-20 03:06:15 +000013827 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13828 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013829
Michael Liaob118a072012-09-20 03:06:15 +000013830 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013831 //
Michael Liaob118a072012-09-20 03:06:15 +000013832 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013833 // t1L = LOAD [MI.addr + 0]
13834 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013835 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013836 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13837 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13838 // t2L = OP MI.val.lo, t4L
13839 // t2H = OP MI.val.hi, t4H
13840 // EBX = t2L
13841 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013842 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013843 // t3L = EAX
13844 // t3H = EDX
13845 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013846 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013847 // dstL = t3L
13848 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013849
Mon P Wang63307c32008-05-05 19:05:59 +000013850 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013851 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13852 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13853 MF->insert(I, mainMBB);
13854 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013855
Michael Liaob118a072012-09-20 03:06:15 +000013856 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013857
Michael Liaob118a072012-09-20 03:06:15 +000013858 // Transfer the remainder of BB and its successor edges to sinkMBB.
13859 sinkMBB->splice(sinkMBB->begin(), MBB,
13860 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13861 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013862
Michael Liaob118a072012-09-20 03:06:15 +000013863 // thisMBB:
13864 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013865 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013866 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013867 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13868 if (NewMO.isReg())
13869 NewMO.setIsKill(false);
13870 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013871 }
Michael Liaoc537f792013-03-06 00:17:04 +000013872 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13873 unsigned flags = (*MMOI)->getFlags();
13874 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13875 MachineMemOperand *MMO =
13876 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13877 (*MMOI)->getSize(),
13878 (*MMOI)->getBaseAlignment(),
13879 (*MMOI)->getTBAAInfo(),
13880 (*MMOI)->getRanges());
13881 MIB.addMemOperand(MMO);
13882 };
13883 MachineInstr *LowMI = MIB;
13884
13885 // Hi
13886 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13887 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13888 if (i == X86::AddrDisp) {
13889 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13890 } else {
13891 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13892 if (NewMO.isReg())
13893 NewMO.setIsKill(false);
13894 MIB.addOperand(NewMO);
13895 }
13896 }
13897 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013898
Michael Liaob118a072012-09-20 03:06:15 +000013899 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013900
Michael Liaob118a072012-09-20 03:06:15 +000013901 // mainMBB:
13902 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013903
Michael Liaoc537f792013-03-06 00:17:04 +000013904 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013905 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13906 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13907 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13908 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013909
Michael Liaob118a072012-09-20 03:06:15 +000013910 unsigned Opc = MI->getOpcode();
13911 switch (Opc) {
13912 default:
13913 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13914 case X86::ATOMAND6432:
13915 case X86::ATOMOR6432:
13916 case X86::ATOMXOR6432:
13917 case X86::ATOMADD6432:
13918 case X86::ATOMSUB6432: {
13919 unsigned HiOpc;
13920 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013921 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13922 .addReg(SrcLoReg);
13923 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13924 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013925 break;
13926 }
13927 case X86::ATOMNAND6432: {
13928 unsigned HiOpc, NOTOpc;
13929 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013930 unsigned TmpL = MRI.createVirtualRegister(RC);
13931 unsigned TmpH = MRI.createVirtualRegister(RC);
13932 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13933 .addReg(t4L);
13934 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13935 .addReg(t4H);
13936 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13937 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013938 break;
13939 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013940 case X86::ATOMMAX6432:
13941 case X86::ATOMMIN6432:
13942 case X86::ATOMUMAX6432:
13943 case X86::ATOMUMIN6432: {
13944 unsigned HiOpc;
13945 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13946 unsigned cL = MRI.createVirtualRegister(RC8);
13947 unsigned cH = MRI.createVirtualRegister(RC8);
13948 unsigned cL32 = MRI.createVirtualRegister(RC);
13949 unsigned cH32 = MRI.createVirtualRegister(RC);
13950 unsigned cc = MRI.createVirtualRegister(RC);
13951 // cl := cmp src_lo, lo
13952 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013953 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013954 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13955 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13956 // ch := cmp src_hi, hi
13957 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013958 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013959 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13960 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13961 // cc := if (src_hi == hi) ? cl : ch;
13962 if (Subtarget->hasCMov()) {
13963 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13964 .addReg(cH32).addReg(cL32);
13965 } else {
13966 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13967 .addReg(cH32).addReg(cL32)
13968 .addImm(X86::COND_E);
13969 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13970 }
13971 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13972 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013973 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13974 .addReg(SrcLoReg).addReg(t4L);
13975 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13976 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013977 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013978 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13979 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013980 .addImm(X86::COND_NE);
13981 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013982 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13983 // 2nd CMOV lowering.
13984 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013985 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13986 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013987 .addImm(X86::COND_NE);
13988 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013989 // Replace the original PHI node as mainMBB is changed after CMOV
13990 // lowering.
13991 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13992 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13993 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13994 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13995 PhiL->eraseFromParent();
13996 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013997 }
13998 break;
13999 }
Michael Liaob118a072012-09-20 03:06:15 +000014000 case X86::ATOMSWAP6432: {
14001 unsigned HiOpc;
14002 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000014003 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
14004 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000014005 break;
14006 }
14007 }
Mon P Wang63307c32008-05-05 19:05:59 +000014008
Michael Liaob118a072012-09-20 03:06:15 +000014009 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000014010 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
14011 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000014012 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000014013 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
14014 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000014015
Michael Liaob118a072012-09-20 03:06:15 +000014016 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000014017 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14018 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
14019 if (NewMO.isReg())
14020 NewMO.setIsKill(false);
14021 MIB.addOperand(NewMO);
14022 }
Michael Liaob118a072012-09-20 03:06:15 +000014023 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000014024
Michael Liaoc537f792013-03-06 00:17:04 +000014025 // Copy EDX:EAX back to t3H:t3L
14026 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
14027 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
14028
Michael Liaob118a072012-09-20 03:06:15 +000014029 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000014030
Michael Liaob118a072012-09-20 03:06:15 +000014031 mainMBB->addSuccessor(origMainMBB);
14032 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000014033
Michael Liaob118a072012-09-20 03:06:15 +000014034 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000014035 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14036 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000014037 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000014038 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14039 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000014040 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000014041
Michael Liaob118a072012-09-20 03:06:15 +000014042 MI->eraseFromParent();
14043 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000014044}
14045
Eric Christopherf83a5de2009-08-27 18:08:16 +000014046// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014047// or XMM0_V32I8 in AVX all of this code can be replaced with that
14048// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000014049static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
14050 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000014051 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000014052 switch (MI->getOpcode()) {
14053 default: llvm_unreachable("illegal opcode!");
14054 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
14055 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
14056 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
14057 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
14058 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
14059 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
14060 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
14061 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014062 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014063
Craig Topper8aae8dd2012-11-10 08:57:41 +000014064 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000014065 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000014066
Craig Topper52ea2452012-11-10 09:25:36 +000014067 unsigned NumArgs = MI->getNumOperands();
14068 for (unsigned i = 1; i < NumArgs; ++i) {
14069 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000014070 if (!(Op.isReg() && Op.isImplicit()))
14071 MIB.addOperand(Op);
14072 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000014073 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000014074 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
14075
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014076 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000014077 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000014078 .addReg(X86::XMM0);
14079
Dan Gohman14152b42010-07-06 20:24:04 +000014080 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000014081 return BB;
14082}
14083
Craig Topper9c7ae012012-11-10 01:23:36 +000014084// FIXME: Custom handling because TableGen doesn't support multiple implicit
14085// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000014086static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
14087 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000014088 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000014089 switch (MI->getOpcode()) {
14090 default: llvm_unreachable("illegal opcode!");
14091 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
14092 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
14093 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
14094 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
14095 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
14096 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
14097 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
14098 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000014099 }
14100
Craig Topper8aae8dd2012-11-10 08:57:41 +000014101 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000014102 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000014103
Craig Topper52ea2452012-11-10 09:25:36 +000014104 unsigned NumArgs = MI->getNumOperands(); // remove the results
14105 for (unsigned i = 1; i < NumArgs; ++i) {
14106 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000014107 if (!(Op.isReg() && Op.isImplicit()))
14108 MIB.addOperand(Op);
14109 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000014110 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000014111 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
14112
14113 BuildMI(*BB, MI, dl,
14114 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
14115 .addReg(X86::ECX);
14116
14117 MI->eraseFromParent();
14118 return BB;
14119}
14120
Craig Topper2da36912012-11-11 22:45:02 +000014121static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
14122 const TargetInstrInfo *TII,
14123 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000014124 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014125
Eric Christopher228232b2010-11-30 07:20:12 +000014126 // Address into RAX/EAX, other two args into ECX, EDX.
14127 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
14128 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
14129 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
14130 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000014131 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014132
Eric Christopher228232b2010-11-30 07:20:12 +000014133 unsigned ValOps = X86::AddrNumOperands;
14134 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
14135 .addReg(MI->getOperand(ValOps).getReg());
14136 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
14137 .addReg(MI->getOperand(ValOps+1).getReg());
14138
14139 // The instruction doesn't actually take any operands though.
14140 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014141
Eric Christopher228232b2010-11-30 07:20:12 +000014142 MI->eraseFromParent(); // The pseudo is gone now.
14143 return BB;
14144}
14145
14146MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000014147X86TargetLowering::EmitVAARG64WithCustomInserter(
14148 MachineInstr *MI,
14149 MachineBasicBlock *MBB) const {
14150 // Emit va_arg instruction on X86-64.
14151
14152 // Operands to this pseudo-instruction:
14153 // 0 ) Output : destination address (reg)
14154 // 1-5) Input : va_list address (addr, i64mem)
14155 // 6 ) ArgSize : Size (in bytes) of vararg type
14156 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
14157 // 8 ) Align : Alignment of type
14158 // 9 ) EFLAGS (implicit-def)
14159
14160 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
14161 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
14162
14163 unsigned DestReg = MI->getOperand(0).getReg();
14164 MachineOperand &Base = MI->getOperand(1);
14165 MachineOperand &Scale = MI->getOperand(2);
14166 MachineOperand &Index = MI->getOperand(3);
14167 MachineOperand &Disp = MI->getOperand(4);
14168 MachineOperand &Segment = MI->getOperand(5);
14169 unsigned ArgSize = MI->getOperand(6).getImm();
14170 unsigned ArgMode = MI->getOperand(7).getImm();
14171 unsigned Align = MI->getOperand(8).getImm();
14172
14173 // Memory Reference
14174 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
14175 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14176 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14177
14178 // Machine Information
14179 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14180 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
14181 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
14182 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
14183 DebugLoc DL = MI->getDebugLoc();
14184
14185 // struct va_list {
14186 // i32 gp_offset
14187 // i32 fp_offset
14188 // i64 overflow_area (address)
14189 // i64 reg_save_area (address)
14190 // }
14191 // sizeof(va_list) = 24
14192 // alignment(va_list) = 8
14193
14194 unsigned TotalNumIntRegs = 6;
14195 unsigned TotalNumXMMRegs = 8;
14196 bool UseGPOffset = (ArgMode == 1);
14197 bool UseFPOffset = (ArgMode == 2);
14198 unsigned MaxOffset = TotalNumIntRegs * 8 +
14199 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
14200
14201 /* Align ArgSize to a multiple of 8 */
14202 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
14203 bool NeedsAlign = (Align > 8);
14204
14205 MachineBasicBlock *thisMBB = MBB;
14206 MachineBasicBlock *overflowMBB;
14207 MachineBasicBlock *offsetMBB;
14208 MachineBasicBlock *endMBB;
14209
14210 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
14211 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
14212 unsigned OffsetReg = 0;
14213
14214 if (!UseGPOffset && !UseFPOffset) {
14215 // If we only pull from the overflow region, we don't create a branch.
14216 // We don't need to alter control flow.
14217 OffsetDestReg = 0; // unused
14218 OverflowDestReg = DestReg;
14219
14220 offsetMBB = NULL;
14221 overflowMBB = thisMBB;
14222 endMBB = thisMBB;
14223 } else {
14224 // First emit code to check if gp_offset (or fp_offset) is below the bound.
14225 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
14226 // If not, pull from overflow_area. (branch to overflowMBB)
14227 //
14228 // thisMBB
14229 // | .
14230 // | .
14231 // offsetMBB overflowMBB
14232 // | .
14233 // | .
14234 // endMBB
14235
14236 // Registers for the PHI in endMBB
14237 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
14238 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
14239
14240 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14241 MachineFunction *MF = MBB->getParent();
14242 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14243 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14244 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14245
14246 MachineFunction::iterator MBBIter = MBB;
14247 ++MBBIter;
14248
14249 // Insert the new basic blocks
14250 MF->insert(MBBIter, offsetMBB);
14251 MF->insert(MBBIter, overflowMBB);
14252 MF->insert(MBBIter, endMBB);
14253
14254 // Transfer the remainder of MBB and its successor edges to endMBB.
14255 endMBB->splice(endMBB->begin(), thisMBB,
14256 llvm::next(MachineBasicBlock::iterator(MI)),
14257 thisMBB->end());
14258 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
14259
14260 // Make offsetMBB and overflowMBB successors of thisMBB
14261 thisMBB->addSuccessor(offsetMBB);
14262 thisMBB->addSuccessor(overflowMBB);
14263
14264 // endMBB is a successor of both offsetMBB and overflowMBB
14265 offsetMBB->addSuccessor(endMBB);
14266 overflowMBB->addSuccessor(endMBB);
14267
14268 // Load the offset value into a register
14269 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
14270 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
14271 .addOperand(Base)
14272 .addOperand(Scale)
14273 .addOperand(Index)
14274 .addDisp(Disp, UseFPOffset ? 4 : 0)
14275 .addOperand(Segment)
14276 .setMemRefs(MMOBegin, MMOEnd);
14277
14278 // Check if there is enough room left to pull this argument.
14279 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
14280 .addReg(OffsetReg)
14281 .addImm(MaxOffset + 8 - ArgSizeA8);
14282
14283 // Branch to "overflowMBB" if offset >= max
14284 // Fall through to "offsetMBB" otherwise
14285 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
14286 .addMBB(overflowMBB);
14287 }
14288
14289 // In offsetMBB, emit code to use the reg_save_area.
14290 if (offsetMBB) {
14291 assert(OffsetReg != 0);
14292
14293 // Read the reg_save_area address.
14294 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
14295 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
14296 .addOperand(Base)
14297 .addOperand(Scale)
14298 .addOperand(Index)
14299 .addDisp(Disp, 16)
14300 .addOperand(Segment)
14301 .setMemRefs(MMOBegin, MMOEnd);
14302
14303 // Zero-extend the offset
14304 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
14305 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
14306 .addImm(0)
14307 .addReg(OffsetReg)
14308 .addImm(X86::sub_32bit);
14309
14310 // Add the offset to the reg_save_area to get the final address.
14311 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
14312 .addReg(OffsetReg64)
14313 .addReg(RegSaveReg);
14314
14315 // Compute the offset for the next argument
14316 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
14317 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
14318 .addReg(OffsetReg)
14319 .addImm(UseFPOffset ? 16 : 8);
14320
14321 // Store it back into the va_list.
14322 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
14323 .addOperand(Base)
14324 .addOperand(Scale)
14325 .addOperand(Index)
14326 .addDisp(Disp, UseFPOffset ? 4 : 0)
14327 .addOperand(Segment)
14328 .addReg(NextOffsetReg)
14329 .setMemRefs(MMOBegin, MMOEnd);
14330
14331 // Jump to endMBB
14332 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
14333 .addMBB(endMBB);
14334 }
14335
14336 //
14337 // Emit code to use overflow area
14338 //
14339
14340 // Load the overflow_area address into a register.
14341 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
14342 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
14343 .addOperand(Base)
14344 .addOperand(Scale)
14345 .addOperand(Index)
14346 .addDisp(Disp, 8)
14347 .addOperand(Segment)
14348 .setMemRefs(MMOBegin, MMOEnd);
14349
14350 // If we need to align it, do so. Otherwise, just copy the address
14351 // to OverflowDestReg.
14352 if (NeedsAlign) {
14353 // Align the overflow address
14354 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
14355 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
14356
14357 // aligned_addr = (addr + (align-1)) & ~(align-1)
14358 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
14359 .addReg(OverflowAddrReg)
14360 .addImm(Align-1);
14361
14362 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
14363 .addReg(TmpReg)
14364 .addImm(~(uint64_t)(Align-1));
14365 } else {
14366 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
14367 .addReg(OverflowAddrReg);
14368 }
14369
14370 // Compute the next overflow address after this argument.
14371 // (the overflow address should be kept 8-byte aligned)
14372 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
14373 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
14374 .addReg(OverflowDestReg)
14375 .addImm(ArgSizeA8);
14376
14377 // Store the new overflow address.
14378 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
14379 .addOperand(Base)
14380 .addOperand(Scale)
14381 .addOperand(Index)
14382 .addDisp(Disp, 8)
14383 .addOperand(Segment)
14384 .addReg(NextAddrReg)
14385 .setMemRefs(MMOBegin, MMOEnd);
14386
14387 // If we branched, emit the PHI to the front of endMBB.
14388 if (offsetMBB) {
14389 BuildMI(*endMBB, endMBB->begin(), DL,
14390 TII->get(X86::PHI), DestReg)
14391 .addReg(OffsetDestReg).addMBB(offsetMBB)
14392 .addReg(OverflowDestReg).addMBB(overflowMBB);
14393 }
14394
14395 // Erase the pseudo instruction
14396 MI->eraseFromParent();
14397
14398 return endMBB;
14399}
14400
14401MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000014402X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
14403 MachineInstr *MI,
14404 MachineBasicBlock *MBB) const {
14405 // Emit code to save XMM registers to the stack. The ABI says that the
14406 // number of registers to save is given in %al, so it's theoretically
14407 // possible to do an indirect jump trick to avoid saving all of them,
14408 // however this code takes a simpler approach and just executes all
14409 // of the stores if %al is non-zero. It's less code, and it's probably
14410 // easier on the hardware branch predictor, and stores aren't all that
14411 // expensive anyway.
14412
14413 // Create the new basic blocks. One block contains all the XMM stores,
14414 // and one block is the final destination regardless of whether any
14415 // stores were performed.
14416 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14417 MachineFunction *F = MBB->getParent();
14418 MachineFunction::iterator MBBIter = MBB;
14419 ++MBBIter;
14420 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
14421 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
14422 F->insert(MBBIter, XMMSaveMBB);
14423 F->insert(MBBIter, EndMBB);
14424
Dan Gohman14152b42010-07-06 20:24:04 +000014425 // Transfer the remainder of MBB and its successor edges to EndMBB.
14426 EndMBB->splice(EndMBB->begin(), MBB,
14427 llvm::next(MachineBasicBlock::iterator(MI)),
14428 MBB->end());
14429 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
14430
Dan Gohmand6708ea2009-08-15 01:38:56 +000014431 // The original block will now fall through to the XMM save block.
14432 MBB->addSuccessor(XMMSaveMBB);
14433 // The XMMSaveMBB will fall through to the end block.
14434 XMMSaveMBB->addSuccessor(EndMBB);
14435
14436 // Now add the instructions.
14437 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14438 DebugLoc DL = MI->getDebugLoc();
14439
14440 unsigned CountReg = MI->getOperand(0).getReg();
14441 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
14442 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
14443
14444 if (!Subtarget->isTargetWin64()) {
14445 // If %al is 0, branch around the XMM save block.
14446 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000014447 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014448 MBB->addSuccessor(EndMBB);
14449 }
14450
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014451 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000014452 // In the XMM save block, save all the XMM argument registers.
14453 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
14454 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000014455 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000014456 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000014457 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000014458 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000014459 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014460 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000014461 .addFrameIndex(RegSaveFrameIndex)
14462 .addImm(/*Scale=*/1)
14463 .addReg(/*IndexReg=*/0)
14464 .addImm(/*Disp=*/Offset)
14465 .addReg(/*Segment=*/0)
14466 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000014467 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014468 }
14469
Dan Gohman14152b42010-07-06 20:24:04 +000014470 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000014471
14472 return EndMBB;
14473}
Mon P Wang63307c32008-05-05 19:05:59 +000014474
Lang Hames6e3f7e42012-02-03 01:13:49 +000014475// The EFLAGS operand of SelectItr might be missing a kill marker
14476// because there were multiple uses of EFLAGS, and ISel didn't know
14477// which to mark. Figure out whether SelectItr should have had a
14478// kill marker, and set it if it should. Returns the correct kill
14479// marker value.
14480static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
14481 MachineBasicBlock* BB,
14482 const TargetRegisterInfo* TRI) {
14483 // Scan forward through BB for a use/def of EFLAGS.
14484 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
14485 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000014486 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014487 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000014488 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014489 if (mi.definesRegister(X86::EFLAGS))
14490 break; // Should have kill-flag - update below.
14491 }
14492
14493 // If we hit the end of the block, check whether EFLAGS is live into a
14494 // successor.
14495 if (miI == BB->end()) {
14496 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
14497 sEnd = BB->succ_end();
14498 sItr != sEnd; ++sItr) {
14499 MachineBasicBlock* succ = *sItr;
14500 if (succ->isLiveIn(X86::EFLAGS))
14501 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000014502 }
14503 }
14504
Lang Hames6e3f7e42012-02-03 01:13:49 +000014505 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
14506 // out. SelectMI should have a kill flag on EFLAGS.
14507 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000014508 return true;
14509}
14510
Evan Cheng60c07e12006-07-05 22:17:51 +000014511MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000014512X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014513 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000014514 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14515 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000014516
Chris Lattner52600972009-09-02 05:57:00 +000014517 // To "insert" a SELECT_CC instruction, we actually have to insert the
14518 // diamond control-flow pattern. The incoming instruction knows the
14519 // destination vreg to set, the condition code register to branch on, the
14520 // true/false values to select between, and a branch opcode to use.
14521 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14522 MachineFunction::iterator It = BB;
14523 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000014524
Chris Lattner52600972009-09-02 05:57:00 +000014525 // thisMBB:
14526 // ...
14527 // TrueVal = ...
14528 // cmpTY ccX, r1, r2
14529 // bCC copy1MBB
14530 // fallthrough --> copy0MBB
14531 MachineBasicBlock *thisMBB = BB;
14532 MachineFunction *F = BB->getParent();
14533 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
14534 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000014535 F->insert(It, copy0MBB);
14536 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000014537
Bill Wendling730c07e2010-06-25 20:48:10 +000014538 // If the EFLAGS register isn't dead in the terminator, then claim that it's
14539 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000014540 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
14541 if (!MI->killsRegister(X86::EFLAGS) &&
14542 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
14543 copy0MBB->addLiveIn(X86::EFLAGS);
14544 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000014545 }
14546
Dan Gohman14152b42010-07-06 20:24:04 +000014547 // Transfer the remainder of BB and its successor edges to sinkMBB.
14548 sinkMBB->splice(sinkMBB->begin(), BB,
14549 llvm::next(MachineBasicBlock::iterator(MI)),
14550 BB->end());
14551 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
14552
14553 // Add the true and fallthrough blocks as its successors.
14554 BB->addSuccessor(copy0MBB);
14555 BB->addSuccessor(sinkMBB);
14556
14557 // Create the conditional branch instruction.
14558 unsigned Opc =
14559 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14560 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14561
Chris Lattner52600972009-09-02 05:57:00 +000014562 // copy0MBB:
14563 // %FalseValue = ...
14564 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014565 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014566
Chris Lattner52600972009-09-02 05:57:00 +000014567 // sinkMBB:
14568 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14569 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014570 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14571 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014572 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14573 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14574
Dan Gohman14152b42010-07-06 20:24:04 +000014575 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014576 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014577}
14578
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014579MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014580X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14581 bool Is64Bit) const {
14582 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14583 DebugLoc DL = MI->getDebugLoc();
14584 MachineFunction *MF = BB->getParent();
14585 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14586
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014587 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014588
14589 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14590 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14591
14592 // BB:
14593 // ... [Till the alloca]
14594 // If stacklet is not large enough, jump to mallocMBB
14595 //
14596 // bumpMBB:
14597 // Allocate by subtracting from RSP
14598 // Jump to continueMBB
14599 //
14600 // mallocMBB:
14601 // Allocate by call to runtime
14602 //
14603 // continueMBB:
14604 // ...
14605 // [rest of original BB]
14606 //
14607
14608 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14609 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14610 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14611
14612 MachineRegisterInfo &MRI = MF->getRegInfo();
14613 const TargetRegisterClass *AddrRegClass =
14614 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14615
14616 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14617 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14618 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014619 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014620 sizeVReg = MI->getOperand(1).getReg(),
14621 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14622
14623 MachineFunction::iterator MBBIter = BB;
14624 ++MBBIter;
14625
14626 MF->insert(MBBIter, bumpMBB);
14627 MF->insert(MBBIter, mallocMBB);
14628 MF->insert(MBBIter, continueMBB);
14629
14630 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14631 (MachineBasicBlock::iterator(MI)), BB->end());
14632 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14633
14634 // Add code to the main basic block to check if the stack limit has been hit,
14635 // and if so, jump to mallocMBB otherwise to bumpMBB.
14636 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014637 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014638 .addReg(tmpSPVReg).addReg(sizeVReg);
14639 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014640 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014641 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014642 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14643
14644 // bumpMBB simply decreases the stack pointer, since we know the current
14645 // stacklet has enough space.
14646 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014647 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014648 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014649 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014650 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14651
14652 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014653 const uint32_t *RegMask =
14654 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014655 if (Is64Bit) {
14656 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14657 .addReg(sizeVReg);
14658 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014659 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014660 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014661 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014662 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014663 } else {
14664 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14665 .addImm(12);
14666 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14667 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014668 .addExternalSymbol("__morestack_allocate_stack_space")
14669 .addRegMask(RegMask)
14670 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014671 }
14672
14673 if (!Is64Bit)
14674 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14675 .addImm(16);
14676
14677 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14678 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14679 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14680
14681 // Set up the CFG correctly.
14682 BB->addSuccessor(bumpMBB);
14683 BB->addSuccessor(mallocMBB);
14684 mallocMBB->addSuccessor(continueMBB);
14685 bumpMBB->addSuccessor(continueMBB);
14686
14687 // Take care of the PHI nodes.
14688 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14689 MI->getOperand(0).getReg())
14690 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14691 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14692
14693 // Delete the original pseudo instruction.
14694 MI->eraseFromParent();
14695
14696 // And we're done.
14697 return continueMBB;
14698}
14699
14700MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014701X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014702 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014703 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14704 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014705
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014706 assert(!Subtarget->isTargetEnvMacho());
14707
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014708 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14709 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014710
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014711 if (Subtarget->isTargetWin64()) {
14712 if (Subtarget->isTargetCygMing()) {
14713 // ___chkstk(Mingw64):
14714 // Clobbers R10, R11, RAX and EFLAGS.
14715 // Updates RSP.
14716 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14717 .addExternalSymbol("___chkstk")
14718 .addReg(X86::RAX, RegState::Implicit)
14719 .addReg(X86::RSP, RegState::Implicit)
14720 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14721 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14722 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14723 } else {
14724 // __chkstk(MSVCRT): does not update stack pointer.
14725 // Clobbers R10, R11 and EFLAGS.
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014726 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14727 .addExternalSymbol("__chkstk")
14728 .addReg(X86::RAX, RegState::Implicit)
14729 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Nico Rieck40101102013-07-08 11:20:11 +000014730 // RAX has the offset to be subtracted from RSP.
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014731 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14732 .addReg(X86::RSP)
14733 .addReg(X86::RAX);
14734 }
14735 } else {
14736 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014737 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14738
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014739 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14740 .addExternalSymbol(StackProbeSymbol)
14741 .addReg(X86::EAX, RegState::Implicit)
14742 .addReg(X86::ESP, RegState::Implicit)
14743 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14744 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14745 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14746 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014747
Dan Gohman14152b42010-07-06 20:24:04 +000014748 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014749 return BB;
14750}
Chris Lattner52600972009-09-02 05:57:00 +000014751
14752MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014753X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14754 MachineBasicBlock *BB) const {
14755 // This is pretty easy. We're taking the value that we received from
14756 // our load from the relocation, sticking it in either RDI (x86-64)
14757 // or EAX and doing an indirect call. The return value will then
14758 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014759 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014760 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014761 DebugLoc DL = MI->getDebugLoc();
14762 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014763
14764 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014765 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014766
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014767 // Get a register mask for the lowered call.
14768 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14769 // proper register mask.
14770 const uint32_t *RegMask =
14771 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014772 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014773 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14774 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014775 .addReg(X86::RIP)
14776 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014777 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014778 MI->getOperand(3).getTargetFlags())
14779 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014780 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014781 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014782 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014783 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014784 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14785 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014786 .addReg(0)
14787 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014788 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014789 MI->getOperand(3).getTargetFlags())
14790 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014791 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014792 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014793 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014794 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014795 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14796 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014797 .addReg(TII->getGlobalBaseReg(F))
14798 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014799 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014800 MI->getOperand(3).getTargetFlags())
14801 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014802 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014803 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014804 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014805 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014806
Dan Gohman14152b42010-07-06 20:24:04 +000014807 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014808 return BB;
14809}
14810
14811MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014812X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14813 MachineBasicBlock *MBB) const {
14814 DebugLoc DL = MI->getDebugLoc();
14815 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14816
14817 MachineFunction *MF = MBB->getParent();
14818 MachineRegisterInfo &MRI = MF->getRegInfo();
14819
14820 const BasicBlock *BB = MBB->getBasicBlock();
14821 MachineFunction::iterator I = MBB;
14822 ++I;
14823
14824 // Memory Reference
14825 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14826 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14827
14828 unsigned DstReg;
14829 unsigned MemOpndSlot = 0;
14830
14831 unsigned CurOp = 0;
14832
14833 DstReg = MI->getOperand(CurOp++).getReg();
14834 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14835 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14836 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14837 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14838
14839 MemOpndSlot = CurOp;
14840
14841 MVT PVT = getPointerTy();
14842 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14843 "Invalid Pointer Size!");
14844
14845 // For v = setjmp(buf), we generate
14846 //
14847 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014848 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014849 // SjLjSetup restoreMBB
14850 //
14851 // mainMBB:
14852 // v_main = 0
14853 //
14854 // sinkMBB:
14855 // v = phi(main, restore)
14856 //
14857 // restoreMBB:
14858 // v_restore = 1
14859
14860 MachineBasicBlock *thisMBB = MBB;
14861 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14862 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14863 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14864 MF->insert(I, mainMBB);
14865 MF->insert(I, sinkMBB);
14866 MF->push_back(restoreMBB);
14867
14868 MachineInstrBuilder MIB;
14869
14870 // Transfer the remainder of BB and its successor edges to sinkMBB.
14871 sinkMBB->splice(sinkMBB->begin(), MBB,
14872 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14873 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14874
14875 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014876 unsigned PtrStoreOpc = 0;
14877 unsigned LabelReg = 0;
14878 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14879 Reloc::Model RM = getTargetMachine().getRelocationModel();
14880 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14881 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014882
Michael Liao281ae5a2012-10-17 02:22:27 +000014883 // Prepare IP either in reg or imm.
14884 if (!UseImmLabel) {
14885 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14886 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14887 LabelReg = MRI.createVirtualRegister(PtrRC);
14888 if (Subtarget->is64Bit()) {
14889 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14890 .addReg(X86::RIP)
14891 .addImm(0)
14892 .addReg(0)
14893 .addMBB(restoreMBB)
14894 .addReg(0);
14895 } else {
14896 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14897 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14898 .addReg(XII->getGlobalBaseReg(MF))
14899 .addImm(0)
14900 .addReg(0)
14901 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14902 .addReg(0);
14903 }
14904 } else
14905 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014906 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014907 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014908 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14909 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014910 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014911 else
14912 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14913 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014914 if (!UseImmLabel)
14915 MIB.addReg(LabelReg);
14916 else
14917 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014918 MIB.setMemRefs(MMOBegin, MMOEnd);
14919 // Setup
14920 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14921 .addMBB(restoreMBB);
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000014922
14923 const X86RegisterInfo *RegInfo =
14924 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liao6c0e04c2012-10-15 22:39:43 +000014925 MIB.addRegMask(RegInfo->getNoPreservedMask());
14926 thisMBB->addSuccessor(mainMBB);
14927 thisMBB->addSuccessor(restoreMBB);
14928
14929 // mainMBB:
14930 // EAX = 0
14931 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14932 mainMBB->addSuccessor(sinkMBB);
14933
14934 // sinkMBB:
14935 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14936 TII->get(X86::PHI), DstReg)
14937 .addReg(mainDstReg).addMBB(mainMBB)
14938 .addReg(restoreDstReg).addMBB(restoreMBB);
14939
14940 // restoreMBB:
14941 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14942 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14943 restoreMBB->addSuccessor(sinkMBB);
14944
14945 MI->eraseFromParent();
14946 return sinkMBB;
14947}
14948
14949MachineBasicBlock *
14950X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14951 MachineBasicBlock *MBB) const {
14952 DebugLoc DL = MI->getDebugLoc();
14953 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14954
14955 MachineFunction *MF = MBB->getParent();
14956 MachineRegisterInfo &MRI = MF->getRegInfo();
14957
14958 // Memory Reference
14959 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14960 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14961
14962 MVT PVT = getPointerTy();
14963 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14964 "Invalid Pointer Size!");
14965
14966 const TargetRegisterClass *RC =
14967 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14968 unsigned Tmp = MRI.createVirtualRegister(RC);
14969 // Since FP is only updated here but NOT referenced, it's treated as GPR.
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000014970 const X86RegisterInfo *RegInfo =
14971 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liao6c0e04c2012-10-15 22:39:43 +000014972 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14973 unsigned SP = RegInfo->getStackRegister();
14974
14975 MachineInstrBuilder MIB;
14976
Michael Liao281ae5a2012-10-17 02:22:27 +000014977 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14978 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014979
14980 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14981 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14982
14983 // Reload FP
14984 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14985 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14986 MIB.addOperand(MI->getOperand(i));
14987 MIB.setMemRefs(MMOBegin, MMOEnd);
14988 // Reload IP
14989 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14990 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14991 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014992 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014993 else
14994 MIB.addOperand(MI->getOperand(i));
14995 }
14996 MIB.setMemRefs(MMOBegin, MMOEnd);
14997 // Reload SP
14998 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14999 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
15000 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000015001 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000015002 else
15003 MIB.addOperand(MI->getOperand(i));
15004 }
15005 MIB.setMemRefs(MMOBegin, MMOEnd);
15006 // Jump
15007 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
15008
15009 MI->eraseFromParent();
15010 return MBB;
15011}
15012
15013MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000015014X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000015015 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000015016 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000015017 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000015018 case X86::TAILJMPd64:
15019 case X86::TAILJMPr64:
15020 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000015021 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000015022 case X86::TCRETURNdi64:
15023 case X86::TCRETURNri64:
15024 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000015025 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000015026 case X86::WIN_ALLOCA:
15027 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000015028 case X86::SEG_ALLOCA_32:
15029 return EmitLoweredSegAlloca(MI, BB, false);
15030 case X86::SEG_ALLOCA_64:
15031 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000015032 case X86::TLSCall_32:
15033 case X86::TLSCall_64:
15034 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000015035 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000015036 case X86::CMOV_FR32:
15037 case X86::CMOV_FR64:
15038 case X86::CMOV_V4F32:
15039 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000015040 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000015041 case X86::CMOV_V8F32:
15042 case X86::CMOV_V4F64:
15043 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000015044 case X86::CMOV_GR16:
15045 case X86::CMOV_GR32:
15046 case X86::CMOV_RFP32:
15047 case X86::CMOV_RFP64:
15048 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000015049 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000015050
Dale Johannesen849f2142007-07-03 00:53:03 +000015051 case X86::FP32_TO_INT16_IN_MEM:
15052 case X86::FP32_TO_INT32_IN_MEM:
15053 case X86::FP32_TO_INT64_IN_MEM:
15054 case X86::FP64_TO_INT16_IN_MEM:
15055 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000015056 case X86::FP64_TO_INT64_IN_MEM:
15057 case X86::FP80_TO_INT16_IN_MEM:
15058 case X86::FP80_TO_INT32_IN_MEM:
15059 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000015060 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
15061 DebugLoc DL = MI->getDebugLoc();
15062
Evan Cheng60c07e12006-07-05 22:17:51 +000015063 // Change the floating point control register to use "round towards zero"
15064 // mode when truncating to an integer value.
15065 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000015066 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000015067 addFrameReference(BuildMI(*BB, MI, DL,
15068 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000015069
15070 // Load the old value of the high byte of the control word...
15071 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000015072 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000015073 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000015074 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000015075
15076 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000015077 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000015078 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000015079
15080 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000015081 addFrameReference(BuildMI(*BB, MI, DL,
15082 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000015083
15084 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000015085 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000015086 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000015087
15088 // Get the X86 opcode to use.
15089 unsigned Opc;
15090 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000015091 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000015092 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
15093 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
15094 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
15095 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
15096 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
15097 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000015098 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
15099 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
15100 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000015101 }
15102
15103 X86AddressMode AM;
15104 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000015105 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000015106 AM.BaseType = X86AddressMode::RegBase;
15107 AM.Base.Reg = Op.getReg();
15108 } else {
15109 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000015110 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000015111 }
15112 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000015113 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000015114 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000015115 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000015116 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000015117 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000015118 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000015119 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000015120 AM.GV = Op.getGlobal();
15121 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000015122 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000015123 }
Dan Gohman14152b42010-07-06 20:24:04 +000015124 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000015125 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000015126
15127 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000015128 addFrameReference(BuildMI(*BB, MI, DL,
15129 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000015130
Dan Gohman14152b42010-07-06 20:24:04 +000015131 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000015132 return BB;
15133 }
Eric Christopherb120ab42009-08-18 22:50:32 +000015134 // String/text processing lowering.
15135 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000015136 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000015137 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000015138 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000015139 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000015140 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000015141 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000015142 case X86::VPCMPESTRM128MEM:
15143 assert(Subtarget->hasSSE42() &&
15144 "Target must have SSE4.2 or AVX features enabled");
15145 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000015146
15147 // String/text processing lowering.
15148 case X86::PCMPISTRIREG:
15149 case X86::VPCMPISTRIREG:
15150 case X86::PCMPISTRIMEM:
15151 case X86::VPCMPISTRIMEM:
15152 case X86::PCMPESTRIREG:
15153 case X86::VPCMPESTRIREG:
15154 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000015155 case X86::VPCMPESTRIMEM:
15156 assert(Subtarget->hasSSE42() &&
15157 "Target must have SSE4.2 or AVX features enabled");
15158 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000015159
Craig Topper8aae8dd2012-11-10 08:57:41 +000015160 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000015161 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000015162 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000015163
Michael Liaobe02a902012-11-08 07:28:54 +000015164 // xbegin
15165 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000015166 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000015167
Craig Topper8aae8dd2012-11-10 08:57:41 +000015168 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000015169 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000015170 case X86::ATOMAND16:
15171 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000015172 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000015173 // Fall through
15174 case X86::ATOMOR8:
15175 case X86::ATOMOR16:
15176 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000015177 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000015178 // Fall through
15179 case X86::ATOMXOR16:
15180 case X86::ATOMXOR8:
15181 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000015182 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000015183 // Fall through
15184 case X86::ATOMNAND8:
15185 case X86::ATOMNAND16:
15186 case X86::ATOMNAND32:
15187 case X86::ATOMNAND64:
15188 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000015189 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000015190 case X86::ATOMMAX16:
15191 case X86::ATOMMAX32:
15192 case X86::ATOMMAX64:
15193 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000015194 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000015195 case X86::ATOMMIN16:
15196 case X86::ATOMMIN32:
15197 case X86::ATOMMIN64:
15198 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000015199 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000015200 case X86::ATOMUMAX16:
15201 case X86::ATOMUMAX32:
15202 case X86::ATOMUMAX64:
15203 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000015204 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000015205 case X86::ATOMUMIN16:
15206 case X86::ATOMUMIN32:
15207 case X86::ATOMUMIN64:
15208 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000015209
15210 // This group does 64-bit operations on a 32-bit host.
15211 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000015212 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000015213 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000015214 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000015215 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000015216 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000015217 case X86::ATOMMAX6432:
15218 case X86::ATOMMIN6432:
15219 case X86::ATOMUMAX6432:
15220 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000015221 case X86::ATOMSWAP6432:
15222 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000015223
Dan Gohmand6708ea2009-08-15 01:38:56 +000015224 case X86::VASTART_SAVE_XMM_REGS:
15225 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000015226
15227 case X86::VAARG_64:
15228 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000015229
15230 case X86::EH_SjLj_SetJmp32:
15231 case X86::EH_SjLj_SetJmp64:
15232 return emitEHSjLjSetJmp(MI, BB);
15233
15234 case X86::EH_SjLj_LongJmp32:
15235 case X86::EH_SjLj_LongJmp64:
15236 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000015237 }
15238}
15239
15240//===----------------------------------------------------------------------===//
15241// X86 Optimization Hooks
15242//===----------------------------------------------------------------------===//
15243
Dan Gohman475871a2008-07-27 21:46:04 +000015244void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000015245 APInt &KnownZero,
15246 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000015247 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000015248 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000015249 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015250 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000015251 assert((Opc >= ISD::BUILTIN_OP_END ||
15252 Opc == ISD::INTRINSIC_WO_CHAIN ||
15253 Opc == ISD::INTRINSIC_W_CHAIN ||
15254 Opc == ISD::INTRINSIC_VOID) &&
15255 "Should use MaskedValueIsZero if you don't know whether Op"
15256 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015257
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000015258 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015259 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000015260 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000015261 case X86ISD::ADD:
15262 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000015263 case X86ISD::ADC:
15264 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000015265 case X86ISD::SMUL:
15266 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000015267 case X86ISD::INC:
15268 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000015269 case X86ISD::OR:
15270 case X86ISD::XOR:
15271 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000015272 // These nodes' second result is a boolean.
15273 if (Op.getResNo() == 0)
15274 break;
15275 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015276 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000015277 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000015278 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000015279 case ISD::INTRINSIC_WO_CHAIN: {
15280 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
15281 unsigned NumLoBits = 0;
15282 switch (IntId) {
15283 default: break;
15284 case Intrinsic::x86_sse_movmsk_ps:
15285 case Intrinsic::x86_avx_movmsk_ps_256:
15286 case Intrinsic::x86_sse2_movmsk_pd:
15287 case Intrinsic::x86_avx_movmsk_pd_256:
15288 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000015289 case Intrinsic::x86_sse2_pmovmskb_128:
15290 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000015291 // High bits of movmskp{s|d}, pmovmskb are known zero.
15292 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000015293 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000015294 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
15295 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
15296 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
15297 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
15298 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
15299 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000015300 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000015301 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000015302 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000015303 break;
15304 }
15305 }
15306 break;
15307 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015308 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015309}
Chris Lattner259e97c2006-01-31 19:43:35 +000015310
Owen Andersonbc146b02010-09-21 20:42:50 +000015311unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
15312 unsigned Depth) const {
15313 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
15314 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
15315 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000015316
Owen Andersonbc146b02010-09-21 20:42:50 +000015317 // Fallback case.
15318 return 1;
15319}
15320
Evan Cheng206ee9d2006-07-07 08:33:52 +000015321/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000015322/// node is a GlobalAddress + offset.
15323bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000015324 const GlobalValue* &GA,
15325 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000015326 if (N->getOpcode() == X86ISD::Wrapper) {
15327 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000015328 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000015329 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000015330 return true;
15331 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000015332 }
Evan Chengad4196b2008-05-12 19:56:52 +000015333 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000015334}
15335
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015336/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
15337/// same as extracting the high 128-bit part of 256-bit vector and then
15338/// inserting the result into the low part of a new 256-bit vector
15339static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
15340 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015341 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015342
15343 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000015344 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015345 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15346 SVOp->getMaskElt(j) >= 0)
15347 return false;
15348
15349 return true;
15350}
15351
15352/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
15353/// same as extracting the low 128-bit part of 256-bit vector and then
15354/// inserting the result into the high part of a new 256-bit vector
15355static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
15356 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015357 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015358
15359 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000015360 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015361 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15362 SVOp->getMaskElt(j) >= 0)
15363 return false;
15364
15365 return true;
15366}
15367
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015368/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
15369static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000015370 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015371 const X86Subtarget* Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000015372 SDLoc dl(N);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015373 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
15374 SDValue V1 = SVOp->getOperand(0);
15375 SDValue V2 = SVOp->getOperand(1);
15376 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015377 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015378
15379 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
15380 V2.getOpcode() == ISD::CONCAT_VECTORS) {
15381 //
15382 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000015383 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015384 // V UNDEF BUILD_VECTOR UNDEF
15385 // \ / \ /
15386 // CONCAT_VECTOR CONCAT_VECTOR
15387 // \ /
15388 // \ /
15389 // RESULT: V + zero extended
15390 //
15391 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
15392 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
15393 V1.getOperand(1).getOpcode() != ISD::UNDEF)
15394 return SDValue();
15395
15396 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
15397 return SDValue();
15398
15399 // To match the shuffle mask, the first half of the mask should
15400 // be exactly the first vector, and all the rest a splat with the
15401 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000015402 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015403 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
15404 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
15405 return SDValue();
15406
Chad Rosier3d1161e2012-01-03 21:05:52 +000015407 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
15408 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000015409 if (Ld->hasNUsesOfValue(1, 0)) {
15410 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
15411 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
15412 SDValue ResNode =
Michael Liao0ee17002013-04-19 04:03:37 +000015413 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops,
15414 array_lengthof(Ops),
Chad Rosier42726832012-05-07 18:47:44 +000015415 Ld->getMemoryVT(),
15416 Ld->getPointerInfo(),
15417 Ld->getAlignment(),
15418 false/*isVolatile*/, true/*ReadMem*/,
15419 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000015420
15421 // Make sure the newly-created LOAD is in the same position as Ld in
15422 // terms of dependency. We create a TokenFactor for Ld and ResNode,
15423 // and update uses of Ld's output chain to use the TokenFactor.
15424 if (Ld->hasAnyUseOfValue(1)) {
15425 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
15426 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
15427 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
15428 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
15429 SDValue(ResNode.getNode(), 1));
15430 }
15431
Chad Rosier42726832012-05-07 18:47:44 +000015432 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
15433 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000015434 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000015435
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015436 // Emit a zeroed vector and insert the desired subvector on its
15437 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015438 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000015439 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015440 return DCI.CombineTo(N, InsV);
15441 }
15442
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015443 //===--------------------------------------------------------------------===//
15444 // Combine some shuffles into subvector extracts and inserts:
15445 //
15446
15447 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
15448 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015449 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
15450 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015451 return DCI.CombineTo(N, InsV);
15452 }
15453
15454 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
15455 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015456 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
15457 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015458 return DCI.CombineTo(N, InsV);
15459 }
15460
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015461 return SDValue();
15462}
15463
15464/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000015465static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015466 TargetLowering::DAGCombinerInfo &DCI,
15467 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000015468 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +000015469 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000015470
Mon P Wanga0fd0d52010-12-19 23:55:53 +000015471 // Don't create instructions with illegal types after legalize types has run.
15472 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15473 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
15474 return SDValue();
15475
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015476 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015477 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015478 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015479 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015480
15481 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015482 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015483 return SDValue();
15484
15485 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
15486 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
15487 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000015488 SmallVector<SDValue, 16> Elts;
15489 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015490 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000015491
Nate Begemanfdea31a2010-03-24 20:49:50 +000015492 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000015493}
Evan Chengd880b972008-05-09 21:53:03 +000015494
Nadav Roteme12bf182013-01-04 17:35:21 +000015495/// PerformTruncateCombine - Converts truncate operation to
15496/// a sequence of vector shuffle operations.
15497/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000015498static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
15499 TargetLowering::DAGCombinerInfo &DCI,
15500 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015501 return SDValue();
15502}
15503
Craig Topper89f4e662012-03-20 07:17:59 +000015504/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
15505/// specific shuffle of a load can be folded into a single element load.
15506/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
15507/// shuffles have been customed lowered so we need to handle those here.
15508static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
15509 TargetLowering::DAGCombinerInfo &DCI) {
15510 if (DCI.isBeforeLegalizeOps())
15511 return SDValue();
15512
15513 SDValue InVec = N->getOperand(0);
15514 SDValue EltNo = N->getOperand(1);
15515
15516 if (!isa<ConstantSDNode>(EltNo))
15517 return SDValue();
15518
15519 EVT VT = InVec.getValueType();
15520
15521 bool HasShuffleIntoBitcast = false;
15522 if (InVec.getOpcode() == ISD::BITCAST) {
15523 // Don't duplicate a load with other uses.
15524 if (!InVec.hasOneUse())
15525 return SDValue();
15526 EVT BCVT = InVec.getOperand(0).getValueType();
15527 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
15528 return SDValue();
15529 InVec = InVec.getOperand(0);
15530 HasShuffleIntoBitcast = true;
15531 }
15532
15533 if (!isTargetShuffle(InVec.getOpcode()))
15534 return SDValue();
15535
15536 // Don't duplicate a load with other uses.
15537 if (!InVec.hasOneUse())
15538 return SDValue();
15539
15540 SmallVector<int, 16> ShuffleMask;
15541 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000015542 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
15543 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000015544 return SDValue();
15545
15546 // Select the input vector, guarding against out of range extract vector.
15547 unsigned NumElems = VT.getVectorNumElements();
15548 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
15549 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
15550 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
15551 : InVec.getOperand(1);
15552
15553 // If inputs to shuffle are the same for both ops, then allow 2 uses
15554 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
15555
15556 if (LdNode.getOpcode() == ISD::BITCAST) {
15557 // Don't duplicate a load with other uses.
15558 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
15559 return SDValue();
15560
15561 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15562 LdNode = LdNode.getOperand(0);
15563 }
15564
15565 if (!ISD::isNormalLoad(LdNode.getNode()))
15566 return SDValue();
15567
15568 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15569
15570 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15571 return SDValue();
15572
15573 if (HasShuffleIntoBitcast) {
15574 // If there's a bitcast before the shuffle, check if the load type and
15575 // alignment is valid.
15576 unsigned Align = LN0->getAlignment();
15577 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015578 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015579 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15580
15581 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15582 return SDValue();
15583 }
15584
15585 // All checks match so transform back to vector_shuffle so that DAG combiner
15586 // can finish the job
Andrew Trickac6d9be2013-05-25 02:42:55 +000015587 SDLoc dl(N);
Craig Topper89f4e662012-03-20 07:17:59 +000015588
15589 // Create shuffle node taking into account the case that its a unary shuffle
15590 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15591 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15592 InVec.getOperand(0), Shuffle,
15593 &ShuffleMask[0]);
15594 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15595 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15596 EltNo);
15597}
15598
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015599/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15600/// generation and convert it from being a bunch of shuffles and extracts
15601/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015602static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015603 TargetLowering::DAGCombinerInfo &DCI) {
15604 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15605 if (NewOp.getNode())
15606 return NewOp;
15607
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015608 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015609 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15610 // from mmx to v2i32 has a single usage.
15611 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15612 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15613 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
Andrew Trickac6d9be2013-05-25 02:42:55 +000015614 return DAG.getNode(X86ISD::MMX_MOVD2W, SDLoc(InputVector),
Manman Ren4c74a952012-10-30 22:15:38 +000015615 N->getValueType(0),
15616 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015617
15618 // Only operate on vectors of 4 elements, where the alternative shuffling
15619 // gets to be more expensive.
15620 if (InputVector.getValueType() != MVT::v4i32)
15621 return SDValue();
15622
15623 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15624 // single use which is a sign-extend or zero-extend, and all elements are
15625 // used.
15626 SmallVector<SDNode *, 4> Uses;
15627 unsigned ExtractedElements = 0;
15628 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15629 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15630 if (UI.getUse().getResNo() != InputVector.getResNo())
15631 return SDValue();
15632
15633 SDNode *Extract = *UI;
15634 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15635 return SDValue();
15636
15637 if (Extract->getValueType(0) != MVT::i32)
15638 return SDValue();
15639 if (!Extract->hasOneUse())
15640 return SDValue();
15641 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15642 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15643 return SDValue();
15644 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15645 return SDValue();
15646
15647 // Record which element was extracted.
15648 ExtractedElements |=
15649 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15650
15651 Uses.push_back(Extract);
15652 }
15653
15654 // If not all the elements were used, this may not be worthwhile.
15655 if (ExtractedElements != 15)
15656 return SDValue();
15657
15658 // Ok, we've now decided to do the transformation.
Andrew Trickac6d9be2013-05-25 02:42:55 +000015659 SDLoc dl(InputVector);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015660
15661 // Store the value to a temporary stack slot.
15662 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015663 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15664 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015665
15666 // Replace each use (extract) with a load of the appropriate element.
15667 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15668 UE = Uses.end(); UI != UE; ++UI) {
15669 SDNode *Extract = *UI;
15670
Nadav Rotem86694292011-05-17 08:31:57 +000015671 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015672 SDValue Idx = Extract->getOperand(1);
15673 unsigned EltSize =
15674 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15675 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015676 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015677 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15678
Nadav Rotem86694292011-05-17 08:31:57 +000015679 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015680 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015681
15682 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015683 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015684 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015685 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015686
15687 // Replace the exact with the load.
15688 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15689 }
15690
15691 // The replacement was made in place; don't return anything.
15692 return SDValue();
15693}
15694
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015695/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15696static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15697 SDValue RHS, SelectionDAG &DAG,
15698 const X86Subtarget *Subtarget) {
15699 if (!VT.isVector())
15700 return 0;
15701
15702 switch (VT.getSimpleVT().SimpleTy) {
15703 default: return 0;
15704 case MVT::v32i8:
15705 case MVT::v16i16:
15706 case MVT::v8i32:
15707 if (!Subtarget->hasAVX2())
15708 return 0;
15709 case MVT::v16i8:
15710 case MVT::v8i16:
15711 case MVT::v4i32:
15712 if (!Subtarget->hasSSE2())
15713 return 0;
15714 }
15715
15716 // SSE2 has only a small subset of the operations.
15717 bool hasUnsigned = Subtarget->hasSSE41() ||
15718 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15719 bool hasSigned = Subtarget->hasSSE41() ||
15720 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15721
15722 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15723
15724 // Check for x CC y ? x : y.
15725 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15726 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15727 switch (CC) {
15728 default: break;
15729 case ISD::SETULT:
15730 case ISD::SETULE:
15731 return hasUnsigned ? X86ISD::UMIN : 0;
15732 case ISD::SETUGT:
15733 case ISD::SETUGE:
15734 return hasUnsigned ? X86ISD::UMAX : 0;
15735 case ISD::SETLT:
15736 case ISD::SETLE:
15737 return hasSigned ? X86ISD::SMIN : 0;
15738 case ISD::SETGT:
15739 case ISD::SETGE:
15740 return hasSigned ? X86ISD::SMAX : 0;
15741 }
15742 // Check for x CC y ? y : x -- a min/max with reversed arms.
15743 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15744 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15745 switch (CC) {
15746 default: break;
15747 case ISD::SETULT:
15748 case ISD::SETULE:
15749 return hasUnsigned ? X86ISD::UMAX : 0;
15750 case ISD::SETUGT:
15751 case ISD::SETUGE:
15752 return hasUnsigned ? X86ISD::UMIN : 0;
15753 case ISD::SETLT:
15754 case ISD::SETLE:
15755 return hasSigned ? X86ISD::SMAX : 0;
15756 case ISD::SETGT:
15757 case ISD::SETGE:
15758 return hasSigned ? X86ISD::SMIN : 0;
15759 }
15760 }
15761
15762 return 0;
15763}
15764
Duncan Sands6bcd2192011-09-17 16:49:39 +000015765/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15766/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015767static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015768 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015769 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000015770 SDLoc DL(N);
Dan Gohman475871a2008-07-27 21:46:04 +000015771 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015772 // Get the LHS/RHS of the select.
15773 SDValue LHS = N->getOperand(1);
15774 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015775 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015776
Dan Gohman670e5392009-09-21 18:03:22 +000015777 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015778 // instructions match the semantics of the common C idiom x<y?x:y but not
15779 // x<=y?x:y, because of how they handle negative zero (which can be
15780 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015781 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15782 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015783 (Subtarget->hasSSE2() ||
15784 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015785 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015786
Chris Lattner47b4ce82009-03-11 05:48:52 +000015787 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015788 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015789 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15790 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015791 switch (CC) {
15792 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015793 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015794 // Converting this to a min would handle NaNs incorrectly, and swapping
15795 // the operands would cause it to handle comparisons between positive
15796 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015797 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015798 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015799 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15800 break;
15801 std::swap(LHS, RHS);
15802 }
Dan Gohman670e5392009-09-21 18:03:22 +000015803 Opcode = X86ISD::FMIN;
15804 break;
15805 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015806 // Converting this to a min would handle comparisons between positive
15807 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015808 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015809 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15810 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015811 Opcode = X86ISD::FMIN;
15812 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015813 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015814 // Converting this to a min would handle both negative zeros and NaNs
15815 // incorrectly, but we can swap the operands to fix both.
15816 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015817 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015818 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015819 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015820 Opcode = X86ISD::FMIN;
15821 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015822
Dan Gohman670e5392009-09-21 18:03:22 +000015823 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015824 // Converting this to a max would handle comparisons between positive
15825 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015826 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015827 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015828 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015829 Opcode = X86ISD::FMAX;
15830 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015831 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015832 // Converting this to a max would handle NaNs incorrectly, and swapping
15833 // the operands would cause it to handle comparisons between positive
15834 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015835 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015836 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015837 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15838 break;
15839 std::swap(LHS, RHS);
15840 }
Dan Gohman670e5392009-09-21 18:03:22 +000015841 Opcode = X86ISD::FMAX;
15842 break;
15843 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015844 // Converting this to a max would handle both negative zeros and NaNs
15845 // incorrectly, but we can swap the operands to fix both.
15846 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015847 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015848 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015849 case ISD::SETGE:
15850 Opcode = X86ISD::FMAX;
15851 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015852 }
Dan Gohman670e5392009-09-21 18:03:22 +000015853 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015854 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15855 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015856 switch (CC) {
15857 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015858 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015859 // Converting this to a min would handle comparisons between positive
15860 // and negative zero incorrectly, and swapping the operands would
15861 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015862 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015863 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015864 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015865 break;
15866 std::swap(LHS, RHS);
15867 }
Dan Gohman670e5392009-09-21 18:03:22 +000015868 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015869 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015870 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015871 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015872 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015873 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15874 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015875 Opcode = X86ISD::FMIN;
15876 break;
15877 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015878 // Converting this to a min would handle both negative zeros and NaNs
15879 // incorrectly, but we can swap the operands to fix both.
15880 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015881 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015882 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015883 case ISD::SETGE:
15884 Opcode = X86ISD::FMIN;
15885 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015886
Dan Gohman670e5392009-09-21 18:03:22 +000015887 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015888 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015889 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015890 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015891 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015892 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015893 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015894 // Converting this to a max would handle comparisons between positive
15895 // and negative zero incorrectly, and swapping the operands would
15896 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015897 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015898 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015899 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015900 break;
15901 std::swap(LHS, RHS);
15902 }
Dan Gohman670e5392009-09-21 18:03:22 +000015903 Opcode = X86ISD::FMAX;
15904 break;
15905 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015906 // Converting this to a max would handle both negative zeros and NaNs
15907 // incorrectly, but we can swap the operands to fix both.
15908 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015909 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015910 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015911 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015912 Opcode = X86ISD::FMAX;
15913 break;
15914 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015915 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015916
Chris Lattner47b4ce82009-03-11 05:48:52 +000015917 if (Opcode)
15918 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015919 }
Eric Christopherfd179292009-08-27 18:07:15 +000015920
Chris Lattnerd1980a52009-03-12 06:52:53 +000015921 // If this is a select between two integer constants, try to do some
15922 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015923 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15924 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015925 // Don't do this for crazy integer types.
15926 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15927 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015928 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015929 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015930
Chris Lattnercee56e72009-03-13 05:53:31 +000015931 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015932 // Efficiently invertible.
15933 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15934 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15935 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15936 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015937 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015938 }
Eric Christopherfd179292009-08-27 18:07:15 +000015939
Chris Lattnerd1980a52009-03-12 06:52:53 +000015940 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015941 if (FalseC->getAPIntValue() == 0 &&
15942 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015943 if (NeedsCondInvert) // Invert the condition if needed.
15944 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15945 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015946
Chris Lattnerd1980a52009-03-12 06:52:53 +000015947 // Zero extend the condition if needed.
15948 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015949
Chris Lattnercee56e72009-03-13 05:53:31 +000015950 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015951 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015952 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015953 }
Eric Christopherfd179292009-08-27 18:07:15 +000015954
Chris Lattner97a29a52009-03-13 05:22:11 +000015955 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015956 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015957 if (NeedsCondInvert) // Invert the condition if needed.
15958 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15959 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015960
Chris Lattner97a29a52009-03-13 05:22:11 +000015961 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015962 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15963 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015964 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015965 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015966 }
Eric Christopherfd179292009-08-27 18:07:15 +000015967
Chris Lattnercee56e72009-03-13 05:53:31 +000015968 // Optimize cases that will turn into an LEA instruction. This requires
15969 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015970 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015971 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015972 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015973
Chris Lattnercee56e72009-03-13 05:53:31 +000015974 bool isFastMultiplier = false;
15975 if (Diff < 10) {
15976 switch ((unsigned char)Diff) {
15977 default: break;
15978 case 1: // result = add base, cond
15979 case 2: // result = lea base( , cond*2)
15980 case 3: // result = lea base(cond, cond*2)
15981 case 4: // result = lea base( , cond*4)
15982 case 5: // result = lea base(cond, cond*4)
15983 case 8: // result = lea base( , cond*8)
15984 case 9: // result = lea base(cond, cond*8)
15985 isFastMultiplier = true;
15986 break;
15987 }
15988 }
Eric Christopherfd179292009-08-27 18:07:15 +000015989
Chris Lattnercee56e72009-03-13 05:53:31 +000015990 if (isFastMultiplier) {
15991 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15992 if (NeedsCondInvert) // Invert the condition if needed.
15993 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15994 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015995
Chris Lattnercee56e72009-03-13 05:53:31 +000015996 // Zero extend the condition if needed.
15997 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15998 Cond);
15999 // Scale the condition by the difference.
16000 if (Diff != 1)
16001 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
16002 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000016003
Chris Lattnercee56e72009-03-13 05:53:31 +000016004 // Add the base if non-zero.
16005 if (FalseC->getAPIntValue() != 0)
16006 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16007 SDValue(FalseC, 0));
16008 return Cond;
16009 }
Eric Christopherfd179292009-08-27 18:07:15 +000016010 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000016011 }
16012 }
Eric Christopherfd179292009-08-27 18:07:15 +000016013
Evan Cheng56f582d2012-01-04 01:41:39 +000016014 // Canonicalize max and min:
16015 // (x > y) ? x : y -> (x >= y) ? x : y
16016 // (x < y) ? x : y -> (x <= y) ? x : y
16017 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
16018 // the need for an extra compare
16019 // against zero. e.g.
16020 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
16021 // subl %esi, %edi
16022 // testl %edi, %edi
16023 // movl $0, %eax
16024 // cmovgl %edi, %eax
16025 // =>
16026 // xorl %eax, %eax
16027 // subl %esi, $edi
16028 // cmovsl %eax, %edi
16029 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
16030 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
16031 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
16032 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
16033 switch (CC) {
16034 default: break;
16035 case ISD::SETLT:
16036 case ISD::SETGT: {
16037 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
Andrew Trickac6d9be2013-05-25 02:42:55 +000016038 Cond = DAG.getSetCC(SDLoc(Cond), Cond.getValueType(),
Evan Cheng56f582d2012-01-04 01:41:39 +000016039 Cond.getOperand(0), Cond.getOperand(1), NewCC);
16040 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
16041 }
16042 }
16043 }
16044
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000016045 // Match VSELECTs into subs with unsigned saturation.
16046 if (!DCI.isBeforeLegalize() &&
16047 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
16048 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
16049 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
16050 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
16051 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
16052
16053 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
16054 // left side invert the predicate to simplify logic below.
16055 SDValue Other;
16056 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
16057 Other = RHS;
16058 CC = ISD::getSetCCInverse(CC, true);
16059 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
16060 Other = LHS;
16061 }
16062
16063 if (Other.getNode() && Other->getNumOperands() == 2 &&
16064 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
16065 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
16066 SDValue CondRHS = Cond->getOperand(1);
16067
16068 // Look for a general sub with unsigned saturation first.
16069 // x >= y ? x-y : 0 --> subus x, y
16070 // x > y ? x-y : 0 --> subus x, y
16071 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
16072 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
16073 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
16074
16075 // If the RHS is a constant we have to reverse the const canonicalization.
16076 // x > C-1 ? x+-C : 0 --> subus x, C
16077 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
16078 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
16079 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016080 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000016081 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016082 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000016083 }
16084
16085 // Another special case: If C was a sign bit, the sub has been
16086 // canonicalized into a xor.
16087 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
16088 // it's safe to decanonicalize the xor?
16089 // x s< 0 ? x^C : 0 --> subus x, C
16090 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
16091 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
16092 isSplatVector(OpRHS.getNode())) {
16093 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
16094 if (A.isSignBit())
16095 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
16096 }
16097 }
16098 }
16099
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000016100 // Try to match a min/max vector operation.
16101 if (!DCI.isBeforeLegalize() &&
16102 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
16103 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
16104 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
16105
Michael Liaobf538412013-04-11 05:15:54 +000016106 // Simplify vector selection if the selector will be produced by CMPP*/PCMP*.
16107 if (!DCI.isBeforeLegalize() && N->getOpcode() == ISD::VSELECT &&
16108 Cond.getOpcode() == ISD::SETCC) {
16109
16110 assert(Cond.getValueType().isVector() &&
16111 "vector select expects a vector selector!");
16112
16113 EVT IntVT = Cond.getValueType();
16114 bool TValIsAllOnes = ISD::isBuildVectorAllOnes(LHS.getNode());
16115 bool FValIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
16116
16117 if (!TValIsAllOnes && !FValIsAllZeros) {
16118 // Try invert the condition if true value is not all 1s and false value
16119 // is not all 0s.
16120 bool TValIsAllZeros = ISD::isBuildVectorAllZeros(LHS.getNode());
16121 bool FValIsAllOnes = ISD::isBuildVectorAllOnes(RHS.getNode());
16122
16123 if (TValIsAllZeros || FValIsAllOnes) {
16124 SDValue CC = Cond.getOperand(2);
16125 ISD::CondCode NewCC =
16126 ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
16127 Cond.getOperand(0).getValueType().isInteger());
16128 Cond = DAG.getSetCC(DL, IntVT, Cond.getOperand(0), Cond.getOperand(1), NewCC);
16129 std::swap(LHS, RHS);
16130 TValIsAllOnes = FValIsAllOnes;
16131 FValIsAllZeros = TValIsAllZeros;
16132 }
16133 }
16134
16135 if (TValIsAllOnes || FValIsAllZeros) {
16136 SDValue Ret;
16137
16138 if (TValIsAllOnes && FValIsAllZeros)
16139 Ret = Cond;
16140 else if (TValIsAllOnes)
16141 Ret = DAG.getNode(ISD::OR, DL, IntVT, Cond,
16142 DAG.getNode(ISD::BITCAST, DL, IntVT, RHS));
16143 else if (FValIsAllZeros)
16144 Ret = DAG.getNode(ISD::AND, DL, IntVT, Cond,
16145 DAG.getNode(ISD::BITCAST, DL, IntVT, LHS));
16146
16147 return DAG.getNode(ISD::BITCAST, DL, VT, Ret);
16148 }
16149 }
16150
Nadav Rotemcc616562012-01-15 19:27:55 +000016151 // If we know that this node is legal then we know that it is going to be
16152 // matched by one of the SSE/AVX BLEND instructions. These instructions only
16153 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
16154 // to simplify previous instructions.
16155 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16156 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000016157 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000016158 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000016159
16160 // Don't optimize vector selects that map to mask-registers.
16161 if (BitWidth == 1)
16162 return SDValue();
16163
Nadav Rotemcc616562012-01-15 19:27:55 +000016164 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
16165 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
16166
16167 APInt KnownZero, KnownOne;
16168 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
16169 DCI.isBeforeLegalizeOps());
16170 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
16171 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
16172 DCI.CommitTargetLoweringOpt(TLO);
16173 }
16174
Dan Gohman475871a2008-07-27 21:46:04 +000016175 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000016176}
16177
Michael Liao2a33cec2012-08-10 19:58:13 +000016178// Check whether a boolean test is testing a boolean value generated by
16179// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
16180// code.
16181//
16182// Simplify the following patterns:
16183// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
16184// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
16185// to (Op EFLAGS Cond)
16186//
16187// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
16188// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
16189// to (Op EFLAGS !Cond)
16190//
16191// where Op could be BRCOND or CMOV.
16192//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016193static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000016194 // Quit if not CMP and SUB with its value result used.
16195 if (Cmp.getOpcode() != X86ISD::CMP &&
16196 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
16197 return SDValue();
16198
16199 // Quit if not used as a boolean value.
16200 if (CC != X86::COND_E && CC != X86::COND_NE)
16201 return SDValue();
16202
16203 // Check CMP operands. One of them should be 0 or 1 and the other should be
16204 // an SetCC or extended from it.
16205 SDValue Op1 = Cmp.getOperand(0);
16206 SDValue Op2 = Cmp.getOperand(1);
16207
16208 SDValue SetCC;
16209 const ConstantSDNode* C = 0;
16210 bool needOppositeCond = (CC == X86::COND_E);
Michael Liao959ddbb2013-04-11 04:43:09 +000016211 bool checkAgainstTrue = false; // Is it a comparison against 1?
Michael Liao2a33cec2012-08-10 19:58:13 +000016212
16213 if ((C = dyn_cast<ConstantSDNode>(Op1)))
16214 SetCC = Op2;
16215 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
16216 SetCC = Op1;
16217 else // Quit if all operands are not constants.
16218 return SDValue();
16219
Michael Liao959ddbb2013-04-11 04:43:09 +000016220 if (C->getZExtValue() == 1) {
Michael Liao2a33cec2012-08-10 19:58:13 +000016221 needOppositeCond = !needOppositeCond;
Michael Liao959ddbb2013-04-11 04:43:09 +000016222 checkAgainstTrue = true;
16223 } else if (C->getZExtValue() != 0)
Michael Liao2a33cec2012-08-10 19:58:13 +000016224 // Quit if the constant is neither 0 or 1.
16225 return SDValue();
16226
Michael Liao959ddbb2013-04-11 04:43:09 +000016227 bool truncatedToBoolWithAnd = false;
16228 // Skip (zext $x), (trunc $x), or (and $x, 1) node.
16229 while (SetCC.getOpcode() == ISD::ZERO_EXTEND ||
16230 SetCC.getOpcode() == ISD::TRUNCATE ||
16231 SetCC.getOpcode() == ISD::AND) {
16232 if (SetCC.getOpcode() == ISD::AND) {
16233 int OpIdx = -1;
16234 ConstantSDNode *CS;
16235 if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(0))) &&
16236 CS->getZExtValue() == 1)
16237 OpIdx = 1;
16238 if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(1))) &&
16239 CS->getZExtValue() == 1)
16240 OpIdx = 0;
16241 if (OpIdx == -1)
16242 break;
16243 SetCC = SetCC.getOperand(OpIdx);
16244 truncatedToBoolWithAnd = true;
16245 } else
16246 SetCC = SetCC.getOperand(0);
16247 }
Michael Liao2a33cec2012-08-10 19:58:13 +000016248
Michael Liao7fdc66b2012-09-10 16:36:16 +000016249 switch (SetCC.getOpcode()) {
Michael Liao959ddbb2013-04-11 04:43:09 +000016250 case X86ISD::SETCC_CARRY:
16251 // Since SETCC_CARRY gives output based on R = CF ? ~0 : 0, it's unsafe to
16252 // simplify it if the result of SETCC_CARRY is not canonicalized to 0 or 1,
16253 // i.e. it's a comparison against true but the result of SETCC_CARRY is not
16254 // truncated to i1 using 'and'.
16255 if (checkAgainstTrue && !truncatedToBoolWithAnd)
16256 break;
16257 assert(X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B &&
16258 "Invalid use of SETCC_CARRY!");
16259 // FALL THROUGH
Michael Liao7fdc66b2012-09-10 16:36:16 +000016260 case X86ISD::SETCC:
16261 // Set the condition code or opposite one if necessary.
16262 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
16263 if (needOppositeCond)
16264 CC = X86::GetOppositeBranchCondition(CC);
16265 return SetCC.getOperand(1);
16266 case X86ISD::CMOV: {
16267 // Check whether false/true value has canonical one, i.e. 0 or 1.
16268 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
16269 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
16270 // Quit if true value is not a constant.
16271 if (!TVal)
16272 return SDValue();
16273 // Quit if false value is not a constant.
16274 if (!FVal) {
Michael Liao7fdc66b2012-09-10 16:36:16 +000016275 SDValue Op = SetCC.getOperand(0);
Michael Liao258d9b72013-03-28 23:38:52 +000016276 // Skip 'zext' or 'trunc' node.
16277 if (Op.getOpcode() == ISD::ZERO_EXTEND ||
16278 Op.getOpcode() == ISD::TRUNCATE)
16279 Op = Op.getOperand(0);
Michael Liaoc26392a2013-03-28 23:41:26 +000016280 // A special case for rdrand/rdseed, where 0 is set if false cond is
16281 // found.
16282 if ((Op.getOpcode() != X86ISD::RDRAND &&
16283 Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0)
Michael Liao7fdc66b2012-09-10 16:36:16 +000016284 return SDValue();
16285 }
16286 // Quit if false value is not the constant 0 or 1.
16287 bool FValIsFalse = true;
16288 if (FVal && FVal->getZExtValue() != 0) {
16289 if (FVal->getZExtValue() != 1)
16290 return SDValue();
16291 // If FVal is 1, opposite cond is needed.
16292 needOppositeCond = !needOppositeCond;
16293 FValIsFalse = false;
16294 }
16295 // Quit if TVal is not the constant opposite of FVal.
16296 if (FValIsFalse && TVal->getZExtValue() != 1)
16297 return SDValue();
16298 if (!FValIsFalse && TVal->getZExtValue() != 0)
16299 return SDValue();
16300 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
16301 if (needOppositeCond)
16302 CC = X86::GetOppositeBranchCondition(CC);
16303 return SetCC.getOperand(3);
16304 }
16305 }
Michael Liao2a33cec2012-08-10 19:58:13 +000016306
Michael Liao7fdc66b2012-09-10 16:36:16 +000016307 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000016308}
16309
Chris Lattnerd1980a52009-03-12 06:52:53 +000016310/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
16311static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016312 TargetLowering::DAGCombinerInfo &DCI,
16313 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000016314 SDLoc DL(N);
Eric Christopherfd179292009-08-27 18:07:15 +000016315
Chris Lattnerd1980a52009-03-12 06:52:53 +000016316 // If the flag operand isn't dead, don't touch this CMOV.
16317 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
16318 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000016319
Evan Chengb5a55d92011-05-24 01:48:22 +000016320 SDValue FalseOp = N->getOperand(0);
16321 SDValue TrueOp = N->getOperand(1);
16322 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
16323 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000016324
Evan Chengb5a55d92011-05-24 01:48:22 +000016325 if (CC == X86::COND_E || CC == X86::COND_NE) {
16326 switch (Cond.getOpcode()) {
16327 default: break;
16328 case X86ISD::BSR:
16329 case X86ISD::BSF:
16330 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
16331 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
16332 return (CC == X86::COND_E) ? FalseOp : TrueOp;
16333 }
16334 }
16335
Michael Liao2a33cec2012-08-10 19:58:13 +000016336 SDValue Flags;
16337
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016338 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000016339 if (Flags.getNode() &&
16340 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000016341 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016342 SDValue Ops[] = { FalseOp, TrueOp,
16343 DAG.getConstant(CC, MVT::i8), Flags };
16344 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
16345 Ops, array_lengthof(Ops));
16346 }
16347
Chris Lattnerd1980a52009-03-12 06:52:53 +000016348 // If this is a select between two integer constants, try to do some
16349 // optimizations. Note that the operands are ordered the opposite of SELECT
16350 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000016351 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
16352 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000016353 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
16354 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000016355 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
16356 CC = X86::GetOppositeBranchCondition(CC);
16357 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016358 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000016359 }
Eric Christopherfd179292009-08-27 18:07:15 +000016360
Chris Lattnerd1980a52009-03-12 06:52:53 +000016361 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000016362 // This is efficient for any integer data type (including i8/i16) and
16363 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000016364 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016365 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16366 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016367
Chris Lattnerd1980a52009-03-12 06:52:53 +000016368 // Zero extend the condition if needed.
16369 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016370
Chris Lattnerd1980a52009-03-12 06:52:53 +000016371 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
16372 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000016373 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000016374 if (N->getNumValues() == 2) // Dead flag value?
16375 return DCI.CombineTo(N, Cond, SDValue());
16376 return Cond;
16377 }
Eric Christopherfd179292009-08-27 18:07:15 +000016378
Chris Lattnercee56e72009-03-13 05:53:31 +000016379 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
16380 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000016381 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016382 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16383 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016384
Chris Lattner97a29a52009-03-13 05:22:11 +000016385 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000016386 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
16387 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000016388 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16389 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000016390
Chris Lattner97a29a52009-03-13 05:22:11 +000016391 if (N->getNumValues() == 2) // Dead flag value?
16392 return DCI.CombineTo(N, Cond, SDValue());
16393 return Cond;
16394 }
Eric Christopherfd179292009-08-27 18:07:15 +000016395
Chris Lattnercee56e72009-03-13 05:53:31 +000016396 // Optimize cases that will turn into an LEA instruction. This requires
16397 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000016398 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000016399 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000016400 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000016401
Chris Lattnercee56e72009-03-13 05:53:31 +000016402 bool isFastMultiplier = false;
16403 if (Diff < 10) {
16404 switch ((unsigned char)Diff) {
16405 default: break;
16406 case 1: // result = add base, cond
16407 case 2: // result = lea base( , cond*2)
16408 case 3: // result = lea base(cond, cond*2)
16409 case 4: // result = lea base( , cond*4)
16410 case 5: // result = lea base(cond, cond*4)
16411 case 8: // result = lea base( , cond*8)
16412 case 9: // result = lea base(cond, cond*8)
16413 isFastMultiplier = true;
16414 break;
16415 }
16416 }
Eric Christopherfd179292009-08-27 18:07:15 +000016417
Chris Lattnercee56e72009-03-13 05:53:31 +000016418 if (isFastMultiplier) {
16419 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000016420 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16421 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000016422 // Zero extend the condition if needed.
16423 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
16424 Cond);
16425 // Scale the condition by the difference.
16426 if (Diff != 1)
16427 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
16428 DAG.getConstant(Diff, Cond.getValueType()));
16429
16430 // Add the base if non-zero.
16431 if (FalseC->getAPIntValue() != 0)
16432 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16433 SDValue(FalseC, 0));
16434 if (N->getNumValues() == 2) // Dead flag value?
16435 return DCI.CombineTo(N, Cond, SDValue());
16436 return Cond;
16437 }
Eric Christopherfd179292009-08-27 18:07:15 +000016438 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000016439 }
16440 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016441
16442 // Handle these cases:
16443 // (select (x != c), e, c) -> select (x != c), e, x),
16444 // (select (x == c), c, e) -> select (x == c), x, e)
16445 // where the c is an integer constant, and the "select" is the combination
16446 // of CMOV and CMP.
16447 //
16448 // The rationale for this change is that the conditional-move from a constant
16449 // needs two instructions, however, conditional-move from a register needs
16450 // only one instruction.
16451 //
16452 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
16453 // some instruction-combining opportunities. This opt needs to be
16454 // postponed as late as possible.
16455 //
16456 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
16457 // the DCI.xxxx conditions are provided to postpone the optimization as
16458 // late as possible.
16459
16460 ConstantSDNode *CmpAgainst = 0;
16461 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
16462 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016463 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016464
16465 if (CC == X86::COND_NE &&
16466 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
16467 CC = X86::GetOppositeBranchCondition(CC);
16468 std::swap(TrueOp, FalseOp);
16469 }
16470
16471 if (CC == X86::COND_E &&
16472 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
16473 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
16474 DAG.getConstant(CC, MVT::i8), Cond };
16475 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
16476 array_lengthof(Ops));
16477 }
16478 }
16479 }
16480
Chris Lattnerd1980a52009-03-12 06:52:53 +000016481 return SDValue();
16482}
16483
Evan Cheng0b0cd912009-03-28 05:57:29 +000016484/// PerformMulCombine - Optimize a single multiply with constant into two
16485/// in order to implement it with two cheaper instructions, e.g.
16486/// LEA + SHL, LEA + LEA.
16487static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
16488 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000016489 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
16490 return SDValue();
16491
Owen Andersone50ed302009-08-10 22:56:29 +000016492 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000016493 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000016494 return SDValue();
16495
16496 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
16497 if (!C)
16498 return SDValue();
16499 uint64_t MulAmt = C->getZExtValue();
16500 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
16501 return SDValue();
16502
16503 uint64_t MulAmt1 = 0;
16504 uint64_t MulAmt2 = 0;
16505 if ((MulAmt % 9) == 0) {
16506 MulAmt1 = 9;
16507 MulAmt2 = MulAmt / 9;
16508 } else if ((MulAmt % 5) == 0) {
16509 MulAmt1 = 5;
16510 MulAmt2 = MulAmt / 5;
16511 } else if ((MulAmt % 3) == 0) {
16512 MulAmt1 = 3;
16513 MulAmt2 = MulAmt / 3;
16514 }
16515 if (MulAmt2 &&
16516 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
Andrew Trickac6d9be2013-05-25 02:42:55 +000016517 SDLoc DL(N);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016518
16519 if (isPowerOf2_64(MulAmt2) &&
16520 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
16521 // If second multiplifer is pow2, issue it first. We want the multiply by
16522 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
16523 // is an add.
16524 std::swap(MulAmt1, MulAmt2);
16525
16526 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000016527 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016528 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000016529 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000016530 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016531 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000016532 DAG.getConstant(MulAmt1, VT));
16533
Eric Christopherfd179292009-08-27 18:07:15 +000016534 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016535 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000016536 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000016537 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016538 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000016539 DAG.getConstant(MulAmt2, VT));
16540
16541 // Do not add new nodes to DAG combiner worklist.
16542 DCI.CombineTo(N, NewMul, false);
16543 }
16544 return SDValue();
16545}
16546
Evan Chengad9c0a32009-12-15 00:53:42 +000016547static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
16548 SDValue N0 = N->getOperand(0);
16549 SDValue N1 = N->getOperand(1);
16550 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
16551 EVT VT = N0.getValueType();
16552
16553 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
16554 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016555 if (VT.isInteger() && !VT.isVector() &&
16556 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000016557 N0.getOperand(1).getOpcode() == ISD::Constant) {
16558 SDValue N00 = N0.getOperand(0);
16559 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
16560 ((N00.getOpcode() == ISD::ANY_EXTEND ||
16561 N00.getOpcode() == ISD::ZERO_EXTEND) &&
16562 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
16563 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
16564 APInt ShAmt = N1C->getAPIntValue();
16565 Mask = Mask.shl(ShAmt);
16566 if (Mask != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +000016567 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Evan Chengad9c0a32009-12-15 00:53:42 +000016568 N00, DAG.getConstant(Mask, VT));
16569 }
16570 }
16571
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016572 // Hardware support for vector shifts is sparse which makes us scalarize the
16573 // vector operations in many cases. Also, on sandybridge ADD is faster than
16574 // shl.
16575 // (shl V, 1) -> add V,V
16576 if (isSplatVector(N1.getNode())) {
16577 assert(N0.getValueType().isVector() && "Invalid vector shift type");
16578 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
16579 // We shift all of the values by one. In many cases we do not have
16580 // hardware support for this operation. This is better expressed as an ADD
16581 // of two values.
16582 if (N1C && (1 == N1C->getZExtValue())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000016583 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016584 }
16585 }
16586
Evan Chengad9c0a32009-12-15 00:53:42 +000016587 return SDValue();
16588}
Evan Cheng0b0cd912009-03-28 05:57:29 +000016589
Stephen Linfff96732013-07-12 15:31:36 +000016590/// \brief Returns a vector of 0s if the node in input is a vector logical
16591/// shift by a constant amount which is known to be bigger than or equal
16592/// to the vector element size in bits.
16593static SDValue performShiftToAllZeros(SDNode *N, SelectionDAG &DAG,
16594 const X86Subtarget *Subtarget) {
16595 EVT VT = N->getValueType(0);
16596
16597 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
16598 (!Subtarget->hasInt256() ||
16599 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
16600 return SDValue();
16601
16602 SDValue Amt = N->getOperand(1);
16603 SDLoc DL(N);
16604 if (isSplatVector(Amt.getNode())) {
16605 SDValue SclrAmt = Amt->getOperand(0);
16606 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
16607 APInt ShiftAmt = C->getAPIntValue();
16608 unsigned MaxAmount = VT.getVectorElementType().getSizeInBits();
16609
16610 // SSE2/AVX2 logical shifts always return a vector of 0s
16611 // if the shift amount is bigger than or equal to
16612 // the element size. The constant shift amount will be
16613 // encoded as a 8-bit immediate.
16614 if (ShiftAmt.trunc(8).uge(MaxAmount))
16615 return getZeroVector(VT, Subtarget, DAG, DL);
16616 }
16617 }
16618
16619 return SDValue();
16620}
16621
Nadav Rotem0fb65232013-05-04 23:24:56 +000016622/// PerformShiftCombine - Combine shifts.
Nate Begeman740ab032009-01-26 00:52:55 +000016623static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000016624 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000016625 const X86Subtarget *Subtarget) {
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016626 if (N->getOpcode() == ISD::SHL) {
16627 SDValue V = PerformSHLCombine(N, DAG);
16628 if (V.getNode()) return V;
16629 }
Evan Chengad9c0a32009-12-15 00:53:42 +000016630
Stephen Linfff96732013-07-12 15:31:36 +000016631 if (N->getOpcode() != ISD::SRA) {
16632 // Try to fold this logical shift into a zero vector.
16633 SDValue V = performShiftToAllZeros(N, DAG, Subtarget);
16634 if (V.getNode()) return V;
16635 }
16636
Michael Liao42317cc2013-03-20 02:33:21 +000016637 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016638}
16639
Stuart Hastings865f0932011-06-03 23:53:54 +000016640// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16641// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16642// and friends. Likewise for OR -> CMPNEQSS.
16643static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16644 TargetLowering::DAGCombinerInfo &DCI,
16645 const X86Subtarget *Subtarget) {
16646 unsigned opcode;
16647
16648 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16649 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016650 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016651 SDValue N0 = N->getOperand(0);
16652 SDValue N1 = N->getOperand(1);
16653 SDValue CMP0 = N0->getOperand(1);
16654 SDValue CMP1 = N1->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016655 SDLoc DL(N);
Stuart Hastings865f0932011-06-03 23:53:54 +000016656
16657 // The SETCCs should both refer to the same CMP.
16658 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16659 return SDValue();
16660
16661 SDValue CMP00 = CMP0->getOperand(0);
16662 SDValue CMP01 = CMP0->getOperand(1);
16663 EVT VT = CMP00.getValueType();
16664
16665 if (VT == MVT::f32 || VT == MVT::f64) {
16666 bool ExpectingFlags = false;
16667 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016668 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016669 !ExpectingFlags && UI != UE; ++UI)
16670 switch (UI->getOpcode()) {
16671 default:
16672 case ISD::BR_CC:
16673 case ISD::BRCOND:
16674 case ISD::SELECT:
16675 ExpectingFlags = true;
16676 break;
16677 case ISD::CopyToReg:
16678 case ISD::SIGN_EXTEND:
16679 case ISD::ZERO_EXTEND:
16680 case ISD::ANY_EXTEND:
16681 break;
16682 }
16683
16684 if (!ExpectingFlags) {
16685 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16686 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16687
16688 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16689 X86::CondCode tmp = cc0;
16690 cc0 = cc1;
16691 cc1 = tmp;
16692 }
16693
16694 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16695 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16696 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16697 X86ISD::NodeType NTOperator = is64BitFP ?
16698 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16699 // FIXME: need symbolic constants for these magic numbers.
16700 // See X86ATTInstPrinter.cpp:printSSECC().
16701 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16702 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16703 DAG.getConstant(x86cc, MVT::i8));
16704 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16705 OnesOrZeroesF);
16706 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16707 DAG.getConstant(1, MVT::i32));
16708 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16709 return OneBitOfTruth;
16710 }
16711 }
16712 }
16713 }
16714 return SDValue();
16715}
16716
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016717/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16718/// so it can be folded inside ANDNP.
16719static bool CanFoldXORWithAllOnes(const SDNode *N) {
16720 EVT VT = N->getValueType(0);
16721
16722 // Match direct AllOnes for 128 and 256-bit vectors
16723 if (ISD::isBuildVectorAllOnes(N))
16724 return true;
16725
16726 // Look through a bit convert.
16727 if (N->getOpcode() == ISD::BITCAST)
16728 N = N->getOperand(0).getNode();
16729
16730 // Sometimes the operand may come from a insert_subvector building a 256-bit
16731 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016732 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016733 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16734 SDValue V1 = N->getOperand(0);
16735 SDValue V2 = N->getOperand(1);
16736
16737 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16738 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16739 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16740 ISD::isBuildVectorAllOnes(V2.getNode()))
16741 return true;
16742 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016743
16744 return false;
16745}
16746
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016747// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16748// register. In most cases we actually compare or select YMM-sized registers
16749// and mixing the two types creates horrible code. This method optimizes
16750// some of the transition sequences.
16751static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16752 TargetLowering::DAGCombinerInfo &DCI,
16753 const X86Subtarget *Subtarget) {
16754 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016755 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016756 return SDValue();
16757
16758 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16759 N->getOpcode() == ISD::ZERO_EXTEND ||
16760 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16761
16762 SDValue Narrow = N->getOperand(0);
16763 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016764 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016765 return SDValue();
16766
16767 if (Narrow->getOpcode() != ISD::XOR &&
16768 Narrow->getOpcode() != ISD::AND &&
16769 Narrow->getOpcode() != ISD::OR)
16770 return SDValue();
16771
16772 SDValue N0 = Narrow->getOperand(0);
16773 SDValue N1 = Narrow->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016774 SDLoc DL(Narrow);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016775
16776 // The Left side has to be a trunc.
16777 if (N0.getOpcode() != ISD::TRUNCATE)
16778 return SDValue();
16779
16780 // The type of the truncated inputs.
16781 EVT WideVT = N0->getOperand(0)->getValueType(0);
16782 if (WideVT != VT)
16783 return SDValue();
16784
16785 // The right side has to be a 'trunc' or a constant vector.
16786 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16787 bool RHSConst = (isSplatVector(N1.getNode()) &&
16788 isa<ConstantSDNode>(N1->getOperand(0)));
16789 if (!RHSTrunc && !RHSConst)
16790 return SDValue();
16791
16792 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16793
16794 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16795 return SDValue();
16796
16797 // Set N0 and N1 to hold the inputs to the new wide operation.
16798 N0 = N0->getOperand(0);
16799 if (RHSConst) {
16800 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16801 N1->getOperand(0));
16802 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16803 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16804 } else if (RHSTrunc) {
16805 N1 = N1->getOperand(0);
16806 }
16807
16808 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016809 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016810 unsigned Opcode = N->getOpcode();
16811 switch (Opcode) {
16812 case ISD::ANY_EXTEND:
16813 return Op;
16814 case ISD::ZERO_EXTEND: {
16815 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16816 APInt Mask = APInt::getAllOnesValue(InBits);
16817 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16818 return DAG.getNode(ISD::AND, DL, VT,
16819 Op, DAG.getConstant(Mask, VT));
16820 }
16821 case ISD::SIGN_EXTEND:
16822 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16823 Op, DAG.getValueType(NarrowVT));
16824 default:
16825 llvm_unreachable("Unexpected opcode");
16826 }
16827}
16828
Nate Begemanb65c1752010-12-17 22:55:37 +000016829static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16830 TargetLowering::DAGCombinerInfo &DCI,
16831 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016832 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016833 if (DCI.isBeforeLegalizeOps())
16834 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016835
Stuart Hastings865f0932011-06-03 23:53:54 +000016836 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16837 if (R.getNode())
16838 return R;
16839
Craig Topperb926afc2012-12-17 05:12:30 +000016840 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016841 // BLSI is X & (-X)
16842 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016843 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16844 SDValue N0 = N->getOperand(0);
16845 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016846 SDLoc DL(N);
Craig Topper54a11172011-10-14 07:06:56 +000016847
Craig Topperb4c94572011-10-21 06:55:01 +000016848 // Check LHS for neg
16849 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16850 isZero(N0.getOperand(0)))
16851 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16852
16853 // Check RHS for neg
16854 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16855 isZero(N1.getOperand(0)))
16856 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16857
16858 // Check LHS for X-1
16859 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16860 isAllOnes(N0.getOperand(1)))
16861 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16862
16863 // Check RHS for X-1
16864 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16865 isAllOnes(N1.getOperand(1)))
16866 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16867
Craig Topper54a11172011-10-14 07:06:56 +000016868 return SDValue();
16869 }
16870
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016871 // Want to form ANDNP nodes:
16872 // 1) In the hopes of then easily combining them with OR and AND nodes
16873 // to form PBLEND/PSIGN.
16874 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016875 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016876 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016877
Nate Begemanb65c1752010-12-17 22:55:37 +000016878 SDValue N0 = N->getOperand(0);
16879 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016880 SDLoc DL(N);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016881
Nate Begemanb65c1752010-12-17 22:55:37 +000016882 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016883 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016884 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16885 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016886 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016887
16888 // Check RHS for vnot
16889 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016890 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16891 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016892 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016893
Nate Begemanb65c1752010-12-17 22:55:37 +000016894 return SDValue();
16895}
16896
Evan Cheng760d1942010-01-04 21:22:48 +000016897static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016898 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016899 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016900 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016901 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016902 return SDValue();
16903
Stuart Hastings865f0932011-06-03 23:53:54 +000016904 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16905 if (R.getNode())
16906 return R;
16907
Evan Cheng760d1942010-01-04 21:22:48 +000016908 SDValue N0 = N->getOperand(0);
16909 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016910
Nate Begemanb65c1752010-12-17 22:55:37 +000016911 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016912 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016913 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016914 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016915 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016916
Craig Topper1666cb62011-11-19 07:07:26 +000016917 // Canonicalize pandn to RHS
16918 if (N0.getOpcode() == X86ISD::ANDNP)
16919 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016920 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016921 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16922 SDValue Mask = N1.getOperand(0);
16923 SDValue X = N1.getOperand(1);
16924 SDValue Y;
16925 if (N0.getOperand(0) == Mask)
16926 Y = N0.getOperand(1);
16927 if (N0.getOperand(1) == Mask)
16928 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016929
Craig Topper1666cb62011-11-19 07:07:26 +000016930 // Check to see if the mask appeared in both the AND and ANDNP and
16931 if (!Y.getNode())
16932 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016933
Craig Topper1666cb62011-11-19 07:07:26 +000016934 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016935 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016936 if (Mask.getOpcode() == ISD::BITCAST)
16937 Mask = Mask.getOperand(0);
16938 if (X.getOpcode() == ISD::BITCAST)
16939 X = X.getOperand(0);
16940 if (Y.getOpcode() == ISD::BITCAST)
16941 Y = Y.getOperand(0);
16942
Craig Topper1666cb62011-11-19 07:07:26 +000016943 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016944
Craig Toppered2e13d2012-01-22 19:15:14 +000016945 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016946 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16947 // there is no psrai.b
Craig Topper1666cb62011-11-19 07:07:26 +000016948 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
Michael Liao42317cc2013-03-20 02:33:21 +000016949 unsigned SraAmt = ~0;
16950 if (Mask.getOpcode() == ISD::SRA) {
16951 SDValue Amt = Mask.getOperand(1);
16952 if (isSplatVector(Amt.getNode())) {
16953 SDValue SclrAmt = Amt->getOperand(0);
16954 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt))
16955 SraAmt = C->getZExtValue();
16956 }
16957 } else if (Mask.getOpcode() == X86ISD::VSRAI) {
16958 SDValue SraC = Mask.getOperand(1);
16959 SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16960 }
Craig Topper1666cb62011-11-19 07:07:26 +000016961 if ((SraAmt + 1) != EltBits)
16962 return SDValue();
16963
Andrew Trickac6d9be2013-05-25 02:42:55 +000016964 SDLoc DL(N);
Craig Topper1666cb62011-11-19 07:07:26 +000016965
16966 // Now we know we at least have a plendvb with the mask val. See if
16967 // we can form a psignb/w/d.
16968 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016969 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16970 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016971 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16972 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16973 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016974 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016975 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016976 }
16977 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016978 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016979 return SDValue();
16980
16981 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16982
16983 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16984 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16985 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016986 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016987 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016988 }
16989 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016990
Craig Topper1666cb62011-11-19 07:07:26 +000016991 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16992 return SDValue();
16993
Nate Begemanb65c1752010-12-17 22:55:37 +000016994 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016995 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16996 std::swap(N0, N1);
16997 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16998 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016999 if (!N0.hasOneUse() || !N1.hasOneUse())
17000 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000017001
17002 SDValue ShAmt0 = N0.getOperand(1);
17003 if (ShAmt0.getValueType() != MVT::i8)
17004 return SDValue();
17005 SDValue ShAmt1 = N1.getOperand(1);
17006 if (ShAmt1.getValueType() != MVT::i8)
17007 return SDValue();
17008 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
17009 ShAmt0 = ShAmt0.getOperand(0);
17010 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
17011 ShAmt1 = ShAmt1.getOperand(0);
17012
Andrew Trickac6d9be2013-05-25 02:42:55 +000017013 SDLoc DL(N);
Evan Cheng760d1942010-01-04 21:22:48 +000017014 unsigned Opc = X86ISD::SHLD;
17015 SDValue Op0 = N0.getOperand(0);
17016 SDValue Op1 = N1.getOperand(0);
17017 if (ShAmt0.getOpcode() == ISD::SUB) {
17018 Opc = X86ISD::SHRD;
17019 std::swap(Op0, Op1);
17020 std::swap(ShAmt0, ShAmt1);
17021 }
17022
Evan Cheng8b1190a2010-04-28 01:18:01 +000017023 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000017024 if (ShAmt1.getOpcode() == ISD::SUB) {
17025 SDValue Sum = ShAmt1.getOperand(0);
17026 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000017027 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
17028 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
17029 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
17030 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000017031 return DAG.getNode(Opc, DL, VT,
17032 Op0, Op1,
17033 DAG.getNode(ISD::TRUNCATE, DL,
17034 MVT::i8, ShAmt0));
17035 }
17036 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
17037 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
17038 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000017039 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000017040 return DAG.getNode(Opc, DL, VT,
17041 N0.getOperand(0), N1.getOperand(0),
17042 DAG.getNode(ISD::TRUNCATE, DL,
17043 MVT::i8, ShAmt0));
17044 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017045
Evan Cheng760d1942010-01-04 21:22:48 +000017046 return SDValue();
17047}
17048
Manman Ren92363622012-06-07 22:39:10 +000017049// Generate NEG and CMOV for integer abs.
17050static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
17051 EVT VT = N->getValueType(0);
17052
17053 // Since X86 does not have CMOV for 8-bit integer, we don't convert
17054 // 8-bit integer abs to NEG and CMOV.
17055 if (VT.isInteger() && VT.getSizeInBits() == 8)
17056 return SDValue();
17057
17058 SDValue N0 = N->getOperand(0);
17059 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000017060 SDLoc DL(N);
Manman Ren92363622012-06-07 22:39:10 +000017061
17062 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
17063 // and change it to SUB and CMOV.
17064 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
17065 N0.getOpcode() == ISD::ADD &&
17066 N0.getOperand(1) == N1 &&
17067 N1.getOpcode() == ISD::SRA &&
17068 N1.getOperand(0) == N0.getOperand(0))
17069 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
17070 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
17071 // Generate SUB & CMOV.
17072 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
17073 DAG.getConstant(0, VT), N0.getOperand(0));
17074
17075 SDValue Ops[] = { N0.getOperand(0), Neg,
17076 DAG.getConstant(X86::COND_GE, MVT::i8),
17077 SDValue(Neg.getNode(), 1) };
17078 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
17079 Ops, array_lengthof(Ops));
17080 }
17081 return SDValue();
17082}
17083
Craig Topper3738ccd2011-12-27 06:27:23 +000017084// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000017085static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
17086 TargetLowering::DAGCombinerInfo &DCI,
17087 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017088 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000017089 if (DCI.isBeforeLegalizeOps())
17090 return SDValue();
17091
Manman Ren45d53b82012-06-08 18:58:26 +000017092 if (Subtarget->hasCMov()) {
17093 SDValue RV = performIntegerAbsCombine(N, DAG);
17094 if (RV.getNode())
17095 return RV;
17096 }
Manman Ren92363622012-06-07 22:39:10 +000017097
17098 // Try forming BMI if it is available.
17099 if (!Subtarget->hasBMI())
17100 return SDValue();
17101
Craig Topperb4c94572011-10-21 06:55:01 +000017102 if (VT != MVT::i32 && VT != MVT::i64)
17103 return SDValue();
17104
Craig Topper3738ccd2011-12-27 06:27:23 +000017105 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
17106
Craig Topperb4c94572011-10-21 06:55:01 +000017107 // Create BLSMSK instructions by finding X ^ (X-1)
17108 SDValue N0 = N->getOperand(0);
17109 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000017110 SDLoc DL(N);
Craig Topperb4c94572011-10-21 06:55:01 +000017111
17112 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
17113 isAllOnes(N0.getOperand(1)))
17114 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
17115
17116 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
17117 isAllOnes(N1.getOperand(1)))
17118 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
17119
17120 return SDValue();
17121}
17122
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017123/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
17124static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017125 TargetLowering::DAGCombinerInfo &DCI,
17126 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017127 LoadSDNode *Ld = cast<LoadSDNode>(N);
17128 EVT RegVT = Ld->getValueType(0);
17129 EVT MemVT = Ld->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000017130 SDLoc dl(Ld);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017131 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000017132 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017133
Michael Liaod4584c92013-03-25 23:50:10 +000017134 // On Sandybridge unaligned 256bit loads are inefficient.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017135 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000017136 unsigned Alignment = Ld->getAlignment();
Michael Liaod4584c92013-03-25 23:50:10 +000017137 bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000017138 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000017139 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000017140 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000017141 if (NumElems < 2)
17142 return SDValue();
17143
Nadav Rotem48177ac2013-01-18 23:10:30 +000017144 SDValue Ptr = Ld->getBasePtr();
17145 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
17146
17147 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
17148 NumElems/2);
17149 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
17150 Ld->getPointerInfo(), Ld->isVolatile(),
17151 Ld->isNonTemporal(), Ld->isInvariant(),
17152 Alignment);
17153 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
17154 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
17155 Ld->getPointerInfo(), Ld->isVolatile(),
17156 Ld->isNonTemporal(), Ld->isInvariant(),
Michael Liaod4584c92013-03-25 23:50:10 +000017157 std::min(16U, Alignment));
Nadav Rotem48177ac2013-01-18 23:10:30 +000017158 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
17159 Load1.getValue(1),
17160 Load2.getValue(1));
17161
17162 SDValue NewVec = DAG.getUNDEF(RegVT);
17163 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
17164 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
17165 return DCI.CombineTo(N, NewVec, TF, true);
17166 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017167
Nadav Rotemca6f2962011-09-18 19:00:23 +000017168 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000017169 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
17170 // expansion is still better than scalar code.
17171 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
17172 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017173 // TODO: It is possible to support ZExt by zeroing the undef values
17174 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000017175 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
17176 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017177 assert(MemVT != RegVT && "Cannot extend to the same type");
17178 assert(MemVT.isVector() && "Must load a vector from memory");
17179
17180 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017181 unsigned MemSz = MemVT.getSizeInBits();
17182 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017183
Elena Demikhovsky4b977312012-12-19 07:50:20 +000017184 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
17185 return SDValue();
17186
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017187 // All sizes must be a power of two.
17188 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
17189 return SDValue();
17190
17191 // Attempt to load the original value using scalar loads.
17192 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017193 MVT SclrLoadTy = MVT::i8;
17194 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
17195 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
17196 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017197 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017198 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017199 }
17200 }
17201
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017202 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
17203 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
17204 (64 <= MemSz))
17205 SclrLoadTy = MVT::f64;
17206
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017207 // Calculate the number of scalar loads that we need to perform
17208 // in order to load our vector from memory.
17209 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000017210 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
17211 return SDValue();
17212
17213 unsigned loadRegZize = RegSz;
17214 if (Ext == ISD::SEXTLOAD && RegSz == 256)
17215 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017216
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017217 // Represent our vector as a sequence of elements which are the
17218 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017219 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000017220 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017221
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017222 // Represent the data using the same element type that is stored in
17223 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000017224 EVT WideVecVT =
17225 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000017226 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017227
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017228 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
17229 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017230
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017231 // We can't shuffle using an illegal type.
17232 if (!TLI.isTypeLegal(WideVecVT))
17233 return SDValue();
17234
17235 SmallVector<SDValue, 8> Chains;
17236 SDValue Ptr = Ld->getBasePtr();
17237 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
17238 TLI.getPointerTy());
17239 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
17240
17241 for (unsigned i = 0; i < NumLoads; ++i) {
17242 // Perform a single load.
17243 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
17244 Ptr, Ld->getPointerInfo(),
17245 Ld->isVolatile(), Ld->isNonTemporal(),
17246 Ld->isInvariant(), Ld->getAlignment());
17247 Chains.push_back(ScalarLoad.getValue(1));
17248 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
17249 // another round of DAGCombining.
17250 if (i == 0)
17251 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
17252 else
17253 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
17254 ScalarLoad, DAG.getIntPtrConstant(i));
17255
17256 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
17257 }
17258
17259 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
17260 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017261
17262 // Bitcast the loaded value to a vector of the original element type, in
17263 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017264 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017265 unsigned SizeRatio = RegSz/MemSz;
17266
Elena Demikhovsky4b977312012-12-19 07:50:20 +000017267 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000017268 // If we have SSE4.1 we can directly emit a VSEXT node.
17269 if (Subtarget->hasSSE41()) {
17270 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
17271 return DCI.CombineTo(N, Sext, TF, true);
17272 }
17273
17274 // Otherwise we'll shuffle the small elements in the high bits of the
17275 // larger type and perform an arithmetic shift. If the shift is not legal
17276 // it's better to scalarize.
17277 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
17278 return SDValue();
17279
17280 // Redistribute the loaded elements into the different locations.
17281 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
17282 for (unsigned i = 0; i != NumElems; ++i)
17283 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
17284
17285 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
17286 DAG.getUNDEF(WideVecVT),
17287 &ShuffleVec[0]);
17288
17289 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
17290
17291 // Build the arithmetic shift.
17292 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
17293 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000017294 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
17295 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000017296
17297 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000017298 }
Benjamin Kramer17347912012-12-22 11:34:28 +000017299
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017300 // Redistribute the loaded elements into the different locations.
17301 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000017302 for (unsigned i = 0; i != NumElems; ++i)
17303 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017304
17305 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000017306 DAG.getUNDEF(WideVecVT),
17307 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017308
17309 // Bitcast to the requested type.
17310 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
17311 // Replace the original load with the new sequence
17312 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017313 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017314 }
17315
17316 return SDValue();
17317}
17318
Chris Lattner149a4e52008-02-22 02:09:43 +000017319/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017320static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000017321 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000017322 StoreSDNode *St = cast<StoreSDNode>(N);
17323 EVT VT = St->getValue().getValueType();
17324 EVT StVT = St->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000017325 SDLoc dl(St);
Nadav Rotem5e742a32011-08-11 16:41:21 +000017326 SDValue StoredVal = St->getOperand(1);
17327 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
17328
Nick Lewycky8a8d4792011-12-02 22:16:29 +000017329 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000017330 // On Sandy Bridge, 256-bit memory operations are executed by two
17331 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
17332 // memory operation.
Michael Liaod4584c92013-03-25 23:50:10 +000017333 unsigned Alignment = St->getAlignment();
17334 bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017335 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000017336 StVT == VT && !IsAligned) {
17337 unsigned NumElems = VT.getVectorNumElements();
17338 if (NumElems < 2)
17339 return SDValue();
17340
17341 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
17342 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000017343
17344 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
17345 SDValue Ptr0 = St->getBasePtr();
17346 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
17347
17348 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
17349 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000017350 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000017351 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
17352 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000017353 St->isNonTemporal(),
Michael Liaod4584c92013-03-25 23:50:10 +000017354 std::min(16U, Alignment));
Nadav Rotem5e742a32011-08-11 16:41:21 +000017355 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
17356 }
Nadav Rotem614061b2011-08-10 19:30:14 +000017357
17358 // Optimize trunc store (of multiple scalars) to shuffle and store.
17359 // First, pack all of the elements in one place. Next, store to memory
17360 // in fewer chunks.
17361 if (St->isTruncatingStore() && VT.isVector()) {
17362 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
17363 unsigned NumElems = VT.getVectorNumElements();
17364 assert(StVT != VT && "Cannot truncate to the same type");
17365 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
17366 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
17367
17368 // From, To sizes and ElemCount must be pow of two
17369 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000017370 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000017371 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000017372 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017373
Nadav Rotem614061b2011-08-10 19:30:14 +000017374 unsigned SizeRatio = FromSz / ToSz;
17375
17376 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
17377
17378 // Create a type on which we perform the shuffle
17379 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
17380 StVT.getScalarType(), NumElems*SizeRatio);
17381
17382 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
17383
17384 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
17385 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000017386 for (unsigned i = 0; i != NumElems; ++i)
17387 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000017388
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017389 // Can't shuffle using an illegal type.
17390 if (!TLI.isTypeLegal(WideVecVT))
17391 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000017392
17393 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000017394 DAG.getUNDEF(WideVecVT),
17395 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000017396 // At this point all of the data is stored at the bottom of the
17397 // register. We now need to save it to mem.
17398
17399 // Find the largest store unit
17400 MVT StoreType = MVT::i8;
17401 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
17402 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
17403 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017404 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000017405 StoreType = Tp;
17406 }
17407
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017408 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
17409 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
17410 (64 <= NumElems * ToSz))
17411 StoreType = MVT::f64;
17412
Nadav Rotem614061b2011-08-10 19:30:14 +000017413 // Bitcast the original vector into a vector of store-size units
17414 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017415 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000017416 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
17417 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
17418 SmallVector<SDValue, 8> Chains;
17419 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
17420 TLI.getPointerTy());
17421 SDValue Ptr = St->getBasePtr();
17422
17423 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000017424 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000017425 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
17426 StoreType, ShuffWide,
17427 DAG.getIntPtrConstant(i));
17428 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
17429 St->getPointerInfo(), St->isVolatile(),
17430 St->isNonTemporal(), St->getAlignment());
17431 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
17432 Chains.push_back(Ch);
17433 }
17434
17435 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
17436 Chains.size());
17437 }
17438
Chris Lattner149a4e52008-02-22 02:09:43 +000017439 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
17440 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000017441 // A preferable solution to the general problem is to figure out the right
17442 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000017443
17444 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000017445 if (VT.getSizeInBits() != 64)
17446 return SDValue();
17447
Devang Patel578efa92009-06-05 21:57:13 +000017448 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000017449 bool NoImplicitFloatOps = F->getAttributes().
17450 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000017451 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000017452 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000017453 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000017454 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000017455 isa<LoadSDNode>(St->getValue()) &&
17456 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
17457 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017458 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017459 LoadSDNode *Ld = 0;
17460 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000017461 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000017462 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017463 // Must be a store of a load. We currently handle two cases: the load
17464 // is a direct child, and it's under an intervening TokenFactor. It is
17465 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000017466 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000017467 Ld = cast<LoadSDNode>(St->getChain());
17468 else if (St->getValue().hasOneUse() &&
17469 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000017470 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017471 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000017472 TokenFactorIndex = i;
17473 Ld = cast<LoadSDNode>(St->getValue());
17474 } else
17475 Ops.push_back(ChainVal->getOperand(i));
17476 }
17477 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000017478
Evan Cheng536e6672009-03-12 05:59:15 +000017479 if (!Ld || !ISD::isNormalLoad(Ld))
17480 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017481
Evan Cheng536e6672009-03-12 05:59:15 +000017482 // If this is not the MMX case, i.e. we are just turning i64 load/store
17483 // into f64 load/store, avoid the transformation if there are multiple
17484 // uses of the loaded value.
17485 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
17486 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017487
Andrew Trickac6d9be2013-05-25 02:42:55 +000017488 SDLoc LdDL(Ld);
17489 SDLoc StDL(N);
Evan Cheng536e6672009-03-12 05:59:15 +000017490 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
17491 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
17492 // pair instead.
17493 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017494 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000017495 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
17496 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017497 Ld->isNonTemporal(), Ld->isInvariant(),
17498 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017499 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000017500 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000017501 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000017502 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000017503 Ops.size());
17504 }
Evan Cheng536e6672009-03-12 05:59:15 +000017505 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000017506 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017507 St->isVolatile(), St->isNonTemporal(),
17508 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000017509 }
Evan Cheng536e6672009-03-12 05:59:15 +000017510
17511 // Otherwise, lower to two pairs of 32-bit loads / stores.
17512 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017513 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
17514 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017515
Owen Anderson825b72b2009-08-11 20:47:22 +000017516 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017517 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017518 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017519 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000017520 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017521 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000017522 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017523 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000017524 MinAlign(Ld->getAlignment(), 4));
17525
17526 SDValue NewChain = LoLd.getValue(1);
17527 if (TokenFactorIndex != -1) {
17528 Ops.push_back(LoLd);
17529 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000017530 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000017531 Ops.size());
17532 }
17533
17534 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017535 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
17536 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017537
17538 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017539 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017540 St->isVolatile(), St->isNonTemporal(),
17541 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017542 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017543 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000017544 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000017545 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000017546 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000017547 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000017548 }
Dan Gohman475871a2008-07-27 21:46:04 +000017549 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000017550}
17551
Duncan Sands17470be2011-09-22 20:15:48 +000017552/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
17553/// and return the operands for the horizontal operation in LHS and RHS. A
17554/// horizontal operation performs the binary operation on successive elements
17555/// of its first operand, then on successive elements of its second operand,
17556/// returning the resulting values in a vector. For example, if
17557/// A = < float a0, float a1, float a2, float a3 >
17558/// and
17559/// B = < float b0, float b1, float b2, float b3 >
17560/// then the result of doing a horizontal operation on A and B is
17561/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
17562/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
17563/// A horizontal-op B, for some already available A and B, and if so then LHS is
17564/// set to A, RHS to B, and the routine returns 'true'.
17565/// Note that the binary operation should have the property that if one of the
17566/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017567static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000017568 // Look for the following pattern: if
17569 // A = < float a0, float a1, float a2, float a3 >
17570 // B = < float b0, float b1, float b2, float b3 >
17571 // and
17572 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17573 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17574 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17575 // which is A horizontal-op B.
17576
17577 // At least one of the operands should be a vector shuffle.
17578 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17579 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17580 return false;
17581
17582 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017583
17584 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17585 "Unsupported vector type for horizontal add/sub");
17586
17587 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17588 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017589 unsigned NumElts = VT.getVectorNumElements();
17590 unsigned NumLanes = VT.getSizeInBits()/128;
17591 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017592 assert((NumLaneElts % 2 == 0) &&
17593 "Vector type should have an even number of elements in each lane");
17594 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017595
17596 // View LHS in the form
17597 // LHS = VECTOR_SHUFFLE A, B, LMask
17598 // If LHS is not a shuffle then pretend it is the shuffle
17599 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17600 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17601 // type VT.
17602 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017603 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017604 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17605 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17606 A = LHS.getOperand(0);
17607 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17608 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017609 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17610 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017611 } else {
17612 if (LHS.getOpcode() != ISD::UNDEF)
17613 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017614 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017615 LMask[i] = i;
17616 }
17617
17618 // Likewise, view RHS in the form
17619 // RHS = VECTOR_SHUFFLE C, D, RMask
17620 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017621 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017622 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17623 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17624 C = RHS.getOperand(0);
17625 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17626 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017627 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17628 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017629 } else {
17630 if (RHS.getOpcode() != ISD::UNDEF)
17631 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017632 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017633 RMask[i] = i;
17634 }
17635
17636 // Check that the shuffles are both shuffling the same vectors.
17637 if (!(A == C && B == D) && !(A == D && B == C))
17638 return false;
17639
17640 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17641 if (!A.getNode() && !B.getNode())
17642 return false;
17643
17644 // If A and B occur in reverse order in RHS, then "swap" them (which means
17645 // rewriting the mask).
17646 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017647 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017648
17649 // At this point LHS and RHS are equivalent to
17650 // LHS = VECTOR_SHUFFLE A, B, LMask
17651 // RHS = VECTOR_SHUFFLE A, B, RMask
17652 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017653 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017654 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017655
Craig Topperf8363302011-12-02 08:18:41 +000017656 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017657 if (LIdx < 0 || RIdx < 0 ||
17658 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17659 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017660 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017661
Craig Topperf8363302011-12-02 08:18:41 +000017662 // Check that successive elements are being operated on. If not, this is
17663 // not a horizontal operation.
17664 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17665 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017666 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017667 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017668 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017669 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017670 }
17671
17672 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17673 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17674 return true;
17675}
17676
17677/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17678static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17679 const X86Subtarget *Subtarget) {
17680 EVT VT = N->getValueType(0);
17681 SDValue LHS = N->getOperand(0);
17682 SDValue RHS = N->getOperand(1);
17683
17684 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017685 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017686 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017687 isHorizontalBinOp(LHS, RHS, true))
Andrew Trickac6d9be2013-05-25 02:42:55 +000017688 return DAG.getNode(X86ISD::FHADD, SDLoc(N), VT, LHS, RHS);
Duncan Sands17470be2011-09-22 20:15:48 +000017689 return SDValue();
17690}
17691
17692/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17693static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17694 const X86Subtarget *Subtarget) {
17695 EVT VT = N->getValueType(0);
17696 SDValue LHS = N->getOperand(0);
17697 SDValue RHS = N->getOperand(1);
17698
17699 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017700 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017701 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017702 isHorizontalBinOp(LHS, RHS, false))
Andrew Trickac6d9be2013-05-25 02:42:55 +000017703 return DAG.getNode(X86ISD::FHSUB, SDLoc(N), VT, LHS, RHS);
Duncan Sands17470be2011-09-22 20:15:48 +000017704 return SDValue();
17705}
17706
Chris Lattner6cf73262008-01-25 06:14:17 +000017707/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17708/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017709static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017710 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17711 // F[X]OR(0.0, x) -> x
17712 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017713 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17714 if (C->getValueAPF().isPosZero())
17715 return N->getOperand(1);
17716 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17717 if (C->getValueAPF().isPosZero())
17718 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017719 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017720}
17721
Nadav Rotemd60cb112012-08-19 13:06:16 +000017722/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17723/// X86ISD::FMAX nodes.
17724static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17725 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17726
17727 // Only perform optimizations if UnsafeMath is used.
17728 if (!DAG.getTarget().Options.UnsafeFPMath)
17729 return SDValue();
17730
17731 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017732 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017733 unsigned NewOp = 0;
17734 switch (N->getOpcode()) {
17735 default: llvm_unreachable("unknown opcode");
17736 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17737 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17738 }
17739
Andrew Trickac6d9be2013-05-25 02:42:55 +000017740 return DAG.getNode(NewOp, SDLoc(N), N->getValueType(0),
Nadav Rotemd60cb112012-08-19 13:06:16 +000017741 N->getOperand(0), N->getOperand(1));
17742}
17743
Chris Lattneraf723b92008-01-25 05:46:26 +000017744/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017745static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017746 // FAND(0.0, x) -> 0.0
17747 // FAND(x, 0.0) -> 0.0
17748 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17749 if (C->getValueAPF().isPosZero())
17750 return N->getOperand(0);
17751 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17752 if (C->getValueAPF().isPosZero())
17753 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017754 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017755}
17756
Dan Gohmane5af2d32009-01-29 01:59:02 +000017757static SDValue PerformBTCombine(SDNode *N,
17758 SelectionDAG &DAG,
17759 TargetLowering::DAGCombinerInfo &DCI) {
17760 // BT ignores high bits in the bit index operand.
17761 SDValue Op1 = N->getOperand(1);
17762 if (Op1.hasOneUse()) {
17763 unsigned BitWidth = Op1.getValueSizeInBits();
17764 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17765 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017766 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17767 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017768 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017769 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17770 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17771 DCI.CommitTargetLoweringOpt(TLO);
17772 }
17773 return SDValue();
17774}
Chris Lattner83e6c992006-10-04 06:57:07 +000017775
Eli Friedman7a5e5552009-06-07 06:52:44 +000017776static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17777 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017778 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017779 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017780 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017781 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017782 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017783 OpVT.getVectorElementType().getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017784 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017785 }
17786 return SDValue();
17787}
17788
Matt Arsenault225ed702013-05-18 00:21:46 +000017789static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017790 const X86Subtarget *Subtarget) {
17791 EVT VT = N->getValueType(0);
17792 if (!VT.isVector())
17793 return SDValue();
17794
17795 SDValue N0 = N->getOperand(0);
17796 SDValue N1 = N->getOperand(1);
17797 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000017798 SDLoc dl(N);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017799
17800 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17801 // both SSE and AVX2 since there is no sign-extended shift right
17802 // operation on a vector with 64-bit elements.
17803 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17804 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17805 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17806 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17807 SDValue N00 = N0.getOperand(0);
17808
Matt Arsenault225ed702013-05-18 00:21:46 +000017809 // EXTLOAD has a better solution on AVX2,
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017810 // it may be replaced with X86ISD::VSEXT node.
17811 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17812 if (!ISD::isNormalLoad(N00.getNode()))
17813 return SDValue();
17814
17815 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
Matt Arsenault225ed702013-05-18 00:21:46 +000017816 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017817 N00, N1);
17818 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17819 }
17820 }
17821 return SDValue();
17822}
17823
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017824static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17825 TargetLowering::DAGCombinerInfo &DCI,
17826 const X86Subtarget *Subtarget) {
17827 if (!DCI.isBeforeLegalizeOps())
17828 return SDValue();
17829
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017830 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017831 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017832
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017833 EVT VT = N->getValueType(0);
17834 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017835 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17836 if (R.getNode())
17837 return R;
17838 }
17839
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017840 return SDValue();
17841}
17842
Michael Liaof6c24ee2012-08-10 14:39:24 +000017843static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017844 const X86Subtarget* Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017845 SDLoc dl(N);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017846 EVT VT = N->getValueType(0);
17847
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017848 // Let legalize expand this if it isn't a legal type yet.
17849 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17850 return SDValue();
17851
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017852 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017853 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17854 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017855 return SDValue();
17856
17857 SDValue A = N->getOperand(0);
17858 SDValue B = N->getOperand(1);
17859 SDValue C = N->getOperand(2);
17860
17861 bool NegA = (A.getOpcode() == ISD::FNEG);
17862 bool NegB = (B.getOpcode() == ISD::FNEG);
17863 bool NegC = (C.getOpcode() == ISD::FNEG);
17864
Michael Liaof6c24ee2012-08-10 14:39:24 +000017865 // Negative multiplication when NegA xor NegB
17866 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017867 if (NegA)
17868 A = A.getOperand(0);
17869 if (NegB)
17870 B = B.getOperand(0);
17871 if (NegC)
17872 C = C.getOperand(0);
17873
17874 unsigned Opcode;
17875 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017876 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017877 else
Craig Topperbf404372012-08-31 15:40:30 +000017878 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17879
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017880 return DAG.getNode(Opcode, dl, VT, A, B, C);
17881}
17882
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017883static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017884 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017885 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017886 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17887 // (and (i32 x86isd::setcc_carry), 1)
17888 // This eliminates the zext. This transformation is necessary because
17889 // ISD::SETCC is always legalized to i8.
Andrew Trickac6d9be2013-05-25 02:42:55 +000017890 SDLoc dl(N);
Evan Cheng2e489c42009-12-16 00:53:11 +000017891 SDValue N0 = N->getOperand(0);
17892 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017893
Evan Cheng2e489c42009-12-16 00:53:11 +000017894 if (N0.getOpcode() == ISD::AND &&
17895 N0.hasOneUse() &&
17896 N0.getOperand(0).hasOneUse()) {
17897 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017898 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17899 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17900 if (!C || C->getZExtValue() != 1)
17901 return SDValue();
17902 return DAG.getNode(ISD::AND, dl, VT,
17903 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17904 N00.getOperand(0), N00.getOperand(1)),
17905 DAG.getConstant(1, VT));
17906 }
17907 }
17908
Craig Topper5a529e42013-01-18 06:44:29 +000017909 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017910 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17911 if (R.getNode())
17912 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017913 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017914
Evan Cheng2e489c42009-12-16 00:53:11 +000017915 return SDValue();
17916}
17917
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017918// Optimize x == -y --> x+y == 0
17919// x != -y --> x+y != 0
17920static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17921 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17922 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017923 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017924
17925 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17926 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17927 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017928 SDValue addV = DAG.getNode(ISD::ADD, SDLoc(N),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017929 LHS.getValueType(), RHS, LHS.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +000017930 return DAG.getSetCC(SDLoc(N), N->getValueType(0),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017931 addV, DAG.getConstant(0, addV.getValueType()), CC);
17932 }
17933 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17934 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17935 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017936 SDValue addV = DAG.getNode(ISD::ADD, SDLoc(N),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017937 RHS.getValueType(), LHS, RHS.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +000017938 return DAG.getSetCC(SDLoc(N), N->getValueType(0),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017939 addV, DAG.getConstant(0, addV.getValueType()), CC);
17940 }
17941 return SDValue();
17942}
17943
Eric Christophere187e252013-01-31 00:50:48 +000017944// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17945// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017946// an all-ones bit which is more useful than 0/1 in some cases.
Andrew Trickac6d9be2013-05-25 02:42:55 +000017947static SDValue MaterializeSETB(SDLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
Shuxin Yanga5526a92012-10-31 23:11:48 +000017948 return DAG.getNode(ISD::AND, DL, MVT::i8,
17949 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17950 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17951 DAG.getConstant(1, MVT::i8));
17952}
17953
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017954// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017955static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17956 TargetLowering::DAGCombinerInfo &DCI,
17957 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017958 SDLoc DL(N);
Michael Liao2a33cec2012-08-10 19:58:13 +000017959 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17960 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017961
Shuxin Yanga5526a92012-10-31 23:11:48 +000017962 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017963 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017964 // materializing "setb reg".
17965 //
17966 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17967 // cannot take an immediate as its first operand.
17968 //
Eric Christophere187e252013-01-31 00:50:48 +000017969 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017970 EFLAGS.getValueType().isInteger() &&
17971 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017972 SDValue NewSub = DAG.getNode(X86ISD::SUB, SDLoc(EFLAGS),
Shuxin Yanga5526a92012-10-31 23:11:48 +000017973 EFLAGS.getNode()->getVTList(),
17974 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17975 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17976 return MaterializeSETB(DL, NewEFLAGS, DAG);
17977 }
17978 }
17979
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017980 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17981 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17982 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017983 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017984 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017985
Michael Liao2a33cec2012-08-10 19:58:13 +000017986 SDValue Flags;
17987
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017988 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17989 if (Flags.getNode()) {
17990 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17991 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17992 }
17993
Michael Liao2a33cec2012-08-10 19:58:13 +000017994 return SDValue();
17995}
17996
17997// Optimize branch condition evaluation.
17998//
17999static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
18000 TargetLowering::DAGCombinerInfo &DCI,
18001 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000018002 SDLoc DL(N);
Michael Liao2a33cec2012-08-10 19:58:13 +000018003 SDValue Chain = N->getOperand(0);
18004 SDValue Dest = N->getOperand(1);
18005 SDValue EFLAGS = N->getOperand(3);
18006 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
18007
18008 SDValue Flags;
18009
Michael Liaodbf8b5b2012-08-28 03:34:40 +000018010 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
18011 if (Flags.getNode()) {
18012 SDValue Cond = DAG.getConstant(CC, MVT::i8);
18013 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
18014 Flags);
18015 }
18016
Chris Lattnerc19d1c32010-12-19 22:08:31 +000018017 return SDValue();
18018}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000018019
Benjamin Kramer1396c402011-06-18 11:09:41 +000018020static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
18021 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000018022 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000018023 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000018024
18025 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000018026 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000018027 SDLoc dl(N);
Craig Topper7fd5e162012-04-24 06:02:29 +000018028 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000018029 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
18030 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
18031 }
18032
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000018033 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
18034 // a 32-bit target where SSE doesn't support i64->FP operations.
18035 if (Op0.getOpcode() == ISD::LOAD) {
18036 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
18037 EVT VT = Ld->getValueType(0);
18038 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
18039 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
18040 !XTLI->getSubtarget()->is64Bit() &&
18041 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000018042 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
18043 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000018044 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
18045 return FILDChain;
18046 }
18047 }
18048 return SDValue();
18049}
18050
Chris Lattner23a01992010-12-20 01:37:09 +000018051// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
18052static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
18053 X86TargetLowering::DAGCombinerInfo &DCI) {
18054 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
18055 // the result is either zero or one (depending on the input carry bit).
18056 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
18057 if (X86::isZeroNode(N->getOperand(0)) &&
18058 X86::isZeroNode(N->getOperand(1)) &&
18059 // We don't have a good way to replace an EFLAGS use, so only do this when
18060 // dead right now.
18061 SDValue(N, 1).use_empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000018062 SDLoc DL(N);
Chris Lattner23a01992010-12-20 01:37:09 +000018063 EVT VT = N->getValueType(0);
18064 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
18065 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
18066 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
18067 DAG.getConstant(X86::COND_B,MVT::i8),
18068 N->getOperand(2)),
18069 DAG.getConstant(1, VT));
18070 return DCI.CombineTo(N, Res1, CarryOut);
18071 }
18072
18073 return SDValue();
18074}
18075
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000018076// fold (add Y, (sete X, 0)) -> adc 0, Y
18077// (add Y, (setne X, 0)) -> sbb -1, Y
18078// (sub (sete X, 0), Y) -> sbb 0, Y
18079// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018080static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000018081 SDLoc DL(N);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000018082
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000018083 // Look through ZExts.
18084 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
18085 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
18086 return SDValue();
18087
18088 SDValue SetCC = Ext.getOperand(0);
18089 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
18090 return SDValue();
18091
18092 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
18093 if (CC != X86::COND_E && CC != X86::COND_NE)
18094 return SDValue();
18095
18096 SDValue Cmp = SetCC.getOperand(1);
18097 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000018098 !X86::isZeroNode(Cmp.getOperand(1)) ||
18099 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000018100 return SDValue();
18101
18102 SDValue CmpOp0 = Cmp.getOperand(0);
18103 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
18104 DAG.getConstant(1, CmpOp0.getValueType()));
18105
18106 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
18107 if (CC == X86::COND_NE)
18108 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
18109 DL, OtherVal.getValueType(), OtherVal,
18110 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
18111 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
18112 DL, OtherVal.getValueType(), OtherVal,
18113 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
18114}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000018115
Craig Topper54f952a2011-11-19 09:02:40 +000018116/// PerformADDCombine - Do target-specific dag combines on integer adds.
18117static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
18118 const X86Subtarget *Subtarget) {
18119 EVT VT = N->getValueType(0);
18120 SDValue Op0 = N->getOperand(0);
18121 SDValue Op1 = N->getOperand(1);
18122
18123 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000018124 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018125 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000018126 isHorizontalBinOp(Op0, Op1, true))
Andrew Trickac6d9be2013-05-25 02:42:55 +000018127 return DAG.getNode(X86ISD::HADD, SDLoc(N), VT, Op0, Op1);
Craig Topper54f952a2011-11-19 09:02:40 +000018128
18129 return OptimizeConditionalInDecrement(N, DAG);
18130}
18131
18132static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
18133 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018134 SDValue Op0 = N->getOperand(0);
18135 SDValue Op1 = N->getOperand(1);
18136
18137 // X86 can't encode an immediate LHS of a sub. See if we can push the
18138 // negation into a preceding instruction.
18139 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018140 // If the RHS of the sub is a XOR with one use and a constant, invert the
18141 // immediate. Then add one to the LHS of the sub so we can turn
18142 // X-Y -> X+~Y+1, saving one register.
18143 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
18144 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000018145 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018146 EVT VT = Op0.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000018147 SDValue NewXor = DAG.getNode(ISD::XOR, SDLoc(Op1), VT,
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018148 Op1.getOperand(0),
18149 DAG.getConstant(~XorC, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +000018150 return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000018151 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018152 }
18153 }
18154
Craig Topper54f952a2011-11-19 09:02:40 +000018155 // Try to synthesize horizontal adds from adds of shuffles.
18156 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000018157 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018158 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000018159 isHorizontalBinOp(Op0, Op1, true))
Andrew Trickac6d9be2013-05-25 02:42:55 +000018160 return DAG.getNode(X86ISD::HSUB, SDLoc(N), VT, Op0, Op1);
Craig Topper54f952a2011-11-19 09:02:40 +000018161
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000018162 return OptimizeConditionalInDecrement(N, DAG);
18163}
18164
Michael Liaod9d09602012-10-23 17:34:00 +000018165/// performVZEXTCombine - Performs build vector combines
18166static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
18167 TargetLowering::DAGCombinerInfo &DCI,
18168 const X86Subtarget *Subtarget) {
18169 // (vzext (bitcast (vzext (x)) -> (vzext x)
18170 SDValue In = N->getOperand(0);
18171 while (In.getOpcode() == ISD::BITCAST)
18172 In = In.getOperand(0);
18173
18174 if (In.getOpcode() != X86ISD::VZEXT)
18175 return SDValue();
18176
Andrew Trickac6d9be2013-05-25 02:42:55 +000018177 return DAG.getNode(X86ISD::VZEXT, SDLoc(N), N->getValueType(0),
Nadav Rotemb39a5522013-02-14 18:20:48 +000018178 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000018179}
18180
Dan Gohman475871a2008-07-27 21:46:04 +000018181SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000018182 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000018183 SelectionDAG &DAG = DCI.DAG;
18184 switch (N->getOpcode()) {
18185 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000018186 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000018187 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000018188 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000018189 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000018190 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000018191 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
18192 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000018193 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000018194 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000018195 case ISD::SHL:
18196 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000018197 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000018198 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000018199 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000018200 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000018201 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000018202 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000018203 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000018204 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
18205 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000018206 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000018207 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000018208 case X86ISD::FMIN:
18209 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000018210 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000018211 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000018212 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000018213 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000018214 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000018215 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000018216 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000018217 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000018218 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000018219 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000018220 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000018221 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000018222 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000018223 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000018224 case X86ISD::UNPCKH:
18225 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000018226 case X86ISD::MOVHLPS:
18227 case X86ISD::MOVLHPS:
18228 case X86ISD::PSHUFD:
18229 case X86ISD::PSHUFHW:
18230 case X86ISD::PSHUFLW:
18231 case X86ISD::MOVSS:
18232 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000018233 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000018234 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000018235 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000018236 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000018237 }
18238
Dan Gohman475871a2008-07-27 21:46:04 +000018239 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000018240}
18241
Evan Chenge5b51ac2010-04-17 06:13:15 +000018242/// isTypeDesirableForOp - Return true if the target has native support for
18243/// the specified value type and it is 'desirable' to use the type for the
18244/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
18245/// instruction encodings are longer and some i16 instructions are slow.
18246bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
18247 if (!isTypeLegal(VT))
18248 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000018249 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000018250 return true;
18251
18252 switch (Opc) {
18253 default:
18254 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000018255 case ISD::LOAD:
18256 case ISD::SIGN_EXTEND:
18257 case ISD::ZERO_EXTEND:
18258 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000018259 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000018260 case ISD::SRL:
18261 case ISD::SUB:
18262 case ISD::ADD:
18263 case ISD::MUL:
18264 case ISD::AND:
18265 case ISD::OR:
18266 case ISD::XOR:
18267 return false;
18268 }
18269}
18270
18271/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000018272/// beneficial for dag combiner to promote the specified node. If true, it
18273/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000018274bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000018275 EVT VT = Op.getValueType();
18276 if (VT != MVT::i16)
18277 return false;
18278
Evan Cheng4c26e932010-04-19 19:29:22 +000018279 bool Promote = false;
18280 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000018281 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000018282 default: break;
18283 case ISD::LOAD: {
18284 LoadSDNode *LD = cast<LoadSDNode>(Op);
18285 // If the non-extending load has a single use and it's not live out, then it
18286 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000018287 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
18288 Op.hasOneUse()*/) {
18289 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
18290 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
18291 // The only case where we'd want to promote LOAD (rather then it being
18292 // promoted as an operand is when it's only use is liveout.
18293 if (UI->getOpcode() != ISD::CopyToReg)
18294 return false;
18295 }
18296 }
Evan Cheng4c26e932010-04-19 19:29:22 +000018297 Promote = true;
18298 break;
18299 }
18300 case ISD::SIGN_EXTEND:
18301 case ISD::ZERO_EXTEND:
18302 case ISD::ANY_EXTEND:
18303 Promote = true;
18304 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000018305 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000018306 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000018307 SDValue N0 = Op.getOperand(0);
18308 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000018309 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000018310 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000018311 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000018312 break;
18313 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000018314 case ISD::ADD:
18315 case ISD::MUL:
18316 case ISD::AND:
18317 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000018318 case ISD::XOR:
18319 Commute = true;
18320 // fallthrough
18321 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000018322 SDValue N0 = Op.getOperand(0);
18323 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000018324 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000018325 return false;
18326 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000018327 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000018328 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000018329 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000018330 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000018331 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000018332 }
18333 }
18334
18335 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000018336 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000018337}
18338
Evan Cheng60c07e12006-07-05 22:17:51 +000018339//===----------------------------------------------------------------------===//
18340// X86 Inline Assembly Support
18341//===----------------------------------------------------------------------===//
18342
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018343namespace {
18344 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000018345 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018346 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018347
Benjamin Kramer0581ed72011-12-18 20:51:31 +000018348 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018349 StringRef piece(*args[i]);
18350 if (!s.startswith(piece)) // Check if the piece matches.
18351 return false;
18352
18353 s = s.substr(piece.size());
18354 StringRef::size_type pos = s.find_first_not_of(" \t");
18355 if (pos == 0) // We matched a prefix.
18356 return false;
18357
18358 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018359 }
18360
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018361 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018362 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000018363 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018364}
18365
Chris Lattnerb8105652009-07-20 17:51:36 +000018366bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
18367 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000018368
18369 std::string AsmStr = IA->getAsmString();
18370
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018371 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
18372 if (!Ty || Ty->getBitWidth() % 16 != 0)
18373 return false;
18374
Chris Lattnerb8105652009-07-20 17:51:36 +000018375 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000018376 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000018377 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000018378
18379 switch (AsmPieces.size()) {
18380 default: return false;
18381 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000018382 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018383 // we will turn this bswap into something that will be lowered to logical
18384 // ops instead of emitting the bswap asm. For now, we don't support 486 or
18385 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000018386 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018387 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
18388 matchAsm(AsmPieces[0], "bswapl", "$0") ||
18389 matchAsm(AsmPieces[0], "bswapq", "$0") ||
18390 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
18391 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
18392 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000018393 // No need to check constraints, nothing other than the equivalent of
18394 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000018395 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018396 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018397
Chris Lattnerb8105652009-07-20 17:51:36 +000018398 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000018399 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018400 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018401 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
18402 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000018403 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000018404 const std::string &ConstraintsStr = IA->getConstraintString();
18405 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000018406 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000018407 if (AsmPieces.size() == 4 &&
18408 AsmPieces[0] == "~{cc}" &&
18409 AsmPieces[1] == "~{dirflag}" &&
18410 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018411 AsmPieces[3] == "~{fpsr}")
18412 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018413 }
18414 break;
18415 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000018416 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018417 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018418 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
18419 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
18420 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018421 AsmPieces.clear();
18422 const std::string &ConstraintsStr = IA->getConstraintString();
18423 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000018424 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018425 if (AsmPieces.size() == 4 &&
18426 AsmPieces[0] == "~{cc}" &&
18427 AsmPieces[1] == "~{dirflag}" &&
18428 AsmPieces[2] == "~{flags}" &&
18429 AsmPieces[3] == "~{fpsr}")
18430 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000018431 }
Evan Cheng55d42002011-01-08 01:24:27 +000018432
18433 if (CI->getType()->isIntegerTy(64)) {
18434 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
18435 if (Constraints.size() >= 2 &&
18436 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
18437 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
18438 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018439 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
18440 matchAsm(AsmPieces[1], "bswap", "%edx") &&
18441 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018442 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018443 }
18444 }
18445 break;
18446 }
18447 return false;
18448}
18449
Chris Lattnerf4dff842006-07-11 02:54:03 +000018450/// getConstraintType - Given a constraint letter, return the type of
18451/// constraint it is for this target.
18452X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000018453X86TargetLowering::getConstraintType(const std::string &Constraint) const {
18454 if (Constraint.size() == 1) {
18455 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000018456 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000018457 case 'q':
18458 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000018459 case 'f':
18460 case 't':
18461 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000018462 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000018463 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000018464 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000018465 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000018466 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000018467 case 'a':
18468 case 'b':
18469 case 'c':
18470 case 'd':
18471 case 'S':
18472 case 'D':
18473 case 'A':
18474 return C_Register;
18475 case 'I':
18476 case 'J':
18477 case 'K':
18478 case 'L':
18479 case 'M':
18480 case 'N':
18481 case 'G':
18482 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000018483 case 'e':
18484 case 'Z':
18485 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000018486 default:
18487 break;
18488 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000018489 }
Chris Lattner4234f572007-03-25 02:14:49 +000018490 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000018491}
18492
John Thompson44ab89e2010-10-29 17:29:13 +000018493/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000018494/// This object must already have been set up with the operand type
18495/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000018496TargetLowering::ConstraintWeight
18497 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000018498 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000018499 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018500 Value *CallOperandVal = info.CallOperandVal;
18501 // If we don't have a value, we can't do a match,
18502 // but allow it at the lowest weight.
18503 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000018504 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000018505 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000018506 // Look at the constraint type.
18507 switch (*constraint) {
18508 default:
John Thompson44ab89e2010-10-29 17:29:13 +000018509 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
18510 case 'R':
18511 case 'q':
18512 case 'Q':
18513 case 'a':
18514 case 'b':
18515 case 'c':
18516 case 'd':
18517 case 'S':
18518 case 'D':
18519 case 'A':
18520 if (CallOperandVal->getType()->isIntegerTy())
18521 weight = CW_SpecificReg;
18522 break;
18523 case 'f':
18524 case 't':
18525 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018526 if (type->isFloatingPointTy())
18527 weight = CW_SpecificReg;
18528 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018529 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018530 if (type->isX86_MMXTy() && Subtarget->hasMMX())
18531 weight = CW_SpecificReg;
18532 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018533 case 'x':
18534 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000018535 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018536 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000018537 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018538 break;
18539 case 'I':
18540 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
18541 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000018542 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018543 }
18544 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018545 case 'J':
18546 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18547 if (C->getZExtValue() <= 63)
18548 weight = CW_Constant;
18549 }
18550 break;
18551 case 'K':
18552 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18553 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
18554 weight = CW_Constant;
18555 }
18556 break;
18557 case 'L':
18558 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18559 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
18560 weight = CW_Constant;
18561 }
18562 break;
18563 case 'M':
18564 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18565 if (C->getZExtValue() <= 3)
18566 weight = CW_Constant;
18567 }
18568 break;
18569 case 'N':
18570 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18571 if (C->getZExtValue() <= 0xff)
18572 weight = CW_Constant;
18573 }
18574 break;
18575 case 'G':
18576 case 'C':
18577 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18578 weight = CW_Constant;
18579 }
18580 break;
18581 case 'e':
18582 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18583 if ((C->getSExtValue() >= -0x80000000LL) &&
18584 (C->getSExtValue() <= 0x7fffffffLL))
18585 weight = CW_Constant;
18586 }
18587 break;
18588 case 'Z':
18589 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18590 if (C->getZExtValue() <= 0xffffffff)
18591 weight = CW_Constant;
18592 }
18593 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018594 }
18595 return weight;
18596}
18597
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018598/// LowerXConstraint - try to replace an X constraint, which matches anything,
18599/// with another that has more specific requirements based on the type of the
18600/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018601const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018602LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018603 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18604 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018605 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018606 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018607 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018608 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018609 return "x";
18610 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018611
Chris Lattner5e764232008-04-26 23:02:14 +000018612 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018613}
18614
Chris Lattner48884cd2007-08-25 00:47:38 +000018615/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18616/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018617void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018618 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018619 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018620 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018621 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018622
Eric Christopher100c8332011-06-02 23:16:42 +000018623 // Only support length 1 constraints for now.
18624 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018625
Eric Christopher100c8332011-06-02 23:16:42 +000018626 char ConstraintLetter = Constraint[0];
18627 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018628 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018629 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018630 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018631 if (C->getZExtValue() <= 31) {
18632 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018633 break;
18634 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018635 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018636 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018637 case 'J':
18638 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018639 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018640 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18641 break;
18642 }
18643 }
18644 return;
18645 case 'K':
18646 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018647 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018648 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18649 break;
18650 }
18651 }
18652 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018653 case 'N':
18654 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018655 if (C->getZExtValue() <= 255) {
18656 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018657 break;
18658 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018659 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018660 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018661 case 'e': {
18662 // 32-bit signed value
18663 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018664 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18665 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018666 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018667 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018668 break;
18669 }
18670 // FIXME gcc accepts some relocatable values here too, but only in certain
18671 // memory models; it's complicated.
18672 }
18673 return;
18674 }
18675 case 'Z': {
18676 // 32-bit unsigned value
18677 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018678 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18679 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018680 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18681 break;
18682 }
18683 }
18684 // FIXME gcc accepts some relocatable values here too, but only in certain
18685 // memory models; it's complicated.
18686 return;
18687 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018688 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018689 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018690 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018691 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018692 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018693 break;
18694 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018695
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018696 // In any sort of PIC mode addresses need to be computed at runtime by
18697 // adding in a register or some sort of table lookup. These can't
18698 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018699 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018700 return;
18701
Chris Lattnerdc43a882007-05-03 16:52:29 +000018702 // If we are in non-pic codegen mode, we allow the address of a global (with
18703 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018704 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018705 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018706
Chris Lattner49921962009-05-08 18:23:14 +000018707 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18708 while (1) {
18709 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18710 Offset += GA->getOffset();
18711 break;
18712 } else if (Op.getOpcode() == ISD::ADD) {
18713 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18714 Offset += C->getZExtValue();
18715 Op = Op.getOperand(0);
18716 continue;
18717 }
18718 } else if (Op.getOpcode() == ISD::SUB) {
18719 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18720 Offset += -C->getZExtValue();
18721 Op = Op.getOperand(0);
18722 continue;
18723 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018724 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018725
Chris Lattner49921962009-05-08 18:23:14 +000018726 // Otherwise, this isn't something we can handle, reject it.
18727 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018728 }
Eric Christopherfd179292009-08-27 18:07:15 +000018729
Dan Gohman46510a72010-04-15 01:51:59 +000018730 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018731 // If we require an extra load to get this address, as in PIC mode, we
18732 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018733 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18734 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018735 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018736
Andrew Trickac6d9be2013-05-25 02:42:55 +000018737 Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
Devang Patel0d881da2010-07-06 22:08:15 +000018738 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018739 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018740 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018741 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018742
Gabor Greifba36cb52008-08-28 21:40:38 +000018743 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018744 Ops.push_back(Result);
18745 return;
18746 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018747 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018748}
18749
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018750std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018751X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier5b3fca52013-06-22 18:37:38 +000018752 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018753 // First, see if this is a constraint that directly corresponds to an LLVM
18754 // register class.
18755 if (Constraint.size() == 1) {
18756 // GCC Constraint Letters
18757 switch (Constraint[0]) {
18758 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018759 // TODO: Slight differences here in allocation order and leaving
18760 // RIP in the class. Do they matter any more here than they do
18761 // in the normal allocation?
18762 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18763 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018764 if (VT == MVT::i32 || VT == MVT::f32)
18765 return std::make_pair(0U, &X86::GR32RegClass);
18766 if (VT == MVT::i16)
18767 return std::make_pair(0U, &X86::GR16RegClass);
18768 if (VT == MVT::i8 || VT == MVT::i1)
18769 return std::make_pair(0U, &X86::GR8RegClass);
18770 if (VT == MVT::i64 || VT == MVT::f64)
18771 return std::make_pair(0U, &X86::GR64RegClass);
18772 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018773 }
18774 // 32-bit fallthrough
18775 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018776 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018777 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18778 if (VT == MVT::i16)
18779 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18780 if (VT == MVT::i8 || VT == MVT::i1)
18781 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18782 if (VT == MVT::i64)
18783 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018784 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018785 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018786 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018787 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018788 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018789 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018790 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018791 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018792 return std::make_pair(0U, &X86::GR32RegClass);
18793 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018794 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018795 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018796 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018797 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018798 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018799 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018800 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18801 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018802 case 'f': // FP Stack registers.
18803 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18804 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018805 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018806 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018807 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018808 return std::make_pair(0U, &X86::RFP64RegClass);
18809 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018810 case 'y': // MMX_REGS if MMX allowed.
18811 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018812 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018813 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018814 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018815 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018816 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018817 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018818
Chad Rosier5b3fca52013-06-22 18:37:38 +000018819 switch (VT.SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018820 default: break;
18821 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018822 case MVT::f32:
18823 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018824 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018825 case MVT::f64:
18826 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018827 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018828 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018829 case MVT::v16i8:
18830 case MVT::v8i16:
18831 case MVT::v4i32:
18832 case MVT::v2i64:
18833 case MVT::v4f32:
18834 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018835 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018836 // AVX types.
18837 case MVT::v32i8:
18838 case MVT::v16i16:
18839 case MVT::v8i32:
18840 case MVT::v4i64:
18841 case MVT::v8f32:
18842 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018843 return std::make_pair(0U, &X86::VR256RegClass);
Elena Demikhovskye3809ee2013-07-24 11:02:47 +000018844 case MVT::v8f64:
18845 case MVT::v16f32:
18846 case MVT::v16i32:
18847 case MVT::v8i64:
18848 return std::make_pair(0U, &X86::VR512RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018849 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018850 break;
18851 }
18852 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018853
Chris Lattnerf76d1802006-07-31 23:26:50 +000018854 // Use the default implementation in TargetLowering to convert the register
18855 // constraint into a member of a register class.
18856 std::pair<unsigned, const TargetRegisterClass*> Res;
18857 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018858
18859 // Not found as a standard register?
18860 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018861 // Map st(0) -> st(7) -> ST0
18862 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18863 tolower(Constraint[1]) == 's' &&
18864 tolower(Constraint[2]) == 't' &&
18865 Constraint[3] == '(' &&
18866 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18867 Constraint[5] == ')' &&
18868 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018869
Chris Lattner56d77c72009-09-13 22:41:48 +000018870 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018871 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018872 return Res;
18873 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018874
Chris Lattner56d77c72009-09-13 22:41:48 +000018875 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018876 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018877 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018878 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018879 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018880 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018881
18882 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018883 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018884 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018885 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018886 return Res;
18887 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018888
Dale Johannesen330169f2008-11-13 21:52:36 +000018889 // 'A' means EAX + EDX.
18890 if (Constraint == "A") {
18891 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018892 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018893 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018894 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018895 return Res;
18896 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018897
Chris Lattnerf76d1802006-07-31 23:26:50 +000018898 // Otherwise, check to see if this is a register class of the wrong value
18899 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18900 // turn into {ax},{dx}.
18901 if (Res.second->hasType(VT))
18902 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018903
Chris Lattnerf76d1802006-07-31 23:26:50 +000018904 // All of the single-register GCC register classes map their values onto
18905 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18906 // really want an 8-bit or 32-bit register, map to the appropriate register
18907 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018908 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018909 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018910 unsigned DestReg = 0;
18911 switch (Res.first) {
18912 default: break;
18913 case X86::AX: DestReg = X86::AL; break;
18914 case X86::DX: DestReg = X86::DL; break;
18915 case X86::CX: DestReg = X86::CL; break;
18916 case X86::BX: DestReg = X86::BL; break;
18917 }
18918 if (DestReg) {
18919 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018920 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018921 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018922 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018923 unsigned DestReg = 0;
18924 switch (Res.first) {
18925 default: break;
18926 case X86::AX: DestReg = X86::EAX; break;
18927 case X86::DX: DestReg = X86::EDX; break;
18928 case X86::CX: DestReg = X86::ECX; break;
18929 case X86::BX: DestReg = X86::EBX; break;
18930 case X86::SI: DestReg = X86::ESI; break;
18931 case X86::DI: DestReg = X86::EDI; break;
18932 case X86::BP: DestReg = X86::EBP; break;
18933 case X86::SP: DestReg = X86::ESP; break;
18934 }
18935 if (DestReg) {
18936 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018937 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018938 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018939 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018940 unsigned DestReg = 0;
18941 switch (Res.first) {
18942 default: break;
18943 case X86::AX: DestReg = X86::RAX; break;
18944 case X86::DX: DestReg = X86::RDX; break;
18945 case X86::CX: DestReg = X86::RCX; break;
18946 case X86::BX: DestReg = X86::RBX; break;
18947 case X86::SI: DestReg = X86::RSI; break;
18948 case X86::DI: DestReg = X86::RDI; break;
18949 case X86::BP: DestReg = X86::RBP; break;
18950 case X86::SP: DestReg = X86::RSP; break;
18951 }
18952 if (DestReg) {
18953 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018954 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018955 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018956 }
Craig Topperc9099502012-04-20 06:31:50 +000018957 } else if (Res.second == &X86::FR32RegClass ||
18958 Res.second == &X86::FR64RegClass ||
Elena Demikhovskye3809ee2013-07-24 11:02:47 +000018959 Res.second == &X86::VR128RegClass ||
18960 Res.second == &X86::VR256RegClass ||
18961 Res.second == &X86::FR32XRegClass ||
18962 Res.second == &X86::FR64XRegClass ||
18963 Res.second == &X86::VR128XRegClass ||
18964 Res.second == &X86::VR256XRegClass ||
18965 Res.second == &X86::VR512RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018966 // Handle references to XMM physical registers that got mapped into the
18967 // wrong class. This can happen with constraints like {xmm0} where the
18968 // target independent register mapper will just pick the first match it can
18969 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018970
18971 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018972 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018973 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018974 Res.second = &X86::FR64RegClass;
18975 else if (X86::VR128RegClass.hasType(VT))
18976 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018977 else if (X86::VR256RegClass.hasType(VT))
18978 Res.second = &X86::VR256RegClass;
Elena Demikhovskye3809ee2013-07-24 11:02:47 +000018979 else if (X86::VR512RegClass.hasType(VT))
18980 Res.second = &X86::VR512RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018981 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018982
Chris Lattnerf76d1802006-07-31 23:26:50 +000018983 return Res;
18984}