blob: a680ac09b5f02d12e5cd6d632d7a03588f7b1ce6 [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
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
Andrew Trickac6d9be2013-05-25 02:42:55 +000067 SelectionDAG &DAG, SDLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Benjamin Kramer02c2ecf2013-03-07 18:48:40 +000088 // If the input is a buildvector just emit a smaller one.
89 if (Vec.getOpcode() == ISD::BUILD_VECTOR)
90 return DAG.getNode(ISD::BUILD_VECTOR, dl, ResultVT,
91 Vec->op_begin()+NormalizedIdxVal, ElemsPerChunk);
92
Craig Topperb8d9da12012-09-06 06:09:01 +000093 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000094 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
95 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000096
Craig Topperb14940a2012-04-22 20:55:18 +000097 return Result;
David Greenea5f26012011-02-07 19:36:54 +000098}
99
100/// Generate a DAG to put 128-bits into a vector > 128 bits. This
101/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +0000102/// simple superregister reference. Idx is an index in the 128 bits
103/// we want. It need not be aligned to a 128-bit bounday. That makes
104/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000105static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
106 unsigned IdxVal, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000107 SDLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000108 // Inserting UNDEF is Result
109 if (Vec.getOpcode() == ISD::UNDEF)
110 return Result;
111
Craig Topperb14940a2012-04-22 20:55:18 +0000112 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000113 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000114
Craig Topperb14940a2012-04-22 20:55:18 +0000115 EVT ElVT = VT.getVectorElementType();
116 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000117
Craig Topperb14940a2012-04-22 20:55:18 +0000118 // Insert the relevant 128 bits.
119 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb14940a2012-04-22 20:55:18 +0000121 // This is the index of the first element of the 128-bit chunk
122 // we want.
123 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
124 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000125
Craig Topperb8d9da12012-09-06 06:09:01 +0000126 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000127 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
128 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000129}
130
Craig Topper4c7972d2012-04-22 18:15:59 +0000131/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
132/// instructions. This is used because creating CONCAT_VECTOR nodes of
133/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
134/// large BUILD_VECTORS.
135static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
136 unsigned NumElems, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000137 SDLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000138 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
139 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000140}
141
Chris Lattnerf0144122009-07-28 03:13:23 +0000142static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000143 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
144 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000145
Evan Cheng2bffee22011-02-01 01:14:13 +0000146 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000147 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000148 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000149 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000150 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000151
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000152 if (Subtarget->isTargetLinux())
153 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000154 if (Subtarget->isTargetELF())
155 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000156 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000157 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000158 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000159}
160
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000161X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000162 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000163 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000164 X86ScalarSSEf64 = Subtarget->hasSSE2();
165 X86ScalarSSEf32 = Subtarget->hasSSE1();
Micah Villmow3574eca2012-10-08 16:38:25 +0000166 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000167
Bill Wendling13bbe1f2013-04-05 21:52:40 +0000168 resetOperationActions();
169}
170
171void X86TargetLowering::resetOperationActions() {
172 const TargetMachine &TM = getTargetMachine();
173 static bool FirstTimeThrough = true;
174
175 // If none of the target options have changed, then we don't need to reset the
176 // operation actions.
177 if (!FirstTimeThrough && TO == TM.Options) return;
178
179 if (!FirstTimeThrough) {
180 // Reinitialize the actions.
181 initActions();
182 FirstTimeThrough = false;
183 }
184
185 TO = TM.Options;
186
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000187 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000188 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189
190 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000191 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000192 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
193 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000194
Eric Christopherde5e1012011-03-11 01:05:58 +0000195 // For 64-bit since we have so many registers use the ILP scheduler, for
196 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000197 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000198 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000199 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000200 else if (Subtarget->is64Bit())
201 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000202 else
203 setSchedulingPreference(Sched::RegPressure);
Bill Wendlinga5e5ba62013-06-07 21:00:34 +0000204 const X86RegisterInfo *RegInfo =
205 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Michael Liaoc5c970e2012-10-31 04:14:09 +0000206 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000207
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000208 // Bypass expensive divides on Atom when compiling with O2
209 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default) {
Preston Gurd8d662b52012-10-04 21:33:40 +0000210 addBypassSlowDiv(32, 8);
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000211 if (Subtarget->is64Bit())
212 addBypassSlowDiv(64, 16);
213 }
Preston Gurd2e2efd92012-09-04 18:22:17 +0000214
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000215 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000216 // Setup Windows compiler runtime calls.
217 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000218 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000219 setLibcallName(RTLIB::SREM_I64, "_allrem");
220 setLibcallName(RTLIB::UREM_I64, "_aullrem");
221 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000222 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000223 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000224 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
225 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
226 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000227
228 // The _ftol2 runtime function has an unusual calling conv, which
229 // is modeled by a special pseudo-instruction.
230 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
231 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
232 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
233 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000234 }
235
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000236 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000237 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000238 setUseUnderscoreSetJmp(false);
239 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000240 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000241 // MS runtime is weird: it exports _setjmp, but longjmp!
242 setUseUnderscoreSetJmp(true);
243 setUseUnderscoreLongJmp(false);
244 } else {
245 setUseUnderscoreSetJmp(true);
246 setUseUnderscoreLongJmp(true);
247 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000248
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000249 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000250 addRegisterClass(MVT::i8, &X86::GR8RegClass);
251 addRegisterClass(MVT::i16, &X86::GR16RegClass);
252 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000254 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000255
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000257
Scott Michelfdc40a02009-02-17 22:15:04 +0000258 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000260 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000262 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
264 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000265
266 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
268 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
269 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
270 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
271 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
272 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000273
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000274 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
275 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
277 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
278 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000279
Evan Cheng25ab6902006-09-08 06:48:29 +0000280 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000282 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000283 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000284 // We have an algorithm for SSE2->double, and we turn this into a
285 // 64-bit FILD followed by conditional FADD for other targets.
286 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000287 // We have an algorithm for SSE2, and we turn this into a 64-bit
288 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000289 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000290 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000291
292 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
293 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
295 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000296
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000297 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000298 // SSE has no i16 to fp conversion, only i32
299 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000301 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000303 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
305 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000306 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000307 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
309 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000310 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000311
Dale Johannesen73328d12007-09-19 23:55:34 +0000312 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
313 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
315 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000316
Evan Cheng02568ff2006-01-30 22:13:22 +0000317 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
318 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
320 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000321
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000322 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000324 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000326 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
328 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329 }
330
331 // Handle FP_TO_UINT by promoting the destination to a larger signed
332 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
334 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
335 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000336
Evan Cheng25ab6902006-09-08 06:48:29 +0000337 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
339 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000340 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000341 // Since AVX is a superset of SSE3, only check for SSE here.
342 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000343 // Expand FP_TO_UINT into a select.
344 // FIXME: We would like to use a Custom expander here eventually to do
345 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000348 // With SSE3 we can use fisttpll to convert to a signed i64; without
349 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000351 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000353 if (isTargetFTOL()) {
354 // Use the _ftol2 runtime function, which has a pseudo-instruction
355 // to handle its weird calling convention.
356 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
357 }
358
Chris Lattner399610a2006-12-05 18:22:22 +0000359 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000360 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000361 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
362 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000363 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000365 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000367 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000368 }
Chris Lattner21f66852005-12-23 05:15:23 +0000369
Dan Gohmanb00ee212008-02-18 19:34:53 +0000370 // Scalar integer divide and remainder are lowered to use operations that
371 // produce two results, to match the available instructions. This exposes
372 // the two-result form to trivial CSE, which is able to combine x/y and x%y
373 // into a single instruction.
374 //
375 // Scalar integer multiply-high is also lowered to use two-result
376 // operations, to match the available instructions. However, plain multiply
377 // (low) operations are left as Legal, as there are single-result
378 // instructions for this in x86. Using the two-result multiply instructions
379 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000380 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000381 MVT VT = IntVTs[i];
382 setOperationAction(ISD::MULHS, VT, Expand);
383 setOperationAction(ISD::MULHU, VT, Expand);
384 setOperationAction(ISD::SDIV, VT, Expand);
385 setOperationAction(ISD::UDIV, VT, Expand);
386 setOperationAction(ISD::SREM, VT, Expand);
387 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000388
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000389 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000390 setOperationAction(ISD::ADDC, VT, Custom);
391 setOperationAction(ISD::ADDE, VT, Custom);
392 setOperationAction(ISD::SUBC, VT, Custom);
393 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000394 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000395
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
397 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Tom Stellard3ef53832013-03-08 15:36:57 +0000398 setOperationAction(ISD::BR_CC , MVT::f32, Expand);
399 setOperationAction(ISD::BR_CC , MVT::f64, Expand);
400 setOperationAction(ISD::BR_CC , MVT::f80, Expand);
401 setOperationAction(ISD::BR_CC , MVT::i8, Expand);
402 setOperationAction(ISD::BR_CC , MVT::i16, Expand);
403 setOperationAction(ISD::BR_CC , MVT::i32, Expand);
404 setOperationAction(ISD::BR_CC , MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000406 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
408 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
409 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
410 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
411 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
412 setOperationAction(ISD::FREM , MVT::f32 , Expand);
413 setOperationAction(ISD::FREM , MVT::f64 , Expand);
414 setOperationAction(ISD::FREM , MVT::f80 , Expand);
415 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000416
Chandler Carruth77821022011-12-24 12:12:34 +0000417 // Promote the i8 variants and force them on up to i32 which has a shorter
418 // encoding.
419 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
420 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
421 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
422 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000423 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000424 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
425 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
426 if (Subtarget->is64Bit())
427 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000428 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000429 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
430 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
431 if (Subtarget->is64Bit())
432 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
433 }
Craig Topper37f21672011-10-11 06:44:02 +0000434
435 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000436 // When promoting the i8 variants, force them to i32 for a shorter
437 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000438 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000439 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
440 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
441 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000442 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
443 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
444 if (Subtarget->is64Bit())
445 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000446 } else {
447 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
448 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
449 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000450 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
451 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
452 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
453 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000454 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000455 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
456 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000457 }
458
Benjamin Kramer1292c222010-12-04 20:32:23 +0000459 if (Subtarget->hasPOPCNT()) {
460 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
461 } else {
462 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
463 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
464 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
465 if (Subtarget->is64Bit())
466 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
467 }
468
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
470 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000471
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000472 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000473 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000474 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000475 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000476 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
478 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
479 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
480 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
481 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000482 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
484 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
485 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
486 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000487 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000489 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000490 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Hal Finkele9150472013-03-27 19:10:42 +0000492 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Michael Liao6c0e04c2012-10-15 22:39:43 +0000493 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000494 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000495 // other SjLj exception interfaces are implemented and please don't build
496 // your own exception handling based on them.
497 // LLVM/Clang supports zero-cost DWARF exception handling.
498 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
499 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000500
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000501 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
503 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
504 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
505 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000506 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
508 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000509 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000510 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000511 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
512 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
513 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
514 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000515 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000516 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000517 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
519 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
520 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000521 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
523 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
524 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000525 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000526
Craig Topper1accb7e2012-01-10 06:54:16 +0000527 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000529
Eli Friedman14648462011-07-27 22:21:52 +0000530 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000531
Mon P Wang63307c32008-05-05 19:05:59 +0000532 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000533 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000534 MVT VT = IntVTs[i];
535 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
536 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000537 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000538 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000539
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000540 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000541 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
543 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
544 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
545 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
546 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
547 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
548 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000549 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
550 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
551 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
552 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000553 }
554
Eli Friedman43f51ae2011-08-26 21:21:21 +0000555 if (Subtarget->hasCmpxchg16b()) {
556 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
557 }
558
Evan Cheng3c992d22006-03-07 02:02:57 +0000559 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000560 if (!Subtarget->isTargetDarwin() &&
561 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000562 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000564 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000565
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000566 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000567 setExceptionPointerRegister(X86::RAX);
568 setExceptionSelectorRegister(X86::RDX);
569 } else {
570 setExceptionPointerRegister(X86::EAX);
571 setExceptionSelectorRegister(X86::EDX);
572 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
574 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000575
Duncan Sands4a544a72011-09-06 13:37:06 +0000576 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
577 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000578
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000580 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000581
Nate Begemanacc398c2006-01-25 18:21:52 +0000582 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::VASTART , MVT::Other, Custom);
584 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Nico Rieck63e77782013-07-08 11:19:44 +0000585 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::VAARG , MVT::Other, Custom);
587 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000588 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::VAARG , MVT::Other, Expand);
590 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000591 }
Evan Chengae642192007-03-02 23:16:35 +0000592
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
594 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000595
596 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
597 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
598 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000599 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000600 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
601 MVT::i64 : MVT::i32, Custom);
602 else
603 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
604 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000605
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000606 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000607 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000608 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000609 addRegisterClass(MVT::f32, &X86::FR32RegClass);
610 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000611
Evan Cheng223547a2006-01-31 22:28:30 +0000612 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::FABS , MVT::f64, Custom);
614 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000615
616 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::FNEG , MVT::f64, Custom);
618 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000619
Evan Cheng68c47cb2007-01-05 07:55:56 +0000620 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000623
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000624 // Lower this to FGETSIGNx86 plus an AND.
625 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
626 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
627
Evan Chengd25e9e82006-02-02 00:28:23 +0000628 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000629 setOperationAction(ISD::FSIN , MVT::f64, Expand);
630 setOperationAction(ISD::FCOS , MVT::f64, Expand);
631 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
632 setOperationAction(ISD::FSIN , MVT::f32, Expand);
633 setOperationAction(ISD::FCOS , MVT::f32, Expand);
634 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635
Chris Lattnera54aa942006-01-29 06:26:08 +0000636 // Expand FP immediates into loads from the stack, except for the special
637 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638 addLegalFPImmediate(APFloat(+0.0)); // xorpd
639 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000640 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 // Use SSE for f32, x87 for f64.
642 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000643 addRegisterClass(MVT::f32, &X86::FR32RegClass);
644 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000645
646 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000648
649 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000651
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000653
654 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
656 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657
658 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000659 setOperationAction(ISD::FSIN , MVT::f32, Expand);
660 setOperationAction(ISD::FCOS , MVT::f32, Expand);
661 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000662
Nate Begemane1795842008-02-14 08:57:00 +0000663 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000664 addLegalFPImmediate(APFloat(+0.0f)); // xorps
665 addLegalFPImmediate(APFloat(+0.0)); // FLD0
666 addLegalFPImmediate(APFloat(+1.0)); // FLD1
667 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
668 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
669
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000670 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000671 setOperationAction(ISD::FSIN , MVT::f64, Expand);
672 setOperationAction(ISD::FCOS , MVT::f64, Expand);
673 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000674 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000675 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000676 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000677 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000678 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
679 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
682 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
683 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
684 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000685
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000686 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000687 setOperationAction(ISD::FSIN , MVT::f64, Expand);
688 setOperationAction(ISD::FSIN , MVT::f32, Expand);
689 setOperationAction(ISD::FCOS , MVT::f64, Expand);
690 setOperationAction(ISD::FCOS , MVT::f32, Expand);
691 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
692 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000693 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000694 addLegalFPImmediate(APFloat(+0.0)); // FLD0
695 addLegalFPImmediate(APFloat(+1.0)); // FLD1
696 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
697 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000698 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
699 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
700 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
701 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000702 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000703
Cameron Zwarich33390842011-07-08 21:39:21 +0000704 // We don't support FMA.
705 setOperationAction(ISD::FMA, MVT::f64, Expand);
706 setOperationAction(ISD::FMA, MVT::f32, Expand);
707
Dale Johannesen59a58732007-08-05 18:49:15 +0000708 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000709 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000710 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
712 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000713 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000714 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000715 addLegalFPImmediate(TmpFlt); // FLD0
716 TmpFlt.changeSign();
717 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000718
719 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000720 APFloat TmpFlt2(+1.0);
721 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
722 &ignored);
723 addLegalFPImmediate(TmpFlt2); // FLD1
724 TmpFlt2.changeSign();
725 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
726 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000727
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000728 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000729 setOperationAction(ISD::FSIN , MVT::f80, Expand);
730 setOperationAction(ISD::FCOS , MVT::f80, Expand);
731 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000732 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000733
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000734 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
735 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
736 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
737 setOperationAction(ISD::FRINT, MVT::f80, Expand);
738 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000739 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000740 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000741
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000742 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
744 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
745 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000746
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::FLOG, MVT::f80, Expand);
748 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
749 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
750 setOperationAction(ISD::FEXP, MVT::f80, Expand);
751 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000752
Mon P Wangf007a8b2008-11-06 05:31:54 +0000753 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000754 // (for widening) or expand (for scalarization). Then we will selectively
755 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000756 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
757 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000758 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000759 setOperationAction(ISD::ADD , VT, Expand);
760 setOperationAction(ISD::SUB , VT, Expand);
761 setOperationAction(ISD::FADD, VT, Expand);
762 setOperationAction(ISD::FNEG, VT, Expand);
763 setOperationAction(ISD::FSUB, VT, Expand);
764 setOperationAction(ISD::MUL , VT, Expand);
765 setOperationAction(ISD::FMUL, VT, Expand);
766 setOperationAction(ISD::SDIV, VT, Expand);
767 setOperationAction(ISD::UDIV, VT, Expand);
768 setOperationAction(ISD::FDIV, VT, Expand);
769 setOperationAction(ISD::SREM, VT, Expand);
770 setOperationAction(ISD::UREM, VT, Expand);
771 setOperationAction(ISD::LOAD, VT, Expand);
772 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
774 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
775 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
776 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
777 setOperationAction(ISD::FABS, VT, Expand);
778 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000779 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000780 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000781 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000782 setOperationAction(ISD::FREM, VT, Expand);
783 setOperationAction(ISD::FMA, VT, Expand);
784 setOperationAction(ISD::FPOWI, VT, Expand);
785 setOperationAction(ISD::FSQRT, VT, Expand);
786 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
787 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000788 setOperationAction(ISD::FCEIL, VT, Expand);
789 setOperationAction(ISD::FTRUNC, VT, Expand);
790 setOperationAction(ISD::FRINT, VT, Expand);
791 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000792 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
793 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
794 setOperationAction(ISD::SDIVREM, VT, Expand);
795 setOperationAction(ISD::UDIVREM, VT, Expand);
796 setOperationAction(ISD::FPOW, VT, Expand);
797 setOperationAction(ISD::CTPOP, VT, Expand);
798 setOperationAction(ISD::CTTZ, VT, Expand);
799 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
800 setOperationAction(ISD::CTLZ, VT, Expand);
801 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
802 setOperationAction(ISD::SHL, VT, Expand);
803 setOperationAction(ISD::SRA, VT, Expand);
804 setOperationAction(ISD::SRL, VT, Expand);
805 setOperationAction(ISD::ROTL, VT, Expand);
806 setOperationAction(ISD::ROTR, VT, Expand);
807 setOperationAction(ISD::BSWAP, VT, Expand);
808 setOperationAction(ISD::SETCC, VT, Expand);
809 setOperationAction(ISD::FLOG, VT, Expand);
810 setOperationAction(ISD::FLOG2, VT, Expand);
811 setOperationAction(ISD::FLOG10, VT, Expand);
812 setOperationAction(ISD::FEXP, VT, Expand);
813 setOperationAction(ISD::FEXP2, VT, Expand);
814 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
815 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
816 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
817 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
818 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
819 setOperationAction(ISD::TRUNCATE, VT, Expand);
820 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
821 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
822 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
823 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000824 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
825 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000826 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000827 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000828 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
829 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
830 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000831 }
832
Evan Chengc7ce29b2009-02-13 22:36:38 +0000833 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
834 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000835 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000836 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000837 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000838 }
839
Dale Johannesen0488fb62010-09-30 23:57:10 +0000840 // MMX-sized vectors (other than x86mmx) are expected to be expanded
841 // into smaller operations.
842 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
843 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
844 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
845 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
846 setOperationAction(ISD::AND, MVT::v8i8, Expand);
847 setOperationAction(ISD::AND, MVT::v4i16, Expand);
848 setOperationAction(ISD::AND, MVT::v2i32, Expand);
849 setOperationAction(ISD::AND, MVT::v1i64, Expand);
850 setOperationAction(ISD::OR, MVT::v8i8, Expand);
851 setOperationAction(ISD::OR, MVT::v4i16, Expand);
852 setOperationAction(ISD::OR, MVT::v2i32, Expand);
853 setOperationAction(ISD::OR, MVT::v1i64, Expand);
854 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
855 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
856 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
857 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
858 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
859 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
860 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
861 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
863 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
864 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
865 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
866 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000867 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
868 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
869 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
870 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000871
Craig Topper1accb7e2012-01-10 06:54:16 +0000872 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000873 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000874
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
876 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
877 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
878 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
879 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
880 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000881 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000882 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
883 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
884 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
885 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
886 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000887 }
888
Craig Topper1accb7e2012-01-10 06:54:16 +0000889 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000890 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000891
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000892 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
893 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000894 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
895 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
896 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
897 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
900 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
901 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
902 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000903 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
905 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
906 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
907 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
908 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
909 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
910 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
911 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
912 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
913 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
914 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
915 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000916 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000917
Nadav Rotem354efd82011-09-18 14:57:03 +0000918 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000919 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
920 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
921 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000922
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
924 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
925 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
926 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
927 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000928
Evan Cheng2c3ae372006-04-12 21:21:57 +0000929 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000930 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000931 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000932 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000933 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000934 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000935 // Do not attempt to custom lower non-128-bit vectors
936 if (!VT.is128BitVector())
937 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000938 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
939 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
940 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000941 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000942
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
944 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
945 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
946 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
947 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
948 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000949
Nate Begemancdd1eec2008-02-12 22:51:28 +0000950 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
952 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000953 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000954
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000955 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000956 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000957 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000958
959 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000960 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000961 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000962
Craig Topper0d1f1762012-08-12 00:34:56 +0000963 setOperationAction(ISD::AND, VT, Promote);
964 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
965 setOperationAction(ISD::OR, VT, Promote);
966 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
967 setOperationAction(ISD::XOR, VT, Promote);
968 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
969 setOperationAction(ISD::LOAD, VT, Promote);
970 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
971 setOperationAction(ISD::SELECT, VT, Promote);
972 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000973 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000974
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000976
Evan Cheng2c3ae372006-04-12 21:21:57 +0000977 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
979 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
980 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
981 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000982
Owen Anderson825b72b2009-08-11 20:47:22 +0000983 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
984 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000985
Michael Liaoa7554632012-10-23 17:36:08 +0000986 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
987 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000988 // As there is no 64-bit GPR available, we need build a special custom
989 // sequence to convert from v2i32 to v2f32.
990 if (!Subtarget->is64Bit())
991 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000992
Michael Liao9d796db2012-10-10 16:32:15 +0000993 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000994 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000995
Michael Liaob8150d82012-09-10 18:33:51 +0000996 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000997 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000998
Craig Topperd0a31172012-01-10 06:37:29 +0000999 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +00001000 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
1001 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
1002 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
1003 setOperationAction(ISD::FRINT, MVT::f32, Legal);
1004 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
1005 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1006 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1007 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1008 setOperationAction(ISD::FRINT, MVT::f64, Legal);
1009 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1010
Craig Topper12fb5c62012-09-08 17:42:27 +00001011 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001012 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
1013 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
1014 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
1015 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001016 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001017 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
1018 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
1019 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
1020 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001021
Nate Begeman14d12ca2008-02-11 04:19:36 +00001022 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001023 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001024
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001025 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1026 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1027 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1028 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1029 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001030
Nate Begeman14d12ca2008-02-11 04:19:36 +00001031 // i8 and i16 vectors are custom , because the source register and source
1032 // source memory operand types are not the same width. f32 vectors are
1033 // custom since the immediate controlling the insert encodes additional
1034 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1036 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1037 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1038 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001039
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1041 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1042 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1043 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001044
Pete Coopera77214a2011-11-14 19:38:42 +00001045 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001046 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001047 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001048 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1049 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001050 }
1051 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001052
Craig Topper1accb7e2012-01-10 06:54:16 +00001053 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001054 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001055 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001056
Nadav Rotem43012222011-05-11 08:12:09 +00001057 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001058 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001059
Nadav Rotem43012222011-05-11 08:12:09 +00001060 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001061 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001062
Michael Liao5c5f1902013-03-20 02:28:20 +00001063 // In the customized shift lowering, the legal cases in AVX2 will be
1064 // recognized.
1065 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1066 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001067
Michael Liao5c5f1902013-03-20 02:28:20 +00001068 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1069 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001070
Michael Liao5c5f1902013-03-20 02:28:20 +00001071 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001072
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001073 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1074 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001075 }
1076
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001077 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001078 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1079 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1080 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1081 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1082 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1083 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001084
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1087 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001088
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1090 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1091 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1092 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1093 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001094 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001095 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1096 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1097 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1098 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001100 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001101
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1103 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1104 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1105 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1106 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001107 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001108 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1109 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1110 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1111 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001113 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001114
Michael Liaobedcbd42012-10-16 18:14:11 +00001115 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001116 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001117
1118 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1119
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001120 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
Benjamin Kramerb8f0d892013-03-31 12:49:15 +00001121 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Promote);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001122 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001123 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001124
Michael Liaoa7554632012-10-23 17:36:08 +00001125 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1126 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1127 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1128
Michael Liaob8150d82012-09-10 18:33:51 +00001129 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1130
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001131 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1132 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1133
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001134 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1135 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1136
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001137 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001138 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001139
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001140 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1141
Duncan Sands28b77e92011-09-06 19:07:46 +00001142 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1143 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1144 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1145 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001146
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001147 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1148 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1149 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1150
Craig Topperaaa643c2011-11-09 07:28:55 +00001151 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1152 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1153 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1154 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001155
Nadav Rotem0509db22012-12-28 05:45:24 +00001156 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1157 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1158 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1159 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1160 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1161 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001162
Craig Topperbf404372012-08-31 15:40:30 +00001163 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001164 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1165 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1166 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1167 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1168 setOperationAction(ISD::FMA, MVT::f32, Legal);
1169 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001170 }
Craig Topper880ef452012-08-11 22:34:26 +00001171
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001172 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001173 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1174 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1175 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1176 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001177
Craig Topperaaa643c2011-11-09 07:28:55 +00001178 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1179 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1180 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1181 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001182
Craig Topperaaa643c2011-11-09 07:28:55 +00001183 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1184 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1185 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001186 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001187
1188 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001189
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001190 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001191 } else {
1192 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1193 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1194 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1195 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1196
1197 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1198 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1199 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1200 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1201
1202 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1203 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1204 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1205 // Don't lower v32i8 because there is no 128-bit byte mul
1206 }
Craig Topper13894fa2011-08-24 06:14:18 +00001207
Michael Liao5c5f1902013-03-20 02:28:20 +00001208 // In the customized shift lowering, the legal cases in AVX2 will be
1209 // recognized.
1210 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1211 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1212
1213 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1214 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1215
1216 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1217
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001218 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001219 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1220 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001221 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001222
1223 // Extract subvector is special because the value type
1224 // (result) is 128-bit but the source is 256-bit wide.
1225 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001226 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001227
1228 // Do not attempt to custom lower other non-256-bit vectors
1229 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001230 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001231
Craig Topper0d1f1762012-08-12 00:34:56 +00001232 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1233 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1234 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1235 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1236 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1237 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1238 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001239 }
1240
David Greene54d8eba2011-01-27 22:38:56 +00001241 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001242 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001243 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001244
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001245 // Do not attempt to promote non-256-bit vectors
1246 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001247 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001248
Craig Topper0d1f1762012-08-12 00:34:56 +00001249 setOperationAction(ISD::AND, VT, Promote);
1250 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1251 setOperationAction(ISD::OR, VT, Promote);
1252 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1253 setOperationAction(ISD::XOR, VT, Promote);
1254 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1255 setOperationAction(ISD::LOAD, VT, Promote);
1256 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1257 setOperationAction(ISD::SELECT, VT, Promote);
1258 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001259 }
David Greene9b9838d2009-06-29 16:47:10 +00001260 }
1261
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001262 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1263 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001264 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1265 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001266 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1267 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001268 }
1269
Evan Cheng6be2c582006-04-05 23:38:46 +00001270 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001272 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001273
Eli Friedman962f5492010-06-02 19:35:46 +00001274 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1275 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001276 //
Eli Friedman962f5492010-06-02 19:35:46 +00001277 // FIXME: We really should do custom legalization for addition and
1278 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1279 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001280 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1281 // Add/Sub/Mul with overflow operations are custom lowered.
1282 MVT VT = IntVTs[i];
1283 setOperationAction(ISD::SADDO, VT, Custom);
1284 setOperationAction(ISD::UADDO, VT, Custom);
1285 setOperationAction(ISD::SSUBO, VT, Custom);
1286 setOperationAction(ISD::USUBO, VT, Custom);
1287 setOperationAction(ISD::SMULO, VT, Custom);
1288 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001289 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001290
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001291 // There are no 8-bit 3-address imul/mul instructions
1292 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1293 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001294
Evan Chengd54f2d52009-03-31 19:38:51 +00001295 if (!Subtarget->is64Bit()) {
1296 // These libcalls are not available in 32-bit.
1297 setLibcallName(RTLIB::SHL_I128, 0);
1298 setLibcallName(RTLIB::SRL_I128, 0);
1299 setLibcallName(RTLIB::SRA_I128, 0);
1300 }
1301
Evan Cheng8688a582013-01-29 02:32:37 +00001302 // Combine sin / cos into one node or libcall if possible.
1303 if (Subtarget->hasSinCos()) {
1304 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1305 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001306 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001307 // For MacOSX, we don't want to the normal expansion of a libcall to
1308 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1309 // traffic.
1310 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1311 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1312 }
1313 }
1314
Evan Cheng206ee9d2006-07-07 08:33:52 +00001315 // We have target-specific dag combine patterns for the following nodes:
1316 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001317 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001318 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001319 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001320 setTargetDAGCombine(ISD::SHL);
1321 setTargetDAGCombine(ISD::SRA);
1322 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001323 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001324 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001325 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001326 setTargetDAGCombine(ISD::FADD);
1327 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001328 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001329 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001330 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001331 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001332 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001333 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001334 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001335 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001336 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001337 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001338 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001339 if (Subtarget->is64Bit())
1340 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001341 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001342
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001343 computeRegisterProperties();
1344
Evan Cheng05219282011-01-06 06:52:41 +00001345 // On Darwin, -Os means optimize for size without hurting performance,
1346 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001347 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1348 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1349 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1350 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1351 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1352 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001353 setPrefLoopAlignment(4); // 2^4 bytes.
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001354
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001355 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001356 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001357
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001358 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001359}
1360
Matt Arsenault225ed702013-05-18 00:21:46 +00001361EVT X86TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00001362 if (!VT.isVector()) return MVT::i8;
1363 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001364}
1365
Evan Cheng29286502008-01-23 23:17:41 +00001366/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1367/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001368static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001369 if (MaxAlign == 16)
1370 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001371 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001372 if (VTy->getBitWidth() == 128)
1373 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001374 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001375 unsigned EltAlign = 0;
1376 getMaxByValAlign(ATy->getElementType(), EltAlign);
1377 if (EltAlign > MaxAlign)
1378 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001379 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001380 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1381 unsigned EltAlign = 0;
1382 getMaxByValAlign(STy->getElementType(i), EltAlign);
1383 if (EltAlign > MaxAlign)
1384 MaxAlign = EltAlign;
1385 if (MaxAlign == 16)
1386 break;
1387 }
1388 }
Evan Cheng29286502008-01-23 23:17:41 +00001389}
1390
1391/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1392/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001393/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1394/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001395unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001396 if (Subtarget->is64Bit()) {
1397 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001398 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001399 if (TyAlign > 8)
1400 return TyAlign;
1401 return 8;
1402 }
1403
Evan Cheng29286502008-01-23 23:17:41 +00001404 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001405 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001406 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001407 return Align;
1408}
Chris Lattner2b02a442007-02-25 08:29:00 +00001409
Evan Chengf0df0312008-05-15 08:39:06 +00001410/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001411/// and store operations as a result of memset, memcpy, and memmove
1412/// lowering. If DstAlign is zero that means it's safe to destination
1413/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1414/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001415/// probably because the source does not need to be loaded. If 'IsMemset' is
1416/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1417/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1418/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001419/// It returns EVT::Other if the type should be determined using generic
1420/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001421EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001422X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1423 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001424 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001425 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001426 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001427 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001428 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001429 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1430 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001431 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001432 (Subtarget->isUnalignedMemAccessFast() ||
1433 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001434 (SrcAlign == 0 || SrcAlign >= 16)))) {
1435 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001436 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001437 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001438 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001439 return MVT::v8f32;
1440 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001441 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001442 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001443 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001444 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001445 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001446 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001447 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001448 // Do not use f64 to lower memcpy if source is string constant. It's
1449 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001450 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001451 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001452 }
Evan Chengf0df0312008-05-15 08:39:06 +00001453 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001454 return MVT::i64;
1455 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001456}
1457
Evan Cheng7d342672012-12-12 01:32:07 +00001458bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001459 if (VT == MVT::f32)
1460 return X86ScalarSSEf32;
1461 else if (VT == MVT::f64)
1462 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001463 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001464}
1465
Evan Cheng376642e2012-12-10 23:21:26 +00001466bool
1467X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1468 if (Fast)
1469 *Fast = Subtarget->isUnalignedMemAccessFast();
1470 return true;
1471}
1472
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001473/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1474/// current function. The returned value is a member of the
1475/// MachineJumpTableInfo::JTEntryKind enum.
1476unsigned X86TargetLowering::getJumpTableEncoding() const {
1477 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1478 // symbol.
1479 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1480 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001481 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001482
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001483 // Otherwise, use the normal jump table encoding heuristics.
1484 return TargetLowering::getJumpTableEncoding();
1485}
1486
Chris Lattnerc64daab2010-01-26 05:02:42 +00001487const MCExpr *
1488X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1489 const MachineBasicBlock *MBB,
1490 unsigned uid,MCContext &Ctx) const{
1491 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1492 Subtarget->isPICStyleGOT());
1493 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1494 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001495 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1496 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001497}
1498
Evan Chengcc415862007-11-09 01:32:10 +00001499/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1500/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001501SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001502 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001503 if (!Subtarget->is64Bit())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001504 // This doesn't have SDLoc associated with it, but is not really the
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001505 // same as a Register.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001506 return DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001507 return Table;
1508}
1509
Chris Lattner589c6f62010-01-26 06:28:43 +00001510/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1511/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1512/// MCExpr.
1513const MCExpr *X86TargetLowering::
1514getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1515 MCContext &Ctx) const {
1516 // X86-64 uses RIP relative addressing based on the jump table label.
1517 if (Subtarget->isPICStyleRIPRel())
1518 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1519
1520 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001521 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001522}
1523
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001524// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001525std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001526X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001527 const TargetRegisterClass *RRC = 0;
1528 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001529 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001530 default:
1531 return TargetLowering::findRepresentativeClass(VT);
1532 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001533 RRC = Subtarget->is64Bit() ?
1534 (const TargetRegisterClass*)&X86::GR64RegClass :
1535 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001536 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001537 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001538 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001539 break;
1540 case MVT::f32: case MVT::f64:
1541 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1542 case MVT::v4f32: case MVT::v2f64:
1543 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1544 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001545 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001546 break;
1547 }
1548 return std::make_pair(RRC, Cost);
1549}
1550
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001551bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1552 unsigned &Offset) const {
1553 if (!Subtarget->isTargetLinux())
1554 return false;
1555
1556 if (Subtarget->is64Bit()) {
1557 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1558 Offset = 0x28;
1559 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1560 AddressSpace = 256;
1561 else
1562 AddressSpace = 257;
1563 } else {
1564 // %gs:0x14 on i386
1565 Offset = 0x14;
1566 AddressSpace = 256;
1567 }
1568 return true;
1569}
1570
Chris Lattner2b02a442007-02-25 08:29:00 +00001571//===----------------------------------------------------------------------===//
1572// Return Value Calling Convention Implementation
1573//===----------------------------------------------------------------------===//
1574
Chris Lattner59ed56b2007-02-28 04:55:35 +00001575#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001576
Michael J. Spencerec38de22010-10-10 22:04:20 +00001577bool
Eric Christopher471e4222011-06-08 23:55:35 +00001578X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001579 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001580 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001581 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001582 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001583 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001584 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001585 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001586}
1587
Dan Gohman98ca4f22009-08-05 01:29:28 +00001588SDValue
1589X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001592 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001593 SDLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001594 MachineFunction &MF = DAG.getMachineFunction();
1595 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Chris Lattner9774c912007-02-27 05:28:59 +00001597 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001598 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599 RVLocs, *DAG.getContext());
1600 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Dan Gohman475871a2008-07-27 21:46:04 +00001602 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001603 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001604 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1605 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001606 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1607 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001608
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001609 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001610 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1611 CCValAssign &VA = RVLocs[i];
1612 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001613 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001614 EVT ValVT = ValToCopy.getValueType();
1615
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001616 // Promote values to the appropriate types
1617 if (VA.getLocInfo() == CCValAssign::SExt)
1618 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1619 else if (VA.getLocInfo() == CCValAssign::ZExt)
1620 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1621 else if (VA.getLocInfo() == CCValAssign::AExt)
1622 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1623 else if (VA.getLocInfo() == CCValAssign::BCvt)
1624 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1625
Dale Johannesenc4510512010-09-24 19:05:48 +00001626 // If this is x86-64, and we disabled SSE, we can't return FP values,
1627 // or SSE or MMX vectors.
1628 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1629 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001630 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001631 report_fatal_error("SSE register return with SSE disabled");
1632 }
1633 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1634 // llvm-gcc has never done it right and no one has noticed, so this
1635 // should be OK for now.
1636 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001637 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001638 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001639
Chris Lattner447ff682008-03-11 03:23:40 +00001640 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1641 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001642 if (VA.getLocReg() == X86::ST0 ||
1643 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001644 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1645 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001646 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001648 RetOps.push_back(ValToCopy);
1649 // Don't emit a copytoreg.
1650 continue;
1651 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001652
Evan Cheng242b38b2009-02-23 09:03:22 +00001653 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1654 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001655 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001656 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001657 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001658 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001659 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1660 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001661 // If we don't have SSE2 available, convert to v4f32 so the generated
1662 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001663 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001664 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001665 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001666 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001667 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001668
Dale Johannesendd64c412009-02-04 00:33:20 +00001669 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001670 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001671 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001672 }
Dan Gohman61a92132008-04-21 23:59:07 +00001673
Eli Benderskya5597f02013-01-25 22:07:43 +00001674 // The x86-64 ABIs require that for returning structs by value we copy
1675 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001676 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00001677 // We saved the argument into a virtual register in the entry block,
1678 // so now we copy the value out and into %rax/%eax.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001679 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr() &&
1680 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00001681 MachineFunction &MF = DAG.getMachineFunction();
1682 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1683 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001684 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001685 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001686 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001687
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001688 unsigned RetValReg
1689 = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ?
1690 X86::RAX : X86::EAX;
Eli Benderskya5597f02013-01-25 22:07:43 +00001691 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001692 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001693
Eli Benderskya5597f02013-01-25 22:07:43 +00001694 // RAX/EAX now acts like a return value.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001695 RetOps.push_back(DAG.getRegister(RetValReg, getPointerTy()));
Dan Gohman61a92132008-04-21 23:59:07 +00001696 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001697
Chris Lattner447ff682008-03-11 03:23:40 +00001698 RetOps[0] = Chain; // Update chain.
1699
1700 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001701 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001702 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001703
1704 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001706}
1707
Evan Chengbf010eb2012-04-10 01:51:00 +00001708bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001709 if (N->getNumValues() != 1)
1710 return false;
1711 if (!N->hasNUsesOfValue(1, 0))
1712 return false;
1713
Evan Chengbf010eb2012-04-10 01:51:00 +00001714 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001715 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001716 if (Copy->getOpcode() == ISD::CopyToReg) {
1717 // If the copy has a glue operand, we conservatively assume it isn't safe to
1718 // perform a tail call.
1719 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1720 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001721 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001722 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001723 return false;
1724
Evan Cheng1bf891a2010-12-01 22:59:46 +00001725 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001726 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001727 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001728 if (UI->getOpcode() != X86ISD::RET_FLAG)
1729 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001730 HasRet = true;
1731 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001732
Evan Chengbf010eb2012-04-10 01:51:00 +00001733 if (!HasRet)
1734 return false;
1735
1736 Chain = TCChain;
1737 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001738}
1739
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001740MVT
1741X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001742 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001743 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001744 // TODO: Is this also valid on 32-bit?
1745 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001746 ReturnMVT = MVT::i8;
1747 else
1748 ReturnMVT = MVT::i32;
1749
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001750 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001751 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001752}
1753
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754/// LowerCallResult - Lower the result values of a call into the
1755/// appropriate copies out of appropriate physical registers.
1756///
1757SDValue
1758X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001759 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001761 SDLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001762 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001763
Chris Lattnere32bbf62007-02-28 07:09:55 +00001764 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001765 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001766 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001767 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001768 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001769 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001770
Chris Lattner3085e152007-02-25 08:59:22 +00001771 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001772 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001773 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001774 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001775
Torok Edwin3f142c32009-02-01 18:15:56 +00001776 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001777 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001778 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001779 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001780 }
1781
Evan Cheng79fb3b42009-02-20 20:43:02 +00001782 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001783
1784 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001785 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001786 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001787 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001788 // instead.
1789 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1790 // If we prefer to use the value in xmm registers, copy it out as f80 and
1791 // use a truncate to move it from fp stack reg to xmm reg.
1792 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001793 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001794 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
Michael Liao2a8bea72013-04-19 22:22:57 +00001795 MVT::Other, MVT::Glue, Ops), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001796 Val = Chain.getValue(0);
1797
1798 // Round the f80 to the right size, which also moves it to the appropriate
1799 // xmm register.
1800 if (CopyVT != VA.getValVT())
1801 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1802 // This truncation won't change the value.
1803 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001804 } else {
1805 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1806 CopyVT, InFlag).getValue(1);
1807 Val = Chain.getValue(0);
1808 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001809 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001810 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001811 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001812
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001814}
1815
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001816//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001817// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001818//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001819// StdCall calling convention seems to be standard for many Windows' API
1820// routines and around. It differs from C calling convention just a little:
1821// callee should clean up the stack, not caller. Symbols should be also
1822// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001823// For info on fast calling convention see Fast Calling Convention (tail call)
1824// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001825
Dan Gohman98ca4f22009-08-05 01:29:28 +00001826/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001827/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001828enum StructReturnType {
1829 NotStructReturn,
1830 RegStructReturn,
1831 StackStructReturn
1832};
1833static StructReturnType
1834callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001835 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001836 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001837
Rafael Espindola1cee7102012-07-25 13:41:10 +00001838 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1839 if (!Flags.isSRet())
1840 return NotStructReturn;
1841 if (Flags.isInReg())
1842 return RegStructReturn;
1843 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001844}
1845
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001846/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001847/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001848static StructReturnType
1849argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001851 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001852
Rafael Espindola1cee7102012-07-25 13:41:10 +00001853 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1854 if (!Flags.isSRet())
1855 return NotStructReturn;
1856 if (Flags.isInReg())
1857 return RegStructReturn;
1858 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001859}
1860
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001861/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1862/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001863/// the specific parameter attribute. The copy will be passed as a byval
1864/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001865static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001866CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001867 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001868 SDLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001869 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001870
Dale Johannesendd64c412009-02-04 00:33:20 +00001871 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001872 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001873 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001874}
1875
Chris Lattner29689432010-03-11 00:22:57 +00001876/// IsTailCallConvention - Return true if the calling convention is one that
1877/// supports tail call optimization.
1878static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001879 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1880 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001881}
1882
Evan Cheng485fafc2011-03-21 01:19:09 +00001883bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001884 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001885 return false;
1886
1887 CallSite CS(CI);
1888 CallingConv::ID CalleeCC = CS.getCallingConv();
1889 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1890 return false;
1891
1892 return true;
1893}
1894
Evan Cheng0c439eb2010-01-27 00:07:07 +00001895/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1896/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001897static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1898 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001899 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001900}
1901
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902SDValue
1903X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001904 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001906 SDLoc dl, SelectionDAG &DAG,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001907 const CCValAssign &VA,
1908 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001909 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001910 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001912 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1913 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001914 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001915 EVT ValVT;
1916
1917 // If value is passed by pointer we have address passed instead of the value
1918 // itself.
1919 if (VA.getLocInfo() == CCValAssign::Indirect)
1920 ValVT = VA.getLocVT();
1921 else
1922 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001923
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001924 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001925 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001926 // In case of tail call optimization mark all arguments mutable. Since they
1927 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001928 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001929 unsigned Bytes = Flags.getByValSize();
1930 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1931 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001932 return DAG.getFrameIndex(FI, getPointerTy());
1933 } else {
1934 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001935 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001936 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1937 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001938 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001939 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001940 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001941}
1942
Dan Gohman475871a2008-07-27 21:46:04 +00001943SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001944X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001945 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001946 bool isVarArg,
1947 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001948 SDLoc dl,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001949 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001950 SmallVectorImpl<SDValue> &InVals)
1951 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001952 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001953 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001954
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 const Function* Fn = MF.getFunction();
1956 if (Fn->hasExternalLinkage() &&
1957 Subtarget->isTargetCygMing() &&
1958 Fn->getName() == "main")
1959 FuncInfo->setForceFramePointer(true);
1960
Evan Cheng1bc78042006-04-26 01:20:17 +00001961 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001962 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001963 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001964 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001965
Chris Lattner29689432010-03-11 00:22:57 +00001966 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001967 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001968
Chris Lattner638402b2007-02-28 07:00:42 +00001969 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001970 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001971 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001973
1974 // Allocate shadow area for Win64
1975 if (IsWin64) {
1976 CCInfo.AllocateStack(32, 8);
1977 }
1978
Duncan Sands45907662010-10-31 13:21:44 +00001979 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001980
Chris Lattnerf39f7712007-02-28 05:46:49 +00001981 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001982 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001983 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1984 CCValAssign &VA = ArgLocs[i];
1985 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1986 // places.
1987 assert(VA.getValNo() != LastVal &&
1988 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001989 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001990 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Chris Lattnerf39f7712007-02-28 05:46:49 +00001992 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001993 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001994 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001996 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001998 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00002000 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00002002 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002003 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002004 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002005 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002006 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00002007 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002008 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002009 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002010 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002011
Devang Patel68e6bee2011-02-21 23:21:26 +00002012 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002014
Chris Lattnerf39f7712007-02-28 05:46:49 +00002015 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2016 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2017 // right size.
2018 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002019 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002020 DAG.getValueType(VA.getValVT()));
2021 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002022 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002023 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002024 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002025 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002026
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002027 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002028 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002029 if (RegVT.isVector())
2030 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2031 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002032 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002033 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002034 } else {
2035 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002037 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002038
2039 // If value is passed via pointer - do a load.
2040 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002041 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002042 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002043
Dan Gohman98ca4f22009-08-05 01:29:28 +00002044 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002045 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002046
Eli Benderskya5597f02013-01-25 22:07:43 +00002047 // The x86-64 ABIs require that for returning structs by value we copy
2048 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002049 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00002050 // Save the argument into a virtual register so that we can access it
2051 // from the return points.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002052 if (MF.getFunction()->hasStructRetAttr() &&
2053 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00002054 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2055 unsigned Reg = FuncInfo->getSRetReturnReg();
2056 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002057 MVT PtrTy = getPointerTy();
2058 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002059 FuncInfo->setSRetReturnReg(Reg);
2060 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002061 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002062 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002063 }
2064
Chris Lattnerf39f7712007-02-28 05:46:49 +00002065 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002066 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002067 if (FuncIsMadeTailCallSafe(CallConv,
2068 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002069 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002070
Evan Cheng1bc78042006-04-26 01:20:17 +00002071 // If the function takes variable number of arguments, make a frame index for
2072 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002073 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002074 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2075 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002076 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 }
2078 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002079 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2080
2081 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002082 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002083 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002084 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002085 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002086 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2087 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002088 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002089 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2090 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2091 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002092 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002093 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002094
2095 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096 // The XMM registers which might contain var arg parameters are shadowed
2097 // in their paired GPR. So we only need to save the GPR to their home
2098 // slots.
2099 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002100 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002101 } else {
2102 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2103 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002104
Chad Rosier30450e82011-12-22 22:35:21 +00002105 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2106 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002107 }
2108 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2109 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002110
Bill Wendling831737d2012-12-30 10:32:01 +00002111 bool NoImplicitFloatOps = Fn->getAttributes().
2112 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002113 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002114 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002115 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2116 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002117 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002118 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002119 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002120 // Kernel mode asks for SSE to be disabled, so don't push them
2121 // on the stack.
2122 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002123
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002124 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002125 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002126 // Get to the caller-allocated home save location. Add 8 to account
2127 // for the return address.
2128 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002129 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002130 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002131 // Fixup to set vararg frame on shadow area (4 x i64).
2132 if (NumIntRegs < 4)
2133 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002134 } else {
2135 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002136 // registers, then we must store them to their spots on the stack so
2137 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002138 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2139 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2140 FuncInfo->setRegSaveFrameIndex(
2141 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002142 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002143 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002144
Gordon Henriksen86737662008-01-05 16:56:59 +00002145 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002147 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2148 getPointerTy());
2149 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002150 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002151 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2152 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002153 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002154 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002155 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002156 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002157 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002158 MachinePointerInfo::getFixedStack(
2159 FuncInfo->getRegSaveFrameIndex(), Offset),
2160 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002161 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002162 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002164
Dan Gohmanface41a2009-08-16 21:24:25 +00002165 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2166 // Now store the XMM (fp + vector) parameter registers.
2167 SmallVector<SDValue, 11> SaveXMMOps;
2168 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002169
Craig Topperc9099502012-04-20 06:31:50 +00002170 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002171 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2172 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002173
Dan Gohman1e93df62010-04-17 14:41:14 +00002174 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2175 FuncInfo->getRegSaveFrameIndex()));
2176 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2177 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002178
Dan Gohmanface41a2009-08-16 21:24:25 +00002179 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002180 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002181 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002182 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2183 SaveXMMOps.push_back(Val);
2184 }
2185 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2186 MVT::Other,
2187 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002189
2190 if (!MemOps.empty())
2191 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2192 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002193 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002194 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002195
Gordon Henriksen86737662008-01-05 16:56:59 +00002196 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002197 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2198 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002199 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002200 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002201 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002202 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002203 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002204 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002205 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002206 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002207
Gordon Henriksen86737662008-01-05 16:56:59 +00002208 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002209 // RegSaveFrameIndex is X86-64 only.
2210 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002211 if (CallConv == CallingConv::X86_FastCall ||
2212 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002213 // fastcc functions can't have varargs.
2214 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002215 }
Evan Cheng25caf632006-05-23 21:06:34 +00002216
Rafael Espindola76927d752011-08-30 19:39:58 +00002217 FuncInfo->setArgumentStackSize(StackSize);
2218
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002220}
2221
Dan Gohman475871a2008-07-27 21:46:04 +00002222SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002223X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2224 SDValue StackPtr, SDValue Arg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002225 SDLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002226 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002227 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002228 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002229 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002230 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002231 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002232 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002233
2234 return DAG.getStore(Chain, dl, Arg, PtrOff,
2235 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002236 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002237}
2238
Bill Wendling64e87322009-01-16 19:25:27 +00002239/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002241SDValue
2242X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002243 SDValue &OutRetAddr, SDValue Chain,
2244 bool IsTailCall, bool Is64Bit,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002245 int FPDiff, SDLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002246 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002247 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002248 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002249
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002250 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002251 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002252 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002253 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002254}
2255
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002256/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002257/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002258static SDValue
2259EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002260 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002261 unsigned SlotSize, int FPDiff, SDLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002262 // Store the return address to the appropriate stack slot.
2263 if (!FPDiff) return Chain;
2264 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002265 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002266 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002267 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002268 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002269 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002270 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002271 return Chain;
2272}
2273
Dan Gohman98ca4f22009-08-05 01:29:28 +00002274SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002275X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002276 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002277 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002278 SDLoc &dl = CLI.DL;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002279 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2280 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2281 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2282 SDValue Chain = CLI.Chain;
2283 SDValue Callee = CLI.Callee;
2284 CallingConv::ID CallConv = CLI.CallConv;
2285 bool &isTailCall = CLI.IsTailCall;
2286 bool isVarArg = CLI.IsVarArg;
2287
Dan Gohman98ca4f22009-08-05 01:29:28 +00002288 MachineFunction &MF = DAG.getMachineFunction();
2289 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002290 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002291 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002292 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002293 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002294
Nick Lewycky22de16d2012-01-19 00:34:10 +00002295 if (MF.getTarget().Options.DisableTailCalls)
2296 isTailCall = false;
2297
Evan Cheng5f941932010-02-05 02:21:12 +00002298 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002299 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002300 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002301 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002302 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002303 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002304
2305 // Sibcalls are automatically detected tailcalls which do not require
2306 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002307 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002308 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002309
2310 if (isTailCall)
2311 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002312 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002313
Chris Lattner29689432010-03-11 00:22:57 +00002314 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002315 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002316
Chris Lattner638402b2007-02-28 07:00:42 +00002317 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002318 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002319 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002320 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002321
2322 // Allocate shadow area for Win64
2323 if (IsWin64) {
2324 CCInfo.AllocateStack(32, 8);
2325 }
2326
Duncan Sands45907662010-10-31 13:21:44 +00002327 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002328
Chris Lattner423c5f42007-02-28 05:31:48 +00002329 // Get a count of how many bytes are to be pushed on the stack.
2330 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002331 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002332 // This is a sibcall. The memory operands are available in caller's
2333 // own caller's stack.
2334 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002335 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2336 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002337 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002338
Gordon Henriksen86737662008-01-05 16:56:59 +00002339 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002340 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002341 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002342 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2343 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2344
Gordon Henriksen86737662008-01-05 16:56:59 +00002345 FPDiff = NumBytesCallerPushed - NumBytes;
2346
2347 // Set the delta of movement of the returnaddr stackslot.
2348 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002349 if (FPDiff < X86Info->getTCReturnAddrDelta())
2350 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002351 }
2352
Evan Chengf22f9b32010-02-06 03:28:46 +00002353 if (!IsSibcall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002354 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
2355 dl);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002356
Dan Gohman475871a2008-07-27 21:46:04 +00002357 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002358 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002359 if (isTailCall && FPDiff)
2360 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2361 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002362
Dan Gohman475871a2008-07-27 21:46:04 +00002363 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2364 SmallVector<SDValue, 8> MemOpChains;
2365 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002366
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002367 // Walk the register/memloc assignments, inserting copies/loads. In the case
2368 // of tail call optimization arguments are handle later.
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002369 const X86RegisterInfo *RegInfo =
2370 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Chris Lattner423c5f42007-02-28 05:31:48 +00002371 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2372 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002373 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002374 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002375 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002376 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002377
Chris Lattner423c5f42007-02-28 05:31:48 +00002378 // Promote the value if needed.
2379 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002380 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002381 case CCValAssign::Full: break;
2382 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002383 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002384 break;
2385 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002386 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002387 break;
2388 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002389 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002390 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002391 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002392 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2393 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002394 } else
2395 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2396 break;
2397 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002398 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002399 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002400 case CCValAssign::Indirect: {
2401 // Store the argument.
2402 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002403 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002404 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002405 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002406 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002407 Arg = SpillSlot;
2408 break;
2409 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002411
Chris Lattner423c5f42007-02-28 05:31:48 +00002412 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002413 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2414 if (isVarArg && IsWin64) {
2415 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2416 // shadow reg if callee is a varargs function.
2417 unsigned ShadowReg = 0;
2418 switch (VA.getLocReg()) {
2419 case X86::XMM0: ShadowReg = X86::RCX; break;
2420 case X86::XMM1: ShadowReg = X86::RDX; break;
2421 case X86::XMM2: ShadowReg = X86::R8; break;
2422 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002423 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002424 if (ShadowReg)
2425 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002426 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002427 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002428 assert(VA.isMemLoc());
2429 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002430 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2431 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002432 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2433 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002434 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002436
Evan Cheng32fe1032006-05-25 00:59:30 +00002437 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002438 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002439 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002440
Chris Lattner88e1fd52009-07-09 04:24:46 +00002441 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002442 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2443 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002444 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002445 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
Andrew Trickac6d9be2013-05-25 02:42:55 +00002446 DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002447 } else {
2448 // If we are tail calling and generating PIC/GOT style code load the
2449 // address of the callee into ECX. The value in ecx is used as target of
2450 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2451 // for tail calls on PIC/GOT architectures. Normally we would just put the
2452 // address of GOT into ebx and then call target@PLT. But for tail calls
2453 // ebx would be restored (since ebx is callee saved) before jumping to the
2454 // target@PLT.
2455
2456 // Note: The actual moving to ECX is done further down.
2457 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2458 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2459 !G->getGlobal()->hasProtectedVisibility())
2460 Callee = LowerGlobalAddress(Callee, DAG);
2461 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002462 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002463 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002464 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002465
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002466 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 // From AMD64 ABI document:
2468 // For calls that may call functions that use varargs or stdargs
2469 // (prototype-less calls or calls to functions containing ellipsis (...) in
2470 // the declaration) %al is used as hidden argument to specify the number
2471 // of SSE registers used. The contents of %al do not need to match exactly
2472 // the number of registers, but must be an ubound on the number of SSE
2473 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002474
Gordon Henriksen86737662008-01-05 16:56:59 +00002475 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002476 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002477 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2478 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2479 };
2480 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002481 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002482 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002483
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002484 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2485 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002486 }
2487
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002488 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002489 if (isTailCall) {
2490 // Force all the incoming stack arguments to be loaded from the stack
2491 // before any new outgoing arguments are stored to the stack, because the
2492 // outgoing stack slots may alias the incoming argument stack slots, and
2493 // the alias isn't otherwise explicit. This is slightly more conservative
2494 // than necessary, because it means that each store effectively depends
2495 // on every argument instead of just those arguments it would clobber.
2496 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2497
Dan Gohman475871a2008-07-27 21:46:04 +00002498 SmallVector<SDValue, 8> MemOpChains2;
2499 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002500 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002501 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002502 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2503 CCValAssign &VA = ArgLocs[i];
2504 if (VA.isRegLoc())
2505 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002506 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002507 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002508 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002509 // Create frame index.
2510 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002511 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002512 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002513 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002514
Duncan Sands276dcbd2008-03-21 09:14:45 +00002515 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002516 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002517 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002518 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002519 StackPtr = DAG.getCopyFromReg(Chain, dl,
2520 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002521 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002522 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002523
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2525 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002526 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002527 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002528 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002529 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002530 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002531 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002532 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002533 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 }
2535 }
2536
2537 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002539 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002540
2541 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002542 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2543 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002544 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002545 }
2546
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002547 // Build a sequence of copy-to-reg nodes chained together with token chain
2548 // and flag operands which copy the outgoing args into registers.
2549 SDValue InFlag;
2550 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2551 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2552 RegsToPass[i].second, InFlag);
2553 InFlag = Chain.getValue(1);
2554 }
2555
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002556 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2557 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2558 // In the 64-bit large code model, we have to make all calls
2559 // through a register, since the call instruction's 32-bit
2560 // pc-relative offset may not be large enough to hold the whole
2561 // address.
2562 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002563 // If the callee is a GlobalAddress node (quite common, every direct call
2564 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2565 // it.
2566
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002567 // We should use extra load for direct calls to dllimported functions in
2568 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002569 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002570 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002571 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002572 bool ExtraLoad = false;
2573 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002574
Chris Lattner48a7d022009-07-09 05:02:21 +00002575 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2576 // external symbols most go through the PLT in PIC mode. If the symbol
2577 // has hidden or protected visibility, or if it is static or local, then
2578 // we don't need to use the PLT - we can directly call it.
2579 if (Subtarget->isTargetELF() &&
2580 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002581 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002582 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002583 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002584 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002585 (!Subtarget->getTargetTriple().isMacOSX() ||
2586 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002587 // PC-relative references to external symbols should go through $stub,
2588 // unless we're building with the leopard linker or later, which
2589 // automatically synthesizes these stubs.
2590 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002591 } else if (Subtarget->isPICStyleRIPRel() &&
2592 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002593 cast<Function>(GV)->getAttributes().
2594 hasAttribute(AttributeSet::FunctionIndex,
2595 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002596 // If the function is marked as non-lazy, generate an indirect call
2597 // which loads from the GOT directly. This avoids runtime overhead
2598 // at the cost of eager binding (and one extra byte of encoding).
2599 OpFlags = X86II::MO_GOTPCREL;
2600 WrapperKind = X86ISD::WrapperRIP;
2601 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002602 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002603
Devang Patel0d881da2010-07-06 22:08:15 +00002604 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002605 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002606
2607 // Add a wrapper if needed.
2608 if (WrapperKind != ISD::DELETED_NODE)
2609 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2610 // Add extra indirection if needed.
2611 if (ExtraLoad)
2612 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2613 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002614 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002615 }
Bill Wendling056292f2008-09-16 21:48:12 +00002616 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002617 unsigned char OpFlags = 0;
2618
Evan Cheng1bf891a2010-12-01 22:59:46 +00002619 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2620 // external symbols should go through the PLT.
2621 if (Subtarget->isTargetELF() &&
2622 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2623 OpFlags = X86II::MO_PLT;
2624 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002625 (!Subtarget->getTargetTriple().isMacOSX() ||
2626 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002627 // PC-relative references to external symbols should go through $stub,
2628 // unless we're building with the leopard linker or later, which
2629 // automatically synthesizes these stubs.
2630 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002631 }
Eric Christopherfd179292009-08-27 18:07:15 +00002632
Chris Lattner48a7d022009-07-09 05:02:21 +00002633 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2634 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002635 }
2636
Chris Lattnerd96d0722007-02-25 06:40:16 +00002637 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002638 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002639 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002640
Evan Chengf22f9b32010-02-06 03:28:46 +00002641 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002642 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002643 DAG.getIntPtrConstant(0, true), InFlag, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002644 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002645 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002646
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002647 Ops.push_back(Chain);
2648 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002649
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002652
Gordon Henriksen86737662008-01-05 16:56:59 +00002653 // Add argument registers to the end of the list so that they are known live
2654 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002655 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2656 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2657 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002658
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002659 // Add a register mask operand representing the call-preserved registers.
2660 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2661 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2662 assert(Mask && "Missing call preserved mask for calling convention");
2663 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002664
Gabor Greifba36cb52008-08-28 21:40:38 +00002665 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002666 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002667
Dan Gohman98ca4f22009-08-05 01:29:28 +00002668 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002669 // We used to do:
2670 //// If this is the first return lowered for this function, add the regs
2671 //// to the liveout set for the function.
2672 // This isn't right, although it's probably harmless on x86; liveouts
2673 // should be computed from returns not tail calls. Consider a void
2674 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002675 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002676 }
2677
Dale Johannesenace16102009-02-03 19:33:06 +00002678 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002679 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002680
Chris Lattner2d297092006-05-23 18:50:38 +00002681 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002682 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002683 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2684 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002685 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002686 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002687 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002688 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002689 // pops the hidden struct pointer, so we have to push it back.
2690 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002691 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002692 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002693 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002694 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002695
Gordon Henriksenae636f82008-01-03 16:47:34 +00002696 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002697 if (!IsSibcall) {
2698 Chain = DAG.getCALLSEQ_END(Chain,
2699 DAG.getIntPtrConstant(NumBytes, true),
2700 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2701 true),
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002702 InFlag, dl);
Evan Chengf22f9b32010-02-06 03:28:46 +00002703 InFlag = Chain.getValue(1);
2704 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002705
Chris Lattner3085e152007-02-25 08:59:22 +00002706 // Handle result values, copying them out of physregs into vregs that we
2707 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002708 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2709 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002710}
2711
Evan Cheng25ab6902006-09-08 06:48:29 +00002712//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002713// Fast Calling Convention (tail call) implementation
2714//===----------------------------------------------------------------------===//
2715
2716// Like std call, callee cleans arguments, convention except that ECX is
2717// reserved for storing the tail called function address. Only 2 registers are
2718// free for argument passing (inreg). Tail call optimization is performed
2719// provided:
2720// * tailcallopt is enabled
2721// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002722// On X86_64 architecture with GOT-style position independent code only local
2723// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002724// To keep the stack aligned according to platform abi the function
2725// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2726// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002727// If a tail called function callee has more arguments than the caller the
2728// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002729// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002730// original REtADDR, but before the saved framepointer or the spilled registers
2731// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2732// stack layout:
2733// arg1
2734// arg2
2735// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002736// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002737// move area ]
2738// (possible EBP)
2739// ESI
2740// EDI
2741// local1 ..
2742
2743/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2744/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002745unsigned
2746X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2747 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002748 MachineFunction &MF = DAG.getMachineFunction();
2749 const TargetMachine &TM = MF.getTarget();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002750 const X86RegisterInfo *RegInfo =
2751 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002752 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002753 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002754 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002755 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002756 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002757 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2758 // Number smaller than 12 so just add the difference.
2759 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2760 } else {
2761 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002762 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002763 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002764 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002765 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002766}
2767
Evan Cheng5f941932010-02-05 02:21:12 +00002768/// MatchingStackOffset - Return true if the given stack call argument is
2769/// already available in the same position (relatively) of the caller's
2770/// incoming argument stack.
2771static
2772bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2773 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2774 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002775 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2776 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002777 if (Arg.getOpcode() == ISD::CopyFromReg) {
2778 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002779 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002780 return false;
2781 MachineInstr *Def = MRI->getVRegDef(VR);
2782 if (!Def)
2783 return false;
2784 if (!Flags.isByVal()) {
2785 if (!TII->isLoadFromStackSlot(Def, FI))
2786 return false;
2787 } else {
2788 unsigned Opcode = Def->getOpcode();
2789 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2790 Def->getOperand(1).isFI()) {
2791 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002792 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002793 } else
2794 return false;
2795 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002796 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2797 if (Flags.isByVal())
2798 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002799 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002800 // define @foo(%struct.X* %A) {
2801 // tail call @bar(%struct.X* byval %A)
2802 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002803 return false;
2804 SDValue Ptr = Ld->getBasePtr();
2805 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2806 if (!FINode)
2807 return false;
2808 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002809 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002810 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002811 FI = FINode->getIndex();
2812 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002813 } else
2814 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002815
Evan Cheng4cae1332010-03-05 08:38:04 +00002816 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002817 if (!MFI->isFixedObjectIndex(FI))
2818 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002819 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002820}
2821
Dan Gohman98ca4f22009-08-05 01:29:28 +00002822/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2823/// for tail call optimization. Targets which want to do tail call
2824/// optimization should implement this function.
2825bool
2826X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002827 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002828 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002829 bool isCalleeStructRet,
2830 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002831 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002832 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002833 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002834 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002835 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002836 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002837 CalleeCC != CallingConv::C)
2838 return false;
2839
Evan Cheng7096ae42010-01-29 06:45:59 +00002840 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002841 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002842 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002843
2844 // If the function return type is x86_fp80 and the callee return type is not,
2845 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2846 // perform a tailcall optimization here.
2847 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2848 return false;
2849
Evan Cheng13617962010-04-30 01:12:32 +00002850 CallingConv::ID CallerCC = CallerF->getCallingConv();
2851 bool CCMatch = CallerCC == CalleeCC;
2852
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002853 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002854 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002855 return true;
2856 return false;
2857 }
2858
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002859 // Look for obvious safe cases to perform tail call optimization that do not
2860 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002861
Evan Cheng2c12cb42010-03-26 16:26:03 +00002862 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2863 // emit a special epilogue.
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002864 const X86RegisterInfo *RegInfo =
2865 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Evan Cheng2c12cb42010-03-26 16:26:03 +00002866 if (RegInfo->needsStackRealignment(MF))
2867 return false;
2868
Evan Chenga375d472010-03-15 18:54:48 +00002869 // Also avoid sibcall optimization if either caller or callee uses struct
2870 // return semantics.
2871 if (isCalleeStructRet || isCallerStructRet)
2872 return false;
2873
Chad Rosier2416da32011-06-24 21:15:36 +00002874 // An stdcall caller is expected to clean up its arguments; the callee
2875 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002876 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002877 return false;
2878
Chad Rosier871f6642011-05-18 19:59:50 +00002879 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002880 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002881 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002882
2883 // Optimizing for varargs on Win64 is unlikely to be safe without
2884 // additional testing.
2885 if (Subtarget->isTargetWin64())
2886 return false;
2887
Chad Rosier871f6642011-05-18 19:59:50 +00002888 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002889 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002890 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002891
Chad Rosier871f6642011-05-18 19:59:50 +00002892 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2893 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2894 if (!ArgLocs[i].isRegLoc())
2895 return false;
2896 }
2897
Chad Rosier30450e82011-12-22 22:35:21 +00002898 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2899 // stack. Therefore, if it's not used by the call it is not safe to optimize
2900 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002901 bool Unused = false;
2902 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2903 if (!Ins[i].Used) {
2904 Unused = true;
2905 break;
2906 }
2907 }
2908 if (Unused) {
2909 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002910 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002911 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002912 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002913 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002914 CCValAssign &VA = RVLocs[i];
2915 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2916 return false;
2917 }
2918 }
2919
Evan Cheng13617962010-04-30 01:12:32 +00002920 // If the calling conventions do not match, then we'd better make sure the
2921 // results are returned in the same way as what the caller expects.
2922 if (!CCMatch) {
2923 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002924 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002925 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002926 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2927
2928 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002929 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002930 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002931 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2932
2933 if (RVLocs1.size() != RVLocs2.size())
2934 return false;
2935 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2936 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2937 return false;
2938 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2939 return false;
2940 if (RVLocs1[i].isRegLoc()) {
2941 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2942 return false;
2943 } else {
2944 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2945 return false;
2946 }
2947 }
2948 }
2949
Evan Chenga6bff982010-01-30 01:22:00 +00002950 // If the callee takes no arguments then go on to check the results of the
2951 // call.
2952 if (!Outs.empty()) {
2953 // Check if stack adjustment is needed. For now, do not do this if any
2954 // argument is passed on the stack.
2955 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002956 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002957 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002958
2959 // Allocate shadow area for Win64
2960 if (Subtarget->isTargetWin64()) {
2961 CCInfo.AllocateStack(32, 8);
2962 }
2963
Duncan Sands45907662010-10-31 13:21:44 +00002964 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002965 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002966 MachineFunction &MF = DAG.getMachineFunction();
2967 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2968 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002969
2970 // Check if the arguments are already laid out in the right way as
2971 // the caller's fixed stack objects.
2972 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002973 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2974 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002975 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002976 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2977 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002978 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002979 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002980 if (VA.getLocInfo() == CCValAssign::Indirect)
2981 return false;
2982 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002983 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2984 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002985 return false;
2986 }
2987 }
2988 }
Evan Cheng9c044672010-05-29 01:35:22 +00002989
2990 // If the tailcall address may be in a register, then make sure it's
2991 // possible to register allocate for it. In 32-bit, the call address can
2992 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002993 // callee-saved registers are restored. These happen to be the same
2994 // registers used to pass 'inreg' arguments so watch out for those.
2995 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002996 ((!isa<GlobalAddressSDNode>(Callee) &&
2997 !isa<ExternalSymbolSDNode>(Callee)) ||
2998 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002999 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003000 // In PIC we need an extra register to formulate the address computation
3001 // for the callee.
3002 unsigned MaxInRegs =
3003 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
3004
Evan Cheng9c044672010-05-29 01:35:22 +00003005 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3006 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00003007 if (!VA.isRegLoc())
3008 continue;
3009 unsigned Reg = VA.getLocReg();
3010 switch (Reg) {
3011 default: break;
3012 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003013 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00003014 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00003015 break;
Evan Cheng9c044672010-05-29 01:35:22 +00003016 }
3017 }
3018 }
Evan Chenga6bff982010-01-30 01:22:00 +00003019 }
Evan Chengb1712452010-01-27 06:25:16 +00003020
Evan Cheng86809cc2010-02-03 03:28:02 +00003021 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003022}
3023
Dan Gohman3df24e62008-09-03 23:12:08 +00003024FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003025X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3026 const TargetLibraryInfo *libInfo) const {
3027 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003028}
3029
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003030//===----------------------------------------------------------------------===//
3031// Other Lowering Hooks
3032//===----------------------------------------------------------------------===//
3033
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003034static bool MayFoldLoad(SDValue Op) {
3035 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3036}
3037
3038static bool MayFoldIntoStore(SDValue Op) {
3039 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3040}
3041
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003042static bool isTargetShuffle(unsigned Opcode) {
3043 switch(Opcode) {
3044 default: return false;
3045 case X86ISD::PSHUFD:
3046 case X86ISD::PSHUFHW:
3047 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003048 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003049 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003050 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003051 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003052 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003053 case X86ISD::MOVLPS:
3054 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003055 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003056 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003057 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003058 case X86ISD::MOVSS:
3059 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003060 case X86ISD::UNPCKL:
3061 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003062 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003063 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003064 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003065 return true;
3066 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003067}
3068
Andrew Trickac6d9be2013-05-25 02:42:55 +00003069static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003070 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003071 switch(Opc) {
3072 default: llvm_unreachable("Unknown x86 shuffle node");
3073 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003074 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003075 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003076 return DAG.getNode(Opc, dl, VT, V1);
3077 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003078}
3079
Andrew Trickac6d9be2013-05-25 02:42:55 +00003080static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003081 SDValue V1, unsigned TargetMask,
3082 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003083 switch(Opc) {
3084 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003085 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003086 case X86ISD::PSHUFHW:
3087 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003088 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003089 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003090 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3091 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003092}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003093
Andrew Trickac6d9be2013-05-25 02:42:55 +00003094static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003095 SDValue V1, SDValue V2, unsigned TargetMask,
3096 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003097 switch(Opc) {
3098 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003099 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003100 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003101 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003102 return DAG.getNode(Opc, dl, VT, V1, V2,
3103 DAG.getConstant(TargetMask, MVT::i8));
3104 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003105}
3106
Andrew Trickac6d9be2013-05-25 02:42:55 +00003107static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT,
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003108 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3109 switch(Opc) {
3110 default: llvm_unreachable("Unknown x86 shuffle node");
3111 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003112 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003113 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003114 case X86ISD::MOVLPS:
3115 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003116 case X86ISD::MOVSS:
3117 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003118 case X86ISD::UNPCKL:
3119 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003120 return DAG.getNode(Opc, dl, VT, V1, V2);
3121 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003122}
3123
Dan Gohmand858e902010-04-17 15:26:15 +00003124SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003125 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00003126 const X86RegisterInfo *RegInfo =
3127 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003128 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3129 int ReturnAddrIndex = FuncInfo->getRAIndex();
3130
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003131 if (ReturnAddrIndex == 0) {
3132 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003133 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003134 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003135 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003136 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003137 }
3138
Evan Cheng25ab6902006-09-08 06:48:29 +00003139 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003140}
3141
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003142bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3143 bool hasSymbolicDisplacement) {
3144 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003145 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003146 return false;
3147
3148 // If we don't have a symbolic displacement - we don't have any extra
3149 // restrictions.
3150 if (!hasSymbolicDisplacement)
3151 return true;
3152
3153 // FIXME: Some tweaks might be needed for medium code model.
3154 if (M != CodeModel::Small && M != CodeModel::Kernel)
3155 return false;
3156
3157 // For small code model we assume that latest object is 16MB before end of 31
3158 // bits boundary. We may also accept pretty large negative constants knowing
3159 // that all objects are in the positive half of address space.
3160 if (M == CodeModel::Small && Offset < 16*1024*1024)
3161 return true;
3162
3163 // For kernel code model we know that all object resist in the negative half
3164 // of 32bits address space. We may not accept negative offsets, since they may
3165 // be just off and we may accept pretty large positive ones.
3166 if (M == CodeModel::Kernel && Offset > 0)
3167 return true;
3168
3169 return false;
3170}
3171
Evan Chengef41ff62011-06-23 17:54:54 +00003172/// isCalleePop - Determines whether the callee is required to pop its
3173/// own arguments. Callee pop is necessary to support tail calls.
3174bool X86::isCalleePop(CallingConv::ID CallingConv,
3175 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3176 if (IsVarArg)
3177 return false;
3178
3179 switch (CallingConv) {
3180 default:
3181 return false;
3182 case CallingConv::X86_StdCall:
3183 return !is64Bit;
3184 case CallingConv::X86_FastCall:
3185 return !is64Bit;
3186 case CallingConv::X86_ThisCall:
3187 return !is64Bit;
3188 case CallingConv::Fast:
3189 return TailCallOpt;
3190 case CallingConv::GHC:
3191 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003192 case CallingConv::HiPE:
3193 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003194 }
3195}
3196
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003197/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3198/// specific condition code, returning the condition code and the LHS/RHS of the
3199/// comparison to make.
3200static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3201 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003202 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003203 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3204 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3205 // X > -1 -> X == 0, jump !sign.
3206 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003207 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003208 }
3209 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003210 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003211 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003212 }
3213 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003214 // X < 1 -> X <= 0
3215 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003216 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003217 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003218 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003219
Evan Chengd9558e02006-01-06 00:43:03 +00003220 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003221 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003222 case ISD::SETEQ: return X86::COND_E;
3223 case ISD::SETGT: return X86::COND_G;
3224 case ISD::SETGE: return X86::COND_GE;
3225 case ISD::SETLT: return X86::COND_L;
3226 case ISD::SETLE: return X86::COND_LE;
3227 case ISD::SETNE: return X86::COND_NE;
3228 case ISD::SETULT: return X86::COND_B;
3229 case ISD::SETUGT: return X86::COND_A;
3230 case ISD::SETULE: return X86::COND_BE;
3231 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003232 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003233 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003234
Chris Lattner4c78e022008-12-23 23:42:27 +00003235 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003236
Chris Lattner4c78e022008-12-23 23:42:27 +00003237 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003238 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3239 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003240 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3241 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003242 }
3243
Chris Lattner4c78e022008-12-23 23:42:27 +00003244 switch (SetCCOpcode) {
3245 default: break;
3246 case ISD::SETOLT:
3247 case ISD::SETOLE:
3248 case ISD::SETUGT:
3249 case ISD::SETUGE:
3250 std::swap(LHS, RHS);
3251 break;
3252 }
3253
3254 // On a floating point condition, the flags are set as follows:
3255 // ZF PF CF op
3256 // 0 | 0 | 0 | X > Y
3257 // 0 | 0 | 1 | X < Y
3258 // 1 | 0 | 0 | X == Y
3259 // 1 | 1 | 1 | unordered
3260 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003261 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003262 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003263 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003264 case ISD::SETOLT: // flipped
3265 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003266 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003267 case ISD::SETOLE: // flipped
3268 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003269 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003270 case ISD::SETUGT: // flipped
3271 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003272 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003273 case ISD::SETUGE: // flipped
3274 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003275 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003276 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003277 case ISD::SETNE: return X86::COND_NE;
3278 case ISD::SETUO: return X86::COND_P;
3279 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003280 case ISD::SETOEQ:
3281 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003282 }
Evan Chengd9558e02006-01-06 00:43:03 +00003283}
3284
Evan Cheng4a460802006-01-11 00:33:36 +00003285/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3286/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003287/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003288static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003289 switch (X86CC) {
3290 default:
3291 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003292 case X86::COND_B:
3293 case X86::COND_BE:
3294 case X86::COND_E:
3295 case X86::COND_P:
3296 case X86::COND_A:
3297 case X86::COND_AE:
3298 case X86::COND_NE:
3299 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003300 return true;
3301 }
3302}
3303
Evan Chengeb2f9692009-10-27 19:56:55 +00003304/// isFPImmLegal - Returns true if the target can instruction select the
3305/// specified FP immediate natively. If false, the legalizer will
3306/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003307bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003308 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3309 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3310 return true;
3311 }
3312 return false;
3313}
3314
Nate Begeman9008ca62009-04-27 18:41:29 +00003315/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3316/// the specified range (L, H].
3317static bool isUndefOrInRange(int Val, int Low, int Hi) {
3318 return (Val < 0) || (Val >= Low && Val < Hi);
3319}
3320
3321/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3322/// specified value.
3323static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003324 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003325}
3326
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003327/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003328/// from position Pos and ending in Pos+Size, falls within the specified
3329/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003330static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003331 unsigned Pos, unsigned Size, int Low) {
3332 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003333 if (!isUndefOrEqual(Mask[i], Low))
3334 return false;
3335 return true;
3336}
3337
Nate Begeman9008ca62009-04-27 18:41:29 +00003338/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3339/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3340/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003341static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003342 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003345 return (Mask[0] < 2 && Mask[1] < 2);
3346 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003347}
3348
Nate Begeman9008ca62009-04-27 18:41:29 +00003349/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3350/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003351static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3352 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003353 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003354
Nate Begeman9008ca62009-04-27 18:41:29 +00003355 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003356 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3357 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003358
Evan Cheng506d3df2006-03-29 23:07:14 +00003359 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003360 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003361 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003362 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003363
Craig Toppera9a568a2012-05-02 08:03:44 +00003364 if (VT == MVT::v16i16) {
3365 // Lower quadword copied in order or undef.
3366 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3367 return false;
3368
3369 // Upper quadword shuffled.
3370 for (unsigned i = 12; i != 16; ++i)
3371 if (!isUndefOrInRange(Mask[i], 12, 16))
3372 return false;
3373 }
3374
Evan Cheng506d3df2006-03-29 23:07:14 +00003375 return true;
3376}
3377
Nate Begeman9008ca62009-04-27 18:41:29 +00003378/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3379/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003380static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3381 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003382 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003383
Rafael Espindola15684b22009-04-24 12:40:33 +00003384 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003385 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3386 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003387
Rafael Espindola15684b22009-04-24 12:40:33 +00003388 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003389 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003390 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003391 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003392
Craig Toppera9a568a2012-05-02 08:03:44 +00003393 if (VT == MVT::v16i16) {
3394 // Upper quadword copied in order.
3395 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3396 return false;
3397
3398 // Lower quadword shuffled.
3399 for (unsigned i = 8; i != 12; ++i)
3400 if (!isUndefOrInRange(Mask[i], 8, 12))
3401 return false;
3402 }
3403
Rafael Espindola15684b22009-04-24 12:40:33 +00003404 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003405}
3406
Nate Begemana09008b2009-10-19 02:17:23 +00003407/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3408/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003409static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3410 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003411 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3412 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003413 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003414
Craig Topper0e2037b2012-01-20 05:53:00 +00003415 unsigned NumElts = VT.getVectorNumElements();
3416 unsigned NumLanes = VT.getSizeInBits()/128;
3417 unsigned NumLaneElts = NumElts/NumLanes;
3418
3419 // Do not handle 64-bit element shuffles with palignr.
3420 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003421 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003422
Craig Topper0e2037b2012-01-20 05:53:00 +00003423 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3424 unsigned i;
3425 for (i = 0; i != NumLaneElts; ++i) {
3426 if (Mask[i+l] >= 0)
3427 break;
3428 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003429
Craig Topper0e2037b2012-01-20 05:53:00 +00003430 // Lane is all undef, go to next lane
3431 if (i == NumLaneElts)
3432 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003433
Craig Topper0e2037b2012-01-20 05:53:00 +00003434 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003435
Craig Topper0e2037b2012-01-20 05:53:00 +00003436 // Make sure its in this lane in one of the sources
3437 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3438 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003439 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003440
3441 // If not lane 0, then we must match lane 0
3442 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3443 return false;
3444
3445 // Correct second source to be contiguous with first source
3446 if (Start >= (int)NumElts)
3447 Start -= NumElts - NumLaneElts;
3448
3449 // Make sure we're shifting in the right direction.
3450 if (Start <= (int)(i+l))
3451 return false;
3452
3453 Start -= i;
3454
3455 // Check the rest of the elements to see if they are consecutive.
3456 for (++i; i != NumLaneElts; ++i) {
3457 int Idx = Mask[i+l];
3458
3459 // Make sure its in this lane
3460 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3461 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3462 return false;
3463
3464 // If not lane 0, then we must match lane 0
3465 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3466 return false;
3467
3468 if (Idx >= (int)NumElts)
3469 Idx -= NumElts - NumLaneElts;
3470
3471 if (!isUndefOrEqual(Idx, Start+i))
3472 return false;
3473
3474 }
Nate Begemana09008b2009-10-19 02:17:23 +00003475 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003476
Nate Begemana09008b2009-10-19 02:17:23 +00003477 return true;
3478}
3479
Craig Topper1a7700a2012-01-19 08:19:12 +00003480/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3481/// the two vector operands have swapped position.
3482static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3483 unsigned NumElems) {
3484 for (unsigned i = 0; i != NumElems; ++i) {
3485 int idx = Mask[i];
3486 if (idx < 0)
3487 continue;
3488 else if (idx < (int)NumElems)
3489 Mask[i] = idx + NumElems;
3490 else
3491 Mask[i] = idx - NumElems;
3492 }
3493}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003494
Craig Topper1a7700a2012-01-19 08:19:12 +00003495/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3496/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3497/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3498/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003499static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003500 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003501 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003502 return false;
3503
Craig Topper1a7700a2012-01-19 08:19:12 +00003504 unsigned NumElems = VT.getVectorNumElements();
3505 unsigned NumLanes = VT.getSizeInBits()/128;
3506 unsigned NumLaneElems = NumElems/NumLanes;
3507
3508 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003509 return false;
3510
3511 // VSHUFPSY divides the resulting vector into 4 chunks.
3512 // The sources are also splitted into 4 chunks, and each destination
3513 // chunk must come from a different source chunk.
3514 //
3515 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3516 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3517 //
3518 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3519 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3520 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003521 // VSHUFPDY divides the resulting vector into 4 chunks.
3522 // The sources are also splitted into 4 chunks, and each destination
3523 // chunk must come from a different source chunk.
3524 //
3525 // SRC1 => X3 X2 X1 X0
3526 // SRC2 => Y3 Y2 Y1 Y0
3527 //
3528 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3529 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003530 unsigned HalfLaneElems = NumLaneElems/2;
3531 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3532 for (unsigned i = 0; i != NumLaneElems; ++i) {
3533 int Idx = Mask[i+l];
3534 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3535 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3536 return false;
3537 // For VSHUFPSY, the mask of the second half must be the same as the
3538 // first but with the appropriate offsets. This works in the same way as
3539 // VPERMILPS works with masks.
3540 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3541 continue;
3542 if (!isUndefOrEqual(Idx, Mask[i]+l))
3543 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003544 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003545 }
3546
3547 return true;
3548}
3549
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003550/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3551/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003552static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003553 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003554 return false;
3555
Craig Topper7a9a28b2012-08-12 02:23:29 +00003556 unsigned NumElems = VT.getVectorNumElements();
3557
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003558 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003559 return false;
3560
Evan Cheng2064a2b2006-03-28 06:50:32 +00003561 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003562 return isUndefOrEqual(Mask[0], 6) &&
3563 isUndefOrEqual(Mask[1], 7) &&
3564 isUndefOrEqual(Mask[2], 2) &&
3565 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003566}
3567
Nate Begeman0b10b912009-11-07 23:17:15 +00003568/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3569/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3570/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003571static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003572 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003573 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003574
Craig Topper7a9a28b2012-08-12 02:23:29 +00003575 unsigned NumElems = VT.getVectorNumElements();
3576
Nate Begeman0b10b912009-11-07 23:17:15 +00003577 if (NumElems != 4)
3578 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003579
Craig Topperdd637ae2012-02-19 05:41:45 +00003580 return isUndefOrEqual(Mask[0], 2) &&
3581 isUndefOrEqual(Mask[1], 3) &&
3582 isUndefOrEqual(Mask[2], 2) &&
3583 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003584}
3585
Evan Cheng5ced1d82006-04-06 23:23:56 +00003586/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3587/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003588static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003589 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003590 return false;
3591
Craig Topperdd637ae2012-02-19 05:41:45 +00003592 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003593
Evan Cheng5ced1d82006-04-06 23:23:56 +00003594 if (NumElems != 2 && NumElems != 4)
3595 return false;
3596
Chad Rosier238ae312012-04-30 17:47:15 +00003597 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003598 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003599 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003600
Chad Rosier238ae312012-04-30 17:47:15 +00003601 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003602 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003603 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003604
3605 return true;
3606}
3607
Nate Begeman0b10b912009-11-07 23:17:15 +00003608/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3609/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003610static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003611 if (!VT.is128BitVector())
3612 return false;
3613
Craig Topperdd637ae2012-02-19 05:41:45 +00003614 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003615
Craig Topper7a9a28b2012-08-12 02:23:29 +00003616 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003617 return false;
3618
Chad Rosier238ae312012-04-30 17:47:15 +00003619 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003620 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003621 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003622
Chad Rosier238ae312012-04-30 17:47:15 +00003623 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3624 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003625 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003626
3627 return true;
3628}
3629
Elena Demikhovsky15963732012-06-26 08:04:10 +00003630//
3631// Some special combinations that can be optimized.
3632//
3633static
3634SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3635 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003636 MVT VT = SVOp->getValueType(0).getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003637 SDLoc dl(SVOp);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003638
3639 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3640 return SDValue();
3641
3642 ArrayRef<int> Mask = SVOp->getMask();
3643
3644 // These are the special masks that may be optimized.
3645 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3646 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3647 bool MatchEvenMask = true;
3648 bool MatchOddMask = true;
3649 for (int i=0; i<8; ++i) {
3650 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3651 MatchEvenMask = false;
3652 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3653 MatchOddMask = false;
3654 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003655
Elena Demikhovsky32510202012-09-04 12:49:02 +00003656 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003657 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003658
Elena Demikhovsky15963732012-06-26 08:04:10 +00003659 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3660
Elena Demikhovsky32510202012-09-04 12:49:02 +00003661 SDValue Op0 = SVOp->getOperand(0);
3662 SDValue Op1 = SVOp->getOperand(1);
3663
3664 if (MatchEvenMask) {
3665 // Shift the second operand right to 32 bits.
3666 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3667 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3668 } else {
3669 // Shift the first operand left to 32 bits.
3670 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3671 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3672 }
3673 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3674 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003675}
3676
Evan Cheng0038e592006-03-28 00:39:58 +00003677/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3678/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003679static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003680 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003681 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003682
3683 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3684 "Unsupported vector type for unpckh");
3685
Craig Topper5a529e42013-01-18 06:44:29 +00003686 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003687 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003688 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003689
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003690 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3691 // independently on 128-bit lanes.
3692 unsigned NumLanes = VT.getSizeInBits()/128;
3693 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003694
Craig Topper94438ba2011-12-16 08:06:31 +00003695 for (unsigned l = 0; l != NumLanes; ++l) {
3696 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3697 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003698 i += 2, ++j) {
3699 int BitI = Mask[i];
3700 int BitI1 = Mask[i+1];
3701 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003702 return false;
David Greenea20244d2011-03-02 17:23:43 +00003703 if (V2IsSplat) {
3704 if (!isUndefOrEqual(BitI1, NumElts))
3705 return false;
3706 } else {
3707 if (!isUndefOrEqual(BitI1, j + NumElts))
3708 return false;
3709 }
Evan Cheng39623da2006-04-20 08:58:49 +00003710 }
Evan Cheng0038e592006-03-28 00:39:58 +00003711 }
David Greenea20244d2011-03-02 17:23:43 +00003712
Evan Cheng0038e592006-03-28 00:39:58 +00003713 return true;
3714}
3715
Evan Cheng4fcb9222006-03-28 02:43:26 +00003716/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3717/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003718static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003719 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003720 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003721
3722 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3723 "Unsupported vector type for unpckh");
3724
Craig Topper5a529e42013-01-18 06:44:29 +00003725 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003726 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003727 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003728
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003729 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3730 // independently on 128-bit lanes.
3731 unsigned NumLanes = VT.getSizeInBits()/128;
3732 unsigned NumLaneElts = NumElts/NumLanes;
3733
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003734 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003735 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3736 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003737 int BitI = Mask[i];
3738 int BitI1 = Mask[i+1];
3739 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003740 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003741 if (V2IsSplat) {
3742 if (isUndefOrEqual(BitI1, NumElts))
3743 return false;
3744 } else {
3745 if (!isUndefOrEqual(BitI1, j+NumElts))
3746 return false;
3747 }
Evan Cheng39623da2006-04-20 08:58:49 +00003748 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003749 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003750 return true;
3751}
3752
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003753/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3754/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3755/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003756static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003757 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003758 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003759
3760 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3761 "Unsupported vector type for unpckh");
3762
Craig Topper5a529e42013-01-18 06:44:29 +00003763 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003764 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003765 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003766
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003767 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3768 // FIXME: Need a better way to get rid of this, there's no latency difference
3769 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3770 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003771 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003772 return false;
3773
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003774 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3775 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003776 unsigned NumLanes = VT.getSizeInBits()/128;
3777 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003778
Craig Topper94438ba2011-12-16 08:06:31 +00003779 for (unsigned l = 0; l != NumLanes; ++l) {
3780 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3781 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003782 i += 2, ++j) {
3783 int BitI = Mask[i];
3784 int BitI1 = Mask[i+1];
3785
3786 if (!isUndefOrEqual(BitI, j))
3787 return false;
3788 if (!isUndefOrEqual(BitI1, j))
3789 return false;
3790 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003791 }
David Greenea20244d2011-03-02 17:23:43 +00003792
Rafael Espindola15684b22009-04-24 12:40:33 +00003793 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003794}
3795
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003796/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3797/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3798/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003799static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003800 unsigned NumElts = VT.getVectorNumElements();
3801
3802 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3803 "Unsupported vector type for unpckh");
3804
Craig Topper5a529e42013-01-18 06:44:29 +00003805 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003806 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003807 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003808
Craig Topper94438ba2011-12-16 08:06:31 +00003809 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3810 // independently on 128-bit lanes.
3811 unsigned NumLanes = VT.getSizeInBits()/128;
3812 unsigned NumLaneElts = NumElts/NumLanes;
3813
3814 for (unsigned l = 0; l != NumLanes; ++l) {
3815 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3816 i != (l+1)*NumLaneElts; i += 2, ++j) {
3817 int BitI = Mask[i];
3818 int BitI1 = Mask[i+1];
3819 if (!isUndefOrEqual(BitI, j))
3820 return false;
3821 if (!isUndefOrEqual(BitI1, j))
3822 return false;
3823 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003824 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003825 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003826}
3827
Evan Cheng017dcc62006-04-21 01:05:10 +00003828/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3829/// specifies a shuffle of elements that is suitable for input to MOVSS,
3830/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003831static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003832 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003833 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003834 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003835 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003836
Craig Topperc612d792012-01-02 09:17:37 +00003837 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003838
Nate Begeman9008ca62009-04-27 18:41:29 +00003839 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003840 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003841
Craig Topperc612d792012-01-02 09:17:37 +00003842 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003844 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003845
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003846 return true;
3847}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003848
Craig Topper70b883b2011-11-28 10:14:51 +00003849/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003850/// as permutations between 128-bit chunks or halves. As an example: this
3851/// shuffle bellow:
3852/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3853/// The first half comes from the second half of V1 and the second half from the
3854/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003855static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3856 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003857 return false;
3858
3859 // The shuffle result is divided into half A and half B. In total the two
3860 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3861 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003862 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003863 bool MatchA = false, MatchB = false;
3864
3865 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003866 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003867 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3868 MatchA = true;
3869 break;
3870 }
3871 }
3872
3873 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003874 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003875 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3876 MatchB = true;
3877 break;
3878 }
3879 }
3880
3881 return MatchA && MatchB;
3882}
3883
Craig Topper70b883b2011-11-28 10:14:51 +00003884/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3885/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003886static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003887 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003888
Craig Topperc612d792012-01-02 09:17:37 +00003889 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003890
Craig Topperc612d792012-01-02 09:17:37 +00003891 unsigned FstHalf = 0, SndHalf = 0;
3892 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003893 if (SVOp->getMaskElt(i) > 0) {
3894 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3895 break;
3896 }
3897 }
Craig Topperc612d792012-01-02 09:17:37 +00003898 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003899 if (SVOp->getMaskElt(i) > 0) {
3900 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3901 break;
3902 }
3903 }
3904
3905 return (FstHalf | (SndHalf << 4));
3906}
3907
Craig Topper70b883b2011-11-28 10:14:51 +00003908/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003909/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3910/// Note that VPERMIL mask matching is different depending whether theunderlying
3911/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3912/// to the same elements of the low, but to the higher half of the source.
3913/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003914/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003915static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3916 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003917 return false;
3918
Craig Topperc612d792012-01-02 09:17:37 +00003919 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003920 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003921 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003922 return false;
3923
Craig Topperc612d792012-01-02 09:17:37 +00003924 unsigned NumLanes = VT.getSizeInBits()/128;
3925 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003926 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003927 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003928 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003929 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003930 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003931 continue;
3932 // VPERMILPS handling
3933 if (Mask[i] < 0)
3934 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003935 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003936 return false;
3937 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003938 }
3939
3940 return true;
3941}
3942
Craig Topper5aaffa82012-02-19 02:53:47 +00003943/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003944/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003945/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003946static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003948 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003949 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003950
3951 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003952 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003953 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003954
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003956 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003957
Craig Topperc612d792012-01-02 09:17:37 +00003958 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3960 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3961 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003962 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003963
Evan Cheng39623da2006-04-20 08:58:49 +00003964 return true;
3965}
3966
Evan Chengd9539472006-04-14 21:59:03 +00003967/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3968/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003969/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003970static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003971 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003972 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003973 return false;
3974
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003975 unsigned NumElems = VT.getVectorNumElements();
3976
Craig Topper5a529e42013-01-18 06:44:29 +00003977 if ((VT.is128BitVector() && NumElems != 4) ||
3978 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003979 return false;
3980
3981 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003982 for (unsigned i = 0; i != NumElems; i += 2)
3983 if (!isUndefOrEqual(Mask[i], i+1) ||
3984 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003985 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003986
3987 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003988}
3989
3990/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3991/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003992/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003993static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003994 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003995 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003996 return false;
3997
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003998 unsigned NumElems = VT.getVectorNumElements();
3999
Craig Topper5a529e42013-01-18 06:44:29 +00004000 if ((VT.is128BitVector() && NumElems != 4) ||
4001 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004002 return false;
4003
4004 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00004005 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00004006 if (!isUndefOrEqual(Mask[i], i) ||
4007 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004008 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004009
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004010 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004011}
4012
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004013/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4014/// specifies a shuffle of elements that is suitable for input to 256-bit
4015/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004016static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4017 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00004018 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004019
Craig Topper7a9a28b2012-08-12 02:23:29 +00004020 unsigned NumElts = VT.getVectorNumElements();
4021 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004022 return false;
4023
Craig Topperc612d792012-01-02 09:17:37 +00004024 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004025 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004026 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004027 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004028 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004029 return false;
4030 return true;
4031}
4032
Evan Cheng0b457f02008-09-25 20:50:48 +00004033/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004034/// specifies a shuffle of elements that is suitable for input to 128-bit
4035/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004036static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004037 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004038 return false;
4039
Craig Topperc612d792012-01-02 09:17:37 +00004040 unsigned e = VT.getVectorNumElements() / 2;
4041 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004042 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004043 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004044 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004045 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004046 return false;
4047 return true;
4048}
4049
David Greenec38a03e2011-02-03 15:50:00 +00004050/// isVEXTRACTF128Index - Return true if the specified
4051/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4052/// suitable for input to VEXTRACTF128.
4053bool X86::isVEXTRACTF128Index(SDNode *N) {
4054 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4055 return false;
4056
4057 // The index should be aligned on a 128-bit boundary.
4058 uint64_t Index =
4059 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4060
Craig Topper5141d972013-01-18 08:41:28 +00004061 MVT VT = N->getValueType(0).getSimpleVT();
4062 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004063 bool Result = (Index * ElSize) % 128 == 0;
4064
4065 return Result;
4066}
4067
David Greeneccacdc12011-02-04 16:08:29 +00004068/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4069/// operand specifies a subvector insert that is suitable for input to
4070/// VINSERTF128.
4071bool X86::isVINSERTF128Index(SDNode *N) {
4072 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4073 return false;
4074
4075 // The index should be aligned on a 128-bit boundary.
4076 uint64_t Index =
4077 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4078
Craig Topper5141d972013-01-18 08:41:28 +00004079 MVT VT = N->getValueType(0).getSimpleVT();
4080 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004081 bool Result = (Index * ElSize) % 128 == 0;
4082
4083 return Result;
4084}
4085
Evan Cheng63d33002006-03-22 08:01:21 +00004086/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004087/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004088/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004089static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004090 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004091
Craig Topper1a7700a2012-01-19 08:19:12 +00004092 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4093 "Unsupported vector type for PSHUF/SHUFP");
4094
4095 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4096 // independently on 128-bit lanes.
4097 unsigned NumElts = VT.getVectorNumElements();
4098 unsigned NumLanes = VT.getSizeInBits()/128;
4099 unsigned NumLaneElts = NumElts/NumLanes;
4100
4101 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4102 "Only supports 2 or 4 elements per lane");
4103
4104 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004105 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004106 for (unsigned i = 0; i != NumElts; ++i) {
4107 int Elt = N->getMaskElt(i);
4108 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004109 Elt &= NumLaneElts - 1;
4110 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004111 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004112 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004113
Evan Cheng63d33002006-03-22 08:01:21 +00004114 return Mask;
4115}
4116
Evan Cheng506d3df2006-03-29 23:07:14 +00004117/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004118/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004119static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004120 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004121
4122 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4123 "Unsupported vector type for PSHUFHW");
4124
4125 unsigned NumElts = VT.getVectorNumElements();
4126
Evan Cheng506d3df2006-03-29 23:07:14 +00004127 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004128 for (unsigned l = 0; l != NumElts; l += 8) {
4129 // 8 nodes per lane, but we only care about the last 4.
4130 for (unsigned i = 0; i < 4; ++i) {
4131 int Elt = N->getMaskElt(l+i+4);
4132 if (Elt < 0) continue;
4133 Elt &= 0x3; // only 2-bits.
4134 Mask |= Elt << (i * 2);
4135 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004136 }
Craig Topper6b28d352012-05-03 07:12:59 +00004137
Evan Cheng506d3df2006-03-29 23:07:14 +00004138 return Mask;
4139}
4140
4141/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004142/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004143static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004144 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004145
4146 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4147 "Unsupported vector type for PSHUFHW");
4148
4149 unsigned NumElts = VT.getVectorNumElements();
4150
Evan Cheng506d3df2006-03-29 23:07:14 +00004151 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004152 for (unsigned l = 0; l != NumElts; l += 8) {
4153 // 8 nodes per lane, but we only care about the first 4.
4154 for (unsigned i = 0; i < 4; ++i) {
4155 int Elt = N->getMaskElt(l+i);
4156 if (Elt < 0) continue;
4157 Elt &= 0x3; // only 2-bits
4158 Mask |= Elt << (i * 2);
4159 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004160 }
Craig Topper6b28d352012-05-03 07:12:59 +00004161
Evan Cheng506d3df2006-03-29 23:07:14 +00004162 return Mask;
4163}
4164
Nate Begemana09008b2009-10-19 02:17:23 +00004165/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4166/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004167static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004168 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004169 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004170
Craig Topper0e2037b2012-01-20 05:53:00 +00004171 unsigned NumElts = VT.getVectorNumElements();
4172 unsigned NumLanes = VT.getSizeInBits()/128;
4173 unsigned NumLaneElts = NumElts/NumLanes;
4174
4175 int Val = 0;
4176 unsigned i;
4177 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004178 Val = SVOp->getMaskElt(i);
4179 if (Val >= 0)
4180 break;
4181 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004182 if (Val >= (int)NumElts)
4183 Val -= NumElts - NumLaneElts;
4184
Eli Friedman63f8dde2011-07-25 21:36:45 +00004185 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004186 return (Val - i) * EltSize;
4187}
4188
David Greenec38a03e2011-02-03 15:50:00 +00004189/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4190/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4191/// instructions.
4192unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4193 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4194 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4195
4196 uint64_t Index =
4197 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4198
Craig Toppercfcab212013-01-19 08:27:45 +00004199 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4200 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004201
4202 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004203 return Index / NumElemsPerChunk;
4204}
4205
David Greeneccacdc12011-02-04 16:08:29 +00004206/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4207/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4208/// instructions.
4209unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4210 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4211 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4212
4213 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004214 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004215
Craig Toppercfcab212013-01-19 08:27:45 +00004216 MVT VecVT = N->getValueType(0).getSimpleVT();
4217 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004218
4219 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004220 return Index / NumElemsPerChunk;
4221}
4222
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004223/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4224/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4225/// Handles 256-bit.
4226static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004227 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004228
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004229 unsigned NumElts = VT.getVectorNumElements();
4230
Craig Topper095c5282012-04-15 23:48:57 +00004231 assert((VT.is256BitVector() && NumElts == 4) &&
4232 "Unsupported vector type for VPERMQ/VPERMPD");
4233
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004234 unsigned Mask = 0;
4235 for (unsigned i = 0; i != NumElts; ++i) {
4236 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004237 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004238 continue;
4239 Mask |= Elt << (i*2);
4240 }
4241
4242 return Mask;
4243}
Evan Cheng37b73872009-07-30 08:33:02 +00004244/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4245/// constant +0.0.
4246bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004247 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4248 return CN->isNullValue();
4249 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4250 return CFP->getValueAPF().isPosZero();
4251 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004252}
4253
Nate Begeman9008ca62009-04-27 18:41:29 +00004254/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4255/// their permute mask.
4256static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4257 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004258 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004259 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004260 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004261
Nate Begeman5a5ca152009-04-29 05:20:52 +00004262 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004263 int Idx = SVOp->getMaskElt(i);
4264 if (Idx >= 0) {
4265 if (Idx < (int)NumElems)
4266 Idx += NumElems;
4267 else
4268 Idx -= NumElems;
4269 }
4270 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004271 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004272 return DAG.getVectorShuffle(VT, SDLoc(SVOp), SVOp->getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004274}
4275
Evan Cheng533a0aa2006-04-19 20:35:22 +00004276/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4277/// match movhlps. The lower half elements should come from upper half of
4278/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004279/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004280static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004281 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004282 return false;
4283 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004284 return false;
4285 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004286 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004287 return false;
4288 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004289 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004290 return false;
4291 return true;
4292}
4293
Evan Cheng5ced1d82006-04-06 23:23:56 +00004294/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004295/// is promoted to a vector. It also returns the LoadSDNode by reference if
4296/// required.
4297static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004298 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4299 return false;
4300 N = N->getOperand(0).getNode();
4301 if (!ISD::isNON_EXTLoad(N))
4302 return false;
4303 if (LD)
4304 *LD = cast<LoadSDNode>(N);
4305 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004306}
4307
Dan Gohman65fd6562011-11-03 21:49:52 +00004308// Test whether the given value is a vector value which will be legalized
4309// into a load.
4310static bool WillBeConstantPoolLoad(SDNode *N) {
4311 if (N->getOpcode() != ISD::BUILD_VECTOR)
4312 return false;
4313
4314 // Check for any non-constant elements.
4315 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4316 switch (N->getOperand(i).getNode()->getOpcode()) {
4317 case ISD::UNDEF:
4318 case ISD::ConstantFP:
4319 case ISD::Constant:
4320 break;
4321 default:
4322 return false;
4323 }
4324
4325 // Vectors of all-zeros and all-ones are materialized with special
4326 // instructions rather than being loaded.
4327 return !ISD::isBuildVectorAllZeros(N) &&
4328 !ISD::isBuildVectorAllOnes(N);
4329}
4330
Evan Cheng533a0aa2006-04-19 20:35:22 +00004331/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4332/// match movlp{s|d}. The lower half elements should come from lower half of
4333/// V1 (and in order), and the upper half elements should come from the upper
4334/// half of V2 (and in order). And since V1 will become the source of the
4335/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004336static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004337 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004338 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004339 return false;
4340
Evan Cheng466685d2006-10-09 20:57:25 +00004341 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004342 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004343 // Is V2 is a vector load, don't do this transformation. We will try to use
4344 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004345 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004346 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004347
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004348 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004349
Evan Cheng533a0aa2006-04-19 20:35:22 +00004350 if (NumElems != 2 && NumElems != 4)
4351 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004352 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004353 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004354 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004355 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004356 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004357 return false;
4358 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004359}
4360
Evan Cheng39623da2006-04-20 08:58:49 +00004361/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4362/// all the same.
4363static bool isSplatVector(SDNode *N) {
4364 if (N->getOpcode() != ISD::BUILD_VECTOR)
4365 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004366
Dan Gohman475871a2008-07-27 21:46:04 +00004367 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004368 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4369 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004370 return false;
4371 return true;
4372}
4373
Evan Cheng213d2cf2007-05-17 18:45:50 +00004374/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004375/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004376/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004377static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004378 SDValue V1 = N->getOperand(0);
4379 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004380 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4381 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004383 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004385 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4386 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004387 if (Opc != ISD::BUILD_VECTOR ||
4388 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 return false;
4390 } else if (Idx >= 0) {
4391 unsigned Opc = V1.getOpcode();
4392 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4393 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004394 if (Opc != ISD::BUILD_VECTOR ||
4395 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004396 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004397 }
4398 }
4399 return true;
4400}
4401
4402/// getZeroVector - Returns a vector of specified type with all zero elements.
4403///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004404static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004405 SelectionDAG &DAG, SDLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004406 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004407
Dale Johannesen0488fb62010-09-30 23:57:10 +00004408 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004409 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004410 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004411 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004412 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004413 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4414 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4415 } else { // SSE1
4416 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4417 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4418 }
Craig Topper5a529e42013-01-18 06:44:29 +00004419 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004420 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004421 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4422 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004423 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops,
4424 array_lengthof(Ops));
Craig Topper12216172012-01-13 08:12:35 +00004425 } else {
4426 // 256-bit logic and arithmetic instructions in AVX are all
4427 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4428 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4429 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004430 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops,
4431 array_lengthof(Ops));
Craig Topper12216172012-01-13 08:12:35 +00004432 }
Craig Topper9d352402012-04-23 07:24:41 +00004433 } else
4434 llvm_unreachable("Unexpected vector type");
4435
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004436 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004437}
4438
Chris Lattner8a594482007-11-25 00:24:49 +00004439/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004440/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4441/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4442/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004443static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004444 SDLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004445 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004446
Owen Anderson825b72b2009-08-11 20:47:22 +00004447 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004448 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004449 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004450 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004451 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
Michael Liao0ee17002013-04-19 04:03:37 +00004452 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops,
4453 array_lengthof(Ops));
Craig Topper745a86b2011-11-19 22:34:59 +00004454 } else { // AVX
4455 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004456 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004457 }
Craig Topper5a529e42013-01-18 06:44:29 +00004458 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004459 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004460 } else
4461 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004462
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004463 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004464}
4465
Evan Cheng39623da2006-04-20 08:58:49 +00004466/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4467/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004468static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004469 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004470 if (Mask[i] > (int)NumElems) {
4471 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004472 }
Evan Cheng39623da2006-04-20 08:58:49 +00004473 }
Evan Cheng39623da2006-04-20 08:58:49 +00004474}
4475
Evan Cheng017dcc62006-04-21 01:05:10 +00004476/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4477/// operation of specified width.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004478static SDValue getMOVL(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004479 SDValue V2) {
4480 unsigned NumElems = VT.getVectorNumElements();
4481 SmallVector<int, 8> Mask;
4482 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004483 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 Mask.push_back(i);
4485 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004486}
4487
Nate Begeman9008ca62009-04-27 18:41:29 +00004488/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004489static SDValue getUnpackl(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 SDValue V2) {
4491 unsigned NumElems = VT.getVectorNumElements();
4492 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004493 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004494 Mask.push_back(i);
4495 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004496 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004497 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004498}
4499
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004500/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004501static SDValue getUnpackh(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 SDValue V2) {
4503 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004505 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 Mask.push_back(i + Half);
4507 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004508 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004509 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004510}
4511
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004512// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004513// a generic shuffle instruction because the target has no such instructions.
4514// Generate shuffles which repeat i16 and i8 several times until they can be
4515// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004516static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004518 int NumElems = VT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004519 SDLoc dl(V);
Rafael Espindola15684b22009-04-24 12:40:33 +00004520
Nate Begeman9008ca62009-04-27 18:41:29 +00004521 while (NumElems > 4) {
4522 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004523 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004524 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004525 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004526 EltNo -= NumElems/2;
4527 }
4528 NumElems >>= 1;
4529 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004530 return V;
4531}
Eric Christopherfd179292009-08-27 18:07:15 +00004532
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004533/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4534static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4535 EVT VT = V.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004536 SDLoc dl(V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004537
Craig Topper5a529e42013-01-18 06:44:29 +00004538 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004539 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004540 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004541 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4542 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004543 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004544 // To use VPERMILPS to splat scalars, the second half of indicies must
4545 // refer to the higher part, which is a duplication of the lower one,
4546 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004547 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4548 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004549
4550 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4551 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4552 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004553 } else
4554 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004555
4556 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4557}
4558
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004559/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004560static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4561 EVT SrcVT = SV->getValueType(0);
4562 SDValue V1 = SV->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004563 SDLoc dl(SV);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004564
4565 int EltNo = SV->getSplatIndex();
4566 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004567 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004568
Craig Topper5a529e42013-01-18 06:44:29 +00004569 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4570 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004571
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004572 // Extract the 128-bit part containing the splat element and update
4573 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004574 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004575 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4576 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004577 EltNo -= NumElems/2;
4578 }
4579
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004580 // All i16 and i8 vector types can't be used directly by a generic shuffle
4581 // instruction because the target has no such instruction. Generate shuffles
4582 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004583 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004584 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004585 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004586 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004587
4588 // Recreate the 256-bit vector and place the same 128-bit vector
4589 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004590 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004591 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004592 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004593 }
4594
4595 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004596}
4597
Evan Chengba05f722006-04-21 23:03:30 +00004598/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004599/// vector of zero or undef vector. This produces a shuffle where the low
4600/// element of V2 is swizzled into the zero/undef vector, landing at element
4601/// 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 +00004602static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004603 bool IsZero,
4604 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004605 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004606 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004607 SDValue V1 = IsZero
Andrew Trickac6d9be2013-05-25 02:42:55 +00004608 ? getZeroVector(VT, Subtarget, DAG, SDLoc(V2)) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004609 unsigned NumElems = VT.getVectorNumElements();
4610 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004611 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004612 // If this is the insertion idx, put the low elt of V2 here.
4613 MaskVec.push_back(i == Idx ? NumElems : i);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004614 return DAG.getVectorShuffle(VT, SDLoc(V2), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004615}
4616
Craig Toppera1ffc682012-03-20 06:42:26 +00004617/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4618/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004619/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004620static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004621 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004622 unsigned NumElems = VT.getVectorNumElements();
4623 SDValue ImmN;
4624
Craig Topper89f4e662012-03-20 07:17:59 +00004625 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004626 switch(N->getOpcode()) {
4627 case X86ISD::SHUFP:
4628 ImmN = N->getOperand(N->getNumOperands()-1);
4629 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4630 break;
4631 case X86ISD::UNPCKH:
4632 DecodeUNPCKHMask(VT, Mask);
4633 break;
4634 case X86ISD::UNPCKL:
4635 DecodeUNPCKLMask(VT, Mask);
4636 break;
4637 case X86ISD::MOVHLPS:
4638 DecodeMOVHLPSMask(NumElems, Mask);
4639 break;
4640 case X86ISD::MOVLHPS:
4641 DecodeMOVLHPSMask(NumElems, Mask);
4642 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004643 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004644 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004645 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004646 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004647 case X86ISD::PSHUFD:
4648 case X86ISD::VPERMILP:
4649 ImmN = N->getOperand(N->getNumOperands()-1);
4650 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004651 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004652 break;
4653 case X86ISD::PSHUFHW:
4654 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004655 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004656 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004657 break;
4658 case X86ISD::PSHUFLW:
4659 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004660 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004661 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004662 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004663 case X86ISD::VPERMI:
4664 ImmN = N->getOperand(N->getNumOperands()-1);
4665 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4666 IsUnary = true;
4667 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004668 case X86ISD::MOVSS:
4669 case X86ISD::MOVSD: {
4670 // The index 0 always comes from the first element of the second source,
4671 // this is why MOVSS and MOVSD are used in the first place. The other
4672 // elements come from the other positions of the first source vector
4673 Mask.push_back(NumElems);
4674 for (unsigned i = 1; i != NumElems; ++i) {
4675 Mask.push_back(i);
4676 }
4677 break;
4678 }
4679 case X86ISD::VPERM2X128:
4680 ImmN = N->getOperand(N->getNumOperands()-1);
4681 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004682 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004683 break;
4684 case X86ISD::MOVDDUP:
4685 case X86ISD::MOVLHPD:
4686 case X86ISD::MOVLPD:
4687 case X86ISD::MOVLPS:
4688 case X86ISD::MOVSHDUP:
4689 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004690 // Not yet implemented
4691 return false;
4692 default: llvm_unreachable("unknown target shuffle node");
4693 }
4694
4695 return true;
4696}
4697
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004698/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4699/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004700static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004701 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004702 if (Depth == 6)
4703 return SDValue(); // Limit search depth.
4704
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004705 SDValue V = SDValue(N, 0);
4706 EVT VT = V.getValueType();
4707 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004708
4709 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4710 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004711 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004712
Craig Topper3d092db2012-03-21 02:14:01 +00004713 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004714 return DAG.getUNDEF(VT.getVectorElementType());
4715
Craig Topperd156dc12012-02-06 07:17:51 +00004716 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004717 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4718 : SV->getOperand(1);
4719 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004720 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004721
4722 // Recurse into target specific vector shuffles to find scalars.
4723 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004724 MVT ShufVT = V.getValueType().getSimpleVT();
4725 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004726 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004727 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004728
Craig Topperd978c542012-05-06 19:46:21 +00004729 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004730 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004731
Craig Topper3d092db2012-03-21 02:14:01 +00004732 int Elt = ShuffleMask[Index];
4733 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004734 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004735
Craig Topper3d092db2012-03-21 02:14:01 +00004736 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004737 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004738 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004739 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004740 }
4741
4742 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004743 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004744 V = V.getOperand(0);
4745 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004746 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004747
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004748 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004749 return SDValue();
4750 }
4751
4752 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4753 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004754 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004755
4756 if (V.getOpcode() == ISD::BUILD_VECTOR)
4757 return V.getOperand(Index);
4758
4759 return SDValue();
4760}
4761
4762/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4763/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004764/// search can start in two different directions, from left or right.
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004765/// We count undefs as zeros until PreferredNum is reached.
4766static unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp,
4767 unsigned NumElems, bool ZerosFromLeft,
4768 SelectionDAG &DAG,
4769 unsigned PreferredNum = -1U) {
4770 unsigned NumZeros = 0;
4771 for (unsigned i = 0; i != NumElems; ++i) {
4772 unsigned Index = ZerosFromLeft ? i : NumElems - i - 1;
Craig Topper3d092db2012-03-21 02:14:01 +00004773 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004774 if (!Elt.getNode())
4775 break;
4776
4777 if (X86::isZeroNode(Elt))
4778 ++NumZeros;
4779 else if (Elt.getOpcode() == ISD::UNDEF) // Undef as zero up to PreferredNum.
4780 NumZeros = std::min(NumZeros + 1, PreferredNum);
4781 else
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004782 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004783 }
4784
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004785 return NumZeros;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004786}
4787
Craig Topper3d092db2012-03-21 02:14:01 +00004788/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4789/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004790/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4791static
Craig Topper3d092db2012-03-21 02:14:01 +00004792bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4793 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4794 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004795 bool SeenV1 = false;
4796 bool SeenV2 = false;
4797
Craig Topper3d092db2012-03-21 02:14:01 +00004798 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004799 int Idx = SVOp->getMaskElt(i);
4800 // Ignore undef indicies
4801 if (Idx < 0)
4802 continue;
4803
Craig Topper3d092db2012-03-21 02:14:01 +00004804 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004805 SeenV1 = true;
4806 else
4807 SeenV2 = true;
4808
4809 // Only accept consecutive elements from the same vector
4810 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4811 return false;
4812 }
4813
4814 OpNum = SeenV1 ? 0 : 1;
4815 return true;
4816}
4817
4818/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4819/// logical left shift of a vector.
4820static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4821 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4822 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004823 unsigned NumZeros = getNumOfConsecutiveZeros(
4824 SVOp, NumElems, false /* check zeros from right */, DAG,
4825 SVOp->getMaskElt(0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004826 unsigned OpSrc;
4827
4828 if (!NumZeros)
4829 return false;
4830
4831 // Considering the elements in the mask that are not consecutive zeros,
4832 // check if they consecutively come from only one of the source vectors.
4833 //
4834 // V1 = {X, A, B, C} 0
4835 // \ \ \ /
4836 // vector_shuffle V1, V2 <1, 2, 3, X>
4837 //
4838 if (!isShuffleMaskConsecutive(SVOp,
4839 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004840 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004841 NumZeros, // Where to start looking in the src vector
4842 NumElems, // Number of elements in vector
4843 OpSrc)) // Which source operand ?
4844 return false;
4845
4846 isLeft = false;
4847 ShAmt = NumZeros;
4848 ShVal = SVOp->getOperand(OpSrc);
4849 return true;
4850}
4851
4852/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4853/// logical left shift of a vector.
4854static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4855 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4856 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Benjamin Kramera0de26c2013-05-17 14:48:34 +00004857 unsigned NumZeros = getNumOfConsecutiveZeros(
4858 SVOp, NumElems, true /* check zeros from left */, DAG,
4859 NumElems - SVOp->getMaskElt(NumElems - 1) - 1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004860 unsigned OpSrc;
4861
4862 if (!NumZeros)
4863 return false;
4864
4865 // Considering the elements in the mask that are not consecutive zeros,
4866 // check if they consecutively come from only one of the source vectors.
4867 //
4868 // 0 { A, B, X, X } = V2
4869 // / \ / /
4870 // vector_shuffle V1, V2 <X, X, 4, 5>
4871 //
4872 if (!isShuffleMaskConsecutive(SVOp,
4873 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004874 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004875 0, // Where to start looking in the src vector
4876 NumElems, // Number of elements in vector
4877 OpSrc)) // Which source operand ?
4878 return false;
4879
4880 isLeft = true;
4881 ShAmt = NumZeros;
4882 ShVal = SVOp->getOperand(OpSrc);
4883 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004884}
4885
4886/// isVectorShift - Returns true if the shuffle can be implemented as a
4887/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004888static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004889 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004890 // Although the logic below support any bitwidth size, there are no
4891 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004892 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004893 return false;
4894
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004895 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4896 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4897 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004898
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004899 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004900}
4901
Evan Chengc78d3b42006-04-24 18:01:45 +00004902/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4903///
Dan Gohman475871a2008-07-27 21:46:04 +00004904static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004905 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004906 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004907 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004908 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004909 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004910 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004911
Andrew Trickac6d9be2013-05-25 02:42:55 +00004912 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 bool First = true;
4915 for (unsigned i = 0; i < 16; ++i) {
4916 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4917 if (ThisIsNonZero && First) {
4918 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004919 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004920 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004921 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004922 First = false;
4923 }
4924
4925 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004926 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004927 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4928 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004929 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004930 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004931 }
4932 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4934 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4935 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004936 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004938 } else
4939 ThisElt = LastElt;
4940
Gabor Greifba36cb52008-08-28 21:40:38 +00004941 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004943 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004944 }
4945 }
4946
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004947 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004948}
4949
Bill Wendlinga348c562007-03-22 18:42:45 +00004950/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004951///
Dan Gohman475871a2008-07-27 21:46:04 +00004952static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004953 unsigned NumNonZero, unsigned NumZero,
4954 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004955 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004956 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004957 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004958 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004959
Andrew Trickac6d9be2013-05-25 02:42:55 +00004960 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004961 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004962 bool First = true;
4963 for (unsigned i = 0; i < 8; ++i) {
4964 bool isNonZero = (NonZeros & (1 << i)) != 0;
4965 if (isNonZero) {
4966 if (First) {
4967 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004968 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004969 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004970 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004971 First = false;
4972 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004973 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004974 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004975 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004976 }
4977 }
4978
4979 return V;
4980}
4981
Evan Chengf26ffe92008-05-29 08:22:04 +00004982/// getVShift - Return a vector logical shift node.
4983///
Owen Andersone50ed302009-08-10 22:56:29 +00004984static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004985 unsigned NumBits, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004986 const TargetLowering &TLI, SDLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004987 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004988 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004989 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004990 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4991 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004992 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004993 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004994 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004995}
4996
Dan Gohman475871a2008-07-27 21:46:04 +00004997SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004998X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, SDLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004999 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00005000
Evan Chengc3630942009-12-09 21:00:30 +00005001 // Check if the scalar load can be widened into a vector load. And if
5002 // the address is "base + cst" see if the cst can be "absorbed" into
5003 // the shuffle mask.
5004 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
5005 SDValue Ptr = LD->getBasePtr();
5006 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5007 return SDValue();
5008 EVT PVT = LD->getValueType(0);
5009 if (PVT != MVT::i32 && PVT != MVT::f32)
5010 return SDValue();
5011
5012 int FI = -1;
5013 int64_t Offset = 0;
5014 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5015 FI = FINode->getIndex();
5016 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00005017 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00005018 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5019 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5020 Offset = Ptr.getConstantOperandVal(1);
5021 Ptr = Ptr.getOperand(0);
5022 } else {
5023 return SDValue();
5024 }
5025
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005026 // FIXME: 256-bit vector instructions don't require a strict alignment,
5027 // improve this code to support it better.
5028 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005029 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005030 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005031 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005032 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005033 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005034 // Can't change the alignment. FIXME: It's possible to compute
5035 // the exact stack offset and reference FI + adjust offset instead.
5036 // If someone *really* cares about this. That's the way to implement it.
5037 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005038 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005039 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005040 }
5041 }
5042
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005043 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005044 // Ptr + (Offset & ~15).
5045 if (Offset < 0)
5046 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005047 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005048 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005049 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005050 if (StartOffset)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005051 Ptr = DAG.getNode(ISD::ADD, SDLoc(Ptr), Ptr.getValueType(),
Evan Chengc3630942009-12-09 21:00:30 +00005052 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5053
5054 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005055 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005056
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005057 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5058 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005059 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005060 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005061
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005062 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005063 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005064 Mask.push_back(EltNo);
5065
Craig Toppercc3000632012-01-30 07:50:31 +00005066 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005067 }
5068
5069 return SDValue();
5070}
5071
Michael J. Spencerec38de22010-10-10 22:04:20 +00005072/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5073/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005074/// load which has the same value as a build_vector whose operands are 'elts'.
5075///
5076/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005077///
Nate Begeman1449f292010-03-24 22:19:06 +00005078/// FIXME: we'd also like to handle the case where the last elements are zero
5079/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5080/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005081static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005082 SDLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005083 EVT EltVT = VT.getVectorElementType();
5084 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005085
Nate Begemanfdea31a2010-03-24 20:49:50 +00005086 LoadSDNode *LDBase = NULL;
5087 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005088
Nate Begeman1449f292010-03-24 22:19:06 +00005089 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005090 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005091 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005092 for (unsigned i = 0; i < NumElems; ++i) {
5093 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005094
Nate Begemanfdea31a2010-03-24 20:49:50 +00005095 if (!Elt.getNode() ||
5096 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5097 return SDValue();
5098 if (!LDBase) {
5099 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5100 return SDValue();
5101 LDBase = cast<LoadSDNode>(Elt.getNode());
5102 LastLoadedElt = i;
5103 continue;
5104 }
5105 if (Elt.getOpcode() == ISD::UNDEF)
5106 continue;
5107
5108 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5109 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5110 return SDValue();
5111 LastLoadedElt = i;
5112 }
Nate Begeman1449f292010-03-24 22:19:06 +00005113
5114 // If we have found an entire vector of loads and undefs, then return a large
5115 // load of the entire vector width starting at the base pointer. If we found
5116 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005117 if (LastLoadedElt == NumElems - 1) {
Nadav Rotem23d1d5e2013-05-22 19:28:41 +00005118 SDValue NewLd = SDValue();
Nate Begemanfdea31a2010-03-24 20:49:50 +00005119 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Nadav Rotem23d1d5e2013-05-22 19:28:41 +00005120 NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
5121 LDBase->getPointerInfo(),
5122 LDBase->isVolatile(), LDBase->isNonTemporal(),
5123 LDBase->isInvariant(), 0);
5124 NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
5125 LDBase->getPointerInfo(),
5126 LDBase->isVolatile(), LDBase->isNonTemporal(),
5127 LDBase->isInvariant(), LDBase->getAlignment());
5128
5129 if (LDBase->hasAnyUseOfValue(1)) {
5130 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5131 SDValue(LDBase, 1),
5132 SDValue(NewLd.getNode(), 1));
5133 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5134 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5135 SDValue(NewLd.getNode(), 1));
5136 }
5137
5138 return NewLd;
Craig Topper69947b92012-04-23 06:57:04 +00005139 }
5140 if (NumElems == 4 && LastLoadedElt == 1 &&
5141 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005142 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5143 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005144 SDValue ResNode =
Michael Liao0ee17002013-04-19 04:03:37 +00005145 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops,
5146 array_lengthof(Ops), MVT::i64,
Eli Friedman322ea082011-09-14 23:42:45 +00005147 LDBase->getPointerInfo(),
5148 LDBase->getAlignment(),
5149 false/*isVolatile*/, true/*ReadMem*/,
5150 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005151
5152 // Make sure the newly-created LOAD is in the same position as LDBase in
5153 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5154 // update uses of LDBase's output chain to use the TokenFactor.
5155 if (LDBase->hasAnyUseOfValue(1)) {
5156 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5157 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5158 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5159 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5160 SDValue(ResNode.getNode(), 1));
5161 }
5162
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005163 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005164 }
5165 return SDValue();
5166}
5167
Nadav Rotem9d68b062012-04-08 12:54:54 +00005168/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5169/// to generate a splat value for the following cases:
5170/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005171/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005172/// a scalar load, or a constant.
5173/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005174/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005175SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005176X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005177 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005178 return SDValue();
5179
Craig Topper45e1c752013-01-20 00:38:18 +00005180 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005181 SDLoc dl(Op);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005182
Craig Topper5da8a802012-05-04 05:49:51 +00005183 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5184 "Unsupported vector type for broadcast.");
5185
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005186 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005187 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005188
Nadav Rotem9d68b062012-04-08 12:54:54 +00005189 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005190 default:
5191 // Unknown pattern found.
5192 return SDValue();
5193
5194 case ISD::BUILD_VECTOR: {
5195 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005196 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005197 return SDValue();
5198
Nadav Rotem9d68b062012-04-08 12:54:54 +00005199 Ld = Op.getOperand(0);
5200 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5201 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005202
5203 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005204 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005205 // Constants may have multiple users.
5206 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005207 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005208 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005209 }
5210
5211 case ISD::VECTOR_SHUFFLE: {
5212 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5213
5214 // Shuffles must have a splat mask where the first element is
5215 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005216 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005217 return SDValue();
5218
5219 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005220 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005221 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5222
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005223 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005224 return SDValue();
5225
5226 // Use the register form of the broadcast instruction available on AVX2.
5227 if (VT.is256BitVector())
5228 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5229 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5230 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005231
5232 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005233 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005234 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005235
5236 // The scalar_to_vector node and the suspected
5237 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005238 // Constants may have multiple users.
5239 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005240 return SDValue();
5241 break;
5242 }
5243 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005244
Craig Topper7a9a28b2012-08-12 02:23:29 +00005245 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005246
5247 // Handle the broadcasting a single constant scalar from the constant pool
5248 // into a vector. On Sandybridge it is still better to load a constant vector
5249 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005250 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005251 EVT CVT = Ld.getValueType();
5252 assert(!CVT.isVector() && "Must not broadcast a vector type");
5253 unsigned ScalarSize = CVT.getSizeInBits();
5254
Craig Topper5da8a802012-05-04 05:49:51 +00005255 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005256 const Constant *C = 0;
5257 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5258 C = CI->getConstantIntValue();
5259 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5260 C = CF->getConstantFPValue();
5261
5262 assert(C && "Invalid constant type");
5263
Nadav Rotem154819d2012-04-09 07:45:58 +00005264 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005265 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005266 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005267 MachinePointerInfo::getConstantPool(),
5268 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005269
Nadav Rotem9d68b062012-04-08 12:54:54 +00005270 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5271 }
5272 }
5273
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005274 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005275 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5276
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005277 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005278 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005279 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5280 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5281
5282 // The scalar source must be a normal load.
5283 if (!IsLoad)
5284 return SDValue();
5285
Craig Topper5da8a802012-05-04 05:49:51 +00005286 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005287 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005288
Craig Toppera9376332012-01-10 08:23:59 +00005289 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005290 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005291 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005292 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005293 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005294 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005295
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005296 // Unsupported broadcast.
5297 return SDValue();
5298}
5299
Evan Chengc3630942009-12-09 21:00:30 +00005300SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005301X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5302 EVT VT = Op.getValueType();
5303
5304 // Skip if insert_vec_elt is not supported.
5305 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5306 return SDValue();
5307
Andrew Trickac6d9be2013-05-25 02:42:55 +00005308 SDLoc DL(Op);
Michael Liaofacace82012-10-19 17:15:18 +00005309 unsigned NumElems = Op.getNumOperands();
5310
5311 SDValue VecIn1;
5312 SDValue VecIn2;
5313 SmallVector<unsigned, 4> InsertIndices;
5314 SmallVector<int, 8> Mask(NumElems, -1);
5315
5316 for (unsigned i = 0; i != NumElems; ++i) {
5317 unsigned Opc = Op.getOperand(i).getOpcode();
5318
5319 if (Opc == ISD::UNDEF)
5320 continue;
5321
5322 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5323 // Quit if more than 1 elements need inserting.
5324 if (InsertIndices.size() > 1)
5325 return SDValue();
5326
5327 InsertIndices.push_back(i);
5328 continue;
5329 }
5330
5331 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5332 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5333
5334 // Quit if extracted from vector of different type.
5335 if (ExtractedFromVec.getValueType() != VT)
5336 return SDValue();
5337
5338 // Quit if non-constant index.
5339 if (!isa<ConstantSDNode>(ExtIdx))
5340 return SDValue();
5341
5342 if (VecIn1.getNode() == 0)
5343 VecIn1 = ExtractedFromVec;
5344 else if (VecIn1 != ExtractedFromVec) {
5345 if (VecIn2.getNode() == 0)
5346 VecIn2 = ExtractedFromVec;
5347 else if (VecIn2 != ExtractedFromVec)
5348 // Quit if more than 2 vectors to shuffle
5349 return SDValue();
5350 }
5351
5352 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5353
5354 if (ExtractedFromVec == VecIn1)
5355 Mask[i] = Idx;
5356 else if (ExtractedFromVec == VecIn2)
5357 Mask[i] = Idx + NumElems;
5358 }
5359
5360 if (VecIn1.getNode() == 0)
5361 return SDValue();
5362
5363 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5364 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5365 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5366 unsigned Idx = InsertIndices[i];
5367 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5368 DAG.getIntPtrConstant(Idx));
5369 }
5370
5371 return NV;
5372}
5373
5374SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005375X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005376 SDLoc dl(Op);
David Greenea5f26012011-02-07 19:36:54 +00005377
Craig Topper45e1c752013-01-20 00:38:18 +00005378 MVT VT = Op.getValueType().getSimpleVT();
5379 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005380 unsigned NumElems = Op.getNumOperands();
5381
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005382 // Vectors containing all zeros can be matched by pxor and xorps later
5383 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5384 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5385 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005386 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005387 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005389 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005390 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005392 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005393 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5394 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005395 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005396 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005397 return Op;
5398
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005399 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005400 }
5401
Nadav Rotem154819d2012-04-09 07:45:58 +00005402 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005403 if (Broadcast.getNode())
5404 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005405
Owen Andersone50ed302009-08-10 22:56:29 +00005406 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407
Evan Cheng0db9fe62006-04-25 20:13:52 +00005408 unsigned NumZero = 0;
5409 unsigned NumNonZero = 0;
5410 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005411 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005413 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005415 if (Elt.getOpcode() == ISD::UNDEF)
5416 continue;
5417 Values.insert(Elt);
5418 if (Elt.getOpcode() != ISD::Constant &&
5419 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005420 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005421 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005422 NumZero++;
5423 else {
5424 NonZeros |= (1 << i);
5425 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005426 }
5427 }
5428
Chris Lattner97a2a562010-08-26 05:24:29 +00005429 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5430 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005431 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005432
Chris Lattner67f453a2008-03-09 05:42:06 +00005433 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005434 if (NumNonZero == 1) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +00005435 unsigned Idx = countTrailingZeros(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005437
Chris Lattner62098042008-03-09 01:05:04 +00005438 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5439 // the value are obviously zero, truncate the value to i32 and do the
5440 // insertion that way. Only do this if the value is non-constant or if the
5441 // value is a constant being inserted into element 0. It is cheaper to do
5442 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005443 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005444 (!IsAllConstants || Idx == 0)) {
5445 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005446 // Handle SSE only.
5447 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5448 EVT VecVT = MVT::v4i32;
5449 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005450
Chris Lattner62098042008-03-09 01:05:04 +00005451 // Truncate the value (which may itself be a constant) to i32, and
5452 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005453 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005454 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005455 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005456
Chris Lattner62098042008-03-09 01:05:04 +00005457 // Now we have our 32-bit value zero extended in the low element of
5458 // a vector. If Idx != 0, swizzle it into place.
5459 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005460 SmallVector<int, 4> Mask;
5461 Mask.push_back(Idx);
5462 for (unsigned i = 1; i != VecElts; ++i)
5463 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005464 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005465 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005466 }
Craig Topper07a27622012-01-22 03:07:48 +00005467 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005468 }
5469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005470
Chris Lattner19f79692008-03-08 22:59:52 +00005471 // If we have a constant or non-constant insertion into the low element of
5472 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5473 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005474 // depending on what the source datatype is.
5475 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005476 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005477 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005478
5479 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005480 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005481 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005482 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005483 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5484 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005485 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005486 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005487 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5488 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005489 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005490 }
5491
5492 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005493 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005494 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005495 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005496 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005497 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005498 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005499 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005500 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005501 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005502 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005503 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005504 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005505
5506 // Is it a vector logical left shift?
5507 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005508 X86::isZeroNode(Op.getOperand(0)) &&
5509 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005510 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005511 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005512 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005513 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005514 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005515 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005516
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005517 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005518 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005519
Chris Lattner19f79692008-03-08 22:59:52 +00005520 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5521 // is a non-constant being inserted into an element other than the low one,
5522 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5523 // movd/movss) to move this into the low element, then shuffle it into
5524 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005525 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005526 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005527
Evan Cheng0db9fe62006-04-25 20:13:52 +00005528 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005529 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005530 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005531 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005532 MaskVec.push_back(i == Idx ? 0 : 1);
5533 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005534 }
5535 }
5536
Chris Lattner67f453a2008-03-09 05:42:06 +00005537 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005538 if (Values.size() == 1) {
5539 if (EVTBits == 32) {
5540 // Instead of a shuffle like this:
5541 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5542 // Check if it's possible to issue this instead.
5543 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
Michael J. Spencerc6af2432013-05-24 22:23:49 +00005544 unsigned Idx = countTrailingZeros(NonZeros);
Evan Chengc3630942009-12-09 21:00:30 +00005545 SDValue Item = Op.getOperand(Idx);
5546 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5547 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5548 }
Dan Gohman475871a2008-07-27 21:46:04 +00005549 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005550 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005551
Dan Gohmana3941172007-07-24 22:55:08 +00005552 // A vector full of immediates; various special cases are already
5553 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005554 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005555 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005556
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005557 // For AVX-length vectors, build the individual 128-bit pieces and use
5558 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005559 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005560 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005561 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005562 V.push_back(Op.getOperand(i));
5563
5564 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5565
5566 // Build both the lower and upper subvector.
5567 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5568 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5569 NumElems/2);
5570
5571 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005572 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005573 }
5574
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005575 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005576 if (EVTBits == 64) {
5577 if (NumNonZero == 1) {
5578 // One half is zero or undef.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00005579 unsigned Idx = countTrailingZeros(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005580 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005581 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005582 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005583 }
Dan Gohman475871a2008-07-27 21:46:04 +00005584 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005585 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005586
5587 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005588 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005589 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005590 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005591 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005592 }
5593
Bill Wendling826f36f2007-03-28 00:57:11 +00005594 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005595 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005596 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005597 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005598 }
5599
5600 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005601 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005602 if (NumElems == 4 && NumZero > 0) {
5603 for (unsigned i = 0; i < 4; ++i) {
5604 bool isZero = !(NonZeros & (1 << i));
5605 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005606 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005607 else
Dale Johannesenace16102009-02-03 19:33:06 +00005608 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005609 }
5610
5611 for (unsigned i = 0; i < 2; ++i) {
5612 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5613 default: break;
5614 case 0:
5615 V[i] = V[i*2]; // Must be a zero vector.
5616 break;
5617 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005618 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005619 break;
5620 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005621 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005622 break;
5623 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005625 break;
5626 }
5627 }
5628
Benjamin Kramer9c683542012-01-30 15:16:21 +00005629 bool Reverse1 = (NonZeros & 0x3) == 2;
5630 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5631 int MaskVec[] = {
5632 Reverse1 ? 1 : 0,
5633 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005634 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5635 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005636 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005637 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005638 }
5639
Craig Topper7a9a28b2012-08-12 02:23:29 +00005640 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005641 // Check for a build vector of consecutive loads.
5642 for (unsigned i = 0; i < NumElems; ++i)
5643 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005644
Nate Begemanfdea31a2010-03-24 20:49:50 +00005645 // Check for elements which are consecutive loads.
5646 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5647 if (LD.getNode())
5648 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005649
Michael Liaofacace82012-10-19 17:15:18 +00005650 // Check for a build vector from mostly shuffle plus few inserting.
5651 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5652 if (Sh.getNode())
5653 return Sh;
5654
Michael J. Spencerec38de22010-10-10 22:04:20 +00005655 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005656 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005657 SDValue Result;
5658 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5659 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5660 else
5661 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005662
Chris Lattner24faf612010-08-28 17:59:08 +00005663 for (unsigned i = 1; i < NumElems; ++i) {
5664 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5665 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005666 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005667 }
5668 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005670
Chris Lattner6e80e442010-08-28 17:15:43 +00005671 // Otherwise, expand into a number of unpckl*, start by extending each of
5672 // our (non-undef) elements to the full vector width with the element in the
5673 // bottom slot of the vector (which generates no code for SSE).
5674 for (unsigned i = 0; i < NumElems; ++i) {
5675 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5676 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5677 else
5678 V[i] = DAG.getUNDEF(VT);
5679 }
5680
5681 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005682 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5683 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5684 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005685 unsigned EltStride = NumElems >> 1;
5686 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005687 for (unsigned i = 0; i < EltStride; ++i) {
5688 // If V[i+EltStride] is undef and this is the first round of mixing,
5689 // then it is safe to just drop this shuffle: V[i] is already in the
5690 // right place, the one element (since it's the first round) being
5691 // inserted as undef can be dropped. This isn't safe for successive
5692 // rounds because they will permute elements within both vectors.
5693 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5694 EltStride == NumElems/2)
5695 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005696
Chris Lattner6e80e442010-08-28 17:15:43 +00005697 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005698 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005699 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700 }
5701 return V[0];
5702 }
Dan Gohman475871a2008-07-27 21:46:04 +00005703 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005704}
5705
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005706// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5707// to create 256-bit vectors from two other 128-bit ones.
5708static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005709 SDLoc dl(Op);
Craig Topper45e1c752013-01-20 00:38:18 +00005710 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005711
Craig Topper7a9a28b2012-08-12 02:23:29 +00005712 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005713
5714 SDValue V1 = Op.getOperand(0);
5715 SDValue V2 = Op.getOperand(1);
5716 unsigned NumElems = ResVT.getVectorNumElements();
5717
Craig Topper4c7972d2012-04-22 18:15:59 +00005718 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005719}
5720
Craig Topper55b24052012-09-11 06:15:32 +00005721static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005722 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005723
5724 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5725 // from two other 128-bit ones.
5726 return LowerAVXCONCAT_VECTORS(Op, DAG);
5727}
5728
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005729// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005730static SDValue
5731LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5732 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005733 SDValue V1 = SVOp->getOperand(0);
5734 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005735 SDLoc dl(SVOp);
Craig Topper657a99c2013-01-19 23:36:09 +00005736 MVT VT = SVOp->getValueType(0).getSimpleVT();
5737 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005738 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005739
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005740 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5741 return SDValue();
5742 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005743 return SDValue();
5744
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005745 // Check the mask for BLEND and build the value.
5746 unsigned MaskValue = 0;
5747 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005748 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005749 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005750
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005751 // Blend for v16i16 should be symetric for the both lanes.
5752 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005753
Craig Topper9b33ef72013-01-21 06:57:59 +00005754 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005755 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005756 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005757
Craig Topper04f74a12013-01-21 07:25:16 +00005758 if ((EltIdx < 0 || EltIdx == (int)i) &&
5759 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005760 continue;
5761
Craig Topper9b33ef72013-01-21 06:57:59 +00005762 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005763 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005764 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5765 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005766 else
Craig Topper1842ba02012-04-23 06:38:28 +00005767 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005768 }
5769
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005770 // Convert i32 vectors to floating point if it is not AVX2.
5771 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005772 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005773 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005774 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5775 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005776 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5777 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5778 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005779
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005780 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5781 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005782 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005783}
5784
Nate Begemanb9a47b82009-02-23 08:49:38 +00005785// v8i16 shuffles - Prefer shuffles in the following order:
5786// 1. [all] pshuflw, pshufhw, optional move
5787// 2. [ssse3] 1 x pshufb
5788// 3. [ssse3] 2 x pshufb + 1 x por
5789// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005790static SDValue
5791LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5792 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005793 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005794 SDValue V1 = SVOp->getOperand(0);
5795 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005796 SDLoc dl(SVOp);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005798
Nate Begemanb9a47b82009-02-23 08:49:38 +00005799 // Determine if more than 1 of the words in each of the low and high quadwords
5800 // of the result come from the same quadword of one of the two inputs. Undef
5801 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005802 unsigned LoQuad[] = { 0, 0, 0, 0 };
5803 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005804 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005806 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005807 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 MaskVals.push_back(EltIdx);
5809 if (EltIdx < 0) {
5810 ++Quad[0];
5811 ++Quad[1];
5812 ++Quad[2];
5813 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005814 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 }
5816 ++Quad[EltIdx / 4];
5817 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005818 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005819
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005821 unsigned MaxQuad = 1;
5822 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005823 if (LoQuad[i] > MaxQuad) {
5824 BestLoQuad = i;
5825 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005826 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005827 }
5828
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005830 MaxQuad = 1;
5831 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 if (HiQuad[i] > MaxQuad) {
5833 BestHiQuad = i;
5834 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005835 }
5836 }
5837
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005839 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 // single pshufb instruction is necessary. If There are more than 2 input
5841 // quads, disable the next transformation since it does not help SSSE3.
5842 bool V1Used = InputQuads[0] || InputQuads[1];
5843 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005844 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005846 BestLoQuad = InputQuads[0] ? 0 : 1;
5847 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 }
5849 if (InputQuads.count() > 2) {
5850 BestLoQuad = -1;
5851 BestHiQuad = -1;
5852 }
5853 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005854
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5856 // the shuffle mask. If a quad is scored as -1, that means that it contains
5857 // words from all 4 input quadwords.
5858 SDValue NewV;
5859 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005860 int MaskV[] = {
5861 BestLoQuad < 0 ? 0 : BestLoQuad,
5862 BestHiQuad < 0 ? 1 : BestHiQuad
5863 };
Eric Christopherfd179292009-08-27 18:07:15 +00005864 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005865 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5866 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5867 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005868
Nate Begemanb9a47b82009-02-23 08:49:38 +00005869 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5870 // source words for the shuffle, to aid later transformations.
5871 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005872 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005873 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005874 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005875 if (idx != (int)i)
5876 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005877 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005878 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 AllWordsInNewV = false;
5880 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005881 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005882
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5884 if (AllWordsInNewV) {
5885 for (int i = 0; i != 8; ++i) {
5886 int idx = MaskVals[i];
5887 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005888 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005889 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005890 if ((idx != i) && idx < 4)
5891 pshufhw = false;
5892 if ((idx != i) && idx > 3)
5893 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005894 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 V1 = NewV;
5896 V2Used = false;
5897 BestLoQuad = 0;
5898 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005899 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005900
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5902 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005903 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005904 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5905 unsigned TargetMask = 0;
5906 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005907 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005908 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5909 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5910 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005911 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005912 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005913 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005914 }
Eric Christopherfd179292009-08-27 18:07:15 +00005915
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005916 // Promote splats to a larger type which usually leads to more efficient code.
5917 // FIXME: Is this true if pshufb is available?
5918 if (SVOp->isSplat())
5919 return PromoteSplat(SVOp, DAG);
5920
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 // If we have SSSE3, and all words of the result are from 1 input vector,
5922 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5923 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005924 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005926
Nate Begemanb9a47b82009-02-23 08:49:38 +00005927 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005928 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 // mask, and elements that come from V1 in the V2 mask, so that the two
5930 // results can be OR'd together.
5931 bool TwoInputs = V1Used && V2Used;
5932 for (unsigned i = 0; i != 8; ++i) {
5933 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005934 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5935 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005936 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005937 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005939 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005940 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005941 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005944 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005945
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 // Calculate the shuffle mask for the second input, shuffle it, and
5947 // OR it with the first shuffled input.
5948 pshufbMask.clear();
5949 for (unsigned i = 0; i != 8; ++i) {
5950 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005951 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5952 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5953 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5954 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005956 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005957 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005958 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005959 MVT::v16i8, &pshufbMask[0], 16));
5960 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005961 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005962 }
5963
5964 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5965 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005966 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005967 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005968 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 for (int i = 0; i != 4; ++i) {
5970 int idx = MaskVals[i];
5971 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005972 InOrder.set(i);
5973 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005974 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005975 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 }
5977 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005978 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005979 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005980
Craig Topperdd637ae2012-02-19 05:41:45 +00005981 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5982 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005983 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005984 NewV.getOperand(0),
5985 getShufflePSHUFLWImmediate(SVOp), DAG);
5986 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 }
Eric Christopherfd179292009-08-27 18:07:15 +00005988
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5990 // and update MaskVals with the new element order.
5991 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005992 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 for (unsigned i = 4; i != 8; ++i) {
5994 int idx = MaskVals[i];
5995 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005996 InOrder.set(i);
5997 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005998 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005999 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 }
6001 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00006003 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006004
Craig Topperdd637ae2012-02-19 05:41:45 +00006005 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
6006 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00006007 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00006008 NewV.getOperand(0),
6009 getShufflePSHUFHWImmediate(SVOp), DAG);
6010 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00006011 }
Eric Christopherfd179292009-08-27 18:07:15 +00006012
Nate Begemanb9a47b82009-02-23 08:49:38 +00006013 // In case BestHi & BestLo were both -1, which means each quadword has a word
6014 // from each of the four input quadwords, calculate the InOrder bitvector now
6015 // before falling through to the insert/extract cleanup.
6016 if (BestLoQuad == -1 && BestHiQuad == -1) {
6017 NewV = V1;
6018 for (int i = 0; i != 8; ++i)
6019 if (MaskVals[i] < 0 || MaskVals[i] == i)
6020 InOrder.set(i);
6021 }
Eric Christopherfd179292009-08-27 18:07:15 +00006022
Nate Begemanb9a47b82009-02-23 08:49:38 +00006023 // The other elements are put in the right place using pextrw and pinsrw.
6024 for (unsigned i = 0; i != 8; ++i) {
6025 if (InOrder[i])
6026 continue;
6027 int EltIdx = MaskVals[i];
6028 if (EltIdx < 0)
6029 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00006030 SDValue ExtOp = (EltIdx < 8) ?
6031 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
6032 DAG.getIntPtrConstant(EltIdx)) :
6033 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006034 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006036 DAG.getIntPtrConstant(i));
6037 }
6038 return NewV;
6039}
6040
6041// v16i8 shuffles - Prefer shuffles in the following order:
6042// 1. [ssse3] 1 x pshufb
6043// 2. [ssse3] 2 x pshufb + 1 x por
6044// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6045static
Nate Begeman9008ca62009-04-27 18:41:29 +00006046SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006047 SelectionDAG &DAG,
6048 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006049 SDValue V1 = SVOp->getOperand(0);
6050 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006051 SDLoc dl(SVOp);
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006052 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006053
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006054 // Promote splats to a larger type which usually leads to more efficient code.
6055 // FIXME: Is this true if pshufb is available?
6056 if (SVOp->isSplat())
6057 return PromoteSplat(SVOp, DAG);
6058
Nate Begemanb9a47b82009-02-23 08:49:38 +00006059 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006060 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006061 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006062
Nate Begemanb9a47b82009-02-23 08:49:38 +00006063 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006064 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006065 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006066
Nate Begemanb9a47b82009-02-23 08:49:38 +00006067 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006068 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006069 //
6070 // Otherwise, we have elements from both input vectors, and must zero out
6071 // elements that come from V2 in the first mask, and V1 in the second mask
6072 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006073 for (unsigned i = 0; i != 16; ++i) {
6074 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006075 if (EltIdx < 0 || EltIdx >= 16)
6076 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006077 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006078 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006079 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006080 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006082
6083 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6084 // the 2nd operand if it's undefined or zero.
6085 if (V2.getOpcode() == ISD::UNDEF ||
6086 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006087 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006088
Nate Begemanb9a47b82009-02-23 08:49:38 +00006089 // Calculate the shuffle mask for the second input, shuffle it, and
6090 // OR it with the first shuffled input.
6091 pshufbMask.clear();
6092 for (unsigned i = 0; i != 16; ++i) {
6093 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006094 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006095 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006096 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006097 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006098 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006099 MVT::v16i8, &pshufbMask[0], 16));
6100 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006101 }
Eric Christopherfd179292009-08-27 18:07:15 +00006102
Nate Begemanb9a47b82009-02-23 08:49:38 +00006103 // No SSSE3 - Calculate in place words and then fix all out of place words
6104 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6105 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006106 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6107 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006108 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006109 for (int i = 0; i != 8; ++i) {
6110 int Elt0 = MaskVals[i*2];
6111 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006112
Nate Begemanb9a47b82009-02-23 08:49:38 +00006113 // This word of the result is all undef, skip it.
6114 if (Elt0 < 0 && Elt1 < 0)
6115 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006116
Nate Begemanb9a47b82009-02-23 08:49:38 +00006117 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006118 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006119 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006120
Nate Begemanb9a47b82009-02-23 08:49:38 +00006121 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6122 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6123 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006124
6125 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6126 // using a single extract together, load it and store it.
6127 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006128 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006129 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006130 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006131 DAG.getIntPtrConstant(i));
6132 continue;
6133 }
6134
Nate Begemanb9a47b82009-02-23 08:49:38 +00006135 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006136 // source byte is not also odd, shift the extracted word left 8 bits
6137 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006138 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006139 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006140 DAG.getIntPtrConstant(Elt1 / 2));
6141 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006142 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006143 DAG.getConstant(8,
6144 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006145 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6147 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006148 }
6149 // If Elt0 is defined, extract it from the appropriate source. If the
6150 // source byte is not also even, shift the extracted word right 8 bits. If
6151 // Elt1 was also defined, OR the extracted values together before
6152 // inserting them in the result.
6153 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006154 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006155 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6156 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006157 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006158 DAG.getConstant(8,
6159 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006160 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006161 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6162 DAG.getConstant(0x00FF, MVT::i16));
6163 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006164 : InsElt0;
6165 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006166 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006167 DAG.getIntPtrConstant(i));
6168 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006169 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006170}
6171
Elena Demikhovsky41789462012-09-06 12:42:01 +00006172// v32i8 shuffles - Translate to VPSHUFB if possible.
6173static
6174SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006175 const X86Subtarget *Subtarget,
6176 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006177 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006178 SDValue V1 = SVOp->getOperand(0);
6179 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006180 SDLoc dl(SVOp);
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006181 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006182
6183 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006184 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6185 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006186
Michael Liao471b9172012-10-03 23:43:52 +00006187 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006188 // (1) one of input vector is undefined or zeroinitializer.
6189 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6190 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006191 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006192 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006193 return SDValue();
6194
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006195 if (V1IsAllZero && !V2IsAllZero) {
6196 CommuteVectorShuffleMask(MaskVals, 32);
6197 V1 = V2;
6198 }
6199 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006200 for (unsigned i = 0; i != 32; i++) {
6201 int EltIdx = MaskVals[i];
6202 if (EltIdx < 0 || EltIdx >= 32)
6203 EltIdx = 0x80;
6204 else {
6205 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6206 // Cross lane is not allowed.
6207 return SDValue();
6208 EltIdx &= 0xf;
6209 }
6210 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6211 }
6212 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6213 DAG.getNode(ISD::BUILD_VECTOR, dl,
6214 MVT::v32i8, &pshufbMask[0], 32));
6215}
6216
Evan Cheng7a831ce2007-12-15 03:00:47 +00006217/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006218/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006219/// done when every pair / quad of shuffle mask elements point to elements in
6220/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006221/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006222static
Nate Begeman9008ca62009-04-27 18:41:29 +00006223SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006224 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006225 MVT VT = SVOp->getValueType(0).getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006226 SDLoc dl(SVOp);
Nate Begeman9008ca62009-04-27 18:41:29 +00006227 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006228 MVT NewVT;
6229 unsigned Scale;
6230 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006231 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006232 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6233 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6234 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6235 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6236 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6237 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006238 }
6239
Nate Begeman9008ca62009-04-27 18:41:29 +00006240 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006241 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006242 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006243 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006244 int EltIdx = SVOp->getMaskElt(i+j);
6245 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006246 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006247 if (StartIdx < 0)
6248 StartIdx = (EltIdx / Scale);
6249 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006250 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006251 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006252 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006253 }
6254
Craig Topper11ac1f82012-05-04 04:08:44 +00006255 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6256 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006257 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006258}
6259
Evan Chengd880b972008-05-09 21:53:03 +00006260/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006261///
Craig Topperf84b7502013-01-20 00:50:58 +00006262static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006263 SDValue SrcOp, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006264 const X86Subtarget *Subtarget, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006265 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006266 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006267 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006268 LD = dyn_cast<LoadSDNode>(SrcOp);
6269 if (!LD) {
6270 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6271 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006272 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006273 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006274 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006275 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006276 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006277 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006278 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006279 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006280 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6281 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6282 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006283 SrcOp.getOperand(0)
6284 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006285 }
6286 }
6287 }
6288
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006289 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006290 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006291 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006292 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006293}
6294
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006295/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6296/// which could not be matched by any known target speficic shuffle
6297static SDValue
6298LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006299
6300 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6301 if (NewOp.getNode())
6302 return NewOp;
6303
Craig Topper657a99c2013-01-19 23:36:09 +00006304 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006305
Craig Topper8f35c132012-01-20 09:29:03 +00006306 unsigned NumElems = VT.getVectorNumElements();
6307 unsigned NumLaneElems = NumElems / 2;
6308
Andrew Trickac6d9be2013-05-25 02:42:55 +00006309 SDLoc dl(SVOp);
Craig Topper657a99c2013-01-19 23:36:09 +00006310 MVT EltVT = VT.getVectorElementType();
6311 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006312 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006313
Craig Topper9a2b6e12012-04-06 07:45:23 +00006314 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006315 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006316 // Build a shuffle mask for the output, discovering on the fly which
6317 // input vectors to use as shuffle operands (recorded in InputUsed).
6318 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006319 // out with UseBuildVector set.
6320 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006321 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006322 unsigned LaneStart = l * NumLaneElems;
6323 for (unsigned i = 0; i != NumLaneElems; ++i) {
6324 // The mask element. This indexes into the input.
6325 int Idx = SVOp->getMaskElt(i+LaneStart);
6326 if (Idx < 0) {
6327 // the mask element does not index into any input vector.
6328 Mask.push_back(-1);
6329 continue;
6330 }
Craig Topper8f35c132012-01-20 09:29:03 +00006331
Craig Topper9a2b6e12012-04-06 07:45:23 +00006332 // The input vector this mask element indexes into.
6333 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006334
Craig Topper9a2b6e12012-04-06 07:45:23 +00006335 // Turn the index into an offset from the start of the input vector.
6336 Idx -= Input * NumLaneElems;
6337
6338 // Find or create a shuffle vector operand to hold this input.
6339 unsigned OpNo;
6340 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6341 if (InputUsed[OpNo] == Input)
6342 // This input vector is already an operand.
6343 break;
6344 if (InputUsed[OpNo] < 0) {
6345 // Create a new operand for this input vector.
6346 InputUsed[OpNo] = Input;
6347 break;
6348 }
6349 }
6350
6351 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006352 // More than two input vectors used! Give up on trying to create a
6353 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6354 UseBuildVector = true;
6355 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006356 }
6357
6358 // Add the mask index for the new shuffle vector.
6359 Mask.push_back(Idx + OpNo * NumLaneElems);
6360 }
6361
Craig Topper8ae97ba2012-05-21 06:40:16 +00006362 if (UseBuildVector) {
6363 SmallVector<SDValue, 16> SVOps;
6364 for (unsigned i = 0; i != NumLaneElems; ++i) {
6365 // The mask element. This indexes into the input.
6366 int Idx = SVOp->getMaskElt(i+LaneStart);
6367 if (Idx < 0) {
6368 SVOps.push_back(DAG.getUNDEF(EltVT));
6369 continue;
6370 }
6371
6372 // The input vector this mask element indexes into.
6373 int Input = Idx / NumElems;
6374
6375 // Turn the index into an offset from the start of the input vector.
6376 Idx -= Input * NumElems;
6377
6378 // Extract the vector element by hand.
6379 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6380 SVOp->getOperand(Input),
6381 DAG.getIntPtrConstant(Idx)));
6382 }
6383
6384 // Construct the output using a BUILD_VECTOR.
6385 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6386 SVOps.size());
6387 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006388 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006389 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006390 } else {
6391 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006392 (InputUsed[0] % 2) * NumLaneElems,
6393 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006394 // If only one input was used, use an undefined vector for the other.
6395 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6396 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006397 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006398 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006399 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006400 }
6401
6402 Mask.clear();
6403 }
Craig Topper8f35c132012-01-20 09:29:03 +00006404
6405 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006406 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006407}
6408
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006409/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6410/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006411static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006412LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006413 SDValue V1 = SVOp->getOperand(0);
6414 SDValue V2 = SVOp->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006415 SDLoc dl(SVOp);
Craig Topper657a99c2013-01-19 23:36:09 +00006416 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006417
Craig Topper7a9a28b2012-08-12 02:23:29 +00006418 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006419
Benjamin Kramer9c683542012-01-30 15:16:21 +00006420 std::pair<int, int> Locs[4];
6421 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006422 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006423
Evan Chengace3c172008-07-22 21:13:36 +00006424 unsigned NumHi = 0;
6425 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006426 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006427 int Idx = PermMask[i];
6428 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006429 Locs[i] = std::make_pair(-1, -1);
6430 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006431 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6432 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006433 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006434 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006435 NumLo++;
6436 } else {
6437 Locs[i] = std::make_pair(1, NumHi);
6438 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006439 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006440 NumHi++;
6441 }
6442 }
6443 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006444
Evan Chengace3c172008-07-22 21:13:36 +00006445 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006446 // If no more than two elements come from either vector. This can be
6447 // implemented with two shuffles. First shuffle gather the elements.
6448 // The second shuffle, which takes the first shuffle as both of its
6449 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006450 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006451
Benjamin Kramer9c683542012-01-30 15:16:21 +00006452 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006453
Benjamin Kramer9c683542012-01-30 15:16:21 +00006454 for (unsigned i = 0; i != 4; ++i)
6455 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006456 unsigned Idx = (i < 2) ? 0 : 4;
6457 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006458 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006459 }
Evan Chengace3c172008-07-22 21:13:36 +00006460
Nate Begeman9008ca62009-04-27 18:41:29 +00006461 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006462 }
6463
6464 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006465 // Otherwise, we must have three elements from one vector, call it X, and
6466 // one element from the other, call it Y. First, use a shufps to build an
6467 // intermediate vector with the one element from Y and the element from X
6468 // that will be in the same half in the final destination (the indexes don't
6469 // matter). Then, use a shufps to build the final vector, taking the half
6470 // containing the element from Y from the intermediate, and the other half
6471 // from X.
6472 if (NumHi == 3) {
6473 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006474 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006475 std::swap(V1, V2);
6476 }
6477
6478 // Find the element from V2.
6479 unsigned HiIndex;
6480 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006481 int Val = PermMask[HiIndex];
6482 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006483 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006484 if (Val >= 4)
6485 break;
6486 }
6487
Nate Begeman9008ca62009-04-27 18:41:29 +00006488 Mask1[0] = PermMask[HiIndex];
6489 Mask1[1] = -1;
6490 Mask1[2] = PermMask[HiIndex^1];
6491 Mask1[3] = -1;
6492 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006493
6494 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006495 Mask1[0] = PermMask[0];
6496 Mask1[1] = PermMask[1];
6497 Mask1[2] = HiIndex & 1 ? 6 : 4;
6498 Mask1[3] = HiIndex & 1 ? 4 : 6;
6499 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006500 }
Craig Topper69947b92012-04-23 06:57:04 +00006501
6502 Mask1[0] = HiIndex & 1 ? 2 : 0;
6503 Mask1[1] = HiIndex & 1 ? 0 : 2;
6504 Mask1[2] = PermMask[2];
6505 Mask1[3] = PermMask[3];
6506 if (Mask1[2] >= 0)
6507 Mask1[2] += 4;
6508 if (Mask1[3] >= 0)
6509 Mask1[3] += 4;
6510 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006511 }
6512
6513 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006514 int LoMask[] = { -1, -1, -1, -1 };
6515 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006516
Benjamin Kramer9c683542012-01-30 15:16:21 +00006517 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006518 unsigned MaskIdx = 0;
6519 unsigned LoIdx = 0;
6520 unsigned HiIdx = 2;
6521 for (unsigned i = 0; i != 4; ++i) {
6522 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006523 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006524 MaskIdx = 1;
6525 LoIdx = 0;
6526 HiIdx = 2;
6527 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006528 int Idx = PermMask[i];
6529 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006530 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006531 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006532 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006533 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006534 LoIdx++;
6535 } else {
6536 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006537 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006538 HiIdx++;
6539 }
6540 }
6541
Nate Begeman9008ca62009-04-27 18:41:29 +00006542 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6543 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006544 int MaskOps[] = { -1, -1, -1, -1 };
6545 for (unsigned i = 0; i != 4; ++i)
6546 if (Locs[i].first != -1)
6547 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006548 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006549}
6550
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006551static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006552 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006553 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006554
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006555 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6556 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006557 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6558 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6559 // BUILD_VECTOR (load), undef
6560 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006561
6562 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006563}
6564
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006565static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006566SDValue getMOVDDup(SDValue &Op, SDLoc &dl, SDValue V1, SelectionDAG &DAG) {
Evan Cheng835580f2010-10-07 20:50:20 +00006567 EVT VT = Op.getValueType();
6568
6569 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006570 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6571 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006572 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6573 V1, DAG));
6574}
6575
6576static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006577SDValue getMOVLowToHigh(SDValue &Op, SDLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006578 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006579 SDValue V1 = Op.getOperand(0);
6580 SDValue V2 = Op.getOperand(1);
6581 EVT VT = Op.getValueType();
6582
6583 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6584
Craig Topper1accb7e2012-01-10 06:54:16 +00006585 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006586 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6587
Evan Cheng0899f5c2011-08-31 02:05:24 +00006588 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6589 return DAG.getNode(ISD::BITCAST, dl, VT,
6590 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6591 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6592 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006593}
6594
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006595static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006596SDValue getMOVHighToLow(SDValue &Op, SDLoc &dl, SelectionDAG &DAG) {
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006597 SDValue V1 = Op.getOperand(0);
6598 SDValue V2 = Op.getOperand(1);
6599 EVT VT = Op.getValueType();
6600
6601 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6602 "unsupported shuffle type");
6603
6604 if (V2.getOpcode() == ISD::UNDEF)
6605 V2 = V1;
6606
6607 // v4i32 or v4f32
6608 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6609}
6610
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006611static
Andrew Trickac6d9be2013-05-25 02:42:55 +00006612SDValue getMOVLP(SDValue &Op, SDLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006613 SDValue V1 = Op.getOperand(0);
6614 SDValue V2 = Op.getOperand(1);
6615 EVT VT = Op.getValueType();
6616 unsigned NumElems = VT.getVectorNumElements();
6617
6618 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6619 // operand of these instructions is only memory, so check if there's a
6620 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6621 // same masks.
6622 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006623
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006624 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006625 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006626 CanFoldLoad = true;
6627
6628 // When V1 is a load, it can be folded later into a store in isel, example:
6629 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6630 // turns into:
6631 // (MOVLPSmr addr:$src1, VR128:$src2)
6632 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006633 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006634 CanFoldLoad = true;
6635
Dan Gohman65fd6562011-11-03 21:49:52 +00006636 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006637 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006638 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006639 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6640
6641 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006642 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006643 if (SVOp->getMaskElt(1) != -1)
6644 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006645 }
6646
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006647 // movl and movlp will both match v2i64, but v2i64 is never matched by
6648 // movl earlier because we make it strict to avoid messing with the movlp load
6649 // folding logic (see the code above getMOVLP call). Match it here then,
6650 // this is horrible, but will stay like this until we move all shuffle
6651 // matching to x86 specific nodes. Note that for the 1st condition all
6652 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006653 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006654 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6655 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006656 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006657 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006658 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006659 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006660
6661 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6662
6663 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006664 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006665 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006666}
6667
Michael Liaod9d09602012-10-23 17:34:00 +00006668// Reduce a vector shuffle to zext.
6669SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006670X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006671 // PMOVZX is only available from SSE41.
6672 if (!Subtarget->hasSSE41())
6673 return SDValue();
6674
6675 EVT VT = Op.getValueType();
6676
6677 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006678 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006679 return SDValue();
6680
6681 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006682 SDLoc DL(Op);
Michael Liaod9d09602012-10-23 17:34:00 +00006683 SDValue V1 = Op.getOperand(0);
6684 SDValue V2 = Op.getOperand(1);
6685 unsigned NumElems = VT.getVectorNumElements();
6686
6687 // Extending is an unary operation and the element type of the source vector
6688 // won't be equal to or larger than i64.
6689 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6690 VT.getVectorElementType() == MVT::i64)
6691 return SDValue();
6692
6693 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6694 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006695 while ((1U << Shift) < NumElems) {
6696 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006697 break;
6698 Shift += 1;
6699 // The maximal ratio is 8, i.e. from i8 to i64.
6700 if (Shift > 3)
6701 return SDValue();
6702 }
6703
6704 // Check the shuffle mask.
6705 unsigned Mask = (1U << Shift) - 1;
6706 for (unsigned i = 0; i != NumElems; ++i) {
6707 int EltIdx = SVOp->getMaskElt(i);
6708 if ((i & Mask) != 0 && EltIdx != -1)
6709 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006710 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006711 return SDValue();
6712 }
6713
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006714 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006715 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006716 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6717 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006718
6719 if (!isTypeLegal(NVT))
6720 return SDValue();
6721
6722 // Simplify the operand as it's prepared to be fed into shuffle.
6723 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6724 if (V1.getOpcode() == ISD::BITCAST &&
6725 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6726 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6727 V1.getOperand(0)
6728 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6729 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6730 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006731 ConstantSDNode *CIdx =
6732 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006733 // If it's foldable, i.e. normal load with single use, we will let code
6734 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006735 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006736 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6737 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6738 // The "ext_vec_elt" node is wider than the result node.
6739 // In this case we should extract subvector from V.
6740 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6741 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6742 EVT FullVT = V.getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00006743 EVT SubVecVT = EVT::getVectorVT(*Context,
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006744 FullVT.getVectorElementType(),
6745 FullVT.getVectorNumElements()/Ratio);
Matt Arsenault225ed702013-05-18 00:21:46 +00006746 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006747 DAG.getIntPtrConstant(0));
6748 }
Michael Liaod9d09602012-10-23 17:34:00 +00006749 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006750 }
Michael Liaod9d09602012-10-23 17:34:00 +00006751 }
6752
6753 return DAG.getNode(ISD::BITCAST, DL, VT,
6754 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6755}
6756
Nadav Rotem154819d2012-04-09 07:45:58 +00006757SDValue
6758X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006759 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006760 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006761 SDLoc dl(Op);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006762 SDValue V1 = Op.getOperand(0);
6763 SDValue V2 = Op.getOperand(1);
6764
6765 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006766 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006767
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006768 // Handle splat operations
6769 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006770 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006771 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006772 if (Broadcast.getNode())
6773 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006774 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006775
Michael Liaod9d09602012-10-23 17:34:00 +00006776 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006777 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006778 if (NewOp.getNode())
6779 return NewOp;
6780
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006781 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6782 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006783 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6784 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006785 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006786 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006787 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006788 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006789 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006790 // FIXME: Figure out a cleaner way to do this.
6791 // Try to make use of movq to zero out the top part.
6792 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006793 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006794 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006795 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006796 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6797 NewVT, true, false))
6798 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006799 DAG, Subtarget, dl);
6800 }
6801 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006802 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006803 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006804 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006805 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6806 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6807 DAG, Subtarget, dl);
6808 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006809 }
6810 }
6811 return SDValue();
6812}
6813
Dan Gohman475871a2008-07-27 21:46:04 +00006814SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006815X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006816 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue V1 = Op.getOperand(0);
6818 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006819 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006820 SDLoc dl(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00006821 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006822 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006823 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006824 bool V1IsSplat = false;
6825 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006826 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006827 bool HasFp256 = Subtarget->hasFp256();
6828 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006829 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006830 bool OptForSize = MF.getFunction()->getAttributes().
6831 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006832
Craig Topper3426a3e2011-11-14 06:46:21 +00006833 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006834
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006835 if (V1IsUndef && V2IsUndef)
6836 return DAG.getUNDEF(VT);
6837
6838 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006839
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006840 // Vector shuffle lowering takes 3 steps:
6841 //
6842 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6843 // narrowing and commutation of operands should be handled.
6844 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6845 // shuffle nodes.
6846 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6847 // so the shuffle can be broken into other shuffles and the legalizer can
6848 // try the lowering again.
6849 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006850 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006851 // be matched during isel, all of them must be converted to a target specific
6852 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006853
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006854 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6855 // narrowing and commutation of operands should be handled. The actual code
6856 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006857 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006858 if (NewOp.getNode())
6859 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006860
Craig Topper5aaffa82012-02-19 02:53:47 +00006861 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6862
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006863 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6864 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006865 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006866 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006867 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006868 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006869
Craig Topperdd637ae2012-02-19 05:41:45 +00006870 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006871 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006872 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006873
Craig Topperdd637ae2012-02-19 05:41:45 +00006874 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006875 return getMOVHighToLow(Op, dl, DAG);
6876
6877 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006878 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006879 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006880 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006881
Craig Topper5aaffa82012-02-19 02:53:47 +00006882 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006883 // The actual implementation will match the mask in the if above and then
6884 // during isel it can match several different instructions, not only pshufd
6885 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006886 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6887 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006888
Craig Topper5aaffa82012-02-19 02:53:47 +00006889 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006890
Craig Topper1accb7e2012-01-10 06:54:16 +00006891 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006892 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6893
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006894 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6895 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6896 DAG);
6897
Craig Topperb3982da2011-12-31 23:50:21 +00006898 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006899 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006900 }
Eric Christopherfd179292009-08-27 18:07:15 +00006901
Benjamin Kramera0de26c2013-05-17 14:48:34 +00006902 if (isPALIGNRMask(M, VT, Subtarget))
6903 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
6904 getShufflePALIGNRImmediate(SVOp),
6905 DAG);
6906
Evan Chengf26ffe92008-05-29 08:22:04 +00006907 // Check if this can be converted into a logical shift.
6908 bool isLeft = false;
6909 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006910 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006911 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006912 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006913 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006914 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006915 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006916 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006917 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006918 }
Eric Christopherfd179292009-08-27 18:07:15 +00006919
Craig Topper5aaffa82012-02-19 02:53:47 +00006920 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006921 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006922 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006923 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006924 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006925 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6926
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006927 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006928 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6929 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006930 }
Eric Christopherfd179292009-08-27 18:07:15 +00006931
Nate Begeman9008ca62009-04-27 18:41:29 +00006932 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006933 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006934 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006935
Craig Topperdd637ae2012-02-19 05:41:45 +00006936 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006937 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006938
Craig Topperdd637ae2012-02-19 05:41:45 +00006939 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006940 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006941
Craig Topperdd637ae2012-02-19 05:41:45 +00006942 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006943 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006944
Craig Topperdd637ae2012-02-19 05:41:45 +00006945 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006946 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006947
Craig Topperdd637ae2012-02-19 05:41:45 +00006948 if (ShouldXformToMOVHLPS(M, VT) ||
6949 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006950 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006951
Evan Chengf26ffe92008-05-29 08:22:04 +00006952 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006953 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006954 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006955 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006956 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006957 }
Eric Christopherfd179292009-08-27 18:07:15 +00006958
Evan Cheng9eca5e82006-10-25 21:49:50 +00006959 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006960 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6961 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006962 V1IsSplat = isSplatVector(V1.getNode());
6963 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006964
Chris Lattner8a594482007-11-25 00:24:49 +00006965 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006966 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6967 CommuteVectorShuffleMask(M, NumElems);
6968 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006969 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006970 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006971 }
6972
Craig Topperbeabc6c2011-12-05 06:56:46 +00006973 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006974 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006975 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006976 return V1;
6977 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6978 // the instruction selector will not match, so get a canonical MOVL with
6979 // swapped operands to undo the commute.
6980 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006981 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006982
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006983 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006984 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006985
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006986 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006987 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006988
Evan Cheng9bbbb982006-10-25 20:48:19 +00006989 if (V2IsSplat) {
6990 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006991 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006992 // new vector_shuffle with the corrected mask.p
6993 SmallVector<int, 8> NewMask(M.begin(), M.end());
6994 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006995 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006996 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006997 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006998 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006999 }
7000
Evan Cheng9eca5e82006-10-25 21:49:50 +00007001 if (Commuted) {
7002 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00007003 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00007004 CommuteVectorShuffleMask(M, NumElems);
7005 std::swap(V1, V2);
7006 std::swap(V1IsSplat, V2IsSplat);
7007 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00007008
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007009 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00007010 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00007011
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007012 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00007013 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00007014 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007015
Nate Begeman9008ca62009-04-27 18:41:29 +00007016 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007017 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00007018 return CommuteVectorShuffle(SVOp, DAG);
7019
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00007020 // The checks below are all present in isShuffleMaskLegal, but they are
7021 // inlined here right now to enable us to directly emit target specific
7022 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00007023
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00007024 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
7025 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00007026 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00007027 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00007028 }
7029
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007030 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007031 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007032 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007033 DAG);
7034
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007035 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007036 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007037 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007038 DAG);
7039
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007040 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00007041 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00007042 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00007043
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007044 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007045 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007046 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007047 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007048
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007049 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007050 // Generate target specific nodes for 128 or 256-bit shuffles only
7051 // supported in the AVX instruction set.
7052 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007053
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007054 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007055 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007056 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7057
Craig Topper70b883b2011-11-28 10:14:51 +00007058 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007059 if (isVPERMILPMask(M, VT, HasFp256)) {
7060 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007061 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007062 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007063 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007064 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007065 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007066
Craig Topper70b883b2011-11-28 10:14:51 +00007067 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007068 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007069 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007070 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007071
Craig Topper1842ba02012-04-23 06:38:28 +00007072 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007073 if (BlendOp.getNode())
7074 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007075
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007076 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007077 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007078 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007079 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007080 }
Craig Topper92040742012-04-16 06:43:40 +00007081 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7082 &permclMask[0], 8);
7083 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007084 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007085 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007086 }
Craig Topper095c5282012-04-15 23:48:57 +00007087
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007088 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007089 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007090 getShuffleCLImmediate(SVOp), DAG);
7091
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007092 //===--------------------------------------------------------------------===//
7093 // Since no target specific shuffle was selected for this generic one,
7094 // lower it into other known shuffles. FIXME: this isn't true yet, but
7095 // this is the plan.
7096 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007097
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007098 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7099 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007100 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007101 if (NewOp.getNode())
7102 return NewOp;
7103 }
7104
7105 if (VT == MVT::v16i8) {
7106 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7107 if (NewOp.getNode())
7108 return NewOp;
7109 }
7110
Elena Demikhovsky41789462012-09-06 12:42:01 +00007111 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007112 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007113 if (NewOp.getNode())
7114 return NewOp;
7115 }
7116
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007117 // Handle all 128-bit wide vectors with 4 elements, and match them with
7118 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007119 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007120 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7121
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007122 // Handle general 256-bit shuffles
7123 if (VT.is256BitVector())
7124 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7125
Dan Gohman475871a2008-07-27 21:46:04 +00007126 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007127}
7128
Craig Topperf84b7502013-01-20 00:50:58 +00007129static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007130 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007131 SDLoc dl(Op);
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007132
Craig Topper45e1c752013-01-20 00:38:18 +00007133 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007134 return SDValue();
7135
Duncan Sands83ec4b62008-06-06 12:08:01 +00007136 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007138 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007139 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007140 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007141 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007142 }
7143
7144 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007145 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7146 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7147 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7149 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007150 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007151 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007152 Op.getOperand(0)),
7153 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007154 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007155 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007156 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007157 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007158 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007159 }
7160
7161 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007162 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7163 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007164 // result has a single use which is a store or a bitcast to i32. And in
7165 // the case of a store, it's not worth it if the index is a constant 0,
7166 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007167 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007168 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007169 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007170 if ((User->getOpcode() != ISD::STORE ||
7171 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7172 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007173 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007175 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007176 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007177 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007178 Op.getOperand(0)),
7179 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007180 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007181 }
7182
7183 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007184 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007185 if (isa<ConstantSDNode>(Op.getOperand(1)))
7186 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007187 }
Dan Gohman475871a2008-07-27 21:46:04 +00007188 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007189}
7190
Dan Gohman475871a2008-07-27 21:46:04 +00007191SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007192X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7193 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007194 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007195 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007196
David Greene74a579d2011-02-10 16:57:36 +00007197 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007198 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007199
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007200 // If this is a 256-bit vector result, first extract the 128-bit vector and
7201 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007202 if (VecVT.is256BitVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007203 SDLoc dl(Op.getNode());
David Greene74a579d2011-02-10 16:57:36 +00007204 unsigned NumElems = VecVT.getVectorNumElements();
7205 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007206 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7207
7208 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007209 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007210
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007211 if (IdxVal >= NumElems/2)
7212 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007213 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007214 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007215 }
7216
Craig Topper7a9a28b2012-08-12 02:23:29 +00007217 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007218
Craig Topperd0a31172012-01-10 06:37:29 +00007219 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007220 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007221 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007222 return Res;
7223 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007224
Craig Topper45e1c752013-01-20 00:38:18 +00007225 MVT VT = Op.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007226 SDLoc dl(Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007227 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007228 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007229 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007230 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007231 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007232 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7233 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007234 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007235 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007236 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007237 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007238 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007239 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007240 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007241 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007242 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007243 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007244 }
7245
7246 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007247 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007248 if (Idx == 0)
7249 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007250
Evan Cheng0db9fe62006-04-25 20:13:52 +00007251 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007252 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007253 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007254 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007255 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007256 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007257 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007258 }
7259
7260 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007261 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7262 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7263 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007264 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007265 if (Idx == 0)
7266 return Op;
7267
7268 // UNPCKHPD the element to the lowest double word, then movsd.
7269 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7270 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007271 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007272 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007273 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007274 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007275 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007276 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007277 }
7278
Dan Gohman475871a2008-07-27 21:46:04 +00007279 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007280}
7281
Craig Topperf84b7502013-01-20 00:50:58 +00007282static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007283 MVT VT = Op.getValueType().getSimpleVT();
7284 MVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007285 SDLoc dl(Op);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007286
Dan Gohman475871a2008-07-27 21:46:04 +00007287 SDValue N0 = Op.getOperand(0);
7288 SDValue N1 = Op.getOperand(1);
7289 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007290
Craig Topper7a9a28b2012-08-12 02:23:29 +00007291 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007292 return SDValue();
7293
Dan Gohman8a55ce42009-09-23 21:02:20 +00007294 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007295 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007296 unsigned Opc;
7297 if (VT == MVT::v8i16)
7298 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007299 else if (VT == MVT::v16i8)
7300 Opc = X86ISD::PINSRB;
7301 else
7302 Opc = X86ISD::PINSRB;
7303
Nate Begeman14d12ca2008-02-11 04:19:36 +00007304 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7305 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007306 if (N1.getValueType() != MVT::i32)
7307 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7308 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007309 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007310 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007311 }
7312
7313 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007314 // Bits [7:6] of the constant are the source select. This will always be
7315 // zero here. The DAG Combiner may combine an extract_elt index into these
7316 // bits. For example (insert (extract, 3), 2) could be matched by putting
7317 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007318 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007319 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007320 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007321 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007322 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007323 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007324 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007325 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007326 }
7327
7328 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007329 // PINSR* works with constant index.
7330 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007331 }
Dan Gohman475871a2008-07-27 21:46:04 +00007332 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007333}
7334
Dan Gohman475871a2008-07-27 21:46:04 +00007335SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007336X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007337 MVT VT = Op.getValueType().getSimpleVT();
7338 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007339
Andrew Trickac6d9be2013-05-25 02:42:55 +00007340 SDLoc dl(Op);
David Greene6b381262011-02-09 15:32:06 +00007341 SDValue N0 = Op.getOperand(0);
7342 SDValue N1 = Op.getOperand(1);
7343 SDValue N2 = Op.getOperand(2);
7344
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007345 // If this is a 256-bit vector result, first extract the 128-bit vector,
7346 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007347 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007348 if (!isa<ConstantSDNode>(N2))
7349 return SDValue();
7350
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007351 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007352 unsigned NumElems = VT.getVectorNumElements();
7353 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007354 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007355
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007356 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007357 bool Upper = IdxVal >= NumElems/2;
7358 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7359 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007360
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007361 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007362 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007363 }
7364
Craig Topperd0a31172012-01-10 06:37:29 +00007365 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007366 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7367
Dan Gohman8a55ce42009-09-23 21:02:20 +00007368 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007369 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007370
Dan Gohman8a55ce42009-09-23 21:02:20 +00007371 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007372 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7373 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007374 if (N1.getValueType() != MVT::i32)
7375 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7376 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007377 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007378 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007379 }
Dan Gohman475871a2008-07-27 21:46:04 +00007380 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007381}
7382
Craig Topper55b24052012-09-11 06:15:32 +00007383static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007384 LLVMContext *Context = DAG.getContext();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007385 SDLoc dl(Op);
Craig Topper45e1c752013-01-20 00:38:18 +00007386 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007387
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007388 // If this is a 256-bit vector result, first insert into a 128-bit
7389 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007390 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007391 // Insert into a 128-bit vector.
7392 EVT VT128 = EVT::getVectorVT(*Context,
7393 OpVT.getVectorElementType(),
7394 OpVT.getVectorNumElements() / 2);
7395
7396 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7397
7398 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007399 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007400 }
7401
Craig Topperd77d2fe2012-04-29 20:22:05 +00007402 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007403 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007405
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007407 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007408 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007409 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007410}
7411
David Greene91585092011-01-26 15:38:49 +00007412// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7413// a simple subregister reference or explicit instructions to grab
7414// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007415static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7416 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007417 if (Subtarget->hasFp256()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007418 SDLoc dl(Op.getNode());
David Greenea5f26012011-02-07 19:36:54 +00007419 SDValue Vec = Op.getNode()->getOperand(0);
7420 SDValue Idx = Op.getNode()->getOperand(1);
7421
Craig Topper7a9a28b2012-08-12 02:23:29 +00007422 if (Op.getNode()->getValueType(0).is128BitVector() &&
7423 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007424 isa<ConstantSDNode>(Idx)) {
7425 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7426 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007427 }
David Greene91585092011-01-26 15:38:49 +00007428 }
7429 return SDValue();
7430}
7431
David Greenecfe33c42011-01-26 19:13:22 +00007432// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7433// simple superregister reference or explicit instructions to insert
7434// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007435static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7436 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007437 if (Subtarget->hasFp256()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007438 SDLoc dl(Op.getNode());
David Greenecfe33c42011-01-26 19:13:22 +00007439 SDValue Vec = Op.getNode()->getOperand(0);
7440 SDValue SubVec = Op.getNode()->getOperand(1);
7441 SDValue Idx = Op.getNode()->getOperand(2);
7442
Craig Topper7a9a28b2012-08-12 02:23:29 +00007443 if (Op.getNode()->getValueType(0).is256BitVector() &&
7444 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007445 isa<ConstantSDNode>(Idx)) {
7446 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7447 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007448 }
7449 }
7450 return SDValue();
7451}
7452
Bill Wendling056292f2008-09-16 21:48:12 +00007453// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7454// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7455// one of the above mentioned nodes. It has to be wrapped because otherwise
7456// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7457// be used to form addressing mode. These wrapped nodes will be selected
7458// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007459SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007460X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007461 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007462
Chris Lattner41621a22009-06-26 19:22:52 +00007463 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7464 // global base reg.
7465 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007466 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007467 CodeModel::Model M = getTargetMachine().getCodeModel();
7468
Chris Lattner4f066492009-07-11 20:29:19 +00007469 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007470 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007471 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007472 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007473 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007474 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007475 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007476
Evan Cheng1606e8e2009-03-13 07:51:59 +00007477 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007478 CP->getAlignment(),
7479 CP->getOffset(), OpFlag);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007480 SDLoc DL(CP);
Chris Lattner18c59872009-06-27 04:16:01 +00007481 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007482 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007483 if (OpFlag) {
7484 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007485 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007486 SDLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007487 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007488 }
7489
7490 return Result;
7491}
7492
Dan Gohmand858e902010-04-17 15:26:15 +00007493SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007494 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007495
Chris Lattner18c59872009-06-27 04:16:01 +00007496 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7497 // global base reg.
7498 unsigned char OpFlag = 0;
7499 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007500 CodeModel::Model M = getTargetMachine().getCodeModel();
7501
Chris Lattner4f066492009-07-11 20:29:19 +00007502 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007503 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007504 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007505 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007506 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007507 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007508 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007509
Chris Lattner18c59872009-06-27 04:16:01 +00007510 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7511 OpFlag);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007512 SDLoc DL(JT);
Chris Lattner18c59872009-06-27 04:16:01 +00007513 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007514
Chris Lattner18c59872009-06-27 04:16:01 +00007515 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007516 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007517 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7518 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007519 SDLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007520 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007521
Chris Lattner18c59872009-06-27 04:16:01 +00007522 return Result;
7523}
7524
7525SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007526X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007527 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007528
Chris Lattner18c59872009-06-27 04:16:01 +00007529 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7530 // global base reg.
7531 unsigned char OpFlag = 0;
7532 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007533 CodeModel::Model M = getTargetMachine().getCodeModel();
7534
Chris Lattner4f066492009-07-11 20:29:19 +00007535 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007536 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7537 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7538 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007539 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007540 } else if (Subtarget->isPICStyleGOT()) {
7541 OpFlag = X86II::MO_GOT;
7542 } else if (Subtarget->isPICStyleStubPIC()) {
7543 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7544 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7545 OpFlag = X86II::MO_DARWIN_NONLAZY;
7546 }
Eric Christopherfd179292009-08-27 18:07:15 +00007547
Chris Lattner18c59872009-06-27 04:16:01 +00007548 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007549
Andrew Trickac6d9be2013-05-25 02:42:55 +00007550 SDLoc DL(Op);
Chris Lattner18c59872009-06-27 04:16:01 +00007551 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007552
Chris Lattner18c59872009-06-27 04:16:01 +00007553 // With PIC, the address is actually $g + Offset.
7554 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007555 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007556 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7557 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007558 SDLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007559 Result);
7560 }
Eric Christopherfd179292009-08-27 18:07:15 +00007561
Eli Friedman586272d2011-08-11 01:48:05 +00007562 // For symbols that require a load from a stub to get the address, emit the
7563 // load.
7564 if (isGlobalStubReference(OpFlag))
7565 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007566 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007567
Chris Lattner18c59872009-06-27 04:16:01 +00007568 return Result;
7569}
7570
Dan Gohman475871a2008-07-27 21:46:04 +00007571SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007572X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007573 // Create the TargetBlockAddressAddress node.
7574 unsigned char OpFlags =
7575 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007576 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007577 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007578 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007579 SDLoc dl(Op);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007580 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7581 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007582
Dan Gohmanf705adb2009-10-30 01:28:02 +00007583 if (Subtarget->isPICStyleRIPRel() &&
7584 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007585 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7586 else
7587 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007588
Dan Gohman29cbade2009-11-20 23:18:13 +00007589 // With PIC, the address is actually $g + Offset.
7590 if (isGlobalRelativeToPICBase(OpFlags)) {
7591 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7592 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7593 Result);
7594 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007595
7596 return Result;
7597}
7598
7599SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00007600X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, SDLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007601 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007602 // Create the TargetGlobalAddress node, folding in the constant
7603 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007604 unsigned char OpFlags =
7605 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007606 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007607 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007608 if (OpFlags == X86II::MO_NO_FLAG &&
7609 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007610 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007611 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007612 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007613 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007614 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007615 }
Eric Christopherfd179292009-08-27 18:07:15 +00007616
Chris Lattner4f066492009-07-11 20:29:19 +00007617 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007618 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007619 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7620 else
7621 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007622
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007623 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007624 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007625 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7626 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007627 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007629
Chris Lattner36c25012009-07-10 07:34:39 +00007630 // For globals that require a load from a stub to get the address, emit the
7631 // load.
7632 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007633 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007634 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007635
Dan Gohman6520e202008-10-18 02:06:02 +00007636 // If there was a non-zero offset that we didn't fold, create an explicit
7637 // addition for it.
7638 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007639 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007640 DAG.getConstant(Offset, getPointerTy()));
7641
Evan Cheng0db9fe62006-04-25 20:13:52 +00007642 return Result;
7643}
7644
Evan Chengda43bcf2008-09-24 00:05:32 +00007645SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007646X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007647 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007648 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007649 return LowerGlobalAddress(GV, SDLoc(Op), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007650}
7651
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007652static SDValue
7653GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007654 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007655 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007656 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007657 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007658 SDLoc dl(GA);
Devang Patel0d881da2010-07-06 22:08:15 +00007659 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007660 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007661 GA->getOffset(),
7662 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007663
7664 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7665 : X86ISD::TLSADDR;
7666
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007667 if (InFlag) {
7668 SDValue Ops[] = { Chain, TGA, *InFlag };
Michael Liao0ee17002013-04-19 04:03:37 +00007669 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops));
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007670 } else {
7671 SDValue Ops[] = { Chain, TGA };
Michael Liao0ee17002013-04-19 04:03:37 +00007672 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops));
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007673 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007674
7675 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007676 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007677
Rafael Espindola15f1b662009-04-24 12:59:40 +00007678 SDValue Flag = Chain.getValue(1);
7679 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007680}
7681
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007682// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007683static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007684LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007685 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007686 SDValue InFlag;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007687 SDLoc dl(GA); // ? function entry point might be better
Dale Johannesendd64c412009-02-04 00:33:20 +00007688 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007689 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007690 SDLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007691 InFlag = Chain.getValue(1);
7692
Chris Lattnerb903bed2009-06-26 21:20:29 +00007693 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007694}
7695
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007696// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007697static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007698LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007699 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007700 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7701 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007702}
7703
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007704static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7705 SelectionDAG &DAG,
7706 const EVT PtrVT,
7707 bool is64Bit) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007708 SDLoc dl(GA);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007709
7710 // Get the start address of the TLS block for this module.
7711 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7712 .getInfo<X86MachineFunctionInfo>();
7713 MFI->incNumLocalDynamicTLSAccesses();
7714
7715 SDValue Base;
7716 if (is64Bit) {
7717 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7718 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7719 } else {
7720 SDValue InFlag;
7721 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007722 DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), InFlag);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007723 InFlag = Chain.getValue(1);
7724 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7725 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7726 }
7727
7728 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7729 // of Base.
7730
7731 // Build x@dtpoff.
7732 unsigned char OperandFlags = X86II::MO_DTPOFF;
7733 unsigned WrapperKind = X86ISD::Wrapper;
7734 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7735 GA->getValueType(0),
7736 GA->getOffset(), OperandFlags);
7737 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7738
7739 // Add x@dtpoff with the base.
7740 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7741}
7742
Hans Wennborg228756c2012-05-11 10:11:01 +00007743// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007744static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007745 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007746 bool is64Bit, bool isPIC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007747 SDLoc dl(GA);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007748
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007749 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7750 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7751 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007752
Michael J. Spencerec38de22010-10-10 22:04:20 +00007753 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007754 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007755 MachinePointerInfo(Ptr),
7756 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007757
Chris Lattnerb903bed2009-06-26 21:20:29 +00007758 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007759 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7760 // initialexec.
7761 unsigned WrapperKind = X86ISD::Wrapper;
7762 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007763 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007764 } else if (model == TLSModel::InitialExec) {
7765 if (is64Bit) {
7766 OperandFlags = X86II::MO_GOTTPOFF;
7767 WrapperKind = X86ISD::WrapperRIP;
7768 } else {
7769 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7770 }
Chris Lattner18c59872009-06-27 04:16:01 +00007771 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007772 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007773 }
Eric Christopherfd179292009-08-27 18:07:15 +00007774
Hans Wennborg228756c2012-05-11 10:11:01 +00007775 // emit "addl x@ntpoff,%eax" (local exec)
7776 // or "addl x@indntpoff,%eax" (initial exec)
7777 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007778 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007779 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007780 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007781 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007782
Hans Wennborg228756c2012-05-11 10:11:01 +00007783 if (model == TLSModel::InitialExec) {
7784 if (isPIC && !is64Bit) {
7785 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007786 DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT),
Hans Wennborg228756c2012-05-11 10:11:01 +00007787 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007788 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007789
7790 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7791 MachinePointerInfo::getGOT(), false, false, false,
7792 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007793 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007794
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007795 // The address of the thread local variable is the add of the thread
7796 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007797 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007798}
7799
Dan Gohman475871a2008-07-27 21:46:04 +00007800SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007801X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007802
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007803 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007804 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007805
Eric Christopher30ef0e52010-06-03 04:07:48 +00007806 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007807 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007808
Eric Christopher30ef0e52010-06-03 04:07:48 +00007809 switch (model) {
7810 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007811 if (Subtarget->is64Bit())
7812 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7813 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007814 case TLSModel::LocalDynamic:
7815 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7816 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007817 case TLSModel::InitialExec:
7818 case TLSModel::LocalExec:
7819 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007820 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007821 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007822 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007823 llvm_unreachable("Unknown TLS model.");
7824 }
7825
7826 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007827 // Darwin only has one model of TLS. Lower to that.
7828 unsigned char OpFlag = 0;
7829 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7830 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007831
Eric Christopher30ef0e52010-06-03 04:07:48 +00007832 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7833 // global base reg.
7834 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7835 !Subtarget->is64Bit();
7836 if (PIC32)
7837 OpFlag = X86II::MO_TLVP_PIC_BASE;
7838 else
7839 OpFlag = X86II::MO_TLVP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007840 SDLoc DL(Op);
Devang Patel0d881da2010-07-06 22:08:15 +00007841 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007842 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007843 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007844 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007845
Eric Christopher30ef0e52010-06-03 04:07:48 +00007846 // With PIC32, the address is actually $g + Offset.
7847 if (PIC32)
7848 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7849 DAG.getNode(X86ISD::GlobalBaseReg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007850 SDLoc(), getPointerTy()),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007851 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007852
Eric Christopher30ef0e52010-06-03 04:07:48 +00007853 // Lowering the machine isd will make sure everything is in the right
7854 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007855 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007856 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007857 SDValue Args[] = { Chain, Offset };
7858 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007859
Eric Christopher30ef0e52010-06-03 04:07:48 +00007860 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7861 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7862 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007863
Eric Christopher30ef0e52010-06-03 04:07:48 +00007864 // And our return value (tls address) is in the standard call return value
7865 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007866 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007867 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7868 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007869 }
7870
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007871 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007872 // Just use the implicit TLS architecture
7873 // Need to generate someting similar to:
7874 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7875 // ; from TEB
7876 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7877 // mov rcx, qword [rdx+rcx*8]
7878 // mov eax, .tls$:tlsvar
7879 // [rax+rcx] contains the address
7880 // Windows 64bit: gs:0x58
7881 // Windows 32bit: fs:__tls_array
7882
7883 // If GV is an alias then use the aliasee for determining
7884 // thread-localness.
7885 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7886 GV = GA->resolveAliasedGlobal(false);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007887 SDLoc dl(GA);
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007888 SDValue Chain = DAG.getEntryNode();
7889
7890 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007891 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
7892 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007893 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7894 ? Type::getInt8PtrTy(*DAG.getContext(),
7895 256)
7896 : Type::getInt32PtrTy(*DAG.getContext(),
7897 257));
7898
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007899 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
7900 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
7901 DAG.getExternalSymbol("_tls_array", getPointerTy()));
7902
7903 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007904 MachinePointerInfo(Ptr),
7905 false, false, false, 0);
7906
7907 // Load the _tls_index variable
7908 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7909 if (Subtarget->is64Bit())
7910 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7911 IDX, MachinePointerInfo(), MVT::i32,
7912 false, false, 0);
7913 else
7914 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7915 false, false, false, 0);
7916
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007917 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007918 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007919 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7920
7921 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7922 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7923 false, false, false, 0);
7924
7925 // Get the offset of start of .tls section
7926 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7927 GA->getValueType(0),
7928 GA->getOffset(), X86II::MO_SECREL);
7929 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7930
7931 // The address of the thread local variable is the add of the thread
7932 // pointer with the offset of the variable.
7933 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007934 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007935
David Blaikie4d6ccb52012-01-20 21:51:11 +00007936 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007937}
7938
Chad Rosierb90d2a92012-01-03 23:19:12 +00007939/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7940/// and take a 2 x i32 value to shift plus a shift amount.
7941SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007942 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007943 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007944 unsigned VTBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007945 SDLoc dl(Op);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007946 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007947 SDValue ShOpLo = Op.getOperand(0);
7948 SDValue ShOpHi = Op.getOperand(1);
7949 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007950 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007952 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007953
Dan Gohman475871a2008-07-27 21:46:04 +00007954 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007955 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007956 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7957 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007958 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007959 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7960 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007961 }
Evan Chenge3413162006-01-09 18:33:28 +00007962
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7964 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007965 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007966 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007967
Dan Gohman475871a2008-07-27 21:46:04 +00007968 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007970 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7971 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007972
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007973 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007974 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7975 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007976 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007977 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7978 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007979 }
7980
Dan Gohman475871a2008-07-27 21:46:04 +00007981 SDValue Ops[2] = { Lo, Hi };
Michael Liao0ee17002013-04-19 04:03:37 +00007982 return DAG.getMergeValues(Ops, array_lengthof(Ops), dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007983}
Evan Chenga3195e82006-01-12 22:54:21 +00007984
Dan Gohmand858e902010-04-17 15:26:15 +00007985SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7986 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007987 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007988
Dale Johannesen0488fb62010-09-30 23:57:10 +00007989 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007990 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007991
Owen Anderson825b72b2009-08-11 20:47:22 +00007992 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007993 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007994
Eli Friedman36df4992009-05-27 00:47:34 +00007995 // These are really Legal; return the operand so the caller accepts it as
7996 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007997 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007998 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007999 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00008000 Subtarget->is64Bit()) {
8001 return Op;
8002 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008003
Andrew Trickac6d9be2013-05-25 02:42:55 +00008004 SDLoc dl(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008005 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00008006 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00008007 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008008 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00008009 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00008010 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008011 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008012 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00008013 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
8014}
Evan Cheng0db9fe62006-04-25 20:13:52 +00008015
Owen Andersone50ed302009-08-10 22:56:29 +00008016SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00008017 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00008018 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008019 // Build the FILD
Andrew Trickac6d9be2013-05-25 02:42:55 +00008020 SDLoc DL(Op);
Chris Lattner5a88b832007-02-25 07:10:00 +00008021 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00008022 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008023 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008024 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00008025 else
Owen Anderson825b72b2009-08-11 20:47:22 +00008026 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008027
Chris Lattner492a43e2010-09-22 01:28:21 +00008028 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00008029
Stuart Hastings84be9582011-06-02 15:57:11 +00008030 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
8031 MachineMemOperand *MMO;
8032 if (FI) {
8033 int SSFI = FI->getIndex();
8034 MMO =
8035 DAG.getMachineFunction()
8036 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8037 MachineMemOperand::MOLoad, ByteSize, ByteSize);
8038 } else {
8039 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
8040 StackSlot = StackSlot.getOperand(1);
8041 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008042 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008043 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
8044 X86ISD::FILD, DL,
8045 Tys, Ops, array_lengthof(Ops),
8046 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008047
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008048 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008049 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008050 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008051
8052 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8053 // shouldn't be necessary except that RFP cannot be live across
8054 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008055 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008056 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8057 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008058 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008059 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008060 SDValue Ops[] = {
8061 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8062 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008063 MachineMemOperand *MMO =
8064 DAG.getMachineFunction()
8065 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008066 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008067
Chris Lattner492a43e2010-09-22 01:28:21 +00008068 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8069 Ops, array_lengthof(Ops),
8070 Op.getValueType(), MMO);
8071 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008072 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008073 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008074 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008075
Evan Cheng0db9fe62006-04-25 20:13:52 +00008076 return Result;
8077}
8078
Bill Wendling8b8a6362009-01-17 03:56:04 +00008079// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008080SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8081 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008082 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008083 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008084 movq %rax, %xmm0
8085 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8086 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8087 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008088 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008089 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008090 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008091 addpd %xmm1, %xmm0
8092 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008093 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008094
Andrew Trickac6d9be2013-05-25 02:42:55 +00008095 SDLoc dl(Op);
Owen Andersona90b3dc2009-07-15 21:51:10 +00008096 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008097
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008098 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008099 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8100 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008101 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008102
Chris Lattner97484792012-01-25 09:56:22 +00008103 SmallVector<Constant*,2> CV1;
8104 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008105 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8106 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008107 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008108 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8109 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008110 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008111 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008112
Bill Wendling397ae212012-01-05 02:13:20 +00008113 // Load the 64-bit value into an XMM register.
8114 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8115 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008116 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008117 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008118 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008119 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8120 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8121 CLod0);
8122
Owen Anderson825b72b2009-08-11 20:47:22 +00008123 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008124 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008125 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008126 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008127 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008128 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008129
Craig Topperd0a31172012-01-10 06:37:29 +00008130 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008131 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8132 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8133 } else {
8134 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8135 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8136 S2F, 0x4E, DAG);
8137 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8138 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8139 Sub);
8140 }
8141
8142 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008143 DAG.getIntPtrConstant(0));
8144}
8145
Bill Wendling8b8a6362009-01-17 03:56:04 +00008146// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008147SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8148 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008149 SDLoc dl(Op);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008150 // FP constant to bias correct the final result.
8151 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008152 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008153
8154 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008155 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008156 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008157
Eli Friedmanf3704762011-08-29 21:15:46 +00008158 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008159 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008160
Owen Anderson825b72b2009-08-11 20:47:22 +00008161 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008162 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008163 DAG.getIntPtrConstant(0));
8164
8165 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008166 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008167 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008168 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008169 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008170 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008171 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008172 MVT::v2f64, Bias)));
8173 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008174 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008175 DAG.getIntPtrConstant(0));
8176
8177 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008178 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008179
8180 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008181 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008182
Craig Topper69947b92012-04-23 06:57:04 +00008183 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008184 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008185 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008186 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008187 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008188
8189 // Handle final rounding.
8190 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008191}
8192
Michael Liaoa7554632012-10-23 17:36:08 +00008193SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8194 SelectionDAG &DAG) const {
8195 SDValue N0 = Op.getOperand(0);
8196 EVT SVT = N0.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008197 SDLoc dl(Op);
Michael Liaoa7554632012-10-23 17:36:08 +00008198
8199 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8200 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8201 "Custom UINT_TO_FP is not supported!");
8202
Craig Topperb99bafe2013-01-21 06:21:54 +00008203 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8204 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008205 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8206 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8207}
8208
Dan Gohmand858e902010-04-17 15:26:15 +00008209SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8210 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008211 SDValue N0 = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008212 SDLoc dl(Op);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008213
Michael Liaoa7554632012-10-23 17:36:08 +00008214 if (Op.getValueType().isVector())
8215 return lowerUINT_TO_FP_vec(Op, DAG);
8216
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008217 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008218 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8219 // the optimization here.
8220 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008221 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008222
Owen Andersone50ed302009-08-10 22:56:29 +00008223 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008224 EVT DstVT = Op.getValueType();
8225 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008226 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008227 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008228 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008229 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008230 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008231
8232 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008233 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008234 if (SrcVT == MVT::i32) {
8235 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8236 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8237 getPointerTy(), StackSlot, WordOff);
8238 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008239 StackSlot, MachinePointerInfo(),
8240 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008241 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008242 OffsetSlot, MachinePointerInfo(),
8243 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008244 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8245 return Fild;
8246 }
8247
8248 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8249 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008250 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008251 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008252 // For i64 source, we need to add the appropriate power of 2 if the input
8253 // was negative. This is the same as the optimization in
8254 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8255 // we must be careful to do the computation in x87 extended precision, not
8256 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008257 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8258 MachineMemOperand *MMO =
8259 DAG.getMachineFunction()
8260 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8261 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008262
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008263 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8264 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Michael Liao0ee17002013-04-19 04:03:37 +00008265 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops,
8266 array_lengthof(Ops), MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008267
8268 APInt FF(32, 0x5F800000ULL);
8269
8270 // Check whether the sign bit is set.
Matt Arsenault225ed702013-05-18 00:21:46 +00008271 SDValue SignSet = DAG.getSetCC(dl,
8272 getSetCCResultType(*DAG.getContext(), MVT::i64),
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008273 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8274 ISD::SETLT);
8275
8276 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8277 SDValue FudgePtr = DAG.getConstantPool(
8278 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8279 getPointerTy());
8280
8281 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8282 SDValue Zero = DAG.getIntPtrConstant(0);
8283 SDValue Four = DAG.getIntPtrConstant(4);
8284 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8285 Zero, Four);
8286 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8287
8288 // Load the value out, extending it from f32 to f80.
8289 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008290 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008291 FudgePtr, MachinePointerInfo::getConstantPool(),
8292 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008293 // Extend everything to 80 bits to force it to be done on x87.
8294 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8295 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008296}
8297
Craig Topperb99bafe2013-01-21 06:21:54 +00008298std::pair<SDValue,SDValue>
8299X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8300 bool IsSigned, bool IsReplace) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008301 SDLoc DL(Op);
Eli Friedman948e95a2009-05-23 09:59:16 +00008302
Owen Andersone50ed302009-08-10 22:56:29 +00008303 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008304
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008305 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008306 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8307 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008308 }
8309
Owen Anderson825b72b2009-08-11 20:47:22 +00008310 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8311 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008312 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008313
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008314 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008315 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008316 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008317 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008318 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008319 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008320 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008321 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008322
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008323 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8324 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008325 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008326 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008327 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008328 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008329
Evan Cheng0db9fe62006-04-25 20:13:52 +00008330 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008331 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8332 Opc = X86ISD::WIN_FTOL;
8333 else
8334 switch (DstTy.getSimpleVT().SimpleTy) {
8335 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8336 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8337 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8338 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8339 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008340
Dan Gohman475871a2008-07-27 21:46:04 +00008341 SDValue Chain = DAG.getEntryNode();
8342 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008343 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008344 // FIXME This causes a redundant load/store if the SSE-class value is already
8345 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008346 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008347 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008348 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008349 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008350 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008351 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008352 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008353 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008354 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008355
Chris Lattner492a43e2010-09-22 01:28:21 +00008356 MachineMemOperand *MMO =
8357 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8358 MachineMemOperand::MOLoad, MemSize, MemSize);
Michael Liao0ee17002013-04-19 04:03:37 +00008359 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops,
8360 array_lengthof(Ops), DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008361 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008362 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008363 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8364 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008365
Chris Lattner07290932010-09-22 01:05:16 +00008366 MachineMemOperand *MMO =
8367 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8368 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008369
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008370 if (Opc != X86ISD::WIN_FTOL) {
8371 // Build the FP_TO_INT*_IN_MEM
8372 SDValue Ops[] = { Chain, Value, StackSlot };
8373 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
Michael Liao0ee17002013-04-19 04:03:37 +00008374 Ops, array_lengthof(Ops), DstTy,
8375 MMO);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008376 return std::make_pair(FIST, StackSlot);
8377 } else {
8378 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8379 DAG.getVTList(MVT::Other, MVT::Glue),
8380 Chain, Value);
8381 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8382 MVT::i32, ftol.getValue(1));
8383 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8384 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008385 SDValue Ops[] = { eax, edx };
8386 SDValue pair = IsReplace
Michael Liao0ee17002013-04-19 04:03:37 +00008387 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, array_lengthof(Ops))
8388 : DAG.getMergeValues(Ops, array_lengthof(Ops), DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008389 return std::make_pair(pair, SDValue());
8390 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008391}
8392
Nadav Rotem0509db22012-12-28 05:45:24 +00008393static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8394 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008395 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008396 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008397 MVT InVT = In.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008398 SDLoc dl(Op);
Nadav Rotem0509db22012-12-28 05:45:24 +00008399
8400 // Optimize vectors in AVX mode:
8401 //
8402 // v8i16 -> v8i32
8403 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8404 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8405 // Concat upper and lower parts.
8406 //
8407 // v4i32 -> v4i64
8408 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8409 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8410 // Concat upper and lower parts.
8411 //
8412
8413 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8414 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8415 return SDValue();
8416
8417 if (Subtarget->hasInt256())
8418 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8419
8420 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8421 SDValue Undef = DAG.getUNDEF(InVT);
8422 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8423 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8424 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8425
Craig Toppera080daf2013-01-20 21:50:27 +00008426 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008427 VT.getVectorNumElements()/2);
8428
8429 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8430 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8431
8432 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8433}
8434
8435SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8436 SelectionDAG &DAG) const {
8437 if (Subtarget->hasFp256()) {
8438 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8439 if (Res.getNode())
8440 return Res;
8441 }
8442
8443 return SDValue();
8444}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008445SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8446 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008447 SDLoc DL(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008448 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008449 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008450 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008451
Nadav Rotem0509db22012-12-28 05:45:24 +00008452 if (Subtarget->hasFp256()) {
8453 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8454 if (Res.getNode())
8455 return Res;
8456 }
8457
Michael Liaoa7554632012-10-23 17:36:08 +00008458 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8459 VT.getVectorNumElements() != SVT.getVectorNumElements())
8460 return SDValue();
8461
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008462 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008463
8464 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008465 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008466 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8467
8468 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8469 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8470 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008471 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8472 DAG.getUNDEF(MVT::v8i16),
8473 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008474
8475 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8476}
8477
Craig Topperd713c0f2013-01-20 21:34:37 +00008478SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008479 SDLoc DL(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008480 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008481 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008482 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008483
Nadav Rotem3c22a442012-12-27 07:45:10 +00008484 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8485 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8486 if (Subtarget->hasInt256()) {
8487 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8488 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8489 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8490 ShufMask);
8491 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8492 DAG.getIntPtrConstant(0));
8493 }
8494
8495 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8496 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8497 DAG.getIntPtrConstant(0));
8498 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8499 DAG.getIntPtrConstant(2));
8500
8501 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8502 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8503
8504 // The PSHUFD mask:
8505 static const int ShufMask1[] = {0, 2, 0, 0};
8506 SDValue Undef = DAG.getUNDEF(VT);
8507 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8508 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8509
8510 // The MOVLHPS mask:
8511 static const int ShufMask2[] = {0, 1, 4, 5};
8512 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8513 }
8514
8515 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8516 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8517 if (Subtarget->hasInt256()) {
8518 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8519
8520 SmallVector<SDValue,32> pshufbMask;
8521 for (unsigned i = 0; i < 2; ++i) {
8522 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8523 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8524 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8525 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8526 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8527 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8528 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8529 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8530 for (unsigned j = 0; j < 8; ++j)
8531 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8532 }
8533 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8534 &pshufbMask[0], 32);
8535 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8536 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8537
8538 static const int ShufMask[] = {0, 2, -1, -1};
8539 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8540 &ShufMask[0]);
8541 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8542 DAG.getIntPtrConstant(0));
8543 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8544 }
8545
8546 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8547 DAG.getIntPtrConstant(0));
8548
8549 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8550 DAG.getIntPtrConstant(4));
8551
8552 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8553 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8554
8555 // The PSHUFB mask:
8556 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8557 -1, -1, -1, -1, -1, -1, -1, -1};
8558
8559 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8560 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8561 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8562
8563 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8564 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8565
8566 // The MOVLHPS Mask:
8567 static const int ShufMask2[] = {0, 1, 4, 5};
8568 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8569 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8570 }
8571
8572 // Handle truncation of V256 to V128 using shuffles.
8573 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008574 return SDValue();
8575
Nadav Rotem3c22a442012-12-27 07:45:10 +00008576 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8577 "Invalid op");
8578 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008579
8580 unsigned NumElems = VT.getVectorNumElements();
8581 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8582 NumElems * 2);
8583
Michael Liaobedcbd42012-10-16 18:14:11 +00008584 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8585 // Prepare truncation shuffle mask
8586 for (unsigned i = 0; i != NumElems; ++i)
8587 MaskVec[i] = i * 2;
8588 SDValue V = DAG.getVectorShuffle(NVT, DL,
8589 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8590 DAG.getUNDEF(NVT), &MaskVec[0]);
8591 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8592 DAG.getIntPtrConstant(0));
8593}
8594
Dan Gohmand858e902010-04-17 15:26:15 +00008595SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8596 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008597 MVT VT = Op.getValueType().getSimpleVT();
8598 if (VT.isVector()) {
8599 if (VT == MVT::v8i16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00008600 return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT,
8601 DAG.getNode(ISD::FP_TO_SINT, SDLoc(Op),
Michael Liaobedcbd42012-10-16 18:14:11 +00008602 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008603 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008604 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008605
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008606 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8607 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008608 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008609 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8610 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008611
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008612 if (StackSlot.getNode())
8613 // Load the result.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008614 return DAG.getLoad(Op.getValueType(), SDLoc(Op),
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008615 FIST, StackSlot, MachinePointerInfo(),
8616 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008617
8618 // The node is the result.
8619 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008620}
8621
Dan Gohmand858e902010-04-17 15:26:15 +00008622SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8623 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008624 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8625 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008626 SDValue FIST = Vals.first, StackSlot = Vals.second;
8627 assert(FIST.getNode() && "Unexpected failure");
8628
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008629 if (StackSlot.getNode())
8630 // Load the result.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008631 return DAG.getLoad(Op.getValueType(), SDLoc(Op),
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008632 FIST, StackSlot, MachinePointerInfo(),
8633 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008634
8635 // The node is the result.
8636 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008637}
8638
Craig Topperb84b4232013-01-21 06:13:28 +00008639static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008640 SDLoc DL(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008641 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008642 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008643 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008644
8645 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8646
8647 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8648 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8649 In, DAG.getUNDEF(SVT)));
8650}
8651
Craig Topper43620672012-09-08 07:31:51 +00008652SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008653 LLVMContext *Context = DAG.getContext();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008654 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008655 MVT VT = Op.getValueType().getSimpleVT();
8656 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008657 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8658 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008659 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008660 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008661 }
Craig Topper43620672012-09-08 07:31:51 +00008662 Constant *C;
8663 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008664 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8665 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008666 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008667 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8668 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008669 C = ConstantVector::getSplat(NumElts, C);
8670 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8671 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008672 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008673 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008674 false, false, false, Alignment);
8675 if (VT.isVector()) {
8676 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8677 return DAG.getNode(ISD::BITCAST, dl, VT,
8678 DAG.getNode(ISD::AND, dl, ANDVT,
8679 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8680 Op.getOperand(0)),
8681 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8682 }
Dale Johannesenace16102009-02-03 19:33:06 +00008683 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008684}
8685
Dan Gohmand858e902010-04-17 15:26:15 +00008686SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008687 LLVMContext *Context = DAG.getContext();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008688 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008689 MVT VT = Op.getValueType().getSimpleVT();
8690 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008691 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8692 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008693 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008694 NumElts = VT.getVectorNumElements();
8695 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008696 Constant *C;
8697 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008698 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8699 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008700 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008701 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8702 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008703 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008704 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8705 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008706 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008707 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008708 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008709 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008710 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008711 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008712 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008713 DAG.getNode(ISD::BITCAST, dl, XORVT,
8714 Op.getOperand(0)),
8715 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008716 }
Craig Topper69947b92012-04-23 06:57:04 +00008717
8718 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008719}
8720
Dan Gohmand858e902010-04-17 15:26:15 +00008721SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008722 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008723 SDValue Op0 = Op.getOperand(0);
8724 SDValue Op1 = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008725 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008726 MVT VT = Op.getValueType().getSimpleVT();
8727 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008728
8729 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008730 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008731 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008732 SrcVT = VT;
8733 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008734 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008735 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008736 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008737 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008738 }
8739
8740 // At this point the operands and the result should have the same
8741 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008742
Evan Cheng68c47cb2007-01-05 07:55:56 +00008743 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008744 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008746 const fltSemantics &Sem = APFloat::IEEEdouble;
8747 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8748 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008749 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008750 const fltSemantics &Sem = APFloat::IEEEsingle;
8751 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8752 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8753 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8754 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008755 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008756 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008757 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008758 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008759 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008760 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008761 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008762
8763 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008764 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008765 // Op0 is MVT::f32, Op1 is MVT::f64.
8766 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8767 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8768 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008769 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008770 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008771 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008772 }
8773
Evan Cheng73d6cf12007-01-05 21:37:56 +00008774 // Clear first operand sign bit.
8775 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008776 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008777 const fltSemantics &Sem = APFloat::IEEEdouble;
8778 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8779 APInt(64, ~(1ULL << 63)))));
8780 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008781 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008782 const fltSemantics &Sem = APFloat::IEEEsingle;
8783 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8784 APInt(32, ~(1U << 31)))));
8785 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8786 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8787 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008788 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008789 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008790 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008791 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008792 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008793 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008794 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008795
8796 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008797 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008798}
8799
Craig Topper55b24052012-09-11 06:15:32 +00008800static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008801 SDValue N0 = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008802 SDLoc dl(Op);
Craig Toppera080daf2013-01-20 21:50:27 +00008803 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008804
8805 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8806 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8807 DAG.getConstant(1, VT));
8808 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8809}
8810
Michael Liaof966e4e2012-09-13 20:24:54 +00008811// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8812//
Craig Topperb99bafe2013-01-21 06:21:54 +00008813SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8814 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008815 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8816
8817 if (!Subtarget->hasSSE41())
8818 return SDValue();
8819
8820 if (!Op->hasOneUse())
8821 return SDValue();
8822
8823 SDNode *N = Op.getNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008824 SDLoc DL(N);
Michael Liaof966e4e2012-09-13 20:24:54 +00008825
8826 SmallVector<SDValue, 8> Opnds;
8827 DenseMap<SDValue, unsigned> VecInMap;
8828 EVT VT = MVT::Other;
8829
8830 // Recognize a special case where a vector is casted into wide integer to
8831 // test all 0s.
8832 Opnds.push_back(N->getOperand(0));
8833 Opnds.push_back(N->getOperand(1));
8834
8835 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
Craig Topper365ef0b2013-07-03 15:07:05 +00008836 SmallVectorImpl<SDValue>::const_iterator I = Opnds.begin() + Slot;
Michael Liaof966e4e2012-09-13 20:24:54 +00008837 // BFS traverse all OR'd operands.
8838 if (I->getOpcode() == ISD::OR) {
8839 Opnds.push_back(I->getOperand(0));
8840 Opnds.push_back(I->getOperand(1));
8841 // Re-evaluate the number of nodes to be traversed.
8842 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8843 continue;
8844 }
8845
8846 // Quit if a non-EXTRACT_VECTOR_ELT
8847 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8848 return SDValue();
8849
8850 // Quit if without a constant index.
8851 SDValue Idx = I->getOperand(1);
8852 if (!isa<ConstantSDNode>(Idx))
8853 return SDValue();
8854
8855 SDValue ExtractedFromVec = I->getOperand(0);
8856 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8857 if (M == VecInMap.end()) {
8858 VT = ExtractedFromVec.getValueType();
8859 // Quit if not 128/256-bit vector.
8860 if (!VT.is128BitVector() && !VT.is256BitVector())
8861 return SDValue();
8862 // Quit if not the same type.
8863 if (VecInMap.begin() != VecInMap.end() &&
8864 VT != VecInMap.begin()->first.getValueType())
8865 return SDValue();
8866 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8867 }
8868 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8869 }
8870
8871 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008872 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008873
8874 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8875 SmallVector<SDValue, 8> VecIns;
8876
8877 for (DenseMap<SDValue, unsigned>::const_iterator
8878 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8879 // Quit if not all elements are used.
8880 if (I->second != FullMask)
8881 return SDValue();
8882 VecIns.push_back(I->first);
8883 }
8884
8885 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8886
8887 // Cast all vectors into TestVT for PTEST.
8888 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8889 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8890
8891 // If more than one full vectors are evaluated, OR them first before PTEST.
8892 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8893 // Each iteration will OR 2 nodes and append the result until there is only
8894 // 1 node left, i.e. the final OR'd value of all vectors.
8895 SDValue LHS = VecIns[Slot];
8896 SDValue RHS = VecIns[Slot + 1];
8897 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8898 }
8899
8900 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8901 VecIns.back(), VecIns.back());
8902}
8903
Dan Gohman076aee32009-03-04 19:44:21 +00008904/// Emit nodes that will be selected as "test Op0,Op0", or something
8905/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008906SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008907 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008908 SDLoc dl(Op);
Dan Gohman076aee32009-03-04 19:44:21 +00008909
Dan Gohman31125812009-03-07 01:58:32 +00008910 // CF and OF aren't always set the way we want. Determine which
8911 // of these we need.
8912 bool NeedCF = false;
8913 bool NeedOF = false;
8914 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008915 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008916 case X86::COND_A: case X86::COND_AE:
8917 case X86::COND_B: case X86::COND_BE:
8918 NeedCF = true;
8919 break;
8920 case X86::COND_G: case X86::COND_GE:
8921 case X86::COND_L: case X86::COND_LE:
8922 case X86::COND_O: case X86::COND_NO:
8923 NeedOF = true;
8924 break;
Dan Gohman31125812009-03-07 01:58:32 +00008925 }
8926
Dan Gohman076aee32009-03-04 19:44:21 +00008927 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008928 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8929 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008930 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8931 // Emit a CMP with 0, which is the TEST pattern.
8932 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8933 DAG.getConstant(0, Op.getValueType()));
8934
8935 unsigned Opcode = 0;
8936 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008937
8938 // Truncate operations may prevent the merge of the SETCC instruction
8939 // and the arithmetic intruction before it. Attempt to truncate the operands
8940 // of the arithmetic instruction and use a reduced bit-width instruction.
8941 bool NeedTruncation = false;
8942 SDValue ArithOp = Op;
8943 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8944 SDValue Arith = Op->getOperand(0);
8945 // Both the trunc and the arithmetic op need to have one user each.
8946 if (Arith->hasOneUse())
8947 switch (Arith.getOpcode()) {
8948 default: break;
8949 case ISD::ADD:
8950 case ISD::SUB:
8951 case ISD::AND:
8952 case ISD::OR:
8953 case ISD::XOR: {
8954 NeedTruncation = true;
8955 ArithOp = Arith;
8956 }
8957 }
8958 }
8959
8960 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8961 // which may be the result of a CAST. We use the variable 'Op', which is the
8962 // non-casted variable when we check for possible users.
8963 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008964 case ISD::ADD:
8965 // Due to an isel shortcoming, be conservative if this add is likely to be
8966 // selected as part of a load-modify-store instruction. When the root node
8967 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8968 // uses of other nodes in the match, such as the ADD in this case. This
8969 // leads to the ADD being left around and reselected, with the result being
8970 // two adds in the output. Alas, even if none our users are stores, that
8971 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8972 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8973 // climbing the DAG back to the root, and it doesn't seem to be worth the
8974 // effort.
8975 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008976 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8977 if (UI->getOpcode() != ISD::CopyToReg &&
8978 UI->getOpcode() != ISD::SETCC &&
8979 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008980 goto default_case;
8981
8982 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008983 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008984 // An add of one will be selected as an INC.
8985 if (C->getAPIntValue() == 1) {
8986 Opcode = X86ISD::INC;
8987 NumOperands = 1;
8988 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008989 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008990
8991 // An add of negative one (subtract of one) will be selected as a DEC.
8992 if (C->getAPIntValue().isAllOnesValue()) {
8993 Opcode = X86ISD::DEC;
8994 NumOperands = 1;
8995 break;
8996 }
Dan Gohman076aee32009-03-04 19:44:21 +00008997 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008998
8999 // Otherwise use a regular EFLAGS-setting add.
9000 Opcode = X86ISD::ADD;
9001 NumOperands = 2;
9002 break;
9003 case ISD::AND: {
9004 // If the primary and result isn't used, don't bother using X86ISD::AND,
9005 // because a TEST instruction will be better.
9006 bool NonFlagUse = false;
9007 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9008 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9009 SDNode *User = *UI;
9010 unsigned UOpNo = UI.getOperandNo();
9011 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
9012 // Look pass truncate.
9013 UOpNo = User->use_begin().getOperandNo();
9014 User = *User->use_begin();
9015 }
9016
9017 if (User->getOpcode() != ISD::BRCOND &&
9018 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009019 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009020 NonFlagUse = true;
9021 break;
9022 }
Dan Gohman076aee32009-03-04 19:44:21 +00009023 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009024
9025 if (!NonFlagUse)
9026 break;
9027 }
9028 // FALL THROUGH
9029 case ISD::SUB:
9030 case ISD::OR:
9031 case ISD::XOR:
9032 // Due to the ISEL shortcoming noted above, be conservative if this op is
9033 // likely to be selected as part of a load-modify-store instruction.
9034 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9035 UE = Op.getNode()->use_end(); UI != UE; ++UI)
9036 if (UI->getOpcode() == ISD::STORE)
9037 goto default_case;
9038
9039 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009040 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009041 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009042 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009043 case ISD::XOR: Opcode = X86ISD::XOR; break;
9044 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00009045 case ISD::OR: {
9046 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
9047 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9048 if (EFLAGS.getNode())
9049 return EFLAGS;
9050 }
9051 Opcode = X86ISD::OR;
9052 break;
9053 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009054 }
9055
9056 NumOperands = 2;
9057 break;
9058 case X86ISD::ADD:
9059 case X86ISD::SUB:
9060 case X86ISD::INC:
9061 case X86ISD::DEC:
9062 case X86ISD::OR:
9063 case X86ISD::XOR:
9064 case X86ISD::AND:
9065 return SDValue(Op.getNode(), 1);
9066 default:
9067 default_case:
9068 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009069 }
9070
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009071 // If we found that truncation is beneficial, perform the truncation and
9072 // update 'Op'.
9073 if (NeedTruncation) {
9074 EVT VT = Op.getValueType();
9075 SDValue WideVal = Op->getOperand(0);
9076 EVT WideVT = WideVal.getValueType();
9077 unsigned ConvertedOp = 0;
9078 // Use a target machine opcode to prevent further DAGCombine
9079 // optimizations that may separate the arithmetic operations
9080 // from the setcc node.
9081 switch (WideVal.getOpcode()) {
9082 default: break;
9083 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9084 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9085 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9086 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9087 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9088 }
9089
9090 if (ConvertedOp) {
9091 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9092 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9093 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9094 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9095 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9096 }
9097 }
9098 }
9099
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009100 if (Opcode == 0)
9101 // Emit a CMP with 0, which is the TEST pattern.
9102 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9103 DAG.getConstant(0, Op.getValueType()));
9104
9105 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9106 SmallVector<SDValue, 4> Ops;
9107 for (unsigned i = 0; i != NumOperands; ++i)
9108 Ops.push_back(Op.getOperand(i));
9109
9110 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9111 DAG.ReplaceAllUsesWith(Op, New);
9112 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009113}
9114
9115/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9116/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009117SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009118 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009119 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9120 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009121 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009122
Andrew Trickac6d9be2013-05-25 02:42:55 +00009123 SDLoc dl(Op0);
Manman Ren39ad5682012-08-08 00:51:41 +00009124 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9125 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9126 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9127 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9128 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9129 Op0, Op1);
9130 return SDValue(Sub.getNode(), 1);
9131 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009132 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009133}
9134
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009135/// Convert a comparison if required by the subtarget.
9136SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9137 SelectionDAG &DAG) const {
9138 // If the subtarget does not support the FUCOMI instruction, floating-point
9139 // comparisons have to be converted.
9140 if (Subtarget->hasCMov() ||
9141 Cmp.getOpcode() != X86ISD::CMP ||
9142 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9143 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9144 return Cmp;
9145
9146 // The instruction selector will select an FUCOM instruction instead of
9147 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9148 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9149 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009150 SDLoc dl(Cmp);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009151 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9152 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9153 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9154 DAG.getConstant(8, MVT::i8));
9155 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9156 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9157}
9158
Evan Cheng4e544802012-12-05 00:10:38 +00009159static bool isAllOnes(SDValue V) {
9160 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9161 return C && C->isAllOnesValue();
9162}
9163
Evan Chengd40d03e2010-01-06 19:38:29 +00009164/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9165/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009166SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
Andrew Trickac6d9be2013-05-25 02:42:55 +00009167 SDLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009168 SDValue Op0 = And.getOperand(0);
9169 SDValue Op1 = And.getOperand(1);
9170 if (Op0.getOpcode() == ISD::TRUNCATE)
9171 Op0 = Op0.getOperand(0);
9172 if (Op1.getOpcode() == ISD::TRUNCATE)
9173 Op1 = Op1.getOperand(0);
9174
Evan Chengd40d03e2010-01-06 19:38:29 +00009175 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009176 if (Op1.getOpcode() == ISD::SHL)
9177 std::swap(Op0, Op1);
9178 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009179 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9180 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009181 // If we looked past a truncate, check that it's only truncating away
9182 // known zeros.
9183 unsigned BitWidth = Op0.getValueSizeInBits();
9184 unsigned AndBitWidth = And.getValueSizeInBits();
9185 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009186 APInt Zeros, Ones;
9187 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009188 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9189 return SDValue();
9190 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009191 LHS = Op1;
9192 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009193 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009194 } else if (Op1.getOpcode() == ISD::Constant) {
9195 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009196 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009197 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009198
9199 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009200 LHS = AndLHS.getOperand(0);
9201 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009202 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009203
9204 // Use BT if the immediate can't be encoded in a TEST instruction.
9205 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9206 LHS = AndLHS;
9207 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9208 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009209 }
Evan Cheng0488db92007-09-25 01:57:46 +00009210
Evan Chengd40d03e2010-01-06 19:38:29 +00009211 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009212 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009213 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009214 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009215 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009216 // Also promote i16 to i32 for performance / code size reason.
9217 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009218 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009219 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009220
Evan Chengd40d03e2010-01-06 19:38:29 +00009221 // If the operand types disagree, extend the shift amount to match. Since
9222 // BT ignores high bits (like shifts) we can use anyextend.
9223 if (LHS.getValueType() != RHS.getValueType())
9224 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009225
Evan Chengd40d03e2010-01-06 19:38:29 +00009226 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009227 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Evan Chengd40d03e2010-01-06 19:38:29 +00009228 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9229 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009230 }
9231
Evan Cheng54de3ea2010-01-05 06:52:31 +00009232 return SDValue();
9233}
9234
Craig Topper89af15e2011-09-18 08:03:58 +00009235// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009236// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009237static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009238 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009239
Craig Topper7a9a28b2012-08-12 02:23:29 +00009240 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009241 "Unsupported value type for operation");
9242
Craig Topper66ddd152012-04-27 22:54:43 +00009243 unsigned NumElems = VT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009244 SDLoc dl(Op);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009245 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009246
9247 // Extract the LHS vectors
9248 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009249 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9250 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009251
9252 // Extract the RHS vectors
9253 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009254 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9255 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009256
9257 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009258 MVT EltVT = VT.getVectorElementType();
9259 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009260 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9261 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9262 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9263}
9264
Craig Topper26827f32013-01-20 09:02:22 +00009265static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9266 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009267 SDValue Cond;
9268 SDValue Op0 = Op.getOperand(0);
9269 SDValue Op1 = Op.getOperand(1);
9270 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009271 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009272 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009273 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009274 SDLoc dl(Op);
Nate Begeman30a0de92008-07-17 16:51:19 +00009275
9276 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009277#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009278 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009279 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9280#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009281
Craig Topper523908d2012-08-13 02:34:03 +00009282 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009283 bool Swap = false;
9284
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009285 // SSE Condition code mapping:
9286 // 0 - EQ
9287 // 1 - LT
9288 // 2 - LE
9289 // 3 - UNORD
9290 // 4 - NEQ
9291 // 5 - NLT
9292 // 6 - NLE
9293 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009294 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009295 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009296 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009297 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009298 case ISD::SETOGT:
9299 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009300 case ISD::SETLT:
9301 case ISD::SETOLT: SSECC = 1; break;
9302 case ISD::SETOGE:
9303 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009304 case ISD::SETLE:
9305 case ISD::SETOLE: SSECC = 2; break;
9306 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009307 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009308 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009309 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009310 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009311 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009312 case ISD::SETUGT: SSECC = 6; break;
9313 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009314 case ISD::SETUEQ:
9315 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009316 }
9317 if (Swap)
9318 std::swap(Op0, Op1);
9319
Nate Begemanfb8ead02008-07-25 19:05:58 +00009320 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009322 unsigned CC0, CC1;
9323 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009324 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009325 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9326 } else {
9327 assert(SetCCOpcode == ISD::SETONE);
9328 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009329 }
Craig Topper523908d2012-08-13 02:34:03 +00009330
9331 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9332 DAG.getConstant(CC0, MVT::i8));
9333 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9334 DAG.getConstant(CC1, MVT::i8));
9335 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009336 }
9337 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009338 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9339 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009341
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009342 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009343 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009344 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009345
Nate Begeman30a0de92008-07-17 16:51:19 +00009346 // We are handling one of the integer comparisons here. Since SSE only has
9347 // GT and EQ comparisons for integer, swapping operands and multiple
9348 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009349 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009350 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009351
Nate Begeman30a0de92008-07-17 16:51:19 +00009352 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009353 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009354 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009355 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009356 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009357 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009358 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009359 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009360 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009361 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009362 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009363 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009364 }
9365 if (Swap)
9366 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009367
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009368 // Check that the operation in question is available (most are plain SSE2,
9369 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009370 if (VT == MVT::v2i64) {
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009371 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42()) {
9372 assert(Subtarget->hasSSE2() && "Don't know how to lower!");
9373
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009374 // First cast everything to the right type.
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009375 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9376 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9377
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009378 // Since SSE has no unsigned integer comparisons, we need to flip the sign
Benjamin Kramer60ef6c92013-05-22 17:01:12 +00009379 // bits of the inputs before performing those operations. The lower
9380 // compare is always unsigned.
9381 SDValue SB;
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009382 if (FlipSigns) {
Benjamin Kramer60ef6c92013-05-22 17:01:12 +00009383 SB = DAG.getConstant(0x80000000U, MVT::v4i32);
9384 } else {
9385 SDValue Sign = DAG.getConstant(0x80000000U, MVT::i32);
9386 SDValue Zero = DAG.getConstant(0x00000000U, MVT::i32);
9387 SB = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
9388 Sign, Zero, Sign, Zero);
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009389 }
Benjamin Kramer60ef6c92013-05-22 17:01:12 +00009390 Op0 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op0, SB);
9391 Op1 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op1, SB);
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009392
Benjamin Kramerfcba22d2013-04-18 21:37:45 +00009393 // Emulate PCMPGTQ with (hi1 > hi2) | ((hi1 == hi2) & (lo1 > lo2))
9394 SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1);
9395 SDValue EQ = DAG.getNode(X86ISD::PCMPEQ, dl, MVT::v4i32, Op0, Op1);
9396
9397 // Create masks for only the low parts/high parts of the 64 bit integers.
9398 const int MaskHi[] = { 1, 1, 3, 3 };
9399 const int MaskLo[] = { 0, 0, 2, 2 };
9400 SDValue EQHi = DAG.getVectorShuffle(MVT::v4i32, dl, EQ, EQ, MaskHi);
9401 SDValue GTLo = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskLo);
9402 SDValue GTHi = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi);
9403
9404 SDValue Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, EQHi, GTLo);
9405 Result = DAG.getNode(ISD::OR, dl, MVT::v4i32, Result, GTHi);
9406
9407 if (Invert)
9408 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9409
9410 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9411 }
9412
Benjamin Kramer382ed782012-12-25 12:54:19 +00009413 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9414 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009415 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009416 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9417
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009418 // First cast everything to the right type.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009419 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9420 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9421
9422 // Do the compare.
9423 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9424
9425 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009426 const int Mask[] = { 1, 0, 3, 2 };
9427 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9428 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009429
9430 if (Invert)
9431 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9432
9433 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9434 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009435 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009436
Benjamin Kramerf106d8b2013-05-21 09:58:54 +00009437 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9438 // bits of the inputs before performing those operations.
9439 if (FlipSigns) {
9440 EVT EltVT = VT.getVectorElementType();
9441 SDValue SB = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), VT);
9442 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SB);
9443 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SB);
9444 }
9445
Dale Johannesenace16102009-02-03 19:33:06 +00009446 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009447
9448 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009449 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009450 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009451
Nate Begeman30a0de92008-07-17 16:51:19 +00009452 return Result;
9453}
Evan Cheng0488db92007-09-25 01:57:46 +00009454
Craig Topper26827f32013-01-20 09:02:22 +00009455SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9456
9457 MVT VT = Op.getValueType().getSimpleVT();
9458
9459 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9460
9461 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9462 SDValue Op0 = Op.getOperand(0);
9463 SDValue Op1 = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009464 SDLoc dl(Op);
Craig Topper26827f32013-01-20 09:02:22 +00009465 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9466
9467 // Optimize to BT if possible.
9468 // Lower (X & (1 << N)) == 0 to BT(X, N).
9469 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9470 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9471 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9472 Op1.getOpcode() == ISD::Constant &&
9473 cast<ConstantSDNode>(Op1)->isNullValue() &&
9474 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9475 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9476 if (NewSetCC.getNode())
9477 return NewSetCC;
9478 }
9479
9480 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9481 // these.
9482 if (Op1.getOpcode() == ISD::Constant &&
9483 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9484 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9485 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9486
9487 // If the input is a setcc, then reuse the input setcc or use a new one with
9488 // the inverted condition.
9489 if (Op0.getOpcode() == X86ISD::SETCC) {
9490 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9491 bool Invert = (CC == ISD::SETNE) ^
9492 cast<ConstantSDNode>(Op1)->isNullValue();
9493 if (!Invert) return Op0;
9494
9495 CCode = X86::GetOppositeBranchCondition(CCode);
9496 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9497 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9498 }
9499 }
9500
9501 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9502 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9503 if (X86CC == X86::COND_INVALID)
9504 return SDValue();
9505
9506 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9507 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9508 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9509 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9510}
9511
Evan Cheng370e5342008-12-03 08:38:43 +00009512// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009513static bool isX86LogicalCmp(SDValue Op) {
9514 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009515 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9516 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009517 return true;
9518 if (Op.getResNo() == 1 &&
9519 (Opc == X86ISD::ADD ||
9520 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009521 Opc == X86ISD::ADC ||
9522 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009523 Opc == X86ISD::SMUL ||
9524 Opc == X86ISD::UMUL ||
9525 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009526 Opc == X86ISD::DEC ||
9527 Opc == X86ISD::OR ||
9528 Opc == X86ISD::XOR ||
9529 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009530 return true;
9531
Chris Lattner9637d5b2010-12-05 07:49:54 +00009532 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9533 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009534
Dan Gohman076aee32009-03-04 19:44:21 +00009535 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009536}
9537
Chris Lattnera2b56002010-12-05 01:23:24 +00009538static bool isZero(SDValue V) {
9539 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9540 return C && C->isNullValue();
9541}
9542
Evan Chengb64dd5f2012-08-07 22:21:00 +00009543static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9544 if (V.getOpcode() != ISD::TRUNCATE)
9545 return false;
9546
9547 SDValue VOp0 = V.getOperand(0);
9548 unsigned InBits = VOp0.getValueSizeInBits();
9549 unsigned Bits = V.getValueSizeInBits();
9550 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9551}
9552
Dan Gohmand858e902010-04-17 15:26:15 +00009553SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009554 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009555 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009556 SDValue Op1 = Op.getOperand(1);
9557 SDValue Op2 = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009558 SDLoc DL(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00009559 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009560
Dan Gohman1a492952009-10-20 16:22:37 +00009561 if (Cond.getOpcode() == ISD::SETCC) {
9562 SDValue NewCond = LowerSETCC(Cond, DAG);
9563 if (NewCond.getNode())
9564 Cond = NewCond;
9565 }
Evan Cheng734503b2006-09-11 02:19:56 +00009566
Chris Lattnera2b56002010-12-05 01:23:24 +00009567 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009568 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009569 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009570 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009571 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009572 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9573 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009574 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009575
Chris Lattnera2b56002010-12-05 01:23:24 +00009576 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009577
9578 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009579 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9580 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009581
9582 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009583 // Apply further optimizations for special cases
9584 // (select (x != 0), -1, 0) -> neg & sbb
9585 // (select (x == 0), 0, -1) -> neg & sbb
9586 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009587 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009588 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9589 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009590 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9591 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009592 CmpOp0);
9593 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9594 DAG.getConstant(X86::COND_B, MVT::i8),
9595 SDValue(Neg.getNode(), 1));
9596 return Res;
9597 }
9598
Chris Lattnera2b56002010-12-05 01:23:24 +00009599 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9600 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009601 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009602
Chris Lattner96908b12010-12-05 02:00:51 +00009603 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009604 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9605 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009606
Chris Lattner96908b12010-12-05 02:00:51 +00009607 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9608 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009609
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009610 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009611 if (N2C == 0 || !N2C->isNullValue())
9612 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9613 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009614 }
9615 }
9616
Chris Lattnera2b56002010-12-05 01:23:24 +00009617 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009618 if (Cond.getOpcode() == ISD::AND &&
9619 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9620 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009621 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009622 Cond = Cond.getOperand(0);
9623 }
9624
Evan Cheng3f41d662007-10-08 22:16:29 +00009625 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9626 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009627 unsigned CondOpcode = Cond.getOpcode();
9628 if (CondOpcode == X86ISD::SETCC ||
9629 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009630 CC = Cond.getOperand(0);
9631
Dan Gohman475871a2008-07-27 21:46:04 +00009632 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009633 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009634 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009635
Evan Cheng3f41d662007-10-08 22:16:29 +00009636 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009637 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009638 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009639 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009640
Chris Lattnerd1980a52009-03-12 06:52:53 +00009641 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9642 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009643 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009644 addTest = false;
9645 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009646 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9647 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9648 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9649 Cond.getOperand(0).getValueType() != MVT::i8)) {
9650 SDValue LHS = Cond.getOperand(0);
9651 SDValue RHS = Cond.getOperand(1);
9652 unsigned X86Opcode;
9653 unsigned X86Cond;
9654 SDVTList VTs;
9655 switch (CondOpcode) {
9656 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9657 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9658 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9659 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9660 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9661 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9662 default: llvm_unreachable("unexpected overflowing operator");
9663 }
9664 if (CondOpcode == ISD::UMULO)
9665 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9666 MVT::i32);
9667 else
9668 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9669
9670 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9671
9672 if (CondOpcode == ISD::UMULO)
9673 Cond = X86Op.getValue(2);
9674 else
9675 Cond = X86Op.getValue(1);
9676
9677 CC = DAG.getConstant(X86Cond, MVT::i8);
9678 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009679 }
9680
9681 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009682 // Look pass the truncate if the high bits are known zero.
9683 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9684 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009685
9686 // We know the result of AND is compared against zero. Try to match
9687 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009688 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009689 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009690 if (NewSetCC.getNode()) {
9691 CC = NewSetCC.getOperand(0);
9692 Cond = NewSetCC.getOperand(1);
9693 addTest = false;
9694 }
9695 }
9696 }
9697
9698 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009699 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009700 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009701 }
9702
Benjamin Kramere915ff32010-12-22 23:09:28 +00009703 // a < b ? -1 : 0 -> RES = ~setcc_carry
9704 // a < b ? 0 : -1 -> RES = setcc_carry
9705 // a >= b ? -1 : 0 -> RES = setcc_carry
9706 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009707 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009708 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009709 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9710
9711 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9712 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9713 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9714 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9715 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9716 return DAG.getNOT(DL, Res, Res.getValueType());
9717 return Res;
9718 }
9719 }
9720
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009721 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9722 // widen the cmov and push the truncate through. This avoids introducing a new
9723 // branch during isel and doesn't add any extensions.
9724 if (Op.getValueType() == MVT::i8 &&
9725 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9726 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9727 if (T1.getValueType() == T2.getValueType() &&
9728 // Blacklist CopyFromReg to avoid partial register stalls.
9729 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9730 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009731 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009732 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9733 }
9734 }
9735
Evan Cheng0488db92007-09-25 01:57:46 +00009736 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9737 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009738 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009739 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009740 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009741}
9742
Nadav Rotem1a330af2012-12-27 22:47:16 +00009743SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9744 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009745 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009746 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009747 MVT InVT = In.getValueType().getSimpleVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009748 SDLoc dl(Op);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009749
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009750 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9751 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9752 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009753
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009754 if (Subtarget->hasInt256())
9755 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009756
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009757 // Optimize vectors in AVX mode
9758 // Sign extend v8i16 to v8i32 and
9759 // v4i32 to v4i64
9760 //
9761 // Divide input vector into two parts
9762 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9763 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9764 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009765
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009766 unsigned NumElems = InVT.getVectorNumElements();
9767 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009768
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009769 SmallVector<int,8> ShufMask1(NumElems, -1);
9770 for (unsigned i = 0; i != NumElems/2; ++i)
9771 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009772
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009773 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009774
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009775 SmallVector<int,8> ShufMask2(NumElems, -1);
9776 for (unsigned i = 0; i != NumElems/2; ++i)
9777 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009778
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009779 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009780
Craig Toppera080daf2013-01-20 21:50:27 +00009781 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009782 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009783
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009784 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9785 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009786
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009787 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009788}
9789
Evan Cheng370e5342008-12-03 08:38:43 +00009790// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9791// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9792// from the AND / OR.
9793static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9794 Opc = Op.getOpcode();
9795 if (Opc != ISD::OR && Opc != ISD::AND)
9796 return false;
9797 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9798 Op.getOperand(0).hasOneUse() &&
9799 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9800 Op.getOperand(1).hasOneUse());
9801}
9802
Evan Cheng961d6d42009-02-02 08:19:07 +00009803// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9804// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009805static bool isXor1OfSetCC(SDValue Op) {
9806 if (Op.getOpcode() != ISD::XOR)
9807 return false;
9808 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9809 if (N1C && N1C->getAPIntValue() == 1) {
9810 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9811 Op.getOperand(0).hasOneUse();
9812 }
9813 return false;
9814}
9815
Dan Gohmand858e902010-04-17 15:26:15 +00009816SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009817 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009818 SDValue Chain = Op.getOperand(0);
9819 SDValue Cond = Op.getOperand(1);
9820 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009821 SDLoc dl(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00009822 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009823 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009824
Dan Gohman1a492952009-10-20 16:22:37 +00009825 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009826 // Check for setcc([su]{add,sub,mul}o == 0).
9827 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9828 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9829 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9830 Cond.getOperand(0).getResNo() == 1 &&
9831 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9832 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9833 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9834 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9835 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9836 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9837 Inverted = true;
9838 Cond = Cond.getOperand(0);
9839 } else {
9840 SDValue NewCond = LowerSETCC(Cond, DAG);
9841 if (NewCond.getNode())
9842 Cond = NewCond;
9843 }
Dan Gohman1a492952009-10-20 16:22:37 +00009844 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009845#if 0
9846 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009847 else if (Cond.getOpcode() == X86ISD::ADD ||
9848 Cond.getOpcode() == X86ISD::SUB ||
9849 Cond.getOpcode() == X86ISD::SMUL ||
9850 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009851 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009852#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009853
Evan Chengad9c0a32009-12-15 00:53:42 +00009854 // Look pass (and (setcc_carry (cmp ...)), 1).
9855 if (Cond.getOpcode() == ISD::AND &&
9856 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9857 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009858 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009859 Cond = Cond.getOperand(0);
9860 }
9861
Evan Cheng3f41d662007-10-08 22:16:29 +00009862 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9863 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009864 unsigned CondOpcode = Cond.getOpcode();
9865 if (CondOpcode == X86ISD::SETCC ||
9866 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009867 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009868
Dan Gohman475871a2008-07-27 21:46:04 +00009869 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009870 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009871 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009872 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009873 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009874 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009875 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009876 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009877 default: break;
9878 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009879 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009880 // These can only come from an arithmetic instruction with overflow,
9881 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009882 Cond = Cond.getNode()->getOperand(1);
9883 addTest = false;
9884 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009885 }
Evan Cheng0488db92007-09-25 01:57:46 +00009886 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009887 }
9888 CondOpcode = Cond.getOpcode();
9889 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9890 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9891 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9892 Cond.getOperand(0).getValueType() != MVT::i8)) {
9893 SDValue LHS = Cond.getOperand(0);
9894 SDValue RHS = Cond.getOperand(1);
9895 unsigned X86Opcode;
9896 unsigned X86Cond;
9897 SDVTList VTs;
9898 switch (CondOpcode) {
9899 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9900 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9901 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9902 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9903 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9904 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9905 default: llvm_unreachable("unexpected overflowing operator");
9906 }
9907 if (Inverted)
9908 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9909 if (CondOpcode == ISD::UMULO)
9910 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9911 MVT::i32);
9912 else
9913 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9914
9915 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9916
9917 if (CondOpcode == ISD::UMULO)
9918 Cond = X86Op.getValue(2);
9919 else
9920 Cond = X86Op.getValue(1);
9921
9922 CC = DAG.getConstant(X86Cond, MVT::i8);
9923 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009924 } else {
9925 unsigned CondOpc;
9926 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9927 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009928 if (CondOpc == ISD::OR) {
9929 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9930 // two branches instead of an explicit OR instruction with a
9931 // separate test.
9932 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009933 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009934 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009935 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009936 Chain, Dest, CC, Cmp);
9937 CC = Cond.getOperand(1).getOperand(0);
9938 Cond = Cmp;
9939 addTest = false;
9940 }
9941 } else { // ISD::AND
9942 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9943 // two branches instead of an explicit AND instruction with a
9944 // separate test. However, we only do this if this block doesn't
9945 // have a fall-through edge, because this requires an explicit
9946 // jmp when the condition is false.
9947 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009948 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009949 Op.getNode()->hasOneUse()) {
9950 X86::CondCode CCode =
9951 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9952 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009953 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009954 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009955 // Look for an unconditional branch following this conditional branch.
9956 // We need this because we need to reverse the successors in order
9957 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009958 if (User->getOpcode() == ISD::BR) {
9959 SDValue FalseBB = User->getOperand(1);
9960 SDNode *NewBR =
9961 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009962 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009963 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009964 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009965
Dale Johannesene4d209d2009-02-03 20:21:25 +00009966 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009967 Chain, Dest, CC, Cmp);
9968 X86::CondCode CCode =
9969 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9970 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009971 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009972 Cond = Cmp;
9973 addTest = false;
9974 }
9975 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009976 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009977 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9978 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9979 // It should be transformed during dag combiner except when the condition
9980 // is set by a arithmetics with overflow node.
9981 X86::CondCode CCode =
9982 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9983 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009984 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009985 Cond = Cond.getOperand(0).getOperand(1);
9986 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009987 } else if (Cond.getOpcode() == ISD::SETCC &&
9988 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9989 // For FCMP_OEQ, we can emit
9990 // two branches instead of an explicit AND instruction with a
9991 // separate test. However, we only do this if this block doesn't
9992 // have a fall-through edge, because this requires an explicit
9993 // jmp when the condition is false.
9994 if (Op.getNode()->hasOneUse()) {
9995 SDNode *User = *Op.getNode()->use_begin();
9996 // Look for an unconditional branch following this conditional branch.
9997 // We need this because we need to reverse the successors in order
9998 // to implement FCMP_OEQ.
9999 if (User->getOpcode() == ISD::BR) {
10000 SDValue FalseBB = User->getOperand(1);
10001 SDNode *NewBR =
10002 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
10003 assert(NewBR == User);
10004 (void)NewBR;
10005 Dest = FalseBB;
10006
10007 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
10008 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010009 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +000010010 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
10011 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
10012 Chain, Dest, CC, Cmp);
10013 CC = DAG.getConstant(X86::COND_P, MVT::i8);
10014 Cond = Cmp;
10015 addTest = false;
10016 }
10017 }
10018 } else if (Cond.getOpcode() == ISD::SETCC &&
10019 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
10020 // For FCMP_UNE, we can emit
10021 // two branches instead of an explicit AND instruction with a
10022 // separate test. However, we only do this if this block doesn't
10023 // have a fall-through edge, because this requires an explicit
10024 // jmp when the condition is false.
10025 if (Op.getNode()->hasOneUse()) {
10026 SDNode *User = *Op.getNode()->use_begin();
10027 // Look for an unconditional branch following this conditional branch.
10028 // We need this because we need to reverse the successors in order
10029 // to implement FCMP_UNE.
10030 if (User->getOpcode() == ISD::BR) {
10031 SDValue FalseBB = User->getOperand(1);
10032 SDNode *NewBR =
10033 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
10034 assert(NewBR == User);
10035 (void)NewBR;
10036
10037 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
10038 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010039 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +000010040 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
10041 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
10042 Chain, Dest, CC, Cmp);
10043 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
10044 Cond = Cmp;
10045 addTest = false;
10046 Dest = FalseBB;
10047 }
10048 }
Dan Gohman279c22e2008-10-21 03:29:32 +000010049 }
Evan Cheng0488db92007-09-25 01:57:46 +000010050 }
10051
10052 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +000010053 // Look pass the truncate if the high bits are known zero.
10054 if (isTruncWithZeroHighBitsInput(Cond, DAG))
10055 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +000010056
10057 // We know the result of AND is compared against zero. Try to match
10058 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010059 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +000010060 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
10061 if (NewSetCC.getNode()) {
10062 CC = NewSetCC.getOperand(0);
10063 Cond = NewSetCC.getOperand(1);
10064 addTest = false;
10065 }
10066 }
10067 }
10068
10069 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010070 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +000010071 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +000010072 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010073 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010074 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010075 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010076}
10077
Anton Korobeynikove060b532007-04-17 19:34:00 +000010078// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10079// Calls to _alloca is needed to probe the stack when allocating more than 4k
10080// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10081// that the guard pages used by the OS virtual memory manager are allocated in
10082// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010083SDValue
10084X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010085 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010086 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010087 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010088 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010089 "are being used");
10090 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Andrew Trickac6d9be2013-05-25 02:42:55 +000010091 SDLoc dl(Op);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010092
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010093 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010094 SDValue Chain = Op.getOperand(0);
10095 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010096 // FIXME: Ensure alignment here
10097
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010098 bool Is64Bit = Subtarget->is64Bit();
10099 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010100
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010101 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010102 MachineFunction &MF = DAG.getMachineFunction();
10103 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010104
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010105 if (Is64Bit) {
10106 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010107 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010108 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010109
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010110 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010111 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010112 if (I->hasNestAttr())
10113 report_fatal_error("Cannot use segmented stacks with functions that "
10114 "have nested arguments.");
10115 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010116
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010117 const TargetRegisterClass *AddrRegClass =
10118 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10119 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10120 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10121 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10122 DAG.getRegister(Vreg, SPTy));
10123 SDValue Ops1[2] = { Value, Chain };
10124 return DAG.getMergeValues(Ops1, 2, dl);
10125 } else {
10126 SDValue Flag;
10127 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010128
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010129 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10130 Flag = Chain.getValue(1);
10131 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010132
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010133 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10134 Flag = Chain.getValue(1);
10135
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000010136 const X86RegisterInfo *RegInfo =
10137 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaoc5c970e2012-10-31 04:14:09 +000010138 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10139 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010140
10141 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10142 return DAG.getMergeValues(Ops1, 2, dl);
10143 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010144}
10145
Dan Gohmand858e902010-04-17 15:26:15 +000010146SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010147 MachineFunction &MF = DAG.getMachineFunction();
10148 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10149
Dan Gohman69de1932008-02-06 22:27:42 +000010150 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010151 SDLoc DL(Op);
Evan Cheng8b2794a2006-10-13 21:14:26 +000010152
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010153 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010154 // vastart just stores the address of the VarArgsFrameIndex slot into the
10155 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010156 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10157 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010158 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10159 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010160 }
10161
10162 // __va_list_tag:
10163 // gp_offset (0 - 6 * 8)
10164 // fp_offset (48 - 48 + 8 * 16)
10165 // overflow_arg_area (point to parameters coming in memory).
10166 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010167 SmallVector<SDValue, 8> MemOps;
10168 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010169 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010170 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010171 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10172 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010173 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010174 MemOps.push_back(Store);
10175
10176 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010177 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010178 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010179 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010180 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10181 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010182 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010183 MemOps.push_back(Store);
10184
10185 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010186 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010187 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010188 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10189 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010190 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10191 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010192 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010193 MemOps.push_back(Store);
10194
10195 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010196 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010197 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010198 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10199 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010200 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10201 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010202 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010203 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010204 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010205}
10206
Dan Gohmand858e902010-04-17 15:26:15 +000010207SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010208 assert(Subtarget->is64Bit() &&
10209 "LowerVAARG only handles 64-bit va_arg!");
10210 assert((Subtarget->isTargetLinux() ||
10211 Subtarget->isTargetDarwin()) &&
10212 "Unhandled target in LowerVAARG");
10213 assert(Op.getNode()->getNumOperands() == 4);
10214 SDValue Chain = Op.getOperand(0);
10215 SDValue SrcPtr = Op.getOperand(1);
10216 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10217 unsigned Align = Op.getConstantOperandVal(3);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010218 SDLoc dl(Op);
Dan Gohman9018e832008-05-10 01:26:14 +000010219
Dan Gohman320afb82010-10-12 18:00:49 +000010220 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010221 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010222 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010223 uint8_t ArgMode;
10224
10225 // Decide which area this value should be read from.
10226 // TODO: Implement the AMD64 ABI in its entirety. This simple
10227 // selection mechanism works only for the basic types.
10228 if (ArgVT == MVT::f80) {
10229 llvm_unreachable("va_arg for f80 not yet implemented");
10230 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10231 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10232 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10233 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10234 } else {
10235 llvm_unreachable("Unhandled argument type in LowerVAARG");
10236 }
10237
10238 if (ArgMode == 2) {
10239 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010240 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010241 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010242 .getFunction()->getAttributes()
10243 .hasAttribute(AttributeSet::FunctionIndex,
10244 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010245 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010246 }
10247
10248 // Insert VAARG_64 node into the DAG
10249 // VAARG_64 returns two values: Variable Argument Address, Chain
10250 SmallVector<SDValue, 11> InstOps;
10251 InstOps.push_back(Chain);
10252 InstOps.push_back(SrcPtr);
10253 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10254 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10255 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10256 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10257 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10258 VTs, &InstOps[0], InstOps.size(),
10259 MVT::i64,
10260 MachinePointerInfo(SV),
10261 /*Align=*/0,
10262 /*Volatile=*/false,
10263 /*ReadMem=*/true,
10264 /*WriteMem=*/true);
10265 Chain = VAARG.getValue(1);
10266
10267 // Load the next argument and return it
10268 return DAG.getLoad(ArgVT, dl,
10269 Chain,
10270 VAARG,
10271 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010272 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010273}
10274
Craig Topper55b24052012-09-11 06:15:32 +000010275static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10276 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010277 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010278 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010279 SDValue Chain = Op.getOperand(0);
10280 SDValue DstPtr = Op.getOperand(1);
10281 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010282 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10283 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010284 SDLoc DL(Op);
Evan Chengae642192007-03-02 23:16:35 +000010285
Chris Lattnere72f2022010-09-21 05:40:29 +000010286 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010287 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010288 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010289 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010290}
10291
Craig Topperff3139f2013-02-19 07:43:59 +000010292// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010293// may or may not be a constant. Takes immediate version of shift as input.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010294static SDValue getTargetVShiftNode(unsigned Opc, SDLoc dl, EVT VT,
Craig Topper80e46362012-01-23 06:16:53 +000010295 SDValue SrcOp, SDValue ShAmt,
10296 SelectionDAG &DAG) {
10297 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10298
10299 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010300 // Constant may be a TargetConstant. Use a regular constant.
10301 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010302 switch (Opc) {
10303 default: llvm_unreachable("Unknown target vector shift node");
10304 case X86ISD::VSHLI:
10305 case X86ISD::VSRLI:
10306 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010307 return DAG.getNode(Opc, dl, VT, SrcOp,
10308 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010309 }
10310 }
10311
10312 // Change opcode to non-immediate version
10313 switch (Opc) {
10314 default: llvm_unreachable("Unknown target vector shift node");
10315 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10316 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10317 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10318 }
10319
10320 // Need to build a vector containing shift amount
10321 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10322 SDValue ShOps[4];
10323 ShOps[0] = ShAmt;
10324 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010325 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010326 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010327
10328 // The return type has to be a 128-bit type with the same element
10329 // type as the input type.
10330 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10331 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10332
10333 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010334 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10335}
10336
Craig Topper55b24052012-09-11 06:15:32 +000010337static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010338 SDLoc dl(Op);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010339 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010340 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010341 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010342 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010343 case Intrinsic::x86_sse_comieq_ss:
10344 case Intrinsic::x86_sse_comilt_ss:
10345 case Intrinsic::x86_sse_comile_ss:
10346 case Intrinsic::x86_sse_comigt_ss:
10347 case Intrinsic::x86_sse_comige_ss:
10348 case Intrinsic::x86_sse_comineq_ss:
10349 case Intrinsic::x86_sse_ucomieq_ss:
10350 case Intrinsic::x86_sse_ucomilt_ss:
10351 case Intrinsic::x86_sse_ucomile_ss:
10352 case Intrinsic::x86_sse_ucomigt_ss:
10353 case Intrinsic::x86_sse_ucomige_ss:
10354 case Intrinsic::x86_sse_ucomineq_ss:
10355 case Intrinsic::x86_sse2_comieq_sd:
10356 case Intrinsic::x86_sse2_comilt_sd:
10357 case Intrinsic::x86_sse2_comile_sd:
10358 case Intrinsic::x86_sse2_comigt_sd:
10359 case Intrinsic::x86_sse2_comige_sd:
10360 case Intrinsic::x86_sse2_comineq_sd:
10361 case Intrinsic::x86_sse2_ucomieq_sd:
10362 case Intrinsic::x86_sse2_ucomilt_sd:
10363 case Intrinsic::x86_sse2_ucomile_sd:
10364 case Intrinsic::x86_sse2_ucomigt_sd:
10365 case Intrinsic::x86_sse2_ucomige_sd:
10366 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010367 unsigned Opc;
10368 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010369 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010370 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010371 case Intrinsic::x86_sse_comieq_ss:
10372 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010373 Opc = X86ISD::COMI;
10374 CC = ISD::SETEQ;
10375 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010376 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010377 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010378 Opc = X86ISD::COMI;
10379 CC = ISD::SETLT;
10380 break;
10381 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010382 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010383 Opc = X86ISD::COMI;
10384 CC = ISD::SETLE;
10385 break;
10386 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010387 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010388 Opc = X86ISD::COMI;
10389 CC = ISD::SETGT;
10390 break;
10391 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010392 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010393 Opc = X86ISD::COMI;
10394 CC = ISD::SETGE;
10395 break;
10396 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010397 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010398 Opc = X86ISD::COMI;
10399 CC = ISD::SETNE;
10400 break;
10401 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010402 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010403 Opc = X86ISD::UCOMI;
10404 CC = ISD::SETEQ;
10405 break;
10406 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010407 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010408 Opc = X86ISD::UCOMI;
10409 CC = ISD::SETLT;
10410 break;
10411 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010412 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010413 Opc = X86ISD::UCOMI;
10414 CC = ISD::SETLE;
10415 break;
10416 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010417 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010418 Opc = X86ISD::UCOMI;
10419 CC = ISD::SETGT;
10420 break;
10421 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010422 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010423 Opc = X86ISD::UCOMI;
10424 CC = ISD::SETGE;
10425 break;
10426 case Intrinsic::x86_sse_ucomineq_ss:
10427 case Intrinsic::x86_sse2_ucomineq_sd:
10428 Opc = X86ISD::UCOMI;
10429 CC = ISD::SETNE;
10430 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010431 }
Evan Cheng734503b2006-09-11 02:19:56 +000010432
Dan Gohman475871a2008-07-27 21:46:04 +000010433 SDValue LHS = Op.getOperand(1);
10434 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010435 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010436 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010437 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10438 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10439 DAG.getConstant(X86CC, MVT::i8), Cond);
10440 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010441 }
Craig Topper6d688152012-08-14 07:43:25 +000010442
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010443 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010444 case Intrinsic::x86_sse2_pmulu_dq:
10445 case Intrinsic::x86_avx2_pmulu_dq:
10446 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10447 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010448
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010449 // SSE2/AVX2 sub with unsigned saturation intrinsics
10450 case Intrinsic::x86_sse2_psubus_b:
10451 case Intrinsic::x86_sse2_psubus_w:
10452 case Intrinsic::x86_avx2_psubus_b:
10453 case Intrinsic::x86_avx2_psubus_w:
10454 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10455 Op.getOperand(1), Op.getOperand(2));
10456
Craig Topper6d688152012-08-14 07:43:25 +000010457 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010458 case Intrinsic::x86_sse3_hadd_ps:
10459 case Intrinsic::x86_sse3_hadd_pd:
10460 case Intrinsic::x86_avx_hadd_ps_256:
10461 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010462 case Intrinsic::x86_sse3_hsub_ps:
10463 case Intrinsic::x86_sse3_hsub_pd:
10464 case Intrinsic::x86_avx_hsub_ps_256:
10465 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010466 case Intrinsic::x86_ssse3_phadd_w_128:
10467 case Intrinsic::x86_ssse3_phadd_d_128:
10468 case Intrinsic::x86_avx2_phadd_w:
10469 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010470 case Intrinsic::x86_ssse3_phsub_w_128:
10471 case Intrinsic::x86_ssse3_phsub_d_128:
10472 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010473 case Intrinsic::x86_avx2_phsub_d: {
10474 unsigned Opcode;
10475 switch (IntNo) {
10476 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10477 case Intrinsic::x86_sse3_hadd_ps:
10478 case Intrinsic::x86_sse3_hadd_pd:
10479 case Intrinsic::x86_avx_hadd_ps_256:
10480 case Intrinsic::x86_avx_hadd_pd_256:
10481 Opcode = X86ISD::FHADD;
10482 break;
10483 case Intrinsic::x86_sse3_hsub_ps:
10484 case Intrinsic::x86_sse3_hsub_pd:
10485 case Intrinsic::x86_avx_hsub_ps_256:
10486 case Intrinsic::x86_avx_hsub_pd_256:
10487 Opcode = X86ISD::FHSUB;
10488 break;
10489 case Intrinsic::x86_ssse3_phadd_w_128:
10490 case Intrinsic::x86_ssse3_phadd_d_128:
10491 case Intrinsic::x86_avx2_phadd_w:
10492 case Intrinsic::x86_avx2_phadd_d:
10493 Opcode = X86ISD::HADD;
10494 break;
10495 case Intrinsic::x86_ssse3_phsub_w_128:
10496 case Intrinsic::x86_ssse3_phsub_d_128:
10497 case Intrinsic::x86_avx2_phsub_w:
10498 case Intrinsic::x86_avx2_phsub_d:
10499 Opcode = X86ISD::HSUB;
10500 break;
10501 }
10502 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010503 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010504 }
10505
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010506 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10507 case Intrinsic::x86_sse2_pmaxu_b:
10508 case Intrinsic::x86_sse41_pmaxuw:
10509 case Intrinsic::x86_sse41_pmaxud:
10510 case Intrinsic::x86_avx2_pmaxu_b:
10511 case Intrinsic::x86_avx2_pmaxu_w:
10512 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010513 case Intrinsic::x86_sse2_pminu_b:
10514 case Intrinsic::x86_sse41_pminuw:
10515 case Intrinsic::x86_sse41_pminud:
10516 case Intrinsic::x86_avx2_pminu_b:
10517 case Intrinsic::x86_avx2_pminu_w:
10518 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010519 case Intrinsic::x86_sse41_pmaxsb:
10520 case Intrinsic::x86_sse2_pmaxs_w:
10521 case Intrinsic::x86_sse41_pmaxsd:
10522 case Intrinsic::x86_avx2_pmaxs_b:
10523 case Intrinsic::x86_avx2_pmaxs_w:
10524 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010525 case Intrinsic::x86_sse41_pminsb:
10526 case Intrinsic::x86_sse2_pmins_w:
10527 case Intrinsic::x86_sse41_pminsd:
10528 case Intrinsic::x86_avx2_pmins_b:
10529 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010530 case Intrinsic::x86_avx2_pmins_d: {
10531 unsigned Opcode;
10532 switch (IntNo) {
10533 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10534 case Intrinsic::x86_sse2_pmaxu_b:
10535 case Intrinsic::x86_sse41_pmaxuw:
10536 case Intrinsic::x86_sse41_pmaxud:
10537 case Intrinsic::x86_avx2_pmaxu_b:
10538 case Intrinsic::x86_avx2_pmaxu_w:
10539 case Intrinsic::x86_avx2_pmaxu_d:
10540 Opcode = X86ISD::UMAX;
10541 break;
10542 case Intrinsic::x86_sse2_pminu_b:
10543 case Intrinsic::x86_sse41_pminuw:
10544 case Intrinsic::x86_sse41_pminud:
10545 case Intrinsic::x86_avx2_pminu_b:
10546 case Intrinsic::x86_avx2_pminu_w:
10547 case Intrinsic::x86_avx2_pminu_d:
10548 Opcode = X86ISD::UMIN;
10549 break;
10550 case Intrinsic::x86_sse41_pmaxsb:
10551 case Intrinsic::x86_sse2_pmaxs_w:
10552 case Intrinsic::x86_sse41_pmaxsd:
10553 case Intrinsic::x86_avx2_pmaxs_b:
10554 case Intrinsic::x86_avx2_pmaxs_w:
10555 case Intrinsic::x86_avx2_pmaxs_d:
10556 Opcode = X86ISD::SMAX;
10557 break;
10558 case Intrinsic::x86_sse41_pminsb:
10559 case Intrinsic::x86_sse2_pmins_w:
10560 case Intrinsic::x86_sse41_pminsd:
10561 case Intrinsic::x86_avx2_pmins_b:
10562 case Intrinsic::x86_avx2_pmins_w:
10563 case Intrinsic::x86_avx2_pmins_d:
10564 Opcode = X86ISD::SMIN;
10565 break;
10566 }
10567 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010568 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010569 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010570
Craig Topper6d183e42012-12-29 16:44:25 +000010571 // SSE/SSE2/AVX floating point max/min intrinsics.
10572 case Intrinsic::x86_sse_max_ps:
10573 case Intrinsic::x86_sse2_max_pd:
10574 case Intrinsic::x86_avx_max_ps_256:
10575 case Intrinsic::x86_avx_max_pd_256:
10576 case Intrinsic::x86_sse_min_ps:
10577 case Intrinsic::x86_sse2_min_pd:
10578 case Intrinsic::x86_avx_min_ps_256:
10579 case Intrinsic::x86_avx_min_pd_256: {
10580 unsigned Opcode;
10581 switch (IntNo) {
10582 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10583 case Intrinsic::x86_sse_max_ps:
10584 case Intrinsic::x86_sse2_max_pd:
10585 case Intrinsic::x86_avx_max_ps_256:
10586 case Intrinsic::x86_avx_max_pd_256:
10587 Opcode = X86ISD::FMAX;
10588 break;
10589 case Intrinsic::x86_sse_min_ps:
10590 case Intrinsic::x86_sse2_min_pd:
10591 case Intrinsic::x86_avx_min_ps_256:
10592 case Intrinsic::x86_avx_min_pd_256:
10593 Opcode = X86ISD::FMIN;
10594 break;
10595 }
10596 return DAG.getNode(Opcode, dl, Op.getValueType(),
10597 Op.getOperand(1), Op.getOperand(2));
10598 }
10599
Craig Topper6d688152012-08-14 07:43:25 +000010600 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010601 case Intrinsic::x86_avx2_psllv_d:
10602 case Intrinsic::x86_avx2_psllv_q:
10603 case Intrinsic::x86_avx2_psllv_d_256:
10604 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010605 case Intrinsic::x86_avx2_psrlv_d:
10606 case Intrinsic::x86_avx2_psrlv_q:
10607 case Intrinsic::x86_avx2_psrlv_d_256:
10608 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010609 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010610 case Intrinsic::x86_avx2_psrav_d_256: {
10611 unsigned Opcode;
10612 switch (IntNo) {
10613 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10614 case Intrinsic::x86_avx2_psllv_d:
10615 case Intrinsic::x86_avx2_psllv_q:
10616 case Intrinsic::x86_avx2_psllv_d_256:
10617 case Intrinsic::x86_avx2_psllv_q_256:
10618 Opcode = ISD::SHL;
10619 break;
10620 case Intrinsic::x86_avx2_psrlv_d:
10621 case Intrinsic::x86_avx2_psrlv_q:
10622 case Intrinsic::x86_avx2_psrlv_d_256:
10623 case Intrinsic::x86_avx2_psrlv_q_256:
10624 Opcode = ISD::SRL;
10625 break;
10626 case Intrinsic::x86_avx2_psrav_d:
10627 case Intrinsic::x86_avx2_psrav_d_256:
10628 Opcode = ISD::SRA;
10629 break;
10630 }
10631 return DAG.getNode(Opcode, dl, Op.getValueType(),
10632 Op.getOperand(1), Op.getOperand(2));
10633 }
10634
Craig Topper969ba282012-01-25 06:43:11 +000010635 case Intrinsic::x86_ssse3_pshuf_b_128:
10636 case Intrinsic::x86_avx2_pshuf_b:
10637 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10638 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010639
Craig Topper969ba282012-01-25 06:43:11 +000010640 case Intrinsic::x86_ssse3_psign_b_128:
10641 case Intrinsic::x86_ssse3_psign_w_128:
10642 case Intrinsic::x86_ssse3_psign_d_128:
10643 case Intrinsic::x86_avx2_psign_b:
10644 case Intrinsic::x86_avx2_psign_w:
10645 case Intrinsic::x86_avx2_psign_d:
10646 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10647 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010648
Craig Toppere566cd02012-01-26 07:18:03 +000010649 case Intrinsic::x86_sse41_insertps:
10650 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10651 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010652
Craig Toppere566cd02012-01-26 07:18:03 +000010653 case Intrinsic::x86_avx_vperm2f128_ps_256:
10654 case Intrinsic::x86_avx_vperm2f128_pd_256:
10655 case Intrinsic::x86_avx_vperm2f128_si_256:
10656 case Intrinsic::x86_avx2_vperm2i128:
10657 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10658 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010659
Craig Topperffa6c402012-04-16 07:13:00 +000010660 case Intrinsic::x86_avx2_permd:
10661 case Intrinsic::x86_avx2_permps:
10662 // Operands intentionally swapped. Mask is last operand to intrinsic,
10663 // but second operand for node/intruction.
10664 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10665 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010666
Craig Topper22d8f0d2012-12-29 18:18:20 +000010667 case Intrinsic::x86_sse_sqrt_ps:
10668 case Intrinsic::x86_sse2_sqrt_pd:
10669 case Intrinsic::x86_avx_sqrt_ps_256:
10670 case Intrinsic::x86_avx_sqrt_pd_256:
10671 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10672
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010673 // ptest and testp intrinsics. The intrinsic these come from are designed to
10674 // return an integer value, not just an instruction so lower it to the ptest
10675 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010676 case Intrinsic::x86_sse41_ptestz:
10677 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010678 case Intrinsic::x86_sse41_ptestnzc:
10679 case Intrinsic::x86_avx_ptestz_256:
10680 case Intrinsic::x86_avx_ptestc_256:
10681 case Intrinsic::x86_avx_ptestnzc_256:
10682 case Intrinsic::x86_avx_vtestz_ps:
10683 case Intrinsic::x86_avx_vtestc_ps:
10684 case Intrinsic::x86_avx_vtestnzc_ps:
10685 case Intrinsic::x86_avx_vtestz_pd:
10686 case Intrinsic::x86_avx_vtestc_pd:
10687 case Intrinsic::x86_avx_vtestnzc_pd:
10688 case Intrinsic::x86_avx_vtestz_ps_256:
10689 case Intrinsic::x86_avx_vtestc_ps_256:
10690 case Intrinsic::x86_avx_vtestnzc_ps_256:
10691 case Intrinsic::x86_avx_vtestz_pd_256:
10692 case Intrinsic::x86_avx_vtestc_pd_256:
10693 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10694 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010695 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010696 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010697 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010698 case Intrinsic::x86_avx_vtestz_ps:
10699 case Intrinsic::x86_avx_vtestz_pd:
10700 case Intrinsic::x86_avx_vtestz_ps_256:
10701 case Intrinsic::x86_avx_vtestz_pd_256:
10702 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010703 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010704 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010705 // ZF = 1
10706 X86CC = X86::COND_E;
10707 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010708 case Intrinsic::x86_avx_vtestc_ps:
10709 case Intrinsic::x86_avx_vtestc_pd:
10710 case Intrinsic::x86_avx_vtestc_ps_256:
10711 case Intrinsic::x86_avx_vtestc_pd_256:
10712 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010713 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010714 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010715 // CF = 1
10716 X86CC = X86::COND_B;
10717 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010718 case Intrinsic::x86_avx_vtestnzc_ps:
10719 case Intrinsic::x86_avx_vtestnzc_pd:
10720 case Intrinsic::x86_avx_vtestnzc_ps_256:
10721 case Intrinsic::x86_avx_vtestnzc_pd_256:
10722 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010723 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010724 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010725 // ZF and CF = 0
10726 X86CC = X86::COND_A;
10727 break;
10728 }
Eric Christopherfd179292009-08-27 18:07:15 +000010729
Eric Christopher71c67532009-07-29 00:28:05 +000010730 SDValue LHS = Op.getOperand(1);
10731 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010732 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10733 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010734 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10735 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10736 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010737 }
Evan Cheng5759f972008-05-04 09:15:50 +000010738
Craig Topper80e46362012-01-23 06:16:53 +000010739 // SSE/AVX shift intrinsics
10740 case Intrinsic::x86_sse2_psll_w:
10741 case Intrinsic::x86_sse2_psll_d:
10742 case Intrinsic::x86_sse2_psll_q:
10743 case Intrinsic::x86_avx2_psll_w:
10744 case Intrinsic::x86_avx2_psll_d:
10745 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010746 case Intrinsic::x86_sse2_psrl_w:
10747 case Intrinsic::x86_sse2_psrl_d:
10748 case Intrinsic::x86_sse2_psrl_q:
10749 case Intrinsic::x86_avx2_psrl_w:
10750 case Intrinsic::x86_avx2_psrl_d:
10751 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010752 case Intrinsic::x86_sse2_psra_w:
10753 case Intrinsic::x86_sse2_psra_d:
10754 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010755 case Intrinsic::x86_avx2_psra_d: {
10756 unsigned Opcode;
10757 switch (IntNo) {
10758 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10759 case Intrinsic::x86_sse2_psll_w:
10760 case Intrinsic::x86_sse2_psll_d:
10761 case Intrinsic::x86_sse2_psll_q:
10762 case Intrinsic::x86_avx2_psll_w:
10763 case Intrinsic::x86_avx2_psll_d:
10764 case Intrinsic::x86_avx2_psll_q:
10765 Opcode = X86ISD::VSHL;
10766 break;
10767 case Intrinsic::x86_sse2_psrl_w:
10768 case Intrinsic::x86_sse2_psrl_d:
10769 case Intrinsic::x86_sse2_psrl_q:
10770 case Intrinsic::x86_avx2_psrl_w:
10771 case Intrinsic::x86_avx2_psrl_d:
10772 case Intrinsic::x86_avx2_psrl_q:
10773 Opcode = X86ISD::VSRL;
10774 break;
10775 case Intrinsic::x86_sse2_psra_w:
10776 case Intrinsic::x86_sse2_psra_d:
10777 case Intrinsic::x86_avx2_psra_w:
10778 case Intrinsic::x86_avx2_psra_d:
10779 Opcode = X86ISD::VSRA;
10780 break;
10781 }
10782 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010783 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010784 }
10785
10786 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010787 case Intrinsic::x86_sse2_pslli_w:
10788 case Intrinsic::x86_sse2_pslli_d:
10789 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010790 case Intrinsic::x86_avx2_pslli_w:
10791 case Intrinsic::x86_avx2_pslli_d:
10792 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010793 case Intrinsic::x86_sse2_psrli_w:
10794 case Intrinsic::x86_sse2_psrli_d:
10795 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010796 case Intrinsic::x86_avx2_psrli_w:
10797 case Intrinsic::x86_avx2_psrli_d:
10798 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010799 case Intrinsic::x86_sse2_psrai_w:
10800 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010801 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010802 case Intrinsic::x86_avx2_psrai_d: {
10803 unsigned Opcode;
10804 switch (IntNo) {
10805 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10806 case Intrinsic::x86_sse2_pslli_w:
10807 case Intrinsic::x86_sse2_pslli_d:
10808 case Intrinsic::x86_sse2_pslli_q:
10809 case Intrinsic::x86_avx2_pslli_w:
10810 case Intrinsic::x86_avx2_pslli_d:
10811 case Intrinsic::x86_avx2_pslli_q:
10812 Opcode = X86ISD::VSHLI;
10813 break;
10814 case Intrinsic::x86_sse2_psrli_w:
10815 case Intrinsic::x86_sse2_psrli_d:
10816 case Intrinsic::x86_sse2_psrli_q:
10817 case Intrinsic::x86_avx2_psrli_w:
10818 case Intrinsic::x86_avx2_psrli_d:
10819 case Intrinsic::x86_avx2_psrli_q:
10820 Opcode = X86ISD::VSRLI;
10821 break;
10822 case Intrinsic::x86_sse2_psrai_w:
10823 case Intrinsic::x86_sse2_psrai_d:
10824 case Intrinsic::x86_avx2_psrai_w:
10825 case Intrinsic::x86_avx2_psrai_d:
10826 Opcode = X86ISD::VSRAI;
10827 break;
10828 }
10829 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010830 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010831 }
10832
Craig Topper4feb6472012-08-06 06:22:36 +000010833 case Intrinsic::x86_sse42_pcmpistria128:
10834 case Intrinsic::x86_sse42_pcmpestria128:
10835 case Intrinsic::x86_sse42_pcmpistric128:
10836 case Intrinsic::x86_sse42_pcmpestric128:
10837 case Intrinsic::x86_sse42_pcmpistrio128:
10838 case Intrinsic::x86_sse42_pcmpestrio128:
10839 case Intrinsic::x86_sse42_pcmpistris128:
10840 case Intrinsic::x86_sse42_pcmpestris128:
10841 case Intrinsic::x86_sse42_pcmpistriz128:
10842 case Intrinsic::x86_sse42_pcmpestriz128: {
10843 unsigned Opcode;
10844 unsigned X86CC;
10845 switch (IntNo) {
10846 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10847 case Intrinsic::x86_sse42_pcmpistria128:
10848 Opcode = X86ISD::PCMPISTRI;
10849 X86CC = X86::COND_A;
10850 break;
10851 case Intrinsic::x86_sse42_pcmpestria128:
10852 Opcode = X86ISD::PCMPESTRI;
10853 X86CC = X86::COND_A;
10854 break;
10855 case Intrinsic::x86_sse42_pcmpistric128:
10856 Opcode = X86ISD::PCMPISTRI;
10857 X86CC = X86::COND_B;
10858 break;
10859 case Intrinsic::x86_sse42_pcmpestric128:
10860 Opcode = X86ISD::PCMPESTRI;
10861 X86CC = X86::COND_B;
10862 break;
10863 case Intrinsic::x86_sse42_pcmpistrio128:
10864 Opcode = X86ISD::PCMPISTRI;
10865 X86CC = X86::COND_O;
10866 break;
10867 case Intrinsic::x86_sse42_pcmpestrio128:
10868 Opcode = X86ISD::PCMPESTRI;
10869 X86CC = X86::COND_O;
10870 break;
10871 case Intrinsic::x86_sse42_pcmpistris128:
10872 Opcode = X86ISD::PCMPISTRI;
10873 X86CC = X86::COND_S;
10874 break;
10875 case Intrinsic::x86_sse42_pcmpestris128:
10876 Opcode = X86ISD::PCMPESTRI;
10877 X86CC = X86::COND_S;
10878 break;
10879 case Intrinsic::x86_sse42_pcmpistriz128:
10880 Opcode = X86ISD::PCMPISTRI;
10881 X86CC = X86::COND_E;
10882 break;
10883 case Intrinsic::x86_sse42_pcmpestriz128:
10884 Opcode = X86ISD::PCMPESTRI;
10885 X86CC = X86::COND_E;
10886 break;
10887 }
10888 SmallVector<SDValue, 5> NewOps;
10889 NewOps.append(Op->op_begin()+1, Op->op_end());
10890 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10891 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10892 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10893 DAG.getConstant(X86CC, MVT::i8),
10894 SDValue(PCMP.getNode(), 1));
10895 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10896 }
Craig Topper6d688152012-08-14 07:43:25 +000010897
Craig Topper4feb6472012-08-06 06:22:36 +000010898 case Intrinsic::x86_sse42_pcmpistri128:
10899 case Intrinsic::x86_sse42_pcmpestri128: {
10900 unsigned Opcode;
10901 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10902 Opcode = X86ISD::PCMPISTRI;
10903 else
10904 Opcode = X86ISD::PCMPESTRI;
10905
10906 SmallVector<SDValue, 5> NewOps;
10907 NewOps.append(Op->op_begin()+1, Op->op_end());
10908 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10909 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10910 }
Craig Topper0e292372012-08-24 04:03:22 +000010911 case Intrinsic::x86_fma_vfmadd_ps:
10912 case Intrinsic::x86_fma_vfmadd_pd:
10913 case Intrinsic::x86_fma_vfmsub_ps:
10914 case Intrinsic::x86_fma_vfmsub_pd:
10915 case Intrinsic::x86_fma_vfnmadd_ps:
10916 case Intrinsic::x86_fma_vfnmadd_pd:
10917 case Intrinsic::x86_fma_vfnmsub_ps:
10918 case Intrinsic::x86_fma_vfnmsub_pd:
10919 case Intrinsic::x86_fma_vfmaddsub_ps:
10920 case Intrinsic::x86_fma_vfmaddsub_pd:
10921 case Intrinsic::x86_fma_vfmsubadd_ps:
10922 case Intrinsic::x86_fma_vfmsubadd_pd:
10923 case Intrinsic::x86_fma_vfmadd_ps_256:
10924 case Intrinsic::x86_fma_vfmadd_pd_256:
10925 case Intrinsic::x86_fma_vfmsub_ps_256:
10926 case Intrinsic::x86_fma_vfmsub_pd_256:
10927 case Intrinsic::x86_fma_vfnmadd_ps_256:
10928 case Intrinsic::x86_fma_vfnmadd_pd_256:
10929 case Intrinsic::x86_fma_vfnmsub_ps_256:
10930 case Intrinsic::x86_fma_vfnmsub_pd_256:
10931 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10932 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10933 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10934 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010935 unsigned Opc;
10936 switch (IntNo) {
10937 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10938 case Intrinsic::x86_fma_vfmadd_ps:
10939 case Intrinsic::x86_fma_vfmadd_pd:
10940 case Intrinsic::x86_fma_vfmadd_ps_256:
10941 case Intrinsic::x86_fma_vfmadd_pd_256:
10942 Opc = X86ISD::FMADD;
10943 break;
10944 case Intrinsic::x86_fma_vfmsub_ps:
10945 case Intrinsic::x86_fma_vfmsub_pd:
10946 case Intrinsic::x86_fma_vfmsub_ps_256:
10947 case Intrinsic::x86_fma_vfmsub_pd_256:
10948 Opc = X86ISD::FMSUB;
10949 break;
10950 case Intrinsic::x86_fma_vfnmadd_ps:
10951 case Intrinsic::x86_fma_vfnmadd_pd:
10952 case Intrinsic::x86_fma_vfnmadd_ps_256:
10953 case Intrinsic::x86_fma_vfnmadd_pd_256:
10954 Opc = X86ISD::FNMADD;
10955 break;
10956 case Intrinsic::x86_fma_vfnmsub_ps:
10957 case Intrinsic::x86_fma_vfnmsub_pd:
10958 case Intrinsic::x86_fma_vfnmsub_ps_256:
10959 case Intrinsic::x86_fma_vfnmsub_pd_256:
10960 Opc = X86ISD::FNMSUB;
10961 break;
10962 case Intrinsic::x86_fma_vfmaddsub_ps:
10963 case Intrinsic::x86_fma_vfmaddsub_pd:
10964 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10965 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10966 Opc = X86ISD::FMADDSUB;
10967 break;
10968 case Intrinsic::x86_fma_vfmsubadd_ps:
10969 case Intrinsic::x86_fma_vfmsubadd_pd:
10970 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10971 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10972 Opc = X86ISD::FMSUBADD;
10973 break;
10974 }
10975
10976 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10977 Op.getOperand(2), Op.getOperand(3));
10978 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010979 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010980}
Evan Cheng72261582005-12-20 06:22:03 +000010981
Craig Topper55b24052012-09-11 06:15:32 +000010982static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010983 SDLoc dl(Op);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010984 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10985 switch (IntNo) {
10986 default: return SDValue(); // Don't custom lower most intrinsics.
10987
Michael Liaoc26392a2013-03-28 23:41:26 +000010988 // RDRAND/RDSEED intrinsics.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010989 case Intrinsic::x86_rdrand_16:
10990 case Intrinsic::x86_rdrand_32:
Michael Liaoc26392a2013-03-28 23:41:26 +000010991 case Intrinsic::x86_rdrand_64:
10992 case Intrinsic::x86_rdseed_16:
10993 case Intrinsic::x86_rdseed_32:
10994 case Intrinsic::x86_rdseed_64: {
10995 unsigned Opcode = (IntNo == Intrinsic::x86_rdseed_16 ||
10996 IntNo == Intrinsic::x86_rdseed_32 ||
10997 IntNo == Intrinsic::x86_rdseed_64) ? X86ISD::RDSEED :
10998 X86ISD::RDRAND;
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010999 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000011000 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
Michael Liaoc26392a2013-03-28 23:41:26 +000011001 SDValue Result = DAG.getNode(Opcode, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011002
Michael Liaoc26392a2013-03-28 23:41:26 +000011003 // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1.
11004 // Otherwise return the value from Rand, which is always 0, casted to i32.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011005 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
11006 DAG.getConstant(1, Op->getValueType(1)),
11007 DAG.getConstant(X86::COND_B, MVT::i32),
11008 SDValue(Result.getNode(), 1) };
11009 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
11010 DAG.getVTList(Op->getValueType(1), MVT::Glue),
Michael Liao0ee17002013-04-19 04:03:37 +000011011 Ops, array_lengthof(Ops));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011012
11013 // Return { result, isValid, chain }.
11014 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000011015 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011016 }
Michael Liaof8fd8832013-03-26 22:47:01 +000011017
11018 // XTEST intrinsics.
11019 case Intrinsic::x86_xtest: {
11020 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
11021 SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0));
11022 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
11023 DAG.getConstant(X86::COND_NE, MVT::i8),
11024 InTrans);
11025 SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
11026 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
11027 Ret, SDValue(InTrans.getNode(), 1));
11028 }
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011029 }
11030}
11031
Dan Gohmand858e902010-04-17 15:26:15 +000011032SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
11033 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000011034 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11035 MFI->setReturnAddressIsTaken(true);
11036
Bill Wendling64e87322009-01-16 19:25:27 +000011037 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011038 SDLoc dl(Op);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011039 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000011040
11041 if (Depth > 0) {
11042 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011043 const X86RegisterInfo *RegInfo =
11044 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
11045 SDValue Offset = DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011046 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
11047 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000011048 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011049 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000011050 }
11051
11052 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000011053 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011054 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011055 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011056}
11057
Dan Gohmand858e902010-04-17 15:26:15 +000011058SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000011059 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11060 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000011061
Owen Andersone50ed302009-08-10 22:56:29 +000011062 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011063 SDLoc dl(Op); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000011064 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011065 const X86RegisterInfo *RegInfo =
11066 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaob9cca132013-05-02 08:21:56 +000011067 unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
11068 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
Michael Liao299eb2e2013-05-02 09:22:04 +000011069 (FrameReg == X86::EBP && VT == MVT::i32)) &&
11070 "Invalid Frame Register!");
Dale Johannesendd64c412009-02-04 00:33:20 +000011071 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000011072 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000011073 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
11074 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000011075 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000011076 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000011077}
11078
Dan Gohman475871a2008-07-27 21:46:04 +000011079SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011080 SelectionDAG &DAG) const {
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011081 const X86RegisterInfo *RegInfo =
11082 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011083 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011084}
11085
Dan Gohmand858e902010-04-17 15:26:15 +000011086SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011087 SDValue Chain = Op.getOperand(0);
11088 SDValue Offset = Op.getOperand(1);
11089 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +000011090 SDLoc dl (Op);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011091
Michael Liaodb7da202013-05-02 09:18:38 +000011092 EVT PtrVT = getPointerTy();
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000011093 const X86RegisterInfo *RegInfo =
11094 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liaodb7da202013-05-02 09:18:38 +000011095 unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
11096 assert(((FrameReg == X86::RBP && PtrVT == MVT::i64) ||
11097 (FrameReg == X86::EBP && PtrVT == MVT::i32)) &&
11098 "Invalid Frame Register!");
11099 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, PtrVT);
11100 unsigned StoreAddrReg = (PtrVT == MVT::i64) ? X86::RCX : X86::ECX;
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011101
Michael Liaodb7da202013-05-02 09:18:38 +000011102 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, Frame,
Michael Liao299eb2e2013-05-02 09:22:04 +000011103 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Michael Liaodb7da202013-05-02 09:18:38 +000011104 StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011105 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11106 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011107 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011108
Michael Liaodb7da202013-05-02 09:18:38 +000011109 return DAG.getNode(X86ISD::EH_RETURN, dl, MVT::Other, Chain,
11110 DAG.getRegister(StoreAddrReg, PtrVT));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011111}
11112
Michael Liao6c0e04c2012-10-15 22:39:43 +000011113SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11114 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011115 SDLoc DL(Op);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011116 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11117 DAG.getVTList(MVT::i32, MVT::Other),
11118 Op.getOperand(0), Op.getOperand(1));
11119}
11120
11121SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11122 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011123 SDLoc DL(Op);
Michael Liao6c0e04c2012-10-15 22:39:43 +000011124 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11125 Op.getOperand(0), Op.getOperand(1));
11126}
11127
Craig Topper55b24052012-09-11 06:15:32 +000011128static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011129 return Op.getOperand(0);
11130}
11131
11132SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11133 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011134 SDValue Root = Op.getOperand(0);
11135 SDValue Trmp = Op.getOperand(1); // trampoline
11136 SDValue FPtr = Op.getOperand(2); // nested function
11137 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Andrew Trickac6d9be2013-05-25 02:42:55 +000011138 SDLoc dl (Op);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011139
Dan Gohman69de1932008-02-06 22:27:42 +000011140 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011141 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011142
11143 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011144 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011145
11146 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011147 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11148 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011149
Michael Liao7abf67a2012-10-04 19:50:43 +000011150 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11151 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011152
11153 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11154
11155 // Load the pointer to the nested function into R11.
11156 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011157 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011158 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011159 Addr, MachinePointerInfo(TrmpAddr),
11160 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011161
Owen Anderson825b72b2009-08-11 20:47:22 +000011162 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11163 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011164 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11165 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011166 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011167
11168 // Load the 'nest' parameter value into R10.
11169 // R10 is specified in X86CallingConv.td
11170 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011171 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11172 DAG.getConstant(10, MVT::i64));
11173 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011174 Addr, MachinePointerInfo(TrmpAddr, 10),
11175 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011176
Owen Anderson825b72b2009-08-11 20:47:22 +000011177 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11178 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011179 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11180 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011181 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011182
11183 // Jump to the nested function.
11184 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011185 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11186 DAG.getConstant(20, MVT::i64));
11187 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011188 Addr, MachinePointerInfo(TrmpAddr, 20),
11189 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011190
11191 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011192 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11193 DAG.getConstant(22, MVT::i64));
11194 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011195 MachinePointerInfo(TrmpAddr, 22),
11196 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011197
Duncan Sands4a544a72011-09-06 13:37:06 +000011198 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011199 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011200 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011201 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011202 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011203 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011204
11205 switch (CC) {
11206 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011207 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011208 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011209 case CallingConv::X86_StdCall: {
11210 // Pass 'nest' parameter in ECX.
11211 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011212 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011213
11214 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011215 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011216 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011217
Chris Lattner58d74912008-03-12 17:45:29 +000011218 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011219 unsigned InRegCount = 0;
11220 unsigned Idx = 1;
11221
11222 for (FunctionType::param_iterator I = FTy->param_begin(),
11223 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011224 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011225 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011226 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011227
11228 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011229 report_fatal_error("Nest register in use - reduce number of inreg"
11230 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011231 }
11232 }
11233 break;
11234 }
11235 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011236 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011237 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011238 // Pass 'nest' parameter in EAX.
11239 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011240 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011241 break;
11242 }
11243
Dan Gohman475871a2008-07-27 21:46:04 +000011244 SDValue OutChains[4];
11245 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011246
Owen Anderson825b72b2009-08-11 20:47:22 +000011247 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11248 DAG.getConstant(10, MVT::i32));
11249 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011250
Chris Lattnera62fe662010-02-05 19:20:30 +000011251 // This is storing the opcode for MOV32ri.
11252 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011253 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011254 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011255 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011256 Trmp, MachinePointerInfo(TrmpAddr),
11257 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011258
Owen Anderson825b72b2009-08-11 20:47:22 +000011259 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11260 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011261 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11262 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011263 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011264
Chris Lattnera62fe662010-02-05 19:20:30 +000011265 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011266 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11267 DAG.getConstant(5, MVT::i32));
11268 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011269 MachinePointerInfo(TrmpAddr, 5),
11270 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011271
Owen Anderson825b72b2009-08-11 20:47:22 +000011272 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11273 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011274 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11275 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011276 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011277
Duncan Sands4a544a72011-09-06 13:37:06 +000011278 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011279 }
11280}
11281
Dan Gohmand858e902010-04-17 15:26:15 +000011282SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11283 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011284 /*
11285 The rounding mode is in bits 11:10 of FPSR, and has the following
11286 settings:
11287 00 Round to nearest
11288 01 Round to -inf
11289 10 Round to +inf
11290 11 Round to 0
11291
11292 FLT_ROUNDS, on the other hand, expects the following:
11293 -1 Undefined
11294 0 Round to 0
11295 1 Round to nearest
11296 2 Round to +inf
11297 3 Round to -inf
11298
11299 To perform the conversion, we do:
11300 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11301 */
11302
11303 MachineFunction &MF = DAG.getMachineFunction();
11304 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011305 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011306 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011307 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011308 SDLoc DL(Op);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011309
11310 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011311 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011312 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011313
Chris Lattner2156b792010-09-22 01:11:26 +000011314 MachineMemOperand *MMO =
11315 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11316 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011317
Chris Lattner2156b792010-09-22 01:11:26 +000011318 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11319 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11320 DAG.getVTList(MVT::Other),
Michael Liao0ee17002013-04-19 04:03:37 +000011321 Ops, array_lengthof(Ops), MVT::i16,
11322 MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011323
11324 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011325 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011326 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011327
11328 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011329 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011330 DAG.getNode(ISD::SRL, DL, MVT::i16,
11331 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011332 CWD, DAG.getConstant(0x800, MVT::i16)),
11333 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011334 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011335 DAG.getNode(ISD::SRL, DL, MVT::i16,
11336 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011337 CWD, DAG.getConstant(0x400, MVT::i16)),
11338 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011339
Dan Gohman475871a2008-07-27 21:46:04 +000011340 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011341 DAG.getNode(ISD::AND, DL, MVT::i16,
11342 DAG.getNode(ISD::ADD, DL, MVT::i16,
11343 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011344 DAG.getConstant(1, MVT::i16)),
11345 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011346
Duncan Sands83ec4b62008-06-06 12:08:01 +000011347 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011348 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011349}
11350
Craig Topper55b24052012-09-11 06:15:32 +000011351static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011352 EVT VT = Op.getValueType();
11353 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011354 unsigned NumBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011355 SDLoc dl(Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011356
11357 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011358 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011359 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011360 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011361 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011362 }
Evan Cheng18efe262007-12-14 02:13:44 +000011363
Evan Cheng152804e2007-12-14 08:30:15 +000011364 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011365 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011366 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011367
11368 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011369 SDValue Ops[] = {
11370 Op,
11371 DAG.getConstant(NumBits+NumBits-1, OpVT),
11372 DAG.getConstant(X86::COND_E, MVT::i8),
11373 Op.getValue(1)
11374 };
11375 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011376
11377 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011378 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011379
Owen Anderson825b72b2009-08-11 20:47:22 +000011380 if (VT == MVT::i8)
11381 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011382 return Op;
11383}
11384
Craig Topper55b24052012-09-11 06:15:32 +000011385static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011386 EVT VT = Op.getValueType();
11387 EVT OpVT = VT;
11388 unsigned NumBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011389 SDLoc dl(Op);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011390
11391 Op = Op.getOperand(0);
11392 if (VT == MVT::i8) {
11393 // Zero extend to i32 since there is not an i8 bsr.
11394 OpVT = MVT::i32;
11395 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11396 }
11397
11398 // Issue a bsr (scan bits in reverse).
11399 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11400 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11401
11402 // And xor with NumBits-1.
11403 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11404
11405 if (VT == MVT::i8)
11406 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11407 return Op;
11408}
11409
Craig Topper55b24052012-09-11 06:15:32 +000011410static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011411 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011412 unsigned NumBits = VT.getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011413 SDLoc dl(Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011414 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011415
11416 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011417 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011418 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011419
11420 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011421 SDValue Ops[] = {
11422 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011423 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011424 DAG.getConstant(X86::COND_E, MVT::i8),
11425 Op.getValue(1)
11426 };
Chandler Carruth77821022011-12-24 12:12:34 +000011427 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011428}
11429
Craig Topper13894fa2011-08-24 06:14:18 +000011430// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11431// ones, and then concatenate the result back.
11432static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011433 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011434
Craig Topper7a9a28b2012-08-12 02:23:29 +000011435 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011436 "Unsupported value type for operation");
11437
Craig Topper66ddd152012-04-27 22:54:43 +000011438 unsigned NumElems = VT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011439 SDLoc dl(Op);
Craig Topper13894fa2011-08-24 06:14:18 +000011440
11441 // Extract the LHS vectors
11442 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011443 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11444 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011445
11446 // Extract the RHS vectors
11447 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011448 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11449 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011450
11451 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11452 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11453
11454 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11455 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11456 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11457}
11458
Craig Topper55b24052012-09-11 06:15:32 +000011459static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011460 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011461 Op.getValueType().isInteger() &&
11462 "Only handle AVX 256-bit vector integer operation");
11463 return Lower256IntArith(Op, DAG);
11464}
11465
Craig Topper55b24052012-09-11 06:15:32 +000011466static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011467 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011468 Op.getValueType().isInteger() &&
11469 "Only handle AVX 256-bit vector integer operation");
11470 return Lower256IntArith(Op, DAG);
11471}
11472
Craig Topper55b24052012-09-11 06:15:32 +000011473static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11474 SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000011475 SDLoc dl(Op);
Craig Topper13894fa2011-08-24 06:14:18 +000011476 EVT VT = Op.getValueType();
11477
11478 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011479 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011480 return Lower256IntArith(Op, DAG);
11481
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011482 SDValue A = Op.getOperand(0);
11483 SDValue B = Op.getOperand(1);
11484
11485 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11486 if (VT == MVT::v4i32) {
11487 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11488 "Should not custom lower when pmuldq is available!");
11489
11490 // Extract the odd parts.
11491 const int UnpackMask[] = { 1, -1, 3, -1 };
11492 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11493 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11494
11495 // Multiply the even parts.
11496 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11497 // Now multiply odd parts.
11498 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11499
11500 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11501 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11502
11503 // Merge the two vectors back together with a shuffle. This expands into 2
11504 // shuffles.
11505 const int ShufMask[] = { 0, 4, 2, 6 };
11506 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11507 }
11508
Craig Topper5b209e82012-02-05 03:14:49 +000011509 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11510 "Only know how to lower V2I64/V4I64 multiply");
11511
Craig Topper5b209e82012-02-05 03:14:49 +000011512 // Ahi = psrlqi(a, 32);
11513 // Bhi = psrlqi(b, 32);
11514 //
11515 // AloBlo = pmuludq(a, b);
11516 // AloBhi = pmuludq(a, Bhi);
11517 // AhiBlo = pmuludq(Ahi, b);
11518
11519 // AloBhi = psllqi(AloBhi, 32);
11520 // AhiBlo = psllqi(AhiBlo, 32);
11521 // return AloBlo + AloBhi + AhiBlo;
11522
Craig Topper5b209e82012-02-05 03:14:49 +000011523 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011524
Craig Topper5b209e82012-02-05 03:14:49 +000011525 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11526 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011527
Craig Topper5b209e82012-02-05 03:14:49 +000011528 // Bit cast to 32-bit vectors for MULUDQ
11529 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11530 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11531 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11532 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11533 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011534
Craig Topper5b209e82012-02-05 03:14:49 +000011535 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11536 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11537 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011538
Craig Topper5b209e82012-02-05 03:14:49 +000011539 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11540 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011541
Dale Johannesene4d209d2009-02-03 20:21:25 +000011542 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011543 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011544}
11545
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011546SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11547 EVT VT = Op.getValueType();
11548 EVT EltTy = VT.getVectorElementType();
11549 unsigned NumElts = VT.getVectorNumElements();
11550 SDValue N0 = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000011551 SDLoc dl(Op);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011552
11553 // Lower sdiv X, pow2-const.
11554 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11555 if (!C)
11556 return SDValue();
11557
11558 APInt SplatValue, SplatUndef;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +000011559 unsigned SplatBitSize;
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011560 bool HasAnyUndefs;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +000011561 if (!C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
11562 HasAnyUndefs) ||
11563 EltTy.getSizeInBits() < SplatBitSize)
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011564 return SDValue();
11565
11566 if ((SplatValue != 0) &&
11567 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11568 unsigned lg2 = SplatValue.countTrailingZeros();
11569 // Splat the sign bit.
11570 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11571 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11572 // Add (N0 < 0) ? abs2 - 1 : 0;
11573 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11574 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11575 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11576 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11577 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11578
11579 // If we're dividing by a positive value, we're done. Otherwise, we must
11580 // negate the result.
11581 if (SplatValue.isNonNegative())
11582 return SRA;
11583
11584 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11585 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11586 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11587 }
11588 return SDValue();
11589}
11590
Michael Liao4b7ab122013-03-20 02:20:36 +000011591static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11592 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011593 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011594 SDLoc dl(Op);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011595 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011596 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011597
Nadav Rotem43012222011-05-11 08:12:09 +000011598 // Optimize shl/srl/sra with constant shift amount.
11599 if (isSplatVector(Amt.getNode())) {
11600 SDValue SclrAmt = Amt->getOperand(0);
11601 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11602 uint64_t ShiftAmt = C->getZExtValue();
11603
Craig Toppered2e13d2012-01-22 19:15:14 +000011604 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011605 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011606 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11607 if (Op.getOpcode() == ISD::SHL)
11608 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11609 DAG.getConstant(ShiftAmt, MVT::i32));
11610 if (Op.getOpcode() == ISD::SRL)
11611 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11612 DAG.getConstant(ShiftAmt, MVT::i32));
11613 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11614 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11615 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011616 }
11617
Craig Toppered2e13d2012-01-22 19:15:14 +000011618 if (VT == MVT::v16i8) {
11619 if (Op.getOpcode() == ISD::SHL) {
11620 // Make a large shift.
11621 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11622 DAG.getConstant(ShiftAmt, MVT::i32));
11623 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11624 // Zero out the rightmost bits.
11625 SmallVector<SDValue, 16> V(16,
11626 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11627 MVT::i8));
11628 return DAG.getNode(ISD::AND, dl, VT, SHL,
11629 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011630 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011631 if (Op.getOpcode() == ISD::SRL) {
11632 // Make a large shift.
11633 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11634 DAG.getConstant(ShiftAmt, MVT::i32));
11635 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11636 // Zero out the leftmost bits.
11637 SmallVector<SDValue, 16> V(16,
11638 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11639 MVT::i8));
11640 return DAG.getNode(ISD::AND, dl, VT, SRL,
11641 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11642 }
11643 if (Op.getOpcode() == ISD::SRA) {
11644 if (ShiftAmt == 7) {
11645 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011646 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011647 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011648 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011649
Craig Toppered2e13d2012-01-22 19:15:14 +000011650 // R s>> a === ((R u>> a) ^ m) - m
11651 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11652 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11653 MVT::i8));
11654 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11655 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11656 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11657 return Res;
11658 }
Craig Topper731dfd02012-04-23 03:42:40 +000011659 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011660 }
Craig Topper46154eb2011-11-11 07:39:23 +000011661
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011662 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011663 if (Op.getOpcode() == ISD::SHL) {
11664 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011665 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11666 DAG.getConstant(ShiftAmt, MVT::i32));
11667 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011668 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011669 SmallVector<SDValue, 32> V(32,
11670 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11671 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011672 return DAG.getNode(ISD::AND, dl, VT, SHL,
11673 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011674 }
Craig Topper0d86d462011-11-20 00:12:05 +000011675 if (Op.getOpcode() == ISD::SRL) {
11676 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011677 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11678 DAG.getConstant(ShiftAmt, MVT::i32));
11679 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011680 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011681 SmallVector<SDValue, 32> V(32,
11682 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11683 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011684 return DAG.getNode(ISD::AND, dl, VT, SRL,
11685 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11686 }
11687 if (Op.getOpcode() == ISD::SRA) {
11688 if (ShiftAmt == 7) {
11689 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011690 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011691 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011692 }
11693
11694 // R s>> a === ((R u>> a) ^ m) - m
11695 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11696 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11697 MVT::i8));
11698 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11699 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11700 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11701 return Res;
11702 }
Craig Topper731dfd02012-04-23 03:42:40 +000011703 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011704 }
Nadav Rotem43012222011-05-11 08:12:09 +000011705 }
11706 }
11707
Michael Liao42317cc2013-03-20 02:33:21 +000011708 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11709 if (!Subtarget->is64Bit() &&
11710 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11711 Amt.getOpcode() == ISD::BITCAST &&
11712 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11713 Amt = Amt.getOperand(0);
11714 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11715 VT.getVectorNumElements();
11716 unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
11717 uint64_t ShiftAmt = 0;
11718 for (unsigned i = 0; i != Ratio; ++i) {
11719 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i));
11720 if (C == 0)
11721 return SDValue();
11722 // 6 == Log2(64)
11723 ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
11724 }
11725 // Check remaining shift amounts.
11726 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11727 uint64_t ShAmt = 0;
11728 for (unsigned j = 0; j != Ratio; ++j) {
11729 ConstantSDNode *C =
11730 dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
11731 if (C == 0)
11732 return SDValue();
11733 // 6 == Log2(64)
11734 ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
11735 }
11736 if (ShAmt != ShiftAmt)
11737 return SDValue();
11738 }
11739 switch (Op.getOpcode()) {
11740 default:
11741 llvm_unreachable("Unknown shift opcode!");
11742 case ISD::SHL:
11743 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11744 DAG.getConstant(ShiftAmt, MVT::i32));
11745 case ISD::SRL:
11746 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11747 DAG.getConstant(ShiftAmt, MVT::i32));
11748 case ISD::SRA:
11749 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11750 DAG.getConstant(ShiftAmt, MVT::i32));
11751 }
11752 }
11753
11754 return SDValue();
11755}
11756
11757static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
11758 const X86Subtarget* Subtarget) {
11759 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011760 SDLoc dl(Op);
Michael Liao42317cc2013-03-20 02:33:21 +000011761 SDValue R = Op.getOperand(0);
11762 SDValue Amt = Op.getOperand(1);
11763
11764 if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) ||
11765 VT == MVT::v4i32 || VT == MVT::v8i16 ||
11766 (Subtarget->hasInt256() &&
11767 ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) ||
11768 VT == MVT::v8i32 || VT == MVT::v16i16))) {
11769 SDValue BaseShAmt;
11770 EVT EltVT = VT.getVectorElementType();
11771
11772 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11773 unsigned NumElts = VT.getVectorNumElements();
11774 unsigned i, j;
11775 for (i = 0; i != NumElts; ++i) {
11776 if (Amt.getOperand(i).getOpcode() == ISD::UNDEF)
11777 continue;
11778 break;
11779 }
11780 for (j = i; j != NumElts; ++j) {
11781 SDValue Arg = Amt.getOperand(j);
11782 if (Arg.getOpcode() == ISD::UNDEF) continue;
11783 if (Arg != Amt.getOperand(i))
11784 break;
11785 }
11786 if (i != NumElts && j == NumElts)
11787 BaseShAmt = Amt.getOperand(i);
11788 } else {
11789 if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11790 Amt = Amt.getOperand(0);
11791 if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE &&
11792 cast<ShuffleVectorSDNode>(Amt)->isSplat()) {
11793 SDValue InVec = Amt.getOperand(0);
11794 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11795 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11796 unsigned i = 0;
11797 for (; i != NumElts; ++i) {
11798 SDValue Arg = InVec.getOperand(i);
11799 if (Arg.getOpcode() == ISD::UNDEF) continue;
11800 BaseShAmt = Arg;
11801 break;
11802 }
11803 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11804 if (ConstantSDNode *C =
11805 dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
11806 unsigned SplatIdx =
11807 cast<ShuffleVectorSDNode>(Amt)->getSplatIndex();
11808 if (C->getZExtValue() == SplatIdx)
11809 BaseShAmt = InVec.getOperand(1);
11810 }
11811 }
11812 if (BaseShAmt.getNode() == 0)
11813 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt,
11814 DAG.getIntPtrConstant(0));
11815 }
11816 }
11817
11818 if (BaseShAmt.getNode()) {
11819 if (EltVT.bitsGT(MVT::i32))
11820 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
11821 else if (EltVT.bitsLT(MVT::i32))
11822 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
11823
11824 switch (Op.getOpcode()) {
11825 default:
11826 llvm_unreachable("Unknown shift opcode!");
11827 case ISD::SHL:
11828 switch (VT.getSimpleVT().SimpleTy) {
11829 default: return SDValue();
11830 case MVT::v2i64:
11831 case MVT::v4i32:
11832 case MVT::v8i16:
11833 case MVT::v4i64:
11834 case MVT::v8i32:
11835 case MVT::v16i16:
11836 return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG);
11837 }
11838 case ISD::SRA:
11839 switch (VT.getSimpleVT().SimpleTy) {
11840 default: return SDValue();
11841 case MVT::v4i32:
11842 case MVT::v8i16:
11843 case MVT::v8i32:
11844 case MVT::v16i16:
11845 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG);
11846 }
11847 case ISD::SRL:
11848 switch (VT.getSimpleVT().SimpleTy) {
11849 default: return SDValue();
11850 case MVT::v2i64:
11851 case MVT::v4i32:
11852 case MVT::v8i16:
11853 case MVT::v4i64:
11854 case MVT::v8i32:
11855 case MVT::v16i16:
11856 return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG);
11857 }
11858 }
11859 }
11860 }
11861
11862 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11863 if (!Subtarget->is64Bit() &&
11864 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11865 Amt.getOpcode() == ISD::BITCAST &&
11866 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11867 Amt = Amt.getOperand(0);
11868 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11869 VT.getVectorNumElements();
11870 std::vector<SDValue> Vals(Ratio);
11871 for (unsigned i = 0; i != Ratio; ++i)
11872 Vals[i] = Amt.getOperand(i);
11873 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11874 for (unsigned j = 0; j != Ratio; ++j)
11875 if (Vals[j] != Amt.getOperand(i + j))
11876 return SDValue();
11877 }
11878 switch (Op.getOpcode()) {
11879 default:
11880 llvm_unreachable("Unknown shift opcode!");
11881 case ISD::SHL:
11882 return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1));
11883 case ISD::SRL:
11884 return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1));
11885 case ISD::SRA:
11886 return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1));
11887 }
11888 }
11889
Michael Liao4b7ab122013-03-20 02:20:36 +000011890 return SDValue();
11891}
11892
11893SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11894
11895 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000011896 SDLoc dl(Op);
Michael Liao4b7ab122013-03-20 02:20:36 +000011897 SDValue R = Op.getOperand(0);
11898 SDValue Amt = Op.getOperand(1);
11899 SDValue V;
11900
11901 if (!Subtarget->hasSSE2())
11902 return SDValue();
11903
11904 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
11905 if (V.getNode())
11906 return V;
11907
Michael Liao42317cc2013-03-20 02:33:21 +000011908 V = LowerScalarVariableShift(Op, DAG, Subtarget);
11909 if (V.getNode())
11910 return V;
11911
Michael Liao5c5f1902013-03-20 02:28:20 +000011912 // AVX2 has VPSLLV/VPSRAV/VPSRLV.
11913 if (Subtarget->hasInt256()) {
11914 if (Op.getOpcode() == ISD::SRL &&
11915 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11916 VT == MVT::v4i64 || VT == MVT::v8i32))
11917 return Op;
11918 if (Op.getOpcode() == ISD::SHL &&
11919 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11920 VT == MVT::v4i64 || VT == MVT::v8i32))
11921 return Op;
11922 if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32))
11923 return Op;
11924 }
11925
Nadav Rotem43012222011-05-11 08:12:09 +000011926 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011927 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011928 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011929
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011930 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011931 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011932 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11933 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11934 }
Nadav Rotem43012222011-05-11 08:12:09 +000011935 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011936 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011937
Nate Begeman51409212010-07-28 00:21:48 +000011938 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011939 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011940 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011941
Lang Hames8b99c1e2011-12-17 01:08:46 +000011942 // Turn 'a' into a mask suitable for VSELECT
11943 SDValue VSelM = DAG.getConstant(0x80, VT);
11944 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011945 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011946
Lang Hames8b99c1e2011-12-17 01:08:46 +000011947 SDValue CM1 = DAG.getConstant(0x0f, VT);
11948 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011949
Lang Hames8b99c1e2011-12-17 01:08:46 +000011950 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11951 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011952 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11953 DAG.getConstant(4, MVT::i32), DAG);
11954 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011955 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11956
Nate Begeman51409212010-07-28 00:21:48 +000011957 // a += a
11958 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011959 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011960 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011961
Lang Hames8b99c1e2011-12-17 01:08:46 +000011962 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11963 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011964 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11965 DAG.getConstant(2, MVT::i32), DAG);
11966 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011967 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11968
Nate Begeman51409212010-07-28 00:21:48 +000011969 // a += a
11970 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011971 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011972 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011973
Lang Hames8b99c1e2011-12-17 01:08:46 +000011974 // return VSELECT(r, r+r, a);
11975 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011976 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011977 return R;
11978 }
Craig Topper46154eb2011-11-11 07:39:23 +000011979
11980 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011981 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011982 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011983 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11984 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11985
11986 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011987 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11988 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011989
11990 // Recreate the shift amount vectors
11991 SDValue Amt1, Amt2;
11992 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11993 // Constant shift amount
11994 SmallVector<SDValue, 4> Amt1Csts;
11995 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011996 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011997 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011998 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011999 Amt2Csts.push_back(Amt->getOperand(i));
12000
12001 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
12002 &Amt1Csts[0], NumElems/2);
12003 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
12004 &Amt2Csts[0], NumElems/2);
12005 } else {
12006 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000012007 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
12008 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000012009 }
12010
12011 // Issue new vector shifts for the smaller types
12012 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
12013 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
12014
12015 // Concatenate the result back
12016 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
12017 }
12018
Nate Begeman51409212010-07-28 00:21:48 +000012019 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000012020}
Mon P Wangaf9b9522008-12-18 21:42:19 +000012021
Craig Topper55b24052012-09-11 06:15:32 +000012022static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000012023 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
12024 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000012025 // looks for this combo and may remove the "setcc" instruction if the "setcc"
12026 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000012027 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000012028 SDValue LHS = N->getOperand(0);
12029 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000012030 unsigned BaseOp = 0;
12031 unsigned Cond = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +000012032 SDLoc DL(Op);
Bill Wendling74c37652008-12-09 22:08:41 +000012033 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012034 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000012035 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000012036 // A subtract of one will be selected as a INC. Note that INC doesn't
12037 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000012038 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
12039 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000012040 BaseOp = X86ISD::INC;
12041 Cond = X86::COND_O;
12042 break;
12043 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012044 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000012045 Cond = X86::COND_O;
12046 break;
12047 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012048 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000012049 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000012050 break;
12051 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000012052 // A subtract of one will be selected as a DEC. Note that DEC doesn't
12053 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000012054 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
12055 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000012056 BaseOp = X86ISD::DEC;
12057 Cond = X86::COND_O;
12058 break;
12059 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012060 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000012061 Cond = X86::COND_O;
12062 break;
12063 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012064 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000012065 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000012066 break;
12067 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000012068 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000012069 Cond = X86::COND_O;
12070 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012071 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
12072 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
12073 MVT::i32);
12074 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012075
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012076 SDValue SetCC =
12077 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
12078 DAG.getConstant(X86::COND_O, MVT::i32),
12079 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012080
Dan Gohman6e5fda22011-07-22 18:45:15 +000012081 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012082 }
Bill Wendling74c37652008-12-09 22:08:41 +000012083 }
Bill Wendling3fafd932008-11-26 22:37:40 +000012084
Bill Wendling61edeb52008-12-02 01:06:39 +000012085 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000012086 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012087 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000012088
Bill Wendling61edeb52008-12-02 01:06:39 +000012089 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012090 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
12091 DAG.getConstant(Cond, MVT::i32),
12092 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000012093
Dan Gohman6e5fda22011-07-22 18:45:15 +000012094 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000012095}
12096
Chad Rosier30450e82011-12-22 22:35:21 +000012097SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
12098 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012099 SDLoc dl(Op);
Craig Toppera124f942011-11-21 01:12:36 +000012100 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
12101 EVT VT = Op.getValueType();
12102
Craig Toppered2e13d2012-01-22 19:15:14 +000012103 if (!Subtarget->hasSSE2() || !VT.isVector())
12104 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012105
Craig Toppered2e13d2012-01-22 19:15:14 +000012106 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
12107 ExtraVT.getScalarType().getSizeInBits();
12108 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
12109
12110 switch (VT.getSimpleVT().SimpleTy) {
12111 default: return SDValue();
12112 case MVT::v8i32:
12113 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012114 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012115 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012116 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012117 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000012118 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000012119
Craig Toppered2e13d2012-01-22 19:15:14 +000012120 // Extract the LHS vectors
12121 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000012122 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
12123 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000012124
Craig Toppered2e13d2012-01-22 19:15:14 +000012125 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12126 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000012127
Craig Toppered2e13d2012-01-22 19:15:14 +000012128 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000012129 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000012130 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
12131 ExtraNumElems/2);
12132 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000012133
Craig Toppered2e13d2012-01-22 19:15:14 +000012134 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
12135 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000012136
Dmitri Gribenko2de05722012-09-10 21:26:47 +000012137 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012138 }
12139 // fall through
12140 case MVT::v4i32:
12141 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000012142 // (sext (vzext x)) -> (vsext x)
12143 SDValue Op0 = Op.getOperand(0);
12144 SDValue Op00 = Op0.getOperand(0);
12145 SDValue Tmp1;
12146 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
12147 if (Op0.getOpcode() == ISD::BITCAST &&
12148 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
12149 Tmp1 = LowerVectorIntExtend(Op00, DAG);
12150 if (Tmp1.getNode()) {
12151 SDValue Tmp1Op0 = Tmp1.getOperand(0);
12152 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
12153 "This optimization is invalid without a VZEXT.");
12154 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
12155 }
12156
12157 // If the above didn't work, then just use Shift-Left + Shift-Right.
12158 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000012159 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012160 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012161 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012162}
12163
Craig Topper55b24052012-09-11 06:15:32 +000012164static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
12165 SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012166 SDLoc dl(Op);
Eli Friedman14648462011-07-27 22:21:52 +000012167 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
12168 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
12169 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
12170 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
12171
12172 // The only fence that needs an instruction is a sequentially-consistent
12173 // cross-thread fence.
12174 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
12175 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
12176 // no-sse2). There isn't any reason to disable it if the target processor
12177 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012178 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000012179 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
12180
12181 SDValue Chain = Op.getOperand(0);
12182 SDValue Zero = DAG.getConstant(0, MVT::i32);
12183 SDValue Ops[] = {
12184 DAG.getRegister(X86::ESP, MVT::i32), // Base
12185 DAG.getTargetConstant(1, MVT::i8), // Scale
12186 DAG.getRegister(0, MVT::i32), // Index
12187 DAG.getTargetConstant(0, MVT::i32), // Disp
12188 DAG.getRegister(0, MVT::i32), // Segment.
12189 Zero,
12190 Chain
12191 };
Michael Liao2a8bea72013-04-19 22:22:57 +000012192 SDNode *Res = DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops);
Eli Friedman14648462011-07-27 22:21:52 +000012193 return SDValue(Res, 0);
12194 }
12195
12196 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
12197 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
12198}
12199
Craig Topper55b24052012-09-11 06:15:32 +000012200static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
12201 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000012202 EVT T = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012203 SDLoc DL(Op);
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000012204 unsigned Reg = 0;
12205 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000012206 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000012207 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000012208 case MVT::i8: Reg = X86::AL; size = 1; break;
12209 case MVT::i16: Reg = X86::AX; size = 2; break;
12210 case MVT::i32: Reg = X86::EAX; size = 4; break;
12211 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000012212 assert(Subtarget->is64Bit() && "Node not type legal!");
12213 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000012214 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000012215 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012216 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000012217 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000012218 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012219 Op.getOperand(1),
12220 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000012221 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012222 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012223 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012224 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
12225 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
Michael Liao0ee17002013-04-19 04:03:37 +000012226 Ops, array_lengthof(Ops), T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000012227 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012228 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000012229 return cpOut;
12230}
12231
Craig Topper55b24052012-09-11 06:15:32 +000012232static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
12233 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000012234 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012235 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012236 SDValue TheChain = Op.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000012237 SDLoc dl(Op);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012238 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012239 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
12240 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000012241 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000012242 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
12243 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000012244 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000012245 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000012246 rdx.getValue(1)
12247 };
Michael Liao0ee17002013-04-19 04:03:37 +000012248 return DAG.getMergeValues(Ops, array_lengthof(Ops), dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012249}
12250
Craig Topper55b24052012-09-11 06:15:32 +000012251SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012252 EVT SrcVT = Op.getOperand(0).getValueType();
12253 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012254 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012255 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012256 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012257 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012258 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012259 // i64 <=> MMX conversions are Legal.
12260 if (SrcVT==MVT::i64 && DstVT.isVector())
12261 return Op;
12262 if (DstVT==MVT::i64 && SrcVT.isVector())
12263 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012264 // MMX <=> MMX conversions are Legal.
12265 if (SrcVT.isVector() && DstVT.isVector())
12266 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012267 // All other conversions need to be expanded.
12268 return SDValue();
12269}
Chris Lattner5b856542010-12-20 00:59:46 +000012270
Craig Topper55b24052012-09-11 06:15:32 +000012271static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012272 SDNode *Node = Op.getNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012273 SDLoc dl(Node);
Owen Andersone50ed302009-08-10 22:56:29 +000012274 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012275 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012276 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012277 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012278 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012279 Node->getOperand(0),
12280 Node->getOperand(1), negOp,
12281 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012282 cast<AtomicSDNode>(Node)->getAlignment(),
12283 cast<AtomicSDNode>(Node)->getOrdering(),
12284 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012285}
12286
Eli Friedman327236c2011-08-24 20:50:09 +000012287static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12288 SDNode *Node = Op.getNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +000012289 SDLoc dl(Node);
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012290 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012291
12292 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012293 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12294 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12295 // (The only way to get a 16-byte store is cmpxchg16b)
12296 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12297 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12298 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012299 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12300 cast<AtomicSDNode>(Node)->getMemoryVT(),
12301 Node->getOperand(0),
12302 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012303 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012304 cast<AtomicSDNode>(Node)->getOrdering(),
12305 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012306 return Swap.getValue(1);
12307 }
12308 // Other atomic stores have a simple pattern.
12309 return Op;
12310}
12311
Chris Lattner5b856542010-12-20 00:59:46 +000012312static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12313 EVT VT = Op.getNode()->getValueType(0);
12314
12315 // Let legalize expand this if it isn't a legal type yet.
12316 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12317 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012318
Chris Lattner5b856542010-12-20 00:59:46 +000012319 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012320
Chris Lattner5b856542010-12-20 00:59:46 +000012321 unsigned Opc;
12322 bool ExtraOp = false;
12323 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012324 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012325 case ISD::ADDC: Opc = X86ISD::ADD; break;
12326 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12327 case ISD::SUBC: Opc = X86ISD::SUB; break;
12328 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12329 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012330
Chris Lattner5b856542010-12-20 00:59:46 +000012331 if (!ExtraOp)
Andrew Trickac6d9be2013-05-25 02:42:55 +000012332 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Chris Lattner5b856542010-12-20 00:59:46 +000012333 Op.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +000012334 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Chris Lattner5b856542010-12-20 00:59:46 +000012335 Op.getOperand(1), Op.getOperand(2));
12336}
12337
Evan Cheng8688a582013-01-29 02:32:37 +000012338SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012339 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012340
Evan Cheng8688a582013-01-29 02:32:37 +000012341 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012342 // which returns the values as { float, float } (in XMM0) or
12343 // { double, double } (which is returned in XMM0, XMM1).
Andrew Trickac6d9be2013-05-25 02:42:55 +000012344 SDLoc dl(Op);
Evan Cheng8688a582013-01-29 02:32:37 +000012345 SDValue Arg = Op.getOperand(0);
12346 EVT ArgVT = Arg.getValueType();
12347 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012348
Evan Cheng8688a582013-01-29 02:32:37 +000012349 ArgListTy Args;
12350 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012351
Evan Cheng8688a582013-01-29 02:32:37 +000012352 Entry.Node = Arg;
12353 Entry.Ty = ArgTy;
12354 Entry.isSExt = false;
12355 Entry.isZExt = false;
12356 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012357
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012358 bool isF64 = ArgVT == MVT::f64;
Evan Chenga66f40a2013-01-30 22:56:35 +000012359 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12360 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12361 // the results are returned via SRet in memory.
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012362 const char *LibcallName = isF64 ? "__sincos_stret" : "__sincosf_stret";
Evan Cheng8688a582013-01-29 02:32:37 +000012363 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012364
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012365 Type *RetTy = isF64
12366 ? (Type*)StructType::get(ArgTy, ArgTy, NULL)
12367 : (Type*)VectorType::get(ArgTy, 4);
Evan Cheng8688a582013-01-29 02:32:37 +000012368 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012369 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12370 false, false, false, false, 0,
12371 CallingConv::C, /*isTaillCall=*/false,
12372 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12373 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012374 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng3a6b7d32013-04-10 01:26:07 +000012375
12376 if (isF64)
12377 // Returned in xmm0 and xmm1.
12378 return CallResult.first;
12379
12380 // Returned in bits 0:31 and 32:64 xmm0.
12381 SDValue SinVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
12382 CallResult.first, DAG.getIntPtrConstant(0));
12383 SDValue CosVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
12384 CallResult.first, DAG.getIntPtrConstant(1));
12385 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
12386 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal);
Evan Cheng8688a582013-01-29 02:32:37 +000012387}
12388
Evan Cheng0db9fe62006-04-25 20:13:52 +000012389/// LowerOperation - Provide custom lowering hooks for some operations.
12390///
Dan Gohmand858e902010-04-17 15:26:15 +000012391SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012392 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012393 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012394 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012395 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12396 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012397 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012398 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012399 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012400 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012401 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12402 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12403 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012404 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12405 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012406 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12407 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12408 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012409 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012410 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012411 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012412 case ISD::SHL_PARTS:
12413 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012414 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012415 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012416 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012417 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012418 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12419 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12420 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012421 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012422 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012423 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012424 case ISD::FABS: return LowerFABS(Op, DAG);
12425 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012426 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012427 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012428 case ISD::SETCC: return LowerSETCC(Op, DAG);
12429 case ISD::SELECT: return LowerSELECT(Op, DAG);
12430 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012431 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012432 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012433 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012434 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012435 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012436 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012437 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12438 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012439 case ISD::FRAME_TO_ARGS_OFFSET:
12440 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012441 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012442 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012443 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12444 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012445 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12446 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012447 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012448 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012449 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012450 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012451 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012452 case ISD::SRA:
12453 case ISD::SRL:
12454 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012455 case ISD::SADDO:
12456 case ISD::UADDO:
12457 case ISD::SSUBO:
12458 case ISD::USUBO:
12459 case ISD::SMULO:
12460 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012461 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012462 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012463 case ISD::ADDC:
12464 case ISD::ADDE:
12465 case ISD::SUBC:
12466 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012467 case ISD::ADD: return LowerADD(Op, DAG);
12468 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012469 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012470 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012471 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012472}
12473
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012474static void ReplaceATOMIC_LOAD(SDNode *Node,
12475 SmallVectorImpl<SDValue> &Results,
12476 SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012477 SDLoc dl(Node);
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012478 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12479
12480 // Convert wide load -> cmpxchg8b/cmpxchg16b
12481 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12482 // (The only way to get a 16-byte load is cmpxchg16b)
12483 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012484 SDValue Zero = DAG.getConstant(0, VT);
12485 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012486 Node->getOperand(0),
12487 Node->getOperand(1), Zero, Zero,
12488 cast<AtomicSDNode>(Node)->getMemOperand(),
12489 cast<AtomicSDNode>(Node)->getOrdering(),
12490 cast<AtomicSDNode>(Node)->getSynchScope());
12491 Results.push_back(Swap.getValue(0));
12492 Results.push_back(Swap.getValue(1));
12493}
12494
Craig Topperc0878702012-08-17 06:55:11 +000012495static void
Duncan Sands1607f052008-12-01 11:39:25 +000012496ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012497 SelectionDAG &DAG, unsigned NewOp) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012498 SDLoc dl(Node);
Duncan Sands17001ce2011-10-18 12:44:00 +000012499 assert (Node->getValueType(0) == MVT::i64 &&
12500 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012501
12502 SDValue Chain = Node->getOperand(0);
12503 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012504 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012505 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012506 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012507 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012508 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012509 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012510 SDValue Result =
Michael Liao0ee17002013-04-19 04:03:37 +000012511 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, array_lengthof(Ops), MVT::i64,
Dan Gohmanc76909a2009-09-25 20:36:54 +000012512 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012513 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012514 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012515 Results.push_back(Result.getValue(2));
12516}
12517
Duncan Sands126d9072008-07-04 11:47:58 +000012518/// ReplaceNodeResults - Replace a node with an illegal result type
12519/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012520void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12521 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012522 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +000012523 SDLoc dl(N);
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012524 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012525 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012526 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012527 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012528 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012529 case ISD::ADDC:
12530 case ISD::ADDE:
12531 case ISD::SUBC:
12532 case ISD::SUBE:
12533 // We don't want to expand or promote these.
12534 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012535 case ISD::FP_TO_SINT:
12536 case ISD::FP_TO_UINT: {
12537 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12538
12539 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12540 return;
12541
Eli Friedman948e95a2009-05-23 09:59:16 +000012542 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012543 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012544 SDValue FIST = Vals.first, StackSlot = Vals.second;
12545 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012546 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012547 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012548 if (StackSlot.getNode() != 0)
12549 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12550 MachinePointerInfo(),
12551 false, false, false, 0));
12552 else
12553 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012554 }
12555 return;
12556 }
Michael Liao991b6a22012-10-24 04:09:32 +000012557 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012558 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12559 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012560 N->getValueType(0) != MVT::v2f32)
12561 return;
12562 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12563 N->getOperand(0));
12564 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12565 MVT::f64);
12566 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12567 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12568 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12569 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12570 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12571 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12572 return;
12573 }
Michael Liao44c2d612012-10-10 16:53:28 +000012574 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012575 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12576 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012577 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12578 Results.push_back(V);
12579 return;
12580 }
Duncan Sands1607f052008-12-01 11:39:25 +000012581 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012582 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012583 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012584 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012585 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012586 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012587 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012588 eax.getValue(2));
12589 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12590 SDValue Ops[] = { eax, edx };
Michael Liao0ee17002013-04-19 04:03:37 +000012591 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops,
12592 array_lengthof(Ops)));
Duncan Sands1607f052008-12-01 11:39:25 +000012593 Results.push_back(edx.getValue(1));
12594 return;
12595 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012596 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012597 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012598 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012599 bool Regs64bit = T == MVT::i128;
12600 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012601 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012602 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12603 DAG.getConstant(0, HalfT));
12604 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12605 DAG.getConstant(1, HalfT));
12606 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12607 Regs64bit ? X86::RAX : X86::EAX,
12608 cpInL, SDValue());
12609 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12610 Regs64bit ? X86::RDX : X86::EDX,
12611 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012612 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012613 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12614 DAG.getConstant(0, HalfT));
12615 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12616 DAG.getConstant(1, HalfT));
12617 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12618 Regs64bit ? X86::RBX : X86::EBX,
12619 swapInL, cpInH.getValue(1));
12620 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012621 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012622 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012623 SDValue Ops[] = { swapInH.getValue(0),
12624 N->getOperand(1),
12625 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012626 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012627 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012628 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12629 X86ISD::LCMPXCHG8_DAG;
12630 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Michael Liao0ee17002013-04-19 04:03:37 +000012631 Ops, array_lengthof(Ops), T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012632 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12633 Regs64bit ? X86::RAX : X86::EAX,
12634 HalfT, Result.getValue(1));
12635 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12636 Regs64bit ? X86::RDX : X86::EDX,
12637 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012638 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012639 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012640 Results.push_back(cpOutH.getValue(1));
12641 return;
12642 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012643 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012644 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012645 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012646 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012647 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012648 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012649 case ISD::ATOMIC_LOAD_MAX:
12650 case ISD::ATOMIC_LOAD_MIN:
12651 case ISD::ATOMIC_LOAD_UMAX:
12652 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012653 case ISD::ATOMIC_SWAP: {
12654 unsigned Opc;
12655 switch (N->getOpcode()) {
12656 default: llvm_unreachable("Unexpected opcode");
12657 case ISD::ATOMIC_LOAD_ADD:
12658 Opc = X86ISD::ATOMADD64_DAG;
12659 break;
12660 case ISD::ATOMIC_LOAD_AND:
12661 Opc = X86ISD::ATOMAND64_DAG;
12662 break;
12663 case ISD::ATOMIC_LOAD_NAND:
12664 Opc = X86ISD::ATOMNAND64_DAG;
12665 break;
12666 case ISD::ATOMIC_LOAD_OR:
12667 Opc = X86ISD::ATOMOR64_DAG;
12668 break;
12669 case ISD::ATOMIC_LOAD_SUB:
12670 Opc = X86ISD::ATOMSUB64_DAG;
12671 break;
12672 case ISD::ATOMIC_LOAD_XOR:
12673 Opc = X86ISD::ATOMXOR64_DAG;
12674 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012675 case ISD::ATOMIC_LOAD_MAX:
12676 Opc = X86ISD::ATOMMAX64_DAG;
12677 break;
12678 case ISD::ATOMIC_LOAD_MIN:
12679 Opc = X86ISD::ATOMMIN64_DAG;
12680 break;
12681 case ISD::ATOMIC_LOAD_UMAX:
12682 Opc = X86ISD::ATOMUMAX64_DAG;
12683 break;
12684 case ISD::ATOMIC_LOAD_UMIN:
12685 Opc = X86ISD::ATOMUMIN64_DAG;
12686 break;
Craig Topperc0878702012-08-17 06:55:11 +000012687 case ISD::ATOMIC_SWAP:
12688 Opc = X86ISD::ATOMSWAP64_DAG;
12689 break;
12690 }
12691 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012692 return;
Craig Topperc0878702012-08-17 06:55:11 +000012693 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012694 case ISD::ATOMIC_LOAD:
12695 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012696 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012697}
12698
Evan Cheng72261582005-12-20 06:22:03 +000012699const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12700 switch (Opcode) {
12701 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012702 case X86ISD::BSF: return "X86ISD::BSF";
12703 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012704 case X86ISD::SHLD: return "X86ISD::SHLD";
12705 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012706 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012707 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012708 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012709 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012710 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012711 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012712 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12713 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12714 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012715 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012716 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012717 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012718 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012719 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012720 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012721 case X86ISD::COMI: return "X86ISD::COMI";
12722 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012723 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012724 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012725 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12726 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012727 case X86ISD::CMOV: return "X86ISD::CMOV";
12728 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012729 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012730 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12731 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012732 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012733 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012734 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012735 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012736 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012737 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12738 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012739 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012740 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012741 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012742 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012743 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012744 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012745 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012746 case X86ISD::HADD: return "X86ISD::HADD";
12747 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012748 case X86ISD::FHADD: return "X86ISD::FHADD";
12749 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012750 case X86ISD::UMAX: return "X86ISD::UMAX";
12751 case X86ISD::UMIN: return "X86ISD::UMIN";
12752 case X86ISD::SMAX: return "X86ISD::SMAX";
12753 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012754 case X86ISD::FMAX: return "X86ISD::FMAX";
12755 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012756 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12757 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012758 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12759 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012760 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012761 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012762 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012763 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12764 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012765 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012766 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012767 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012768 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012769 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12770 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012771 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12772 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12773 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12774 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12775 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12776 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012777 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012778 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012779 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012780 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12781 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012782 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012783 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012784 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12785 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012786 case X86ISD::VSHL: return "X86ISD::VSHL";
12787 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012788 case X86ISD::VSRA: return "X86ISD::VSRA";
12789 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12790 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12791 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012792 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012793 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12794 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012795 case X86ISD::ADD: return "X86ISD::ADD";
12796 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012797 case X86ISD::ADC: return "X86ISD::ADC";
12798 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012799 case X86ISD::SMUL: return "X86ISD::SMUL";
12800 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012801 case X86ISD::INC: return "X86ISD::INC";
12802 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012803 case X86ISD::OR: return "X86ISD::OR";
12804 case X86ISD::XOR: return "X86ISD::XOR";
12805 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012806 case X86ISD::BLSI: return "X86ISD::BLSI";
12807 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12808 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012809 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012810 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012811 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012812 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012813 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12814 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012815 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012816 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012817 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012818 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012819 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012820 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12821 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012822 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12823 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12824 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012825 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12826 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012827 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12828 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012829 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012830 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012831 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012832 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12833 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012834 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012835 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012836 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012837 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012838 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012839 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012840 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012841 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012842 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Michael Liaoc26392a2013-03-28 23:41:26 +000012843 case X86ISD::RDSEED: return "X86ISD::RDSEED";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012844 case X86ISD::FMADD: return "X86ISD::FMADD";
12845 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12846 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12847 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12848 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12849 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012850 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12851 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Michael Liaof8fd8832013-03-26 22:47:01 +000012852 case X86ISD::XTEST: return "X86ISD::XTEST";
Evan Cheng72261582005-12-20 06:22:03 +000012853 }
12854}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012855
Chris Lattnerc9addb72007-03-30 23:15:24 +000012856// isLegalAddressingMode - Return true if the addressing mode represented
12857// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012858bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012859 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012860 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012861 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012862 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012863
Chris Lattnerc9addb72007-03-30 23:15:24 +000012864 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012865 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012866 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012867
Chris Lattnerc9addb72007-03-30 23:15:24 +000012868 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012869 unsigned GVFlags =
12870 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012871
Chris Lattnerdfed4132009-07-10 07:38:24 +000012872 // If a reference to this global requires an extra load, we can't fold it.
12873 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012874 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012875
Chris Lattnerdfed4132009-07-10 07:38:24 +000012876 // If BaseGV requires a register for the PIC base, we cannot also have a
12877 // BaseReg specified.
12878 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012879 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012880
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012881 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012882 if ((M != CodeModel::Small || R != Reloc::Static) &&
12883 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012884 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012885 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012886
Chris Lattnerc9addb72007-03-30 23:15:24 +000012887 switch (AM.Scale) {
12888 case 0:
12889 case 1:
12890 case 2:
12891 case 4:
12892 case 8:
12893 // These scales always work.
12894 break;
12895 case 3:
12896 case 5:
12897 case 9:
12898 // These scales are formed with basereg+scalereg. Only accept if there is
12899 // no basereg yet.
12900 if (AM.HasBaseReg)
12901 return false;
12902 break;
12903 default: // Other stuff never works.
12904 return false;
12905 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012906
Chris Lattnerc9addb72007-03-30 23:15:24 +000012907 return true;
12908}
12909
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012910bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012911 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012912 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012913 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12914 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012915 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012916}
12917
Evan Cheng70e10d32012-07-17 06:53:39 +000012918bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012919 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012920}
12921
12922bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012923 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012924 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012925}
12926
Owen Andersone50ed302009-08-10 22:56:29 +000012927bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012928 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012929 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012930 unsigned NumBits1 = VT1.getSizeInBits();
12931 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012932 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012933}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012934
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012935bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012936 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012937 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012938}
12939
Owen Andersone50ed302009-08-10 22:56:29 +000012940bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012941 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012942 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012943}
12944
Evan Cheng2766a472012-12-06 19:13:27 +000012945bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12946 EVT VT1 = Val.getValueType();
12947 if (isZExtFree(VT1, VT2))
12948 return true;
12949
12950 if (Val.getOpcode() != ISD::LOAD)
12951 return false;
12952
12953 if (!VT1.isSimple() || !VT1.isInteger() ||
12954 !VT2.isSimple() || !VT2.isInteger())
12955 return false;
12956
12957 switch (VT1.getSimpleVT().SimpleTy) {
12958 default: break;
12959 case MVT::i8:
12960 case MVT::i16:
12961 case MVT::i32:
12962 // X86 has 8, 16, and 32-bit zero-extending loads.
12963 return true;
12964 }
12965
12966 return false;
12967}
12968
Owen Andersone50ed302009-08-10 22:56:29 +000012969bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012970 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012971 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012972}
12973
Evan Cheng60c07e12006-07-05 22:17:51 +000012974/// isShuffleMaskLegal - Targets can use this to indicate that they only
12975/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12976/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12977/// are assumed to be legal.
12978bool
Eric Christopherfd179292009-08-27 18:07:15 +000012979X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012980 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012981 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012982 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012983 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012984
Nate Begemana09008b2009-10-19 02:17:23 +000012985 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012986 return (VT.getVectorNumElements() == 2 ||
12987 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12988 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012989 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012990 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012991 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12992 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012993 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012994 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12995 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12996 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12997 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012998}
12999
Dan Gohman7d8143f2008-04-09 20:09:42 +000013000bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000013001X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000013002 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000013003 unsigned NumElts = VT.getVectorNumElements();
13004 // FIXME: This collection of masks seems suspect.
13005 if (NumElts == 2)
13006 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000013007 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000013008 return (isMOVLMask(Mask, VT) ||
13009 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013010 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
13011 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000013012 }
13013 return false;
13014}
13015
13016//===----------------------------------------------------------------------===//
13017// X86 Scheduler Hooks
13018//===----------------------------------------------------------------------===//
13019
Michael Liaobe02a902012-11-08 07:28:54 +000013020/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000013021static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
13022 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000013023 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000013024
13025 const BasicBlock *BB = MBB->getBasicBlock();
13026 MachineFunction::iterator I = MBB;
13027 ++I;
13028
13029 // For the v = xbegin(), we generate
13030 //
13031 // thisMBB:
13032 // xbegin sinkMBB
13033 //
13034 // mainMBB:
13035 // eax = -1
13036 //
13037 // sinkMBB:
13038 // v = eax
13039
13040 MachineBasicBlock *thisMBB = MBB;
13041 MachineFunction *MF = MBB->getParent();
13042 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13043 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13044 MF->insert(I, mainMBB);
13045 MF->insert(I, sinkMBB);
13046
13047 // Transfer the remainder of BB and its successor edges to sinkMBB.
13048 sinkMBB->splice(sinkMBB->begin(), MBB,
13049 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13050 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13051
13052 // thisMBB:
13053 // xbegin sinkMBB
13054 // # fallthrough to mainMBB
13055 // # abortion to sinkMBB
13056 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
13057 thisMBB->addSuccessor(mainMBB);
13058 thisMBB->addSuccessor(sinkMBB);
13059
13060 // mainMBB:
13061 // EAX = -1
13062 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
13063 mainMBB->addSuccessor(sinkMBB);
13064
13065 // sinkMBB:
13066 // EAX is live into the sinkMBB
13067 sinkMBB->addLiveIn(X86::EAX);
13068 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13069 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13070 .addReg(X86::EAX);
13071
13072 MI->eraseFromParent();
13073 return sinkMBB;
13074}
13075
Michael Liaob118a072012-09-20 03:06:15 +000013076// Get CMPXCHG opcode for the specified data type.
13077static unsigned getCmpXChgOpcode(EVT VT) {
13078 switch (VT.getSimpleVT().SimpleTy) {
13079 case MVT::i8: return X86::LCMPXCHG8;
13080 case MVT::i16: return X86::LCMPXCHG16;
13081 case MVT::i32: return X86::LCMPXCHG32;
13082 case MVT::i64: return X86::LCMPXCHG64;
13083 default:
13084 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000013085 }
Michael Liaob118a072012-09-20 03:06:15 +000013086 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000013087}
13088
Michael Liaob118a072012-09-20 03:06:15 +000013089// Get LOAD opcode for the specified data type.
13090static unsigned getLoadOpcode(EVT VT) {
13091 switch (VT.getSimpleVT().SimpleTy) {
13092 case MVT::i8: return X86::MOV8rm;
13093 case MVT::i16: return X86::MOV16rm;
13094 case MVT::i32: return X86::MOV32rm;
13095 case MVT::i64: return X86::MOV64rm;
13096 default:
13097 break;
13098 }
13099 llvm_unreachable("Invalid operand size!");
13100}
13101
13102// Get opcode of the non-atomic one from the specified atomic instruction.
13103static unsigned getNonAtomicOpcode(unsigned Opc) {
13104 switch (Opc) {
13105 case X86::ATOMAND8: return X86::AND8rr;
13106 case X86::ATOMAND16: return X86::AND16rr;
13107 case X86::ATOMAND32: return X86::AND32rr;
13108 case X86::ATOMAND64: return X86::AND64rr;
13109 case X86::ATOMOR8: return X86::OR8rr;
13110 case X86::ATOMOR16: return X86::OR16rr;
13111 case X86::ATOMOR32: return X86::OR32rr;
13112 case X86::ATOMOR64: return X86::OR64rr;
13113 case X86::ATOMXOR8: return X86::XOR8rr;
13114 case X86::ATOMXOR16: return X86::XOR16rr;
13115 case X86::ATOMXOR32: return X86::XOR32rr;
13116 case X86::ATOMXOR64: return X86::XOR64rr;
13117 }
13118 llvm_unreachable("Unhandled atomic-load-op opcode!");
13119}
13120
13121// Get opcode of the non-atomic one from the specified atomic instruction with
13122// extra opcode.
13123static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
13124 unsigned &ExtraOpc) {
13125 switch (Opc) {
13126 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
13127 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
13128 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
13129 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013130 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013131 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
13132 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
13133 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013134 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013135 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
13136 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
13137 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013138 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013139 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
13140 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
13141 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013142 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013143 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
13144 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
13145 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
13146 }
13147 llvm_unreachable("Unhandled atomic-load-op opcode!");
13148}
13149
13150// Get opcode of the non-atomic one from the specified atomic instruction for
13151// 64-bit data type on 32-bit target.
13152static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
13153 switch (Opc) {
13154 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
13155 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
13156 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
13157 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
13158 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
13159 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013160 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
13161 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
13162 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
13163 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000013164 }
13165 llvm_unreachable("Unhandled atomic-load-op opcode!");
13166}
13167
13168// Get opcode of the non-atomic one from the specified atomic instruction for
13169// 64-bit data type on 32-bit target with extra opcode.
13170static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
13171 unsigned &HiOpc,
13172 unsigned &ExtraOpc) {
13173 switch (Opc) {
13174 case X86::ATOMNAND6432:
13175 ExtraOpc = X86::NOT32r;
13176 HiOpc = X86::AND32rr;
13177 return X86::AND32rr;
13178 }
13179 llvm_unreachable("Unhandled atomic-load-op opcode!");
13180}
13181
13182// Get pseudo CMOV opcode from the specified data type.
13183static unsigned getPseudoCMOVOpc(EVT VT) {
13184 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000013185 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000013186 case MVT::i16: return X86::CMOV_GR16;
13187 case MVT::i32: return X86::CMOV_GR32;
13188 default:
13189 break;
13190 }
13191 llvm_unreachable("Unknown CMOV opcode!");
13192}
13193
13194// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
13195// They will be translated into a spin-loop or compare-exchange loop from
13196//
13197// ...
13198// dst = atomic-fetch-op MI.addr, MI.val
13199// ...
13200//
13201// to
13202//
13203// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013204// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000013205// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013206// t4 = phi(t1, t3 / loop)
13207// t2 = OP MI.val, t4
13208// EAX = t4
13209// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
13210// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013211// JNE loop
13212// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013213// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013214// ...
Mon P Wang63307c32008-05-05 19:05:59 +000013215MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000013216X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
13217 MachineBasicBlock *MBB) const {
13218 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13219 DebugLoc DL = MI->getDebugLoc();
13220
13221 MachineFunction *MF = MBB->getParent();
13222 MachineRegisterInfo &MRI = MF->getRegInfo();
13223
13224 const BasicBlock *BB = MBB->getBasicBlock();
13225 MachineFunction::iterator I = MBB;
13226 ++I;
13227
Michael Liao13d08bf2013-01-22 21:47:38 +000013228 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000013229 "Unexpected number of operands");
13230
13231 assert(MI->hasOneMemOperand() &&
13232 "Expected atomic-load-op to have one memoperand");
13233
13234 // Memory Reference
13235 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13236 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13237
13238 unsigned DstReg, SrcReg;
13239 unsigned MemOpndSlot;
13240
13241 unsigned CurOp = 0;
13242
13243 DstReg = MI->getOperand(CurOp++).getReg();
13244 MemOpndSlot = CurOp;
13245 CurOp += X86::AddrNumOperands;
13246 SrcReg = MI->getOperand(CurOp++).getReg();
13247
13248 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000013249 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000013250 unsigned t1 = MRI.createVirtualRegister(RC);
13251 unsigned t2 = MRI.createVirtualRegister(RC);
13252 unsigned t3 = MRI.createVirtualRegister(RC);
13253 unsigned t4 = MRI.createVirtualRegister(RC);
13254 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000013255
13256 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
13257 unsigned LOADOpc = getLoadOpcode(VT);
13258
13259 // For the atomic load-arith operator, we generate
13260 //
13261 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013262 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000013263 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013264 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013265 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013266 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013267 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013268 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013269 // JNE mainMBB
13270 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013271 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013272
13273 MachineBasicBlock *thisMBB = MBB;
13274 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13275 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13276 MF->insert(I, mainMBB);
13277 MF->insert(I, sinkMBB);
13278
13279 MachineInstrBuilder MIB;
13280
13281 // Transfer the remainder of BB and its successor edges to sinkMBB.
13282 sinkMBB->splice(sinkMBB->begin(), MBB,
13283 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13284 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13285
13286 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013287 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13288 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13289 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13290 if (NewMO.isReg())
13291 NewMO.setIsKill(false);
13292 MIB.addOperand(NewMO);
13293 }
13294 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13295 unsigned flags = (*MMOI)->getFlags();
13296 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13297 MachineMemOperand *MMO =
13298 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13299 (*MMOI)->getSize(),
13300 (*MMOI)->getBaseAlignment(),
13301 (*MMOI)->getTBAAInfo(),
13302 (*MMOI)->getRanges());
13303 MIB.addMemOperand(MMO);
13304 }
Michael Liaob118a072012-09-20 03:06:15 +000013305
13306 thisMBB->addSuccessor(mainMBB);
13307
13308 // mainMBB:
13309 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013310
Michael Liaoc537f792013-03-06 00:17:04 +000013311 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013312 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13313 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013314
Michael Liaob118a072012-09-20 03:06:15 +000013315 unsigned Opc = MI->getOpcode();
13316 switch (Opc) {
13317 default:
13318 llvm_unreachable("Unhandled atomic-load-op opcode!");
13319 case X86::ATOMAND8:
13320 case X86::ATOMAND16:
13321 case X86::ATOMAND32:
13322 case X86::ATOMAND64:
13323 case X86::ATOMOR8:
13324 case X86::ATOMOR16:
13325 case X86::ATOMOR32:
13326 case X86::ATOMOR64:
13327 case X86::ATOMXOR8:
13328 case X86::ATOMXOR16:
13329 case X86::ATOMXOR32:
13330 case X86::ATOMXOR64: {
13331 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013332 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13333 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013334 break;
13335 }
13336 case X86::ATOMNAND8:
13337 case X86::ATOMNAND16:
13338 case X86::ATOMNAND32:
13339 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013340 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013341 unsigned NOTOpc;
13342 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013343 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13344 .addReg(t4);
13345 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013346 break;
13347 }
Michael Liao08382492012-09-21 03:00:17 +000013348 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013349 case X86::ATOMMAX16:
13350 case X86::ATOMMAX32:
13351 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013352 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013353 case X86::ATOMMIN16:
13354 case X86::ATOMMIN32:
13355 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013356 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013357 case X86::ATOMUMAX16:
13358 case X86::ATOMUMAX32:
13359 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013360 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013361 case X86::ATOMUMIN16:
13362 case X86::ATOMUMIN32:
13363 case X86::ATOMUMIN64: {
13364 unsigned CMPOpc;
13365 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13366
13367 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13368 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013369 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013370
13371 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013372 if (VT != MVT::i8) {
13373 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013374 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013375 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013376 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013377 } else {
13378 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013379 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13380 const TargetRegisterClass *RC32 =
13381 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013382 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13383 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013384 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013385
13386 unsigned Undef = MRI.createVirtualRegister(RC32);
13387 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13388
13389 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13390 .addReg(Undef)
13391 .addReg(SrcReg)
13392 .addImm(X86::sub_8bit);
13393 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13394 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013395 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013396 .addImm(X86::sub_8bit);
13397
Michael Liaoc537f792013-03-06 00:17:04 +000013398 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013399 .addReg(SrcReg32)
13400 .addReg(AccReg32);
13401
Michael Liaoc537f792013-03-06 00:17:04 +000013402 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13403 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013404 }
Michael Liaob118a072012-09-20 03:06:15 +000013405 } else {
13406 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013407 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013408 "Invalid atomic-load-op transformation!");
13409 unsigned SelOpc = getPseudoCMOVOpc(VT);
13410 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13411 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013412 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13413 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013414 .addImm(CC);
13415 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013416 // Replace the original PHI node as mainMBB is changed after CMOV
13417 // lowering.
13418 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13419 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13420 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013421 }
13422 break;
13423 }
13424 }
13425
Michael Liaoc537f792013-03-06 00:17:04 +000013426 // Copy PhyReg back from virtual register.
13427 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13428 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013429
13430 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013431 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13432 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13433 if (NewMO.isReg())
13434 NewMO.setIsKill(false);
13435 MIB.addOperand(NewMO);
13436 }
13437 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013438 MIB.setMemRefs(MMOBegin, MMOEnd);
13439
Michael Liaoc537f792013-03-06 00:17:04 +000013440 // Copy PhyReg back to virtual register.
13441 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13442 .addReg(PhyReg);
13443
Michael Liaob118a072012-09-20 03:06:15 +000013444 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13445
13446 mainMBB->addSuccessor(origMainMBB);
13447 mainMBB->addSuccessor(sinkMBB);
13448
13449 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013450 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13451 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013452 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013453
13454 MI->eraseFromParent();
13455 return sinkMBB;
13456}
13457
13458// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13459// instructions. They will be translated into a spin-loop or compare-exchange
13460// loop from
13461//
13462// ...
13463// dst = atomic-fetch-op MI.addr, MI.val
13464// ...
13465//
13466// to
13467//
13468// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013469// t1L = LOAD [MI.addr + 0]
13470// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013471// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013472// t4L = phi(t1L, t3L / loop)
13473// t4H = phi(t1H, t3H / loop)
13474// t2L = OP MI.val.lo, t4L
13475// t2H = OP MI.val.hi, t4H
13476// EAX = t4L
13477// EDX = t4H
13478// EBX = t2L
13479// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013480// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013481// t3L = EAX
13482// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013483// JNE loop
13484// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013485// dstL = t3L
13486// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013487// ...
13488MachineBasicBlock *
13489X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13490 MachineBasicBlock *MBB) const {
13491 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13492 DebugLoc DL = MI->getDebugLoc();
13493
13494 MachineFunction *MF = MBB->getParent();
13495 MachineRegisterInfo &MRI = MF->getRegInfo();
13496
13497 const BasicBlock *BB = MBB->getBasicBlock();
13498 MachineFunction::iterator I = MBB;
13499 ++I;
13500
Michael Liao13d08bf2013-01-22 21:47:38 +000013501 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013502 "Unexpected number of operands");
13503
13504 assert(MI->hasOneMemOperand() &&
13505 "Expected atomic-load-op32 to have one memoperand");
13506
13507 // Memory Reference
13508 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13509 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13510
13511 unsigned DstLoReg, DstHiReg;
13512 unsigned SrcLoReg, SrcHiReg;
13513 unsigned MemOpndSlot;
13514
13515 unsigned CurOp = 0;
13516
13517 DstLoReg = MI->getOperand(CurOp++).getReg();
13518 DstHiReg = MI->getOperand(CurOp++).getReg();
13519 MemOpndSlot = CurOp;
13520 CurOp += X86::AddrNumOperands;
13521 SrcLoReg = MI->getOperand(CurOp++).getReg();
13522 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013523
Craig Topperc9099502012-04-20 06:31:50 +000013524 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013525 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013526
Michael Liaoc537f792013-03-06 00:17:04 +000013527 unsigned t1L = MRI.createVirtualRegister(RC);
13528 unsigned t1H = MRI.createVirtualRegister(RC);
13529 unsigned t2L = MRI.createVirtualRegister(RC);
13530 unsigned t2H = MRI.createVirtualRegister(RC);
13531 unsigned t3L = MRI.createVirtualRegister(RC);
13532 unsigned t3H = MRI.createVirtualRegister(RC);
13533 unsigned t4L = MRI.createVirtualRegister(RC);
13534 unsigned t4H = MRI.createVirtualRegister(RC);
13535
Michael Liaob118a072012-09-20 03:06:15 +000013536 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13537 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013538
Michael Liaob118a072012-09-20 03:06:15 +000013539 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013540 //
Michael Liaob118a072012-09-20 03:06:15 +000013541 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013542 // t1L = LOAD [MI.addr + 0]
13543 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013544 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013545 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13546 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13547 // t2L = OP MI.val.lo, t4L
13548 // t2H = OP MI.val.hi, t4H
13549 // EBX = t2L
13550 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013551 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013552 // t3L = EAX
13553 // t3H = EDX
13554 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013555 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013556 // dstL = t3L
13557 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013558
Mon P Wang63307c32008-05-05 19:05:59 +000013559 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013560 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13561 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13562 MF->insert(I, mainMBB);
13563 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013564
Michael Liaob118a072012-09-20 03:06:15 +000013565 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013566
Michael Liaob118a072012-09-20 03:06:15 +000013567 // Transfer the remainder of BB and its successor edges to sinkMBB.
13568 sinkMBB->splice(sinkMBB->begin(), MBB,
13569 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13570 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013571
Michael Liaob118a072012-09-20 03:06:15 +000013572 // thisMBB:
13573 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013574 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013575 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013576 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13577 if (NewMO.isReg())
13578 NewMO.setIsKill(false);
13579 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013580 }
Michael Liaoc537f792013-03-06 00:17:04 +000013581 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13582 unsigned flags = (*MMOI)->getFlags();
13583 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13584 MachineMemOperand *MMO =
13585 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13586 (*MMOI)->getSize(),
13587 (*MMOI)->getBaseAlignment(),
13588 (*MMOI)->getTBAAInfo(),
13589 (*MMOI)->getRanges());
13590 MIB.addMemOperand(MMO);
13591 };
13592 MachineInstr *LowMI = MIB;
13593
13594 // Hi
13595 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13596 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13597 if (i == X86::AddrDisp) {
13598 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13599 } else {
13600 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13601 if (NewMO.isReg())
13602 NewMO.setIsKill(false);
13603 MIB.addOperand(NewMO);
13604 }
13605 }
13606 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013607
Michael Liaob118a072012-09-20 03:06:15 +000013608 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013609
Michael Liaob118a072012-09-20 03:06:15 +000013610 // mainMBB:
13611 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013612
Michael Liaoc537f792013-03-06 00:17:04 +000013613 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013614 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13615 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13616 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13617 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013618
Michael Liaob118a072012-09-20 03:06:15 +000013619 unsigned Opc = MI->getOpcode();
13620 switch (Opc) {
13621 default:
13622 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13623 case X86::ATOMAND6432:
13624 case X86::ATOMOR6432:
13625 case X86::ATOMXOR6432:
13626 case X86::ATOMADD6432:
13627 case X86::ATOMSUB6432: {
13628 unsigned HiOpc;
13629 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013630 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13631 .addReg(SrcLoReg);
13632 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13633 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013634 break;
13635 }
13636 case X86::ATOMNAND6432: {
13637 unsigned HiOpc, NOTOpc;
13638 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013639 unsigned TmpL = MRI.createVirtualRegister(RC);
13640 unsigned TmpH = MRI.createVirtualRegister(RC);
13641 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13642 .addReg(t4L);
13643 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13644 .addReg(t4H);
13645 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13646 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013647 break;
13648 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013649 case X86::ATOMMAX6432:
13650 case X86::ATOMMIN6432:
13651 case X86::ATOMUMAX6432:
13652 case X86::ATOMUMIN6432: {
13653 unsigned HiOpc;
13654 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13655 unsigned cL = MRI.createVirtualRegister(RC8);
13656 unsigned cH = MRI.createVirtualRegister(RC8);
13657 unsigned cL32 = MRI.createVirtualRegister(RC);
13658 unsigned cH32 = MRI.createVirtualRegister(RC);
13659 unsigned cc = MRI.createVirtualRegister(RC);
13660 // cl := cmp src_lo, lo
13661 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013662 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013663 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13664 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13665 // ch := cmp src_hi, hi
13666 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013667 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013668 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13669 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13670 // cc := if (src_hi == hi) ? cl : ch;
13671 if (Subtarget->hasCMov()) {
13672 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13673 .addReg(cH32).addReg(cL32);
13674 } else {
13675 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13676 .addReg(cH32).addReg(cL32)
13677 .addImm(X86::COND_E);
13678 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13679 }
13680 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13681 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013682 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13683 .addReg(SrcLoReg).addReg(t4L);
13684 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13685 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013686 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013687 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13688 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013689 .addImm(X86::COND_NE);
13690 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013691 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13692 // 2nd CMOV lowering.
13693 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013694 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13695 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013696 .addImm(X86::COND_NE);
13697 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013698 // Replace the original PHI node as mainMBB is changed after CMOV
13699 // lowering.
13700 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13701 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13702 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13703 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13704 PhiL->eraseFromParent();
13705 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013706 }
13707 break;
13708 }
Michael Liaob118a072012-09-20 03:06:15 +000013709 case X86::ATOMSWAP6432: {
13710 unsigned HiOpc;
13711 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013712 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13713 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013714 break;
13715 }
13716 }
Mon P Wang63307c32008-05-05 19:05:59 +000013717
Michael Liaob118a072012-09-20 03:06:15 +000013718 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013719 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13720 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013721 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013722 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13723 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013724
Michael Liaob118a072012-09-20 03:06:15 +000013725 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013726 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13727 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13728 if (NewMO.isReg())
13729 NewMO.setIsKill(false);
13730 MIB.addOperand(NewMO);
13731 }
Michael Liaob118a072012-09-20 03:06:15 +000013732 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013733
Michael Liaoc537f792013-03-06 00:17:04 +000013734 // Copy EDX:EAX back to t3H:t3L
13735 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13736 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13737
Michael Liaob118a072012-09-20 03:06:15 +000013738 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013739
Michael Liaob118a072012-09-20 03:06:15 +000013740 mainMBB->addSuccessor(origMainMBB);
13741 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013742
Michael Liaob118a072012-09-20 03:06:15 +000013743 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013744 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13745 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013746 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013747 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13748 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013749 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013750
Michael Liaob118a072012-09-20 03:06:15 +000013751 MI->eraseFromParent();
13752 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013753}
13754
Eric Christopherf83a5de2009-08-27 18:08:16 +000013755// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013756// or XMM0_V32I8 in AVX all of this code can be replaced with that
13757// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013758static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13759 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013760 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013761 switch (MI->getOpcode()) {
13762 default: llvm_unreachable("illegal opcode!");
13763 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13764 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13765 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13766 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13767 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13768 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13769 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13770 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013771 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013772
Craig Topper8aae8dd2012-11-10 08:57:41 +000013773 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013774 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013775
Craig Topper52ea2452012-11-10 09:25:36 +000013776 unsigned NumArgs = MI->getNumOperands();
13777 for (unsigned i = 1; i < NumArgs; ++i) {
13778 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013779 if (!(Op.isReg() && Op.isImplicit()))
13780 MIB.addOperand(Op);
13781 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013782 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013783 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13784
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013785 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013786 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013787 .addReg(X86::XMM0);
13788
Dan Gohman14152b42010-07-06 20:24:04 +000013789 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013790 return BB;
13791}
13792
Craig Topper9c7ae012012-11-10 01:23:36 +000013793// FIXME: Custom handling because TableGen doesn't support multiple implicit
13794// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013795static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13796 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013797 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013798 switch (MI->getOpcode()) {
13799 default: llvm_unreachable("illegal opcode!");
13800 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13801 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13802 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13803 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13804 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13805 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13806 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13807 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013808 }
13809
Craig Topper8aae8dd2012-11-10 08:57:41 +000013810 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013811 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013812
Craig Topper52ea2452012-11-10 09:25:36 +000013813 unsigned NumArgs = MI->getNumOperands(); // remove the results
13814 for (unsigned i = 1; i < NumArgs; ++i) {
13815 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013816 if (!(Op.isReg() && Op.isImplicit()))
13817 MIB.addOperand(Op);
13818 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013819 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013820 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13821
13822 BuildMI(*BB, MI, dl,
13823 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13824 .addReg(X86::ECX);
13825
13826 MI->eraseFromParent();
13827 return BB;
13828}
13829
Craig Topper2da36912012-11-11 22:45:02 +000013830static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13831 const TargetInstrInfo *TII,
13832 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013833 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013834
Eric Christopher228232b2010-11-30 07:20:12 +000013835 // Address into RAX/EAX, other two args into ECX, EDX.
13836 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13837 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13838 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13839 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013840 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013841
Eric Christopher228232b2010-11-30 07:20:12 +000013842 unsigned ValOps = X86::AddrNumOperands;
13843 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13844 .addReg(MI->getOperand(ValOps).getReg());
13845 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13846 .addReg(MI->getOperand(ValOps+1).getReg());
13847
13848 // The instruction doesn't actually take any operands though.
13849 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013850
Eric Christopher228232b2010-11-30 07:20:12 +000013851 MI->eraseFromParent(); // The pseudo is gone now.
13852 return BB;
13853}
13854
13855MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013856X86TargetLowering::EmitVAARG64WithCustomInserter(
13857 MachineInstr *MI,
13858 MachineBasicBlock *MBB) const {
13859 // Emit va_arg instruction on X86-64.
13860
13861 // Operands to this pseudo-instruction:
13862 // 0 ) Output : destination address (reg)
13863 // 1-5) Input : va_list address (addr, i64mem)
13864 // 6 ) ArgSize : Size (in bytes) of vararg type
13865 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13866 // 8 ) Align : Alignment of type
13867 // 9 ) EFLAGS (implicit-def)
13868
13869 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13870 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13871
13872 unsigned DestReg = MI->getOperand(0).getReg();
13873 MachineOperand &Base = MI->getOperand(1);
13874 MachineOperand &Scale = MI->getOperand(2);
13875 MachineOperand &Index = MI->getOperand(3);
13876 MachineOperand &Disp = MI->getOperand(4);
13877 MachineOperand &Segment = MI->getOperand(5);
13878 unsigned ArgSize = MI->getOperand(6).getImm();
13879 unsigned ArgMode = MI->getOperand(7).getImm();
13880 unsigned Align = MI->getOperand(8).getImm();
13881
13882 // Memory Reference
13883 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13884 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13885 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13886
13887 // Machine Information
13888 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13889 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13890 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13891 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13892 DebugLoc DL = MI->getDebugLoc();
13893
13894 // struct va_list {
13895 // i32 gp_offset
13896 // i32 fp_offset
13897 // i64 overflow_area (address)
13898 // i64 reg_save_area (address)
13899 // }
13900 // sizeof(va_list) = 24
13901 // alignment(va_list) = 8
13902
13903 unsigned TotalNumIntRegs = 6;
13904 unsigned TotalNumXMMRegs = 8;
13905 bool UseGPOffset = (ArgMode == 1);
13906 bool UseFPOffset = (ArgMode == 2);
13907 unsigned MaxOffset = TotalNumIntRegs * 8 +
13908 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13909
13910 /* Align ArgSize to a multiple of 8 */
13911 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13912 bool NeedsAlign = (Align > 8);
13913
13914 MachineBasicBlock *thisMBB = MBB;
13915 MachineBasicBlock *overflowMBB;
13916 MachineBasicBlock *offsetMBB;
13917 MachineBasicBlock *endMBB;
13918
13919 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13920 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13921 unsigned OffsetReg = 0;
13922
13923 if (!UseGPOffset && !UseFPOffset) {
13924 // If we only pull from the overflow region, we don't create a branch.
13925 // We don't need to alter control flow.
13926 OffsetDestReg = 0; // unused
13927 OverflowDestReg = DestReg;
13928
13929 offsetMBB = NULL;
13930 overflowMBB = thisMBB;
13931 endMBB = thisMBB;
13932 } else {
13933 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13934 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13935 // If not, pull from overflow_area. (branch to overflowMBB)
13936 //
13937 // thisMBB
13938 // | .
13939 // | .
13940 // offsetMBB overflowMBB
13941 // | .
13942 // | .
13943 // endMBB
13944
13945 // Registers for the PHI in endMBB
13946 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13947 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13948
13949 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13950 MachineFunction *MF = MBB->getParent();
13951 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13952 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13953 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13954
13955 MachineFunction::iterator MBBIter = MBB;
13956 ++MBBIter;
13957
13958 // Insert the new basic blocks
13959 MF->insert(MBBIter, offsetMBB);
13960 MF->insert(MBBIter, overflowMBB);
13961 MF->insert(MBBIter, endMBB);
13962
13963 // Transfer the remainder of MBB and its successor edges to endMBB.
13964 endMBB->splice(endMBB->begin(), thisMBB,
13965 llvm::next(MachineBasicBlock::iterator(MI)),
13966 thisMBB->end());
13967 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13968
13969 // Make offsetMBB and overflowMBB successors of thisMBB
13970 thisMBB->addSuccessor(offsetMBB);
13971 thisMBB->addSuccessor(overflowMBB);
13972
13973 // endMBB is a successor of both offsetMBB and overflowMBB
13974 offsetMBB->addSuccessor(endMBB);
13975 overflowMBB->addSuccessor(endMBB);
13976
13977 // Load the offset value into a register
13978 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13979 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13980 .addOperand(Base)
13981 .addOperand(Scale)
13982 .addOperand(Index)
13983 .addDisp(Disp, UseFPOffset ? 4 : 0)
13984 .addOperand(Segment)
13985 .setMemRefs(MMOBegin, MMOEnd);
13986
13987 // Check if there is enough room left to pull this argument.
13988 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13989 .addReg(OffsetReg)
13990 .addImm(MaxOffset + 8 - ArgSizeA8);
13991
13992 // Branch to "overflowMBB" if offset >= max
13993 // Fall through to "offsetMBB" otherwise
13994 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13995 .addMBB(overflowMBB);
13996 }
13997
13998 // In offsetMBB, emit code to use the reg_save_area.
13999 if (offsetMBB) {
14000 assert(OffsetReg != 0);
14001
14002 // Read the reg_save_area address.
14003 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
14004 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
14005 .addOperand(Base)
14006 .addOperand(Scale)
14007 .addOperand(Index)
14008 .addDisp(Disp, 16)
14009 .addOperand(Segment)
14010 .setMemRefs(MMOBegin, MMOEnd);
14011
14012 // Zero-extend the offset
14013 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
14014 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
14015 .addImm(0)
14016 .addReg(OffsetReg)
14017 .addImm(X86::sub_32bit);
14018
14019 // Add the offset to the reg_save_area to get the final address.
14020 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
14021 .addReg(OffsetReg64)
14022 .addReg(RegSaveReg);
14023
14024 // Compute the offset for the next argument
14025 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
14026 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
14027 .addReg(OffsetReg)
14028 .addImm(UseFPOffset ? 16 : 8);
14029
14030 // Store it back into the va_list.
14031 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
14032 .addOperand(Base)
14033 .addOperand(Scale)
14034 .addOperand(Index)
14035 .addDisp(Disp, UseFPOffset ? 4 : 0)
14036 .addOperand(Segment)
14037 .addReg(NextOffsetReg)
14038 .setMemRefs(MMOBegin, MMOEnd);
14039
14040 // Jump to endMBB
14041 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
14042 .addMBB(endMBB);
14043 }
14044
14045 //
14046 // Emit code to use overflow area
14047 //
14048
14049 // Load the overflow_area address into a register.
14050 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
14051 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
14052 .addOperand(Base)
14053 .addOperand(Scale)
14054 .addOperand(Index)
14055 .addDisp(Disp, 8)
14056 .addOperand(Segment)
14057 .setMemRefs(MMOBegin, MMOEnd);
14058
14059 // If we need to align it, do so. Otherwise, just copy the address
14060 // to OverflowDestReg.
14061 if (NeedsAlign) {
14062 // Align the overflow address
14063 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
14064 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
14065
14066 // aligned_addr = (addr + (align-1)) & ~(align-1)
14067 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
14068 .addReg(OverflowAddrReg)
14069 .addImm(Align-1);
14070
14071 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
14072 .addReg(TmpReg)
14073 .addImm(~(uint64_t)(Align-1));
14074 } else {
14075 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
14076 .addReg(OverflowAddrReg);
14077 }
14078
14079 // Compute the next overflow address after this argument.
14080 // (the overflow address should be kept 8-byte aligned)
14081 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
14082 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
14083 .addReg(OverflowDestReg)
14084 .addImm(ArgSizeA8);
14085
14086 // Store the new overflow address.
14087 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
14088 .addOperand(Base)
14089 .addOperand(Scale)
14090 .addOperand(Index)
14091 .addDisp(Disp, 8)
14092 .addOperand(Segment)
14093 .addReg(NextAddrReg)
14094 .setMemRefs(MMOBegin, MMOEnd);
14095
14096 // If we branched, emit the PHI to the front of endMBB.
14097 if (offsetMBB) {
14098 BuildMI(*endMBB, endMBB->begin(), DL,
14099 TII->get(X86::PHI), DestReg)
14100 .addReg(OffsetDestReg).addMBB(offsetMBB)
14101 .addReg(OverflowDestReg).addMBB(overflowMBB);
14102 }
14103
14104 // Erase the pseudo instruction
14105 MI->eraseFromParent();
14106
14107 return endMBB;
14108}
14109
14110MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000014111X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
14112 MachineInstr *MI,
14113 MachineBasicBlock *MBB) const {
14114 // Emit code to save XMM registers to the stack. The ABI says that the
14115 // number of registers to save is given in %al, so it's theoretically
14116 // possible to do an indirect jump trick to avoid saving all of them,
14117 // however this code takes a simpler approach and just executes all
14118 // of the stores if %al is non-zero. It's less code, and it's probably
14119 // easier on the hardware branch predictor, and stores aren't all that
14120 // expensive anyway.
14121
14122 // Create the new basic blocks. One block contains all the XMM stores,
14123 // and one block is the final destination regardless of whether any
14124 // stores were performed.
14125 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14126 MachineFunction *F = MBB->getParent();
14127 MachineFunction::iterator MBBIter = MBB;
14128 ++MBBIter;
14129 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
14130 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
14131 F->insert(MBBIter, XMMSaveMBB);
14132 F->insert(MBBIter, EndMBB);
14133
Dan Gohman14152b42010-07-06 20:24:04 +000014134 // Transfer the remainder of MBB and its successor edges to EndMBB.
14135 EndMBB->splice(EndMBB->begin(), MBB,
14136 llvm::next(MachineBasicBlock::iterator(MI)),
14137 MBB->end());
14138 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
14139
Dan Gohmand6708ea2009-08-15 01:38:56 +000014140 // The original block will now fall through to the XMM save block.
14141 MBB->addSuccessor(XMMSaveMBB);
14142 // The XMMSaveMBB will fall through to the end block.
14143 XMMSaveMBB->addSuccessor(EndMBB);
14144
14145 // Now add the instructions.
14146 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14147 DebugLoc DL = MI->getDebugLoc();
14148
14149 unsigned CountReg = MI->getOperand(0).getReg();
14150 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
14151 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
14152
14153 if (!Subtarget->isTargetWin64()) {
14154 // If %al is 0, branch around the XMM save block.
14155 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000014156 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014157 MBB->addSuccessor(EndMBB);
14158 }
14159
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014160 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000014161 // In the XMM save block, save all the XMM argument registers.
14162 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
14163 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000014164 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000014165 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000014166 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000014167 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000014168 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014169 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000014170 .addFrameIndex(RegSaveFrameIndex)
14171 .addImm(/*Scale=*/1)
14172 .addReg(/*IndexReg=*/0)
14173 .addImm(/*Disp=*/Offset)
14174 .addReg(/*Segment=*/0)
14175 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000014176 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014177 }
14178
Dan Gohman14152b42010-07-06 20:24:04 +000014179 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000014180
14181 return EndMBB;
14182}
Mon P Wang63307c32008-05-05 19:05:59 +000014183
Lang Hames6e3f7e42012-02-03 01:13:49 +000014184// The EFLAGS operand of SelectItr might be missing a kill marker
14185// because there were multiple uses of EFLAGS, and ISel didn't know
14186// which to mark. Figure out whether SelectItr should have had a
14187// kill marker, and set it if it should. Returns the correct kill
14188// marker value.
14189static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
14190 MachineBasicBlock* BB,
14191 const TargetRegisterInfo* TRI) {
14192 // Scan forward through BB for a use/def of EFLAGS.
14193 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
14194 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000014195 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014196 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000014197 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014198 if (mi.definesRegister(X86::EFLAGS))
14199 break; // Should have kill-flag - update below.
14200 }
14201
14202 // If we hit the end of the block, check whether EFLAGS is live into a
14203 // successor.
14204 if (miI == BB->end()) {
14205 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
14206 sEnd = BB->succ_end();
14207 sItr != sEnd; ++sItr) {
14208 MachineBasicBlock* succ = *sItr;
14209 if (succ->isLiveIn(X86::EFLAGS))
14210 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000014211 }
14212 }
14213
Lang Hames6e3f7e42012-02-03 01:13:49 +000014214 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
14215 // out. SelectMI should have a kill flag on EFLAGS.
14216 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000014217 return true;
14218}
14219
Evan Cheng60c07e12006-07-05 22:17:51 +000014220MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000014221X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014222 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000014223 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14224 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000014225
Chris Lattner52600972009-09-02 05:57:00 +000014226 // To "insert" a SELECT_CC instruction, we actually have to insert the
14227 // diamond control-flow pattern. The incoming instruction knows the
14228 // destination vreg to set, the condition code register to branch on, the
14229 // true/false values to select between, and a branch opcode to use.
14230 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14231 MachineFunction::iterator It = BB;
14232 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000014233
Chris Lattner52600972009-09-02 05:57:00 +000014234 // thisMBB:
14235 // ...
14236 // TrueVal = ...
14237 // cmpTY ccX, r1, r2
14238 // bCC copy1MBB
14239 // fallthrough --> copy0MBB
14240 MachineBasicBlock *thisMBB = BB;
14241 MachineFunction *F = BB->getParent();
14242 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
14243 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000014244 F->insert(It, copy0MBB);
14245 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000014246
Bill Wendling730c07e2010-06-25 20:48:10 +000014247 // If the EFLAGS register isn't dead in the terminator, then claim that it's
14248 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000014249 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
14250 if (!MI->killsRegister(X86::EFLAGS) &&
14251 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
14252 copy0MBB->addLiveIn(X86::EFLAGS);
14253 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000014254 }
14255
Dan Gohman14152b42010-07-06 20:24:04 +000014256 // Transfer the remainder of BB and its successor edges to sinkMBB.
14257 sinkMBB->splice(sinkMBB->begin(), BB,
14258 llvm::next(MachineBasicBlock::iterator(MI)),
14259 BB->end());
14260 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
14261
14262 // Add the true and fallthrough blocks as its successors.
14263 BB->addSuccessor(copy0MBB);
14264 BB->addSuccessor(sinkMBB);
14265
14266 // Create the conditional branch instruction.
14267 unsigned Opc =
14268 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14269 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14270
Chris Lattner52600972009-09-02 05:57:00 +000014271 // copy0MBB:
14272 // %FalseValue = ...
14273 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014274 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014275
Chris Lattner52600972009-09-02 05:57:00 +000014276 // sinkMBB:
14277 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14278 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014279 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14280 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014281 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14282 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14283
Dan Gohman14152b42010-07-06 20:24:04 +000014284 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014285 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014286}
14287
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014288MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014289X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14290 bool Is64Bit) const {
14291 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14292 DebugLoc DL = MI->getDebugLoc();
14293 MachineFunction *MF = BB->getParent();
14294 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14295
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014296 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014297
14298 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14299 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14300
14301 // BB:
14302 // ... [Till the alloca]
14303 // If stacklet is not large enough, jump to mallocMBB
14304 //
14305 // bumpMBB:
14306 // Allocate by subtracting from RSP
14307 // Jump to continueMBB
14308 //
14309 // mallocMBB:
14310 // Allocate by call to runtime
14311 //
14312 // continueMBB:
14313 // ...
14314 // [rest of original BB]
14315 //
14316
14317 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14318 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14319 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14320
14321 MachineRegisterInfo &MRI = MF->getRegInfo();
14322 const TargetRegisterClass *AddrRegClass =
14323 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14324
14325 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14326 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14327 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014328 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014329 sizeVReg = MI->getOperand(1).getReg(),
14330 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14331
14332 MachineFunction::iterator MBBIter = BB;
14333 ++MBBIter;
14334
14335 MF->insert(MBBIter, bumpMBB);
14336 MF->insert(MBBIter, mallocMBB);
14337 MF->insert(MBBIter, continueMBB);
14338
14339 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14340 (MachineBasicBlock::iterator(MI)), BB->end());
14341 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14342
14343 // Add code to the main basic block to check if the stack limit has been hit,
14344 // and if so, jump to mallocMBB otherwise to bumpMBB.
14345 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014346 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014347 .addReg(tmpSPVReg).addReg(sizeVReg);
14348 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014349 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014350 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014351 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14352
14353 // bumpMBB simply decreases the stack pointer, since we know the current
14354 // stacklet has enough space.
14355 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014356 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014357 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014358 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014359 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14360
14361 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014362 const uint32_t *RegMask =
14363 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014364 if (Is64Bit) {
14365 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14366 .addReg(sizeVReg);
14367 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014368 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014369 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014370 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014371 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014372 } else {
14373 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14374 .addImm(12);
14375 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14376 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014377 .addExternalSymbol("__morestack_allocate_stack_space")
14378 .addRegMask(RegMask)
14379 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014380 }
14381
14382 if (!Is64Bit)
14383 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14384 .addImm(16);
14385
14386 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14387 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14388 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14389
14390 // Set up the CFG correctly.
14391 BB->addSuccessor(bumpMBB);
14392 BB->addSuccessor(mallocMBB);
14393 mallocMBB->addSuccessor(continueMBB);
14394 bumpMBB->addSuccessor(continueMBB);
14395
14396 // Take care of the PHI nodes.
14397 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14398 MI->getOperand(0).getReg())
14399 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14400 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14401
14402 // Delete the original pseudo instruction.
14403 MI->eraseFromParent();
14404
14405 // And we're done.
14406 return continueMBB;
14407}
14408
14409MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014410X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014411 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014412 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14413 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014414
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014415 assert(!Subtarget->isTargetEnvMacho());
14416
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014417 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14418 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014419
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014420 if (Subtarget->isTargetWin64()) {
14421 if (Subtarget->isTargetCygMing()) {
14422 // ___chkstk(Mingw64):
14423 // Clobbers R10, R11, RAX and EFLAGS.
14424 // Updates RSP.
14425 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14426 .addExternalSymbol("___chkstk")
14427 .addReg(X86::RAX, RegState::Implicit)
14428 .addReg(X86::RSP, RegState::Implicit)
14429 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14430 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14431 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14432 } else {
14433 // __chkstk(MSVCRT): does not update stack pointer.
14434 // Clobbers R10, R11 and EFLAGS.
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014435 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14436 .addExternalSymbol("__chkstk")
14437 .addReg(X86::RAX, RegState::Implicit)
14438 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Nico Rieck40101102013-07-08 11:20:11 +000014439 // RAX has the offset to be subtracted from RSP.
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014440 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14441 .addReg(X86::RSP)
14442 .addReg(X86::RAX);
14443 }
14444 } else {
14445 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014446 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14447
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014448 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14449 .addExternalSymbol(StackProbeSymbol)
14450 .addReg(X86::EAX, RegState::Implicit)
14451 .addReg(X86::ESP, RegState::Implicit)
14452 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14453 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14454 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14455 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014456
Dan Gohman14152b42010-07-06 20:24:04 +000014457 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014458 return BB;
14459}
Chris Lattner52600972009-09-02 05:57:00 +000014460
14461MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014462X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14463 MachineBasicBlock *BB) const {
14464 // This is pretty easy. We're taking the value that we received from
14465 // our load from the relocation, sticking it in either RDI (x86-64)
14466 // or EAX and doing an indirect call. The return value will then
14467 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014468 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014469 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014470 DebugLoc DL = MI->getDebugLoc();
14471 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014472
14473 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014474 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014475
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014476 // Get a register mask for the lowered call.
14477 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14478 // proper register mask.
14479 const uint32_t *RegMask =
14480 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014481 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014482 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14483 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014484 .addReg(X86::RIP)
14485 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014486 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014487 MI->getOperand(3).getTargetFlags())
14488 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014489 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014490 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014491 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014492 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014493 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14494 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014495 .addReg(0)
14496 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014497 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014498 MI->getOperand(3).getTargetFlags())
14499 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014500 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014501 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014502 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014503 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014504 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14505 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014506 .addReg(TII->getGlobalBaseReg(F))
14507 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014508 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014509 MI->getOperand(3).getTargetFlags())
14510 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014511 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014512 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014513 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014514 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014515
Dan Gohman14152b42010-07-06 20:24:04 +000014516 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014517 return BB;
14518}
14519
14520MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014521X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14522 MachineBasicBlock *MBB) const {
14523 DebugLoc DL = MI->getDebugLoc();
14524 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14525
14526 MachineFunction *MF = MBB->getParent();
14527 MachineRegisterInfo &MRI = MF->getRegInfo();
14528
14529 const BasicBlock *BB = MBB->getBasicBlock();
14530 MachineFunction::iterator I = MBB;
14531 ++I;
14532
14533 // Memory Reference
14534 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14535 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14536
14537 unsigned DstReg;
14538 unsigned MemOpndSlot = 0;
14539
14540 unsigned CurOp = 0;
14541
14542 DstReg = MI->getOperand(CurOp++).getReg();
14543 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14544 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14545 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14546 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14547
14548 MemOpndSlot = CurOp;
14549
14550 MVT PVT = getPointerTy();
14551 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14552 "Invalid Pointer Size!");
14553
14554 // For v = setjmp(buf), we generate
14555 //
14556 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014557 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014558 // SjLjSetup restoreMBB
14559 //
14560 // mainMBB:
14561 // v_main = 0
14562 //
14563 // sinkMBB:
14564 // v = phi(main, restore)
14565 //
14566 // restoreMBB:
14567 // v_restore = 1
14568
14569 MachineBasicBlock *thisMBB = MBB;
14570 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14571 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14572 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14573 MF->insert(I, mainMBB);
14574 MF->insert(I, sinkMBB);
14575 MF->push_back(restoreMBB);
14576
14577 MachineInstrBuilder MIB;
14578
14579 // Transfer the remainder of BB and its successor edges to sinkMBB.
14580 sinkMBB->splice(sinkMBB->begin(), MBB,
14581 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14582 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14583
14584 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014585 unsigned PtrStoreOpc = 0;
14586 unsigned LabelReg = 0;
14587 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14588 Reloc::Model RM = getTargetMachine().getRelocationModel();
14589 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14590 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014591
Michael Liao281ae5a2012-10-17 02:22:27 +000014592 // Prepare IP either in reg or imm.
14593 if (!UseImmLabel) {
14594 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14595 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14596 LabelReg = MRI.createVirtualRegister(PtrRC);
14597 if (Subtarget->is64Bit()) {
14598 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14599 .addReg(X86::RIP)
14600 .addImm(0)
14601 .addReg(0)
14602 .addMBB(restoreMBB)
14603 .addReg(0);
14604 } else {
14605 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14606 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14607 .addReg(XII->getGlobalBaseReg(MF))
14608 .addImm(0)
14609 .addReg(0)
14610 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14611 .addReg(0);
14612 }
14613 } else
14614 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014615 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014616 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014617 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14618 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014619 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014620 else
14621 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14622 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014623 if (!UseImmLabel)
14624 MIB.addReg(LabelReg);
14625 else
14626 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014627 MIB.setMemRefs(MMOBegin, MMOEnd);
14628 // Setup
14629 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14630 .addMBB(restoreMBB);
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000014631
14632 const X86RegisterInfo *RegInfo =
14633 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liao6c0e04c2012-10-15 22:39:43 +000014634 MIB.addRegMask(RegInfo->getNoPreservedMask());
14635 thisMBB->addSuccessor(mainMBB);
14636 thisMBB->addSuccessor(restoreMBB);
14637
14638 // mainMBB:
14639 // EAX = 0
14640 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14641 mainMBB->addSuccessor(sinkMBB);
14642
14643 // sinkMBB:
14644 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14645 TII->get(X86::PHI), DstReg)
14646 .addReg(mainDstReg).addMBB(mainMBB)
14647 .addReg(restoreDstReg).addMBB(restoreMBB);
14648
14649 // restoreMBB:
14650 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14651 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14652 restoreMBB->addSuccessor(sinkMBB);
14653
14654 MI->eraseFromParent();
14655 return sinkMBB;
14656}
14657
14658MachineBasicBlock *
14659X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14660 MachineBasicBlock *MBB) const {
14661 DebugLoc DL = MI->getDebugLoc();
14662 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14663
14664 MachineFunction *MF = MBB->getParent();
14665 MachineRegisterInfo &MRI = MF->getRegInfo();
14666
14667 // Memory Reference
14668 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14669 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14670
14671 MVT PVT = getPointerTy();
14672 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14673 "Invalid Pointer Size!");
14674
14675 const TargetRegisterClass *RC =
14676 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14677 unsigned Tmp = MRI.createVirtualRegister(RC);
14678 // Since FP is only updated here but NOT referenced, it's treated as GPR.
Bill Wendlinga5e5ba62013-06-07 21:00:34 +000014679 const X86RegisterInfo *RegInfo =
14680 static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo());
Michael Liao6c0e04c2012-10-15 22:39:43 +000014681 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14682 unsigned SP = RegInfo->getStackRegister();
14683
14684 MachineInstrBuilder MIB;
14685
Michael Liao281ae5a2012-10-17 02:22:27 +000014686 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14687 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014688
14689 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14690 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14691
14692 // Reload FP
14693 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14694 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14695 MIB.addOperand(MI->getOperand(i));
14696 MIB.setMemRefs(MMOBegin, MMOEnd);
14697 // Reload IP
14698 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14699 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14700 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014701 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014702 else
14703 MIB.addOperand(MI->getOperand(i));
14704 }
14705 MIB.setMemRefs(MMOBegin, MMOEnd);
14706 // Reload SP
14707 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14708 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14709 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014710 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014711 else
14712 MIB.addOperand(MI->getOperand(i));
14713 }
14714 MIB.setMemRefs(MMOBegin, MMOEnd);
14715 // Jump
14716 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14717
14718 MI->eraseFromParent();
14719 return MBB;
14720}
14721
14722MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014723X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014724 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014725 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014726 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014727 case X86::TAILJMPd64:
14728 case X86::TAILJMPr64:
14729 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014730 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014731 case X86::TCRETURNdi64:
14732 case X86::TCRETURNri64:
14733 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014734 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014735 case X86::WIN_ALLOCA:
14736 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014737 case X86::SEG_ALLOCA_32:
14738 return EmitLoweredSegAlloca(MI, BB, false);
14739 case X86::SEG_ALLOCA_64:
14740 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014741 case X86::TLSCall_32:
14742 case X86::TLSCall_64:
14743 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014744 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014745 case X86::CMOV_FR32:
14746 case X86::CMOV_FR64:
14747 case X86::CMOV_V4F32:
14748 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014749 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014750 case X86::CMOV_V8F32:
14751 case X86::CMOV_V4F64:
14752 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014753 case X86::CMOV_GR16:
14754 case X86::CMOV_GR32:
14755 case X86::CMOV_RFP32:
14756 case X86::CMOV_RFP64:
14757 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014758 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014759
Dale Johannesen849f2142007-07-03 00:53:03 +000014760 case X86::FP32_TO_INT16_IN_MEM:
14761 case X86::FP32_TO_INT32_IN_MEM:
14762 case X86::FP32_TO_INT64_IN_MEM:
14763 case X86::FP64_TO_INT16_IN_MEM:
14764 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014765 case X86::FP64_TO_INT64_IN_MEM:
14766 case X86::FP80_TO_INT16_IN_MEM:
14767 case X86::FP80_TO_INT32_IN_MEM:
14768 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014769 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14770 DebugLoc DL = MI->getDebugLoc();
14771
Evan Cheng60c07e12006-07-05 22:17:51 +000014772 // Change the floating point control register to use "round towards zero"
14773 // mode when truncating to an integer value.
14774 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014775 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014776 addFrameReference(BuildMI(*BB, MI, DL,
14777 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014778
14779 // Load the old value of the high byte of the control word...
14780 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014781 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014782 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014783 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014784
14785 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014786 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014787 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014788
14789 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014790 addFrameReference(BuildMI(*BB, MI, DL,
14791 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014792
14793 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014794 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014795 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014796
14797 // Get the X86 opcode to use.
14798 unsigned Opc;
14799 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014800 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014801 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14802 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14803 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14804 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14805 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14806 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014807 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14808 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14809 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014810 }
14811
14812 X86AddressMode AM;
14813 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014814 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014815 AM.BaseType = X86AddressMode::RegBase;
14816 AM.Base.Reg = Op.getReg();
14817 } else {
14818 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014819 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014820 }
14821 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014822 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014823 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014824 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014825 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014826 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014827 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014828 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014829 AM.GV = Op.getGlobal();
14830 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014831 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014832 }
Dan Gohman14152b42010-07-06 20:24:04 +000014833 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014834 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014835
14836 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014837 addFrameReference(BuildMI(*BB, MI, DL,
14838 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014839
Dan Gohman14152b42010-07-06 20:24:04 +000014840 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014841 return BB;
14842 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014843 // String/text processing lowering.
14844 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014845 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014846 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014847 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014848 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014849 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014850 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014851 case X86::VPCMPESTRM128MEM:
14852 assert(Subtarget->hasSSE42() &&
14853 "Target must have SSE4.2 or AVX features enabled");
14854 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014855
14856 // String/text processing lowering.
14857 case X86::PCMPISTRIREG:
14858 case X86::VPCMPISTRIREG:
14859 case X86::PCMPISTRIMEM:
14860 case X86::VPCMPISTRIMEM:
14861 case X86::PCMPESTRIREG:
14862 case X86::VPCMPESTRIREG:
14863 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014864 case X86::VPCMPESTRIMEM:
14865 assert(Subtarget->hasSSE42() &&
14866 "Target must have SSE4.2 or AVX features enabled");
14867 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014868
Craig Topper8aae8dd2012-11-10 08:57:41 +000014869 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014870 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014871 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014872
Michael Liaobe02a902012-11-08 07:28:54 +000014873 // xbegin
14874 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014875 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014876
Craig Topper8aae8dd2012-11-10 08:57:41 +000014877 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014878 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014879 case X86::ATOMAND16:
14880 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014881 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014882 // Fall through
14883 case X86::ATOMOR8:
14884 case X86::ATOMOR16:
14885 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014886 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014887 // Fall through
14888 case X86::ATOMXOR16:
14889 case X86::ATOMXOR8:
14890 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014891 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014892 // Fall through
14893 case X86::ATOMNAND8:
14894 case X86::ATOMNAND16:
14895 case X86::ATOMNAND32:
14896 case X86::ATOMNAND64:
14897 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014898 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014899 case X86::ATOMMAX16:
14900 case X86::ATOMMAX32:
14901 case X86::ATOMMAX64:
14902 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014903 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014904 case X86::ATOMMIN16:
14905 case X86::ATOMMIN32:
14906 case X86::ATOMMIN64:
14907 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014908 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014909 case X86::ATOMUMAX16:
14910 case X86::ATOMUMAX32:
14911 case X86::ATOMUMAX64:
14912 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014913 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014914 case X86::ATOMUMIN16:
14915 case X86::ATOMUMIN32:
14916 case X86::ATOMUMIN64:
14917 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014918
14919 // This group does 64-bit operations on a 32-bit host.
14920 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014921 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014922 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014923 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014924 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014925 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014926 case X86::ATOMMAX6432:
14927 case X86::ATOMMIN6432:
14928 case X86::ATOMUMAX6432:
14929 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014930 case X86::ATOMSWAP6432:
14931 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014932
Dan Gohmand6708ea2009-08-15 01:38:56 +000014933 case X86::VASTART_SAVE_XMM_REGS:
14934 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014935
14936 case X86::VAARG_64:
14937 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014938
14939 case X86::EH_SjLj_SetJmp32:
14940 case X86::EH_SjLj_SetJmp64:
14941 return emitEHSjLjSetJmp(MI, BB);
14942
14943 case X86::EH_SjLj_LongJmp32:
14944 case X86::EH_SjLj_LongJmp64:
14945 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014946 }
14947}
14948
14949//===----------------------------------------------------------------------===//
14950// X86 Optimization Hooks
14951//===----------------------------------------------------------------------===//
14952
Dan Gohman475871a2008-07-27 21:46:04 +000014953void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014954 APInt &KnownZero,
14955 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014956 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014957 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014958 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014959 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014960 assert((Opc >= ISD::BUILTIN_OP_END ||
14961 Opc == ISD::INTRINSIC_WO_CHAIN ||
14962 Opc == ISD::INTRINSIC_W_CHAIN ||
14963 Opc == ISD::INTRINSIC_VOID) &&
14964 "Should use MaskedValueIsZero if you don't know whether Op"
14965 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014966
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014967 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014968 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014969 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014970 case X86ISD::ADD:
14971 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014972 case X86ISD::ADC:
14973 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014974 case X86ISD::SMUL:
14975 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014976 case X86ISD::INC:
14977 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014978 case X86ISD::OR:
14979 case X86ISD::XOR:
14980 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014981 // These nodes' second result is a boolean.
14982 if (Op.getResNo() == 0)
14983 break;
14984 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014985 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014986 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014987 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014988 case ISD::INTRINSIC_WO_CHAIN: {
14989 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14990 unsigned NumLoBits = 0;
14991 switch (IntId) {
14992 default: break;
14993 case Intrinsic::x86_sse_movmsk_ps:
14994 case Intrinsic::x86_avx_movmsk_ps_256:
14995 case Intrinsic::x86_sse2_movmsk_pd:
14996 case Intrinsic::x86_avx_movmsk_pd_256:
14997 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014998 case Intrinsic::x86_sse2_pmovmskb_128:
14999 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000015000 // High bits of movmskp{s|d}, pmovmskb are known zero.
15001 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000015002 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000015003 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
15004 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
15005 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
15006 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
15007 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
15008 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000015009 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000015010 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000015011 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000015012 break;
15013 }
15014 }
15015 break;
15016 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015017 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000015018}
Chris Lattner259e97c2006-01-31 19:43:35 +000015019
Owen Andersonbc146b02010-09-21 20:42:50 +000015020unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
15021 unsigned Depth) const {
15022 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
15023 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
15024 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000015025
Owen Andersonbc146b02010-09-21 20:42:50 +000015026 // Fallback case.
15027 return 1;
15028}
15029
Evan Cheng206ee9d2006-07-07 08:33:52 +000015030/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000015031/// node is a GlobalAddress + offset.
15032bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000015033 const GlobalValue* &GA,
15034 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000015035 if (N->getOpcode() == X86ISD::Wrapper) {
15036 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000015037 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000015038 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000015039 return true;
15040 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000015041 }
Evan Chengad4196b2008-05-12 19:56:52 +000015042 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000015043}
15044
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015045/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
15046/// same as extracting the high 128-bit part of 256-bit vector and then
15047/// inserting the result into the low part of a new 256-bit vector
15048static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
15049 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015050 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015051
15052 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000015053 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015054 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15055 SVOp->getMaskElt(j) >= 0)
15056 return false;
15057
15058 return true;
15059}
15060
15061/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
15062/// same as extracting the low 128-bit part of 256-bit vector and then
15063/// inserting the result into the high part of a new 256-bit vector
15064static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
15065 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015066 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015067
15068 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000015069 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015070 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15071 SVOp->getMaskElt(j) >= 0)
15072 return false;
15073
15074 return true;
15075}
15076
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015077/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
15078static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000015079 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015080 const X86Subtarget* Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000015081 SDLoc dl(N);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015082 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
15083 SDValue V1 = SVOp->getOperand(0);
15084 SDValue V2 = SVOp->getOperand(1);
15085 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015086 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015087
15088 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
15089 V2.getOpcode() == ISD::CONCAT_VECTORS) {
15090 //
15091 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000015092 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015093 // V UNDEF BUILD_VECTOR UNDEF
15094 // \ / \ /
15095 // CONCAT_VECTOR CONCAT_VECTOR
15096 // \ /
15097 // \ /
15098 // RESULT: V + zero extended
15099 //
15100 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
15101 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
15102 V1.getOperand(1).getOpcode() != ISD::UNDEF)
15103 return SDValue();
15104
15105 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
15106 return SDValue();
15107
15108 // To match the shuffle mask, the first half of the mask should
15109 // be exactly the first vector, and all the rest a splat with the
15110 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000015111 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015112 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
15113 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
15114 return SDValue();
15115
Chad Rosier3d1161e2012-01-03 21:05:52 +000015116 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
15117 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000015118 if (Ld->hasNUsesOfValue(1, 0)) {
15119 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
15120 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
15121 SDValue ResNode =
Michael Liao0ee17002013-04-19 04:03:37 +000015122 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops,
15123 array_lengthof(Ops),
Chad Rosier42726832012-05-07 18:47:44 +000015124 Ld->getMemoryVT(),
15125 Ld->getPointerInfo(),
15126 Ld->getAlignment(),
15127 false/*isVolatile*/, true/*ReadMem*/,
15128 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000015129
15130 // Make sure the newly-created LOAD is in the same position as Ld in
15131 // terms of dependency. We create a TokenFactor for Ld and ResNode,
15132 // and update uses of Ld's output chain to use the TokenFactor.
15133 if (Ld->hasAnyUseOfValue(1)) {
15134 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
15135 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
15136 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
15137 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
15138 SDValue(ResNode.getNode(), 1));
15139 }
15140
Chad Rosier42726832012-05-07 18:47:44 +000015141 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
15142 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000015143 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000015144
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015145 // Emit a zeroed vector and insert the desired subvector on its
15146 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015147 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000015148 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015149 return DCI.CombineTo(N, InsV);
15150 }
15151
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015152 //===--------------------------------------------------------------------===//
15153 // Combine some shuffles into subvector extracts and inserts:
15154 //
15155
15156 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
15157 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015158 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
15159 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015160 return DCI.CombineTo(N, InsV);
15161 }
15162
15163 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
15164 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015165 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
15166 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015167 return DCI.CombineTo(N, InsV);
15168 }
15169
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015170 return SDValue();
15171}
15172
15173/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000015174static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015175 TargetLowering::DAGCombinerInfo &DCI,
15176 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000015177 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +000015178 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000015179
Mon P Wanga0fd0d52010-12-19 23:55:53 +000015180 // Don't create instructions with illegal types after legalize types has run.
15181 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15182 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
15183 return SDValue();
15184
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015185 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015186 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015187 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015188 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015189
15190 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015191 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015192 return SDValue();
15193
15194 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
15195 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
15196 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000015197 SmallVector<SDValue, 16> Elts;
15198 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015199 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000015200
Nate Begemanfdea31a2010-03-24 20:49:50 +000015201 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000015202}
Evan Chengd880b972008-05-09 21:53:03 +000015203
Nadav Roteme12bf182013-01-04 17:35:21 +000015204/// PerformTruncateCombine - Converts truncate operation to
15205/// a sequence of vector shuffle operations.
15206/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000015207static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
15208 TargetLowering::DAGCombinerInfo &DCI,
15209 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015210 return SDValue();
15211}
15212
Craig Topper89f4e662012-03-20 07:17:59 +000015213/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
15214/// specific shuffle of a load can be folded into a single element load.
15215/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
15216/// shuffles have been customed lowered so we need to handle those here.
15217static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
15218 TargetLowering::DAGCombinerInfo &DCI) {
15219 if (DCI.isBeforeLegalizeOps())
15220 return SDValue();
15221
15222 SDValue InVec = N->getOperand(0);
15223 SDValue EltNo = N->getOperand(1);
15224
15225 if (!isa<ConstantSDNode>(EltNo))
15226 return SDValue();
15227
15228 EVT VT = InVec.getValueType();
15229
15230 bool HasShuffleIntoBitcast = false;
15231 if (InVec.getOpcode() == ISD::BITCAST) {
15232 // Don't duplicate a load with other uses.
15233 if (!InVec.hasOneUse())
15234 return SDValue();
15235 EVT BCVT = InVec.getOperand(0).getValueType();
15236 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
15237 return SDValue();
15238 InVec = InVec.getOperand(0);
15239 HasShuffleIntoBitcast = true;
15240 }
15241
15242 if (!isTargetShuffle(InVec.getOpcode()))
15243 return SDValue();
15244
15245 // Don't duplicate a load with other uses.
15246 if (!InVec.hasOneUse())
15247 return SDValue();
15248
15249 SmallVector<int, 16> ShuffleMask;
15250 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000015251 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
15252 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000015253 return SDValue();
15254
15255 // Select the input vector, guarding against out of range extract vector.
15256 unsigned NumElems = VT.getVectorNumElements();
15257 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
15258 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
15259 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
15260 : InVec.getOperand(1);
15261
15262 // If inputs to shuffle are the same for both ops, then allow 2 uses
15263 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
15264
15265 if (LdNode.getOpcode() == ISD::BITCAST) {
15266 // Don't duplicate a load with other uses.
15267 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
15268 return SDValue();
15269
15270 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15271 LdNode = LdNode.getOperand(0);
15272 }
15273
15274 if (!ISD::isNormalLoad(LdNode.getNode()))
15275 return SDValue();
15276
15277 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15278
15279 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15280 return SDValue();
15281
15282 if (HasShuffleIntoBitcast) {
15283 // If there's a bitcast before the shuffle, check if the load type and
15284 // alignment is valid.
15285 unsigned Align = LN0->getAlignment();
15286 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015287 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015288 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15289
15290 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15291 return SDValue();
15292 }
15293
15294 // All checks match so transform back to vector_shuffle so that DAG combiner
15295 // can finish the job
Andrew Trickac6d9be2013-05-25 02:42:55 +000015296 SDLoc dl(N);
Craig Topper89f4e662012-03-20 07:17:59 +000015297
15298 // Create shuffle node taking into account the case that its a unary shuffle
15299 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15300 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15301 InVec.getOperand(0), Shuffle,
15302 &ShuffleMask[0]);
15303 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15304 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15305 EltNo);
15306}
15307
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015308/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15309/// generation and convert it from being a bunch of shuffles and extracts
15310/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015311static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015312 TargetLowering::DAGCombinerInfo &DCI) {
15313 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15314 if (NewOp.getNode())
15315 return NewOp;
15316
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015317 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015318 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15319 // from mmx to v2i32 has a single usage.
15320 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15321 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15322 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
Andrew Trickac6d9be2013-05-25 02:42:55 +000015323 return DAG.getNode(X86ISD::MMX_MOVD2W, SDLoc(InputVector),
Manman Ren4c74a952012-10-30 22:15:38 +000015324 N->getValueType(0),
15325 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015326
15327 // Only operate on vectors of 4 elements, where the alternative shuffling
15328 // gets to be more expensive.
15329 if (InputVector.getValueType() != MVT::v4i32)
15330 return SDValue();
15331
15332 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15333 // single use which is a sign-extend or zero-extend, and all elements are
15334 // used.
15335 SmallVector<SDNode *, 4> Uses;
15336 unsigned ExtractedElements = 0;
15337 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15338 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15339 if (UI.getUse().getResNo() != InputVector.getResNo())
15340 return SDValue();
15341
15342 SDNode *Extract = *UI;
15343 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15344 return SDValue();
15345
15346 if (Extract->getValueType(0) != MVT::i32)
15347 return SDValue();
15348 if (!Extract->hasOneUse())
15349 return SDValue();
15350 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15351 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15352 return SDValue();
15353 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15354 return SDValue();
15355
15356 // Record which element was extracted.
15357 ExtractedElements |=
15358 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15359
15360 Uses.push_back(Extract);
15361 }
15362
15363 // If not all the elements were used, this may not be worthwhile.
15364 if (ExtractedElements != 15)
15365 return SDValue();
15366
15367 // Ok, we've now decided to do the transformation.
Andrew Trickac6d9be2013-05-25 02:42:55 +000015368 SDLoc dl(InputVector);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015369
15370 // Store the value to a temporary stack slot.
15371 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015372 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15373 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015374
15375 // Replace each use (extract) with a load of the appropriate element.
15376 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15377 UE = Uses.end(); UI != UE; ++UI) {
15378 SDNode *Extract = *UI;
15379
Nadav Rotem86694292011-05-17 08:31:57 +000015380 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015381 SDValue Idx = Extract->getOperand(1);
15382 unsigned EltSize =
15383 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15384 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015385 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015386 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15387
Nadav Rotem86694292011-05-17 08:31:57 +000015388 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015389 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015390
15391 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015392 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015393 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015394 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015395
15396 // Replace the exact with the load.
15397 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15398 }
15399
15400 // The replacement was made in place; don't return anything.
15401 return SDValue();
15402}
15403
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015404/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15405static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15406 SDValue RHS, SelectionDAG &DAG,
15407 const X86Subtarget *Subtarget) {
15408 if (!VT.isVector())
15409 return 0;
15410
15411 switch (VT.getSimpleVT().SimpleTy) {
15412 default: return 0;
15413 case MVT::v32i8:
15414 case MVT::v16i16:
15415 case MVT::v8i32:
15416 if (!Subtarget->hasAVX2())
15417 return 0;
15418 case MVT::v16i8:
15419 case MVT::v8i16:
15420 case MVT::v4i32:
15421 if (!Subtarget->hasSSE2())
15422 return 0;
15423 }
15424
15425 // SSE2 has only a small subset of the operations.
15426 bool hasUnsigned = Subtarget->hasSSE41() ||
15427 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15428 bool hasSigned = Subtarget->hasSSE41() ||
15429 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15430
15431 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15432
15433 // Check for x CC y ? x : y.
15434 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15435 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15436 switch (CC) {
15437 default: break;
15438 case ISD::SETULT:
15439 case ISD::SETULE:
15440 return hasUnsigned ? X86ISD::UMIN : 0;
15441 case ISD::SETUGT:
15442 case ISD::SETUGE:
15443 return hasUnsigned ? X86ISD::UMAX : 0;
15444 case ISD::SETLT:
15445 case ISD::SETLE:
15446 return hasSigned ? X86ISD::SMIN : 0;
15447 case ISD::SETGT:
15448 case ISD::SETGE:
15449 return hasSigned ? X86ISD::SMAX : 0;
15450 }
15451 // Check for x CC y ? y : x -- a min/max with reversed arms.
15452 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15453 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15454 switch (CC) {
15455 default: break;
15456 case ISD::SETULT:
15457 case ISD::SETULE:
15458 return hasUnsigned ? X86ISD::UMAX : 0;
15459 case ISD::SETUGT:
15460 case ISD::SETUGE:
15461 return hasUnsigned ? X86ISD::UMIN : 0;
15462 case ISD::SETLT:
15463 case ISD::SETLE:
15464 return hasSigned ? X86ISD::SMAX : 0;
15465 case ISD::SETGT:
15466 case ISD::SETGE:
15467 return hasSigned ? X86ISD::SMIN : 0;
15468 }
15469 }
15470
15471 return 0;
15472}
15473
Duncan Sands6bcd2192011-09-17 16:49:39 +000015474/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15475/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015476static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015477 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015478 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000015479 SDLoc DL(N);
Dan Gohman475871a2008-07-27 21:46:04 +000015480 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015481 // Get the LHS/RHS of the select.
15482 SDValue LHS = N->getOperand(1);
15483 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015484 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015485
Dan Gohman670e5392009-09-21 18:03:22 +000015486 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015487 // instructions match the semantics of the common C idiom x<y?x:y but not
15488 // x<=y?x:y, because of how they handle negative zero (which can be
15489 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015490 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15491 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015492 (Subtarget->hasSSE2() ||
15493 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015494 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015495
Chris Lattner47b4ce82009-03-11 05:48:52 +000015496 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015497 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015498 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15499 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015500 switch (CC) {
15501 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015502 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015503 // Converting this to a min would handle NaNs incorrectly, and swapping
15504 // the operands would cause it to handle comparisons between positive
15505 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015506 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015507 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015508 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15509 break;
15510 std::swap(LHS, RHS);
15511 }
Dan Gohman670e5392009-09-21 18:03:22 +000015512 Opcode = X86ISD::FMIN;
15513 break;
15514 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015515 // Converting this to a min would handle comparisons between positive
15516 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015517 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015518 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15519 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015520 Opcode = X86ISD::FMIN;
15521 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015522 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015523 // Converting this to a min would handle both negative zeros and NaNs
15524 // incorrectly, but we can swap the operands to fix both.
15525 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015526 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015527 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015528 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015529 Opcode = X86ISD::FMIN;
15530 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015531
Dan Gohman670e5392009-09-21 18:03:22 +000015532 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015533 // Converting this to a max would handle comparisons between positive
15534 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015535 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015536 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015537 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015538 Opcode = X86ISD::FMAX;
15539 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015540 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015541 // Converting this to a max would handle NaNs incorrectly, and swapping
15542 // the operands would cause it to handle comparisons between positive
15543 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015544 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015545 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015546 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15547 break;
15548 std::swap(LHS, RHS);
15549 }
Dan Gohman670e5392009-09-21 18:03:22 +000015550 Opcode = X86ISD::FMAX;
15551 break;
15552 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015553 // Converting this to a max would handle both negative zeros and NaNs
15554 // incorrectly, but we can swap the operands to fix both.
15555 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015556 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015557 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015558 case ISD::SETGE:
15559 Opcode = X86ISD::FMAX;
15560 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015561 }
Dan Gohman670e5392009-09-21 18:03:22 +000015562 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015563 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15564 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015565 switch (CC) {
15566 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015567 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015568 // Converting this to a min would handle comparisons between positive
15569 // and negative zero incorrectly, and swapping the operands would
15570 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015571 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015572 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015573 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015574 break;
15575 std::swap(LHS, RHS);
15576 }
Dan Gohman670e5392009-09-21 18:03:22 +000015577 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015578 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015579 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015580 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015581 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015582 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15583 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015584 Opcode = X86ISD::FMIN;
15585 break;
15586 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015587 // Converting this to a min would handle both negative zeros and NaNs
15588 // incorrectly, but we can swap the operands to fix both.
15589 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015590 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015591 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015592 case ISD::SETGE:
15593 Opcode = X86ISD::FMIN;
15594 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015595
Dan Gohman670e5392009-09-21 18:03:22 +000015596 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015597 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015598 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015599 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015600 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015601 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015602 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015603 // Converting this to a max would handle comparisons between positive
15604 // and negative zero incorrectly, and swapping the operands would
15605 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015606 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015607 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015608 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015609 break;
15610 std::swap(LHS, RHS);
15611 }
Dan Gohman670e5392009-09-21 18:03:22 +000015612 Opcode = X86ISD::FMAX;
15613 break;
15614 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015615 // Converting this to a max would handle both negative zeros and NaNs
15616 // incorrectly, but we can swap the operands to fix both.
15617 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015618 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015619 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015620 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015621 Opcode = X86ISD::FMAX;
15622 break;
15623 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015624 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015625
Chris Lattner47b4ce82009-03-11 05:48:52 +000015626 if (Opcode)
15627 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015628 }
Eric Christopherfd179292009-08-27 18:07:15 +000015629
Chris Lattnerd1980a52009-03-12 06:52:53 +000015630 // If this is a select between two integer constants, try to do some
15631 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015632 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15633 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015634 // Don't do this for crazy integer types.
15635 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15636 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015637 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015638 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015639
Chris Lattnercee56e72009-03-13 05:53:31 +000015640 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015641 // Efficiently invertible.
15642 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15643 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15644 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15645 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015646 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015647 }
Eric Christopherfd179292009-08-27 18:07:15 +000015648
Chris Lattnerd1980a52009-03-12 06:52:53 +000015649 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015650 if (FalseC->getAPIntValue() == 0 &&
15651 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015652 if (NeedsCondInvert) // Invert the condition if needed.
15653 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15654 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015655
Chris Lattnerd1980a52009-03-12 06:52:53 +000015656 // Zero extend the condition if needed.
15657 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015658
Chris Lattnercee56e72009-03-13 05:53:31 +000015659 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015660 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015661 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015662 }
Eric Christopherfd179292009-08-27 18:07:15 +000015663
Chris Lattner97a29a52009-03-13 05:22:11 +000015664 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015665 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015666 if (NeedsCondInvert) // Invert the condition if needed.
15667 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15668 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015669
Chris Lattner97a29a52009-03-13 05:22:11 +000015670 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015671 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15672 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015673 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015674 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015675 }
Eric Christopherfd179292009-08-27 18:07:15 +000015676
Chris Lattnercee56e72009-03-13 05:53:31 +000015677 // Optimize cases that will turn into an LEA instruction. This requires
15678 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015679 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015680 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015681 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015682
Chris Lattnercee56e72009-03-13 05:53:31 +000015683 bool isFastMultiplier = false;
15684 if (Diff < 10) {
15685 switch ((unsigned char)Diff) {
15686 default: break;
15687 case 1: // result = add base, cond
15688 case 2: // result = lea base( , cond*2)
15689 case 3: // result = lea base(cond, cond*2)
15690 case 4: // result = lea base( , cond*4)
15691 case 5: // result = lea base(cond, cond*4)
15692 case 8: // result = lea base( , cond*8)
15693 case 9: // result = lea base(cond, cond*8)
15694 isFastMultiplier = true;
15695 break;
15696 }
15697 }
Eric Christopherfd179292009-08-27 18:07:15 +000015698
Chris Lattnercee56e72009-03-13 05:53:31 +000015699 if (isFastMultiplier) {
15700 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15701 if (NeedsCondInvert) // Invert the condition if needed.
15702 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15703 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015704
Chris Lattnercee56e72009-03-13 05:53:31 +000015705 // Zero extend the condition if needed.
15706 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15707 Cond);
15708 // Scale the condition by the difference.
15709 if (Diff != 1)
15710 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15711 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015712
Chris Lattnercee56e72009-03-13 05:53:31 +000015713 // Add the base if non-zero.
15714 if (FalseC->getAPIntValue() != 0)
15715 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15716 SDValue(FalseC, 0));
15717 return Cond;
15718 }
Eric Christopherfd179292009-08-27 18:07:15 +000015719 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015720 }
15721 }
Eric Christopherfd179292009-08-27 18:07:15 +000015722
Evan Cheng56f582d2012-01-04 01:41:39 +000015723 // Canonicalize max and min:
15724 // (x > y) ? x : y -> (x >= y) ? x : y
15725 // (x < y) ? x : y -> (x <= y) ? x : y
15726 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15727 // the need for an extra compare
15728 // against zero. e.g.
15729 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15730 // subl %esi, %edi
15731 // testl %edi, %edi
15732 // movl $0, %eax
15733 // cmovgl %edi, %eax
15734 // =>
15735 // xorl %eax, %eax
15736 // subl %esi, $edi
15737 // cmovsl %eax, %edi
15738 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15739 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15740 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15741 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15742 switch (CC) {
15743 default: break;
15744 case ISD::SETLT:
15745 case ISD::SETGT: {
15746 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
Andrew Trickac6d9be2013-05-25 02:42:55 +000015747 Cond = DAG.getSetCC(SDLoc(Cond), Cond.getValueType(),
Evan Cheng56f582d2012-01-04 01:41:39 +000015748 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15749 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15750 }
15751 }
15752 }
15753
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015754 // Match VSELECTs into subs with unsigned saturation.
15755 if (!DCI.isBeforeLegalize() &&
15756 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15757 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15758 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15759 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15760 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15761
15762 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15763 // left side invert the predicate to simplify logic below.
15764 SDValue Other;
15765 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15766 Other = RHS;
15767 CC = ISD::getSetCCInverse(CC, true);
15768 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15769 Other = LHS;
15770 }
15771
15772 if (Other.getNode() && Other->getNumOperands() == 2 &&
15773 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15774 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15775 SDValue CondRHS = Cond->getOperand(1);
15776
15777 // Look for a general sub with unsigned saturation first.
15778 // x >= y ? x-y : 0 --> subus x, y
15779 // x > y ? x-y : 0 --> subus x, y
15780 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15781 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15782 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15783
15784 // If the RHS is a constant we have to reverse the const canonicalization.
15785 // x > C-1 ? x+-C : 0 --> subus x, C
15786 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15787 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15788 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015789 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015790 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015791 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015792 }
15793
15794 // Another special case: If C was a sign bit, the sub has been
15795 // canonicalized into a xor.
15796 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15797 // it's safe to decanonicalize the xor?
15798 // x s< 0 ? x^C : 0 --> subus x, C
15799 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15800 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15801 isSplatVector(OpRHS.getNode())) {
15802 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15803 if (A.isSignBit())
15804 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15805 }
15806 }
15807 }
15808
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015809 // Try to match a min/max vector operation.
15810 if (!DCI.isBeforeLegalize() &&
15811 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15812 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15813 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15814
Michael Liaobf538412013-04-11 05:15:54 +000015815 // Simplify vector selection if the selector will be produced by CMPP*/PCMP*.
15816 if (!DCI.isBeforeLegalize() && N->getOpcode() == ISD::VSELECT &&
15817 Cond.getOpcode() == ISD::SETCC) {
15818
15819 assert(Cond.getValueType().isVector() &&
15820 "vector select expects a vector selector!");
15821
15822 EVT IntVT = Cond.getValueType();
15823 bool TValIsAllOnes = ISD::isBuildVectorAllOnes(LHS.getNode());
15824 bool FValIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
15825
15826 if (!TValIsAllOnes && !FValIsAllZeros) {
15827 // Try invert the condition if true value is not all 1s and false value
15828 // is not all 0s.
15829 bool TValIsAllZeros = ISD::isBuildVectorAllZeros(LHS.getNode());
15830 bool FValIsAllOnes = ISD::isBuildVectorAllOnes(RHS.getNode());
15831
15832 if (TValIsAllZeros || FValIsAllOnes) {
15833 SDValue CC = Cond.getOperand(2);
15834 ISD::CondCode NewCC =
15835 ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
15836 Cond.getOperand(0).getValueType().isInteger());
15837 Cond = DAG.getSetCC(DL, IntVT, Cond.getOperand(0), Cond.getOperand(1), NewCC);
15838 std::swap(LHS, RHS);
15839 TValIsAllOnes = FValIsAllOnes;
15840 FValIsAllZeros = TValIsAllZeros;
15841 }
15842 }
15843
15844 if (TValIsAllOnes || FValIsAllZeros) {
15845 SDValue Ret;
15846
15847 if (TValIsAllOnes && FValIsAllZeros)
15848 Ret = Cond;
15849 else if (TValIsAllOnes)
15850 Ret = DAG.getNode(ISD::OR, DL, IntVT, Cond,
15851 DAG.getNode(ISD::BITCAST, DL, IntVT, RHS));
15852 else if (FValIsAllZeros)
15853 Ret = DAG.getNode(ISD::AND, DL, IntVT, Cond,
15854 DAG.getNode(ISD::BITCAST, DL, IntVT, LHS));
15855
15856 return DAG.getNode(ISD::BITCAST, DL, VT, Ret);
15857 }
15858 }
15859
Nadav Rotemcc616562012-01-15 19:27:55 +000015860 // If we know that this node is legal then we know that it is going to be
15861 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15862 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15863 // to simplify previous instructions.
15864 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15865 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015866 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015867 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015868
15869 // Don't optimize vector selects that map to mask-registers.
15870 if (BitWidth == 1)
15871 return SDValue();
15872
Nadav Rotemcc616562012-01-15 19:27:55 +000015873 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15874 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15875
15876 APInt KnownZero, KnownOne;
15877 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15878 DCI.isBeforeLegalizeOps());
15879 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15880 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15881 DCI.CommitTargetLoweringOpt(TLO);
15882 }
15883
Dan Gohman475871a2008-07-27 21:46:04 +000015884 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015885}
15886
Michael Liao2a33cec2012-08-10 19:58:13 +000015887// Check whether a boolean test is testing a boolean value generated by
15888// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15889// code.
15890//
15891// Simplify the following patterns:
15892// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15893// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15894// to (Op EFLAGS Cond)
15895//
15896// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15897// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15898// to (Op EFLAGS !Cond)
15899//
15900// where Op could be BRCOND or CMOV.
15901//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015902static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015903 // Quit if not CMP and SUB with its value result used.
15904 if (Cmp.getOpcode() != X86ISD::CMP &&
15905 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15906 return SDValue();
15907
15908 // Quit if not used as a boolean value.
15909 if (CC != X86::COND_E && CC != X86::COND_NE)
15910 return SDValue();
15911
15912 // Check CMP operands. One of them should be 0 or 1 and the other should be
15913 // an SetCC or extended from it.
15914 SDValue Op1 = Cmp.getOperand(0);
15915 SDValue Op2 = Cmp.getOperand(1);
15916
15917 SDValue SetCC;
15918 const ConstantSDNode* C = 0;
15919 bool needOppositeCond = (CC == X86::COND_E);
Michael Liao959ddbb2013-04-11 04:43:09 +000015920 bool checkAgainstTrue = false; // Is it a comparison against 1?
Michael Liao2a33cec2012-08-10 19:58:13 +000015921
15922 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15923 SetCC = Op2;
15924 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15925 SetCC = Op1;
15926 else // Quit if all operands are not constants.
15927 return SDValue();
15928
Michael Liao959ddbb2013-04-11 04:43:09 +000015929 if (C->getZExtValue() == 1) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015930 needOppositeCond = !needOppositeCond;
Michael Liao959ddbb2013-04-11 04:43:09 +000015931 checkAgainstTrue = true;
15932 } else if (C->getZExtValue() != 0)
Michael Liao2a33cec2012-08-10 19:58:13 +000015933 // Quit if the constant is neither 0 or 1.
15934 return SDValue();
15935
Michael Liao959ddbb2013-04-11 04:43:09 +000015936 bool truncatedToBoolWithAnd = false;
15937 // Skip (zext $x), (trunc $x), or (and $x, 1) node.
15938 while (SetCC.getOpcode() == ISD::ZERO_EXTEND ||
15939 SetCC.getOpcode() == ISD::TRUNCATE ||
15940 SetCC.getOpcode() == ISD::AND) {
15941 if (SetCC.getOpcode() == ISD::AND) {
15942 int OpIdx = -1;
15943 ConstantSDNode *CS;
15944 if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(0))) &&
15945 CS->getZExtValue() == 1)
15946 OpIdx = 1;
15947 if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(1))) &&
15948 CS->getZExtValue() == 1)
15949 OpIdx = 0;
15950 if (OpIdx == -1)
15951 break;
15952 SetCC = SetCC.getOperand(OpIdx);
15953 truncatedToBoolWithAnd = true;
15954 } else
15955 SetCC = SetCC.getOperand(0);
15956 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015957
Michael Liao7fdc66b2012-09-10 16:36:16 +000015958 switch (SetCC.getOpcode()) {
Michael Liao959ddbb2013-04-11 04:43:09 +000015959 case X86ISD::SETCC_CARRY:
15960 // Since SETCC_CARRY gives output based on R = CF ? ~0 : 0, it's unsafe to
15961 // simplify it if the result of SETCC_CARRY is not canonicalized to 0 or 1,
15962 // i.e. it's a comparison against true but the result of SETCC_CARRY is not
15963 // truncated to i1 using 'and'.
15964 if (checkAgainstTrue && !truncatedToBoolWithAnd)
15965 break;
15966 assert(X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B &&
15967 "Invalid use of SETCC_CARRY!");
15968 // FALL THROUGH
Michael Liao7fdc66b2012-09-10 16:36:16 +000015969 case X86ISD::SETCC:
15970 // Set the condition code or opposite one if necessary.
15971 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15972 if (needOppositeCond)
15973 CC = X86::GetOppositeBranchCondition(CC);
15974 return SetCC.getOperand(1);
15975 case X86ISD::CMOV: {
15976 // Check whether false/true value has canonical one, i.e. 0 or 1.
15977 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15978 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15979 // Quit if true value is not a constant.
15980 if (!TVal)
15981 return SDValue();
15982 // Quit if false value is not a constant.
15983 if (!FVal) {
Michael Liao7fdc66b2012-09-10 16:36:16 +000015984 SDValue Op = SetCC.getOperand(0);
Michael Liao258d9b72013-03-28 23:38:52 +000015985 // Skip 'zext' or 'trunc' node.
15986 if (Op.getOpcode() == ISD::ZERO_EXTEND ||
15987 Op.getOpcode() == ISD::TRUNCATE)
15988 Op = Op.getOperand(0);
Michael Liaoc26392a2013-03-28 23:41:26 +000015989 // A special case for rdrand/rdseed, where 0 is set if false cond is
15990 // found.
15991 if ((Op.getOpcode() != X86ISD::RDRAND &&
15992 Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0)
Michael Liao7fdc66b2012-09-10 16:36:16 +000015993 return SDValue();
15994 }
15995 // Quit if false value is not the constant 0 or 1.
15996 bool FValIsFalse = true;
15997 if (FVal && FVal->getZExtValue() != 0) {
15998 if (FVal->getZExtValue() != 1)
15999 return SDValue();
16000 // If FVal is 1, opposite cond is needed.
16001 needOppositeCond = !needOppositeCond;
16002 FValIsFalse = false;
16003 }
16004 // Quit if TVal is not the constant opposite of FVal.
16005 if (FValIsFalse && TVal->getZExtValue() != 1)
16006 return SDValue();
16007 if (!FValIsFalse && TVal->getZExtValue() != 0)
16008 return SDValue();
16009 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
16010 if (needOppositeCond)
16011 CC = X86::GetOppositeBranchCondition(CC);
16012 return SetCC.getOperand(3);
16013 }
16014 }
Michael Liao2a33cec2012-08-10 19:58:13 +000016015
Michael Liao7fdc66b2012-09-10 16:36:16 +000016016 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000016017}
16018
Chris Lattnerd1980a52009-03-12 06:52:53 +000016019/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
16020static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016021 TargetLowering::DAGCombinerInfo &DCI,
16022 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000016023 SDLoc DL(N);
Eric Christopherfd179292009-08-27 18:07:15 +000016024
Chris Lattnerd1980a52009-03-12 06:52:53 +000016025 // If the flag operand isn't dead, don't touch this CMOV.
16026 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
16027 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000016028
Evan Chengb5a55d92011-05-24 01:48:22 +000016029 SDValue FalseOp = N->getOperand(0);
16030 SDValue TrueOp = N->getOperand(1);
16031 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
16032 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000016033
Evan Chengb5a55d92011-05-24 01:48:22 +000016034 if (CC == X86::COND_E || CC == X86::COND_NE) {
16035 switch (Cond.getOpcode()) {
16036 default: break;
16037 case X86ISD::BSR:
16038 case X86ISD::BSF:
16039 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
16040 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
16041 return (CC == X86::COND_E) ? FalseOp : TrueOp;
16042 }
16043 }
16044
Michael Liao2a33cec2012-08-10 19:58:13 +000016045 SDValue Flags;
16046
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016047 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000016048 if (Flags.getNode() &&
16049 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000016050 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016051 SDValue Ops[] = { FalseOp, TrueOp,
16052 DAG.getConstant(CC, MVT::i8), Flags };
16053 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
16054 Ops, array_lengthof(Ops));
16055 }
16056
Chris Lattnerd1980a52009-03-12 06:52:53 +000016057 // If this is a select between two integer constants, try to do some
16058 // optimizations. Note that the operands are ordered the opposite of SELECT
16059 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000016060 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
16061 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000016062 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
16063 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000016064 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
16065 CC = X86::GetOppositeBranchCondition(CC);
16066 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016067 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000016068 }
Eric Christopherfd179292009-08-27 18:07:15 +000016069
Chris Lattnerd1980a52009-03-12 06:52:53 +000016070 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000016071 // This is efficient for any integer data type (including i8/i16) and
16072 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000016073 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016074 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16075 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016076
Chris Lattnerd1980a52009-03-12 06:52:53 +000016077 // Zero extend the condition if needed.
16078 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016079
Chris Lattnerd1980a52009-03-12 06:52:53 +000016080 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
16081 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000016082 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000016083 if (N->getNumValues() == 2) // Dead flag value?
16084 return DCI.CombineTo(N, Cond, SDValue());
16085 return Cond;
16086 }
Eric Christopherfd179292009-08-27 18:07:15 +000016087
Chris Lattnercee56e72009-03-13 05:53:31 +000016088 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
16089 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000016090 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016091 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16092 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000016093
Chris Lattner97a29a52009-03-13 05:22:11 +000016094 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000016095 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
16096 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000016097 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16098 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000016099
Chris Lattner97a29a52009-03-13 05:22:11 +000016100 if (N->getNumValues() == 2) // Dead flag value?
16101 return DCI.CombineTo(N, Cond, SDValue());
16102 return Cond;
16103 }
Eric Christopherfd179292009-08-27 18:07:15 +000016104
Chris Lattnercee56e72009-03-13 05:53:31 +000016105 // Optimize cases that will turn into an LEA instruction. This requires
16106 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000016107 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000016108 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000016109 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000016110
Chris Lattnercee56e72009-03-13 05:53:31 +000016111 bool isFastMultiplier = false;
16112 if (Diff < 10) {
16113 switch ((unsigned char)Diff) {
16114 default: break;
16115 case 1: // result = add base, cond
16116 case 2: // result = lea base( , cond*2)
16117 case 3: // result = lea base(cond, cond*2)
16118 case 4: // result = lea base( , cond*4)
16119 case 5: // result = lea base(cond, cond*4)
16120 case 8: // result = lea base( , cond*8)
16121 case 9: // result = lea base(cond, cond*8)
16122 isFastMultiplier = true;
16123 break;
16124 }
16125 }
Eric Christopherfd179292009-08-27 18:07:15 +000016126
Chris Lattnercee56e72009-03-13 05:53:31 +000016127 if (isFastMultiplier) {
16128 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000016129 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
16130 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000016131 // Zero extend the condition if needed.
16132 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
16133 Cond);
16134 // Scale the condition by the difference.
16135 if (Diff != 1)
16136 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
16137 DAG.getConstant(Diff, Cond.getValueType()));
16138
16139 // Add the base if non-zero.
16140 if (FalseC->getAPIntValue() != 0)
16141 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16142 SDValue(FalseC, 0));
16143 if (N->getNumValues() == 2) // Dead flag value?
16144 return DCI.CombineTo(N, Cond, SDValue());
16145 return Cond;
16146 }
Eric Christopherfd179292009-08-27 18:07:15 +000016147 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000016148 }
16149 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016150
16151 // Handle these cases:
16152 // (select (x != c), e, c) -> select (x != c), e, x),
16153 // (select (x == c), c, e) -> select (x == c), x, e)
16154 // where the c is an integer constant, and the "select" is the combination
16155 // of CMOV and CMP.
16156 //
16157 // The rationale for this change is that the conditional-move from a constant
16158 // needs two instructions, however, conditional-move from a register needs
16159 // only one instruction.
16160 //
16161 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
16162 // some instruction-combining opportunities. This opt needs to be
16163 // postponed as late as possible.
16164 //
16165 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
16166 // the DCI.xxxx conditions are provided to postpone the optimization as
16167 // late as possible.
16168
16169 ConstantSDNode *CmpAgainst = 0;
16170 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
16171 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016172 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016173
16174 if (CC == X86::COND_NE &&
16175 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
16176 CC = X86::GetOppositeBranchCondition(CC);
16177 std::swap(TrueOp, FalseOp);
16178 }
16179
16180 if (CC == X86::COND_E &&
16181 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
16182 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
16183 DAG.getConstant(CC, MVT::i8), Cond };
16184 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
16185 array_lengthof(Ops));
16186 }
16187 }
16188 }
16189
Chris Lattnerd1980a52009-03-12 06:52:53 +000016190 return SDValue();
16191}
16192
Evan Cheng0b0cd912009-03-28 05:57:29 +000016193/// PerformMulCombine - Optimize a single multiply with constant into two
16194/// in order to implement it with two cheaper instructions, e.g.
16195/// LEA + SHL, LEA + LEA.
16196static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
16197 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000016198 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
16199 return SDValue();
16200
Owen Andersone50ed302009-08-10 22:56:29 +000016201 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000016202 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000016203 return SDValue();
16204
16205 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
16206 if (!C)
16207 return SDValue();
16208 uint64_t MulAmt = C->getZExtValue();
16209 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
16210 return SDValue();
16211
16212 uint64_t MulAmt1 = 0;
16213 uint64_t MulAmt2 = 0;
16214 if ((MulAmt % 9) == 0) {
16215 MulAmt1 = 9;
16216 MulAmt2 = MulAmt / 9;
16217 } else if ((MulAmt % 5) == 0) {
16218 MulAmt1 = 5;
16219 MulAmt2 = MulAmt / 5;
16220 } else if ((MulAmt % 3) == 0) {
16221 MulAmt1 = 3;
16222 MulAmt2 = MulAmt / 3;
16223 }
16224 if (MulAmt2 &&
16225 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
Andrew Trickac6d9be2013-05-25 02:42:55 +000016226 SDLoc DL(N);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016227
16228 if (isPowerOf2_64(MulAmt2) &&
16229 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
16230 // If second multiplifer is pow2, issue it first. We want the multiply by
16231 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
16232 // is an add.
16233 std::swap(MulAmt1, MulAmt2);
16234
16235 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000016236 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016237 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000016238 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000016239 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016240 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000016241 DAG.getConstant(MulAmt1, VT));
16242
Eric Christopherfd179292009-08-27 18:07:15 +000016243 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016244 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000016245 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000016246 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016247 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000016248 DAG.getConstant(MulAmt2, VT));
16249
16250 // Do not add new nodes to DAG combiner worklist.
16251 DCI.CombineTo(N, NewMul, false);
16252 }
16253 return SDValue();
16254}
16255
Evan Chengad9c0a32009-12-15 00:53:42 +000016256static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
16257 SDValue N0 = N->getOperand(0);
16258 SDValue N1 = N->getOperand(1);
16259 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
16260 EVT VT = N0.getValueType();
16261
16262 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
16263 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016264 if (VT.isInteger() && !VT.isVector() &&
16265 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000016266 N0.getOperand(1).getOpcode() == ISD::Constant) {
16267 SDValue N00 = N0.getOperand(0);
16268 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
16269 ((N00.getOpcode() == ISD::ANY_EXTEND ||
16270 N00.getOpcode() == ISD::ZERO_EXTEND) &&
16271 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
16272 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
16273 APInt ShAmt = N1C->getAPIntValue();
16274 Mask = Mask.shl(ShAmt);
16275 if (Mask != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +000016276 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Evan Chengad9c0a32009-12-15 00:53:42 +000016277 N00, DAG.getConstant(Mask, VT));
16278 }
16279 }
16280
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016281 // Hardware support for vector shifts is sparse which makes us scalarize the
16282 // vector operations in many cases. Also, on sandybridge ADD is faster than
16283 // shl.
16284 // (shl V, 1) -> add V,V
16285 if (isSplatVector(N1.getNode())) {
16286 assert(N0.getValueType().isVector() && "Invalid vector shift type");
16287 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
16288 // We shift all of the values by one. In many cases we do not have
16289 // hardware support for this operation. This is better expressed as an ADD
16290 // of two values.
16291 if (N1C && (1 == N1C->getZExtValue())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000016292 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016293 }
16294 }
16295
Evan Chengad9c0a32009-12-15 00:53:42 +000016296 return SDValue();
16297}
Evan Cheng0b0cd912009-03-28 05:57:29 +000016298
Nadav Rotem0fb65232013-05-04 23:24:56 +000016299/// PerformShiftCombine - Combine shifts.
Nate Begeman740ab032009-01-26 00:52:55 +000016300static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000016301 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000016302 const X86Subtarget *Subtarget) {
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016303 if (N->getOpcode() == ISD::SHL) {
16304 SDValue V = PerformSHLCombine(N, DAG);
16305 if (V.getNode()) return V;
16306 }
Evan Chengad9c0a32009-12-15 00:53:42 +000016307
Michael Liao42317cc2013-03-20 02:33:21 +000016308 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016309}
16310
Stuart Hastings865f0932011-06-03 23:53:54 +000016311// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16312// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16313// and friends. Likewise for OR -> CMPNEQSS.
16314static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16315 TargetLowering::DAGCombinerInfo &DCI,
16316 const X86Subtarget *Subtarget) {
16317 unsigned opcode;
16318
16319 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16320 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016321 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016322 SDValue N0 = N->getOperand(0);
16323 SDValue N1 = N->getOperand(1);
16324 SDValue CMP0 = N0->getOperand(1);
16325 SDValue CMP1 = N1->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016326 SDLoc DL(N);
Stuart Hastings865f0932011-06-03 23:53:54 +000016327
16328 // The SETCCs should both refer to the same CMP.
16329 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16330 return SDValue();
16331
16332 SDValue CMP00 = CMP0->getOperand(0);
16333 SDValue CMP01 = CMP0->getOperand(1);
16334 EVT VT = CMP00.getValueType();
16335
16336 if (VT == MVT::f32 || VT == MVT::f64) {
16337 bool ExpectingFlags = false;
16338 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016339 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016340 !ExpectingFlags && UI != UE; ++UI)
16341 switch (UI->getOpcode()) {
16342 default:
16343 case ISD::BR_CC:
16344 case ISD::BRCOND:
16345 case ISD::SELECT:
16346 ExpectingFlags = true;
16347 break;
16348 case ISD::CopyToReg:
16349 case ISD::SIGN_EXTEND:
16350 case ISD::ZERO_EXTEND:
16351 case ISD::ANY_EXTEND:
16352 break;
16353 }
16354
16355 if (!ExpectingFlags) {
16356 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16357 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16358
16359 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16360 X86::CondCode tmp = cc0;
16361 cc0 = cc1;
16362 cc1 = tmp;
16363 }
16364
16365 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16366 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16367 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16368 X86ISD::NodeType NTOperator = is64BitFP ?
16369 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16370 // FIXME: need symbolic constants for these magic numbers.
16371 // See X86ATTInstPrinter.cpp:printSSECC().
16372 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16373 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16374 DAG.getConstant(x86cc, MVT::i8));
16375 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16376 OnesOrZeroesF);
16377 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16378 DAG.getConstant(1, MVT::i32));
16379 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16380 return OneBitOfTruth;
16381 }
16382 }
16383 }
16384 }
16385 return SDValue();
16386}
16387
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016388/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16389/// so it can be folded inside ANDNP.
16390static bool CanFoldXORWithAllOnes(const SDNode *N) {
16391 EVT VT = N->getValueType(0);
16392
16393 // Match direct AllOnes for 128 and 256-bit vectors
16394 if (ISD::isBuildVectorAllOnes(N))
16395 return true;
16396
16397 // Look through a bit convert.
16398 if (N->getOpcode() == ISD::BITCAST)
16399 N = N->getOperand(0).getNode();
16400
16401 // Sometimes the operand may come from a insert_subvector building a 256-bit
16402 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016403 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016404 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16405 SDValue V1 = N->getOperand(0);
16406 SDValue V2 = N->getOperand(1);
16407
16408 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16409 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16410 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16411 ISD::isBuildVectorAllOnes(V2.getNode()))
16412 return true;
16413 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016414
16415 return false;
16416}
16417
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016418// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16419// register. In most cases we actually compare or select YMM-sized registers
16420// and mixing the two types creates horrible code. This method optimizes
16421// some of the transition sequences.
16422static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16423 TargetLowering::DAGCombinerInfo &DCI,
16424 const X86Subtarget *Subtarget) {
16425 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016426 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016427 return SDValue();
16428
16429 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16430 N->getOpcode() == ISD::ZERO_EXTEND ||
16431 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16432
16433 SDValue Narrow = N->getOperand(0);
16434 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016435 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016436 return SDValue();
16437
16438 if (Narrow->getOpcode() != ISD::XOR &&
16439 Narrow->getOpcode() != ISD::AND &&
16440 Narrow->getOpcode() != ISD::OR)
16441 return SDValue();
16442
16443 SDValue N0 = Narrow->getOperand(0);
16444 SDValue N1 = Narrow->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016445 SDLoc DL(Narrow);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016446
16447 // The Left side has to be a trunc.
16448 if (N0.getOpcode() != ISD::TRUNCATE)
16449 return SDValue();
16450
16451 // The type of the truncated inputs.
16452 EVT WideVT = N0->getOperand(0)->getValueType(0);
16453 if (WideVT != VT)
16454 return SDValue();
16455
16456 // The right side has to be a 'trunc' or a constant vector.
16457 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16458 bool RHSConst = (isSplatVector(N1.getNode()) &&
16459 isa<ConstantSDNode>(N1->getOperand(0)));
16460 if (!RHSTrunc && !RHSConst)
16461 return SDValue();
16462
16463 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16464
16465 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16466 return SDValue();
16467
16468 // Set N0 and N1 to hold the inputs to the new wide operation.
16469 N0 = N0->getOperand(0);
16470 if (RHSConst) {
16471 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16472 N1->getOperand(0));
16473 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16474 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16475 } else if (RHSTrunc) {
16476 N1 = N1->getOperand(0);
16477 }
16478
16479 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016480 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016481 unsigned Opcode = N->getOpcode();
16482 switch (Opcode) {
16483 case ISD::ANY_EXTEND:
16484 return Op;
16485 case ISD::ZERO_EXTEND: {
16486 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16487 APInt Mask = APInt::getAllOnesValue(InBits);
16488 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16489 return DAG.getNode(ISD::AND, DL, VT,
16490 Op, DAG.getConstant(Mask, VT));
16491 }
16492 case ISD::SIGN_EXTEND:
16493 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16494 Op, DAG.getValueType(NarrowVT));
16495 default:
16496 llvm_unreachable("Unexpected opcode");
16497 }
16498}
16499
Nate Begemanb65c1752010-12-17 22:55:37 +000016500static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16501 TargetLowering::DAGCombinerInfo &DCI,
16502 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016503 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016504 if (DCI.isBeforeLegalizeOps())
16505 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016506
Stuart Hastings865f0932011-06-03 23:53:54 +000016507 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16508 if (R.getNode())
16509 return R;
16510
Craig Topperb926afc2012-12-17 05:12:30 +000016511 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016512 // BLSI is X & (-X)
16513 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016514 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16515 SDValue N0 = N->getOperand(0);
16516 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016517 SDLoc DL(N);
Craig Topper54a11172011-10-14 07:06:56 +000016518
Craig Topperb4c94572011-10-21 06:55:01 +000016519 // Check LHS for neg
16520 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16521 isZero(N0.getOperand(0)))
16522 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16523
16524 // Check RHS for neg
16525 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16526 isZero(N1.getOperand(0)))
16527 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16528
16529 // Check LHS for X-1
16530 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16531 isAllOnes(N0.getOperand(1)))
16532 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16533
16534 // Check RHS for X-1
16535 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16536 isAllOnes(N1.getOperand(1)))
16537 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16538
Craig Topper54a11172011-10-14 07:06:56 +000016539 return SDValue();
16540 }
16541
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016542 // Want to form ANDNP nodes:
16543 // 1) In the hopes of then easily combining them with OR and AND nodes
16544 // to form PBLEND/PSIGN.
16545 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016546 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016547 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016548
Nate Begemanb65c1752010-12-17 22:55:37 +000016549 SDValue N0 = N->getOperand(0);
16550 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016551 SDLoc DL(N);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016552
Nate Begemanb65c1752010-12-17 22:55:37 +000016553 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016554 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016555 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16556 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016557 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016558
16559 // Check RHS for vnot
16560 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016561 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16562 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016563 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016564
Nate Begemanb65c1752010-12-17 22:55:37 +000016565 return SDValue();
16566}
16567
Evan Cheng760d1942010-01-04 21:22:48 +000016568static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016569 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016570 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016571 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016572 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016573 return SDValue();
16574
Stuart Hastings865f0932011-06-03 23:53:54 +000016575 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16576 if (R.getNode())
16577 return R;
16578
Evan Cheng760d1942010-01-04 21:22:48 +000016579 SDValue N0 = N->getOperand(0);
16580 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016581
Nate Begemanb65c1752010-12-17 22:55:37 +000016582 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016583 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016584 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016585 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016586 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016587
Craig Topper1666cb62011-11-19 07:07:26 +000016588 // Canonicalize pandn to RHS
16589 if (N0.getOpcode() == X86ISD::ANDNP)
16590 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016591 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016592 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16593 SDValue Mask = N1.getOperand(0);
16594 SDValue X = N1.getOperand(1);
16595 SDValue Y;
16596 if (N0.getOperand(0) == Mask)
16597 Y = N0.getOperand(1);
16598 if (N0.getOperand(1) == Mask)
16599 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016600
Craig Topper1666cb62011-11-19 07:07:26 +000016601 // Check to see if the mask appeared in both the AND and ANDNP and
16602 if (!Y.getNode())
16603 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016604
Craig Topper1666cb62011-11-19 07:07:26 +000016605 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016606 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016607 if (Mask.getOpcode() == ISD::BITCAST)
16608 Mask = Mask.getOperand(0);
16609 if (X.getOpcode() == ISD::BITCAST)
16610 X = X.getOperand(0);
16611 if (Y.getOpcode() == ISD::BITCAST)
16612 Y = Y.getOperand(0);
16613
Craig Topper1666cb62011-11-19 07:07:26 +000016614 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016615
Craig Toppered2e13d2012-01-22 19:15:14 +000016616 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016617 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16618 // there is no psrai.b
Craig Topper1666cb62011-11-19 07:07:26 +000016619 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
Michael Liao42317cc2013-03-20 02:33:21 +000016620 unsigned SraAmt = ~0;
16621 if (Mask.getOpcode() == ISD::SRA) {
16622 SDValue Amt = Mask.getOperand(1);
16623 if (isSplatVector(Amt.getNode())) {
16624 SDValue SclrAmt = Amt->getOperand(0);
16625 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt))
16626 SraAmt = C->getZExtValue();
16627 }
16628 } else if (Mask.getOpcode() == X86ISD::VSRAI) {
16629 SDValue SraC = Mask.getOperand(1);
16630 SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16631 }
Craig Topper1666cb62011-11-19 07:07:26 +000016632 if ((SraAmt + 1) != EltBits)
16633 return SDValue();
16634
Andrew Trickac6d9be2013-05-25 02:42:55 +000016635 SDLoc DL(N);
Craig Topper1666cb62011-11-19 07:07:26 +000016636
16637 // Now we know we at least have a plendvb with the mask val. See if
16638 // we can form a psignb/w/d.
16639 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016640 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16641 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016642 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16643 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16644 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016645 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016646 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016647 }
16648 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016649 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016650 return SDValue();
16651
16652 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16653
16654 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16655 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16656 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016657 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016658 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016659 }
16660 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016661
Craig Topper1666cb62011-11-19 07:07:26 +000016662 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16663 return SDValue();
16664
Nate Begemanb65c1752010-12-17 22:55:37 +000016665 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016666 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16667 std::swap(N0, N1);
16668 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16669 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016670 if (!N0.hasOneUse() || !N1.hasOneUse())
16671 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016672
16673 SDValue ShAmt0 = N0.getOperand(1);
16674 if (ShAmt0.getValueType() != MVT::i8)
16675 return SDValue();
16676 SDValue ShAmt1 = N1.getOperand(1);
16677 if (ShAmt1.getValueType() != MVT::i8)
16678 return SDValue();
16679 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16680 ShAmt0 = ShAmt0.getOperand(0);
16681 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16682 ShAmt1 = ShAmt1.getOperand(0);
16683
Andrew Trickac6d9be2013-05-25 02:42:55 +000016684 SDLoc DL(N);
Evan Cheng760d1942010-01-04 21:22:48 +000016685 unsigned Opc = X86ISD::SHLD;
16686 SDValue Op0 = N0.getOperand(0);
16687 SDValue Op1 = N1.getOperand(0);
16688 if (ShAmt0.getOpcode() == ISD::SUB) {
16689 Opc = X86ISD::SHRD;
16690 std::swap(Op0, Op1);
16691 std::swap(ShAmt0, ShAmt1);
16692 }
16693
Evan Cheng8b1190a2010-04-28 01:18:01 +000016694 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016695 if (ShAmt1.getOpcode() == ISD::SUB) {
16696 SDValue Sum = ShAmt1.getOperand(0);
16697 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016698 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16699 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16700 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16701 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016702 return DAG.getNode(Opc, DL, VT,
16703 Op0, Op1,
16704 DAG.getNode(ISD::TRUNCATE, DL,
16705 MVT::i8, ShAmt0));
16706 }
16707 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16708 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16709 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016710 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016711 return DAG.getNode(Opc, DL, VT,
16712 N0.getOperand(0), N1.getOperand(0),
16713 DAG.getNode(ISD::TRUNCATE, DL,
16714 MVT::i8, ShAmt0));
16715 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016716
Evan Cheng760d1942010-01-04 21:22:48 +000016717 return SDValue();
16718}
16719
Manman Ren92363622012-06-07 22:39:10 +000016720// Generate NEG and CMOV for integer abs.
16721static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16722 EVT VT = N->getValueType(0);
16723
16724 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16725 // 8-bit integer abs to NEG and CMOV.
16726 if (VT.isInteger() && VT.getSizeInBits() == 8)
16727 return SDValue();
16728
16729 SDValue N0 = N->getOperand(0);
16730 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016731 SDLoc DL(N);
Manman Ren92363622012-06-07 22:39:10 +000016732
16733 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16734 // and change it to SUB and CMOV.
16735 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16736 N0.getOpcode() == ISD::ADD &&
16737 N0.getOperand(1) == N1 &&
16738 N1.getOpcode() == ISD::SRA &&
16739 N1.getOperand(0) == N0.getOperand(0))
16740 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16741 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16742 // Generate SUB & CMOV.
16743 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16744 DAG.getConstant(0, VT), N0.getOperand(0));
16745
16746 SDValue Ops[] = { N0.getOperand(0), Neg,
16747 DAG.getConstant(X86::COND_GE, MVT::i8),
16748 SDValue(Neg.getNode(), 1) };
16749 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16750 Ops, array_lengthof(Ops));
16751 }
16752 return SDValue();
16753}
16754
Craig Topper3738ccd2011-12-27 06:27:23 +000016755// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016756static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16757 TargetLowering::DAGCombinerInfo &DCI,
16758 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016759 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016760 if (DCI.isBeforeLegalizeOps())
16761 return SDValue();
16762
Manman Ren45d53b82012-06-08 18:58:26 +000016763 if (Subtarget->hasCMov()) {
16764 SDValue RV = performIntegerAbsCombine(N, DAG);
16765 if (RV.getNode())
16766 return RV;
16767 }
Manman Ren92363622012-06-07 22:39:10 +000016768
16769 // Try forming BMI if it is available.
16770 if (!Subtarget->hasBMI())
16771 return SDValue();
16772
Craig Topperb4c94572011-10-21 06:55:01 +000016773 if (VT != MVT::i32 && VT != MVT::i64)
16774 return SDValue();
16775
Craig Topper3738ccd2011-12-27 06:27:23 +000016776 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16777
Craig Topperb4c94572011-10-21 06:55:01 +000016778 // Create BLSMSK instructions by finding X ^ (X-1)
16779 SDValue N0 = N->getOperand(0);
16780 SDValue N1 = N->getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +000016781 SDLoc DL(N);
Craig Topperb4c94572011-10-21 06:55:01 +000016782
16783 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16784 isAllOnes(N0.getOperand(1)))
16785 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16786
16787 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16788 isAllOnes(N1.getOperand(1)))
16789 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16790
16791 return SDValue();
16792}
16793
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016794/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16795static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016796 TargetLowering::DAGCombinerInfo &DCI,
16797 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016798 LoadSDNode *Ld = cast<LoadSDNode>(N);
16799 EVT RegVT = Ld->getValueType(0);
16800 EVT MemVT = Ld->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000016801 SDLoc dl(Ld);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016802 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016803 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016804
Michael Liaod4584c92013-03-25 23:50:10 +000016805 // On Sandybridge unaligned 256bit loads are inefficient.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016806 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016807 unsigned Alignment = Ld->getAlignment();
Michael Liaod4584c92013-03-25 23:50:10 +000016808 bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016809 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016810 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016811 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016812 if (NumElems < 2)
16813 return SDValue();
16814
Nadav Rotem48177ac2013-01-18 23:10:30 +000016815 SDValue Ptr = Ld->getBasePtr();
16816 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16817
16818 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16819 NumElems/2);
16820 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16821 Ld->getPointerInfo(), Ld->isVolatile(),
16822 Ld->isNonTemporal(), Ld->isInvariant(),
16823 Alignment);
16824 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16825 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16826 Ld->getPointerInfo(), Ld->isVolatile(),
16827 Ld->isNonTemporal(), Ld->isInvariant(),
Michael Liaod4584c92013-03-25 23:50:10 +000016828 std::min(16U, Alignment));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016829 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16830 Load1.getValue(1),
16831 Load2.getValue(1));
16832
16833 SDValue NewVec = DAG.getUNDEF(RegVT);
16834 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16835 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16836 return DCI.CombineTo(N, NewVec, TF, true);
16837 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016838
Nadav Rotemca6f2962011-09-18 19:00:23 +000016839 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016840 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16841 // expansion is still better than scalar code.
16842 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16843 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016844 // TODO: It is possible to support ZExt by zeroing the undef values
16845 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016846 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16847 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016848 assert(MemVT != RegVT && "Cannot extend to the same type");
16849 assert(MemVT.isVector() && "Must load a vector from memory");
16850
16851 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016852 unsigned MemSz = MemVT.getSizeInBits();
16853 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016854
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016855 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16856 return SDValue();
16857
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016858 // All sizes must be a power of two.
16859 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16860 return SDValue();
16861
16862 // Attempt to load the original value using scalar loads.
16863 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016864 MVT SclrLoadTy = MVT::i8;
16865 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16866 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16867 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016868 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016869 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016870 }
16871 }
16872
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016873 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16874 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16875 (64 <= MemSz))
16876 SclrLoadTy = MVT::f64;
16877
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016878 // Calculate the number of scalar loads that we need to perform
16879 // in order to load our vector from memory.
16880 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016881 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16882 return SDValue();
16883
16884 unsigned loadRegZize = RegSz;
16885 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16886 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016887
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016888 // Represent our vector as a sequence of elements which are the
16889 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016890 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016891 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016892
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016893 // Represent the data using the same element type that is stored in
16894 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016895 EVT WideVecVT =
16896 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016897 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016898
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016899 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16900 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016901
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016902 // We can't shuffle using an illegal type.
16903 if (!TLI.isTypeLegal(WideVecVT))
16904 return SDValue();
16905
16906 SmallVector<SDValue, 8> Chains;
16907 SDValue Ptr = Ld->getBasePtr();
16908 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16909 TLI.getPointerTy());
16910 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16911
16912 for (unsigned i = 0; i < NumLoads; ++i) {
16913 // Perform a single load.
16914 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16915 Ptr, Ld->getPointerInfo(),
16916 Ld->isVolatile(), Ld->isNonTemporal(),
16917 Ld->isInvariant(), Ld->getAlignment());
16918 Chains.push_back(ScalarLoad.getValue(1));
16919 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16920 // another round of DAGCombining.
16921 if (i == 0)
16922 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16923 else
16924 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16925 ScalarLoad, DAG.getIntPtrConstant(i));
16926
16927 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16928 }
16929
16930 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16931 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016932
16933 // Bitcast the loaded value to a vector of the original element type, in
16934 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016935 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016936 unsigned SizeRatio = RegSz/MemSz;
16937
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016938 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016939 // If we have SSE4.1 we can directly emit a VSEXT node.
16940 if (Subtarget->hasSSE41()) {
16941 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16942 return DCI.CombineTo(N, Sext, TF, true);
16943 }
16944
16945 // Otherwise we'll shuffle the small elements in the high bits of the
16946 // larger type and perform an arithmetic shift. If the shift is not legal
16947 // it's better to scalarize.
16948 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16949 return SDValue();
16950
16951 // Redistribute the loaded elements into the different locations.
16952 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16953 for (unsigned i = 0; i != NumElems; ++i)
16954 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16955
16956 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16957 DAG.getUNDEF(WideVecVT),
16958 &ShuffleVec[0]);
16959
16960 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16961
16962 // Build the arithmetic shift.
16963 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16964 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016965 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16966 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016967
16968 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016969 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016970
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016971 // Redistribute the loaded elements into the different locations.
16972 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016973 for (unsigned i = 0; i != NumElems; ++i)
16974 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016975
16976 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016977 DAG.getUNDEF(WideVecVT),
16978 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016979
16980 // Bitcast to the requested type.
16981 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16982 // Replace the original load with the new sequence
16983 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016984 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016985 }
16986
16987 return SDValue();
16988}
16989
Chris Lattner149a4e52008-02-22 02:09:43 +000016990/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016991static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016992 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016993 StoreSDNode *St = cast<StoreSDNode>(N);
16994 EVT VT = St->getValue().getValueType();
16995 EVT StVT = St->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000016996 SDLoc dl(St);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016997 SDValue StoredVal = St->getOperand(1);
16998 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16999
Nick Lewycky8a8d4792011-12-02 22:16:29 +000017000 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000017001 // On Sandy Bridge, 256-bit memory operations are executed by two
17002 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
17003 // memory operation.
Michael Liaod4584c92013-03-25 23:50:10 +000017004 unsigned Alignment = St->getAlignment();
17005 bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017006 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000017007 StVT == VT && !IsAligned) {
17008 unsigned NumElems = VT.getVectorNumElements();
17009 if (NumElems < 2)
17010 return SDValue();
17011
17012 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
17013 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000017014
17015 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
17016 SDValue Ptr0 = St->getBasePtr();
17017 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
17018
17019 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
17020 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000017021 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000017022 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
17023 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000017024 St->isNonTemporal(),
Michael Liaod4584c92013-03-25 23:50:10 +000017025 std::min(16U, Alignment));
Nadav Rotem5e742a32011-08-11 16:41:21 +000017026 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
17027 }
Nadav Rotem614061b2011-08-10 19:30:14 +000017028
17029 // Optimize trunc store (of multiple scalars) to shuffle and store.
17030 // First, pack all of the elements in one place. Next, store to memory
17031 // in fewer chunks.
17032 if (St->isTruncatingStore() && VT.isVector()) {
17033 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
17034 unsigned NumElems = VT.getVectorNumElements();
17035 assert(StVT != VT && "Cannot truncate to the same type");
17036 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
17037 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
17038
17039 // From, To sizes and ElemCount must be pow of two
17040 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000017041 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000017042 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000017043 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000017044
Nadav Rotem614061b2011-08-10 19:30:14 +000017045 unsigned SizeRatio = FromSz / ToSz;
17046
17047 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
17048
17049 // Create a type on which we perform the shuffle
17050 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
17051 StVT.getScalarType(), NumElems*SizeRatio);
17052
17053 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
17054
17055 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
17056 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000017057 for (unsigned i = 0; i != NumElems; ++i)
17058 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000017059
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017060 // Can't shuffle using an illegal type.
17061 if (!TLI.isTypeLegal(WideVecVT))
17062 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000017063
17064 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000017065 DAG.getUNDEF(WideVecVT),
17066 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000017067 // At this point all of the data is stored at the bottom of the
17068 // register. We now need to save it to mem.
17069
17070 // Find the largest store unit
17071 MVT StoreType = MVT::i8;
17072 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
17073 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
17074 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017075 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000017076 StoreType = Tp;
17077 }
17078
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017079 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
17080 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
17081 (64 <= NumElems * ToSz))
17082 StoreType = MVT::f64;
17083
Nadav Rotem614061b2011-08-10 19:30:14 +000017084 // Bitcast the original vector into a vector of store-size units
17085 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000017086 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000017087 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
17088 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
17089 SmallVector<SDValue, 8> Chains;
17090 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
17091 TLI.getPointerTy());
17092 SDValue Ptr = St->getBasePtr();
17093
17094 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000017095 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000017096 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
17097 StoreType, ShuffWide,
17098 DAG.getIntPtrConstant(i));
17099 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
17100 St->getPointerInfo(), St->isVolatile(),
17101 St->isNonTemporal(), St->getAlignment());
17102 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
17103 Chains.push_back(Ch);
17104 }
17105
17106 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
17107 Chains.size());
17108 }
17109
Chris Lattner149a4e52008-02-22 02:09:43 +000017110 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
17111 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000017112 // A preferable solution to the general problem is to figure out the right
17113 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000017114
17115 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000017116 if (VT.getSizeInBits() != 64)
17117 return SDValue();
17118
Devang Patel578efa92009-06-05 21:57:13 +000017119 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000017120 bool NoImplicitFloatOps = F->getAttributes().
17121 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000017122 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000017123 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000017124 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000017125 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000017126 isa<LoadSDNode>(St->getValue()) &&
17127 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
17128 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017129 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017130 LoadSDNode *Ld = 0;
17131 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000017132 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000017133 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017134 // Must be a store of a load. We currently handle two cases: the load
17135 // is a direct child, and it's under an intervening TokenFactor. It is
17136 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000017137 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000017138 Ld = cast<LoadSDNode>(St->getChain());
17139 else if (St->getValue().hasOneUse() &&
17140 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000017141 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017142 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000017143 TokenFactorIndex = i;
17144 Ld = cast<LoadSDNode>(St->getValue());
17145 } else
17146 Ops.push_back(ChainVal->getOperand(i));
17147 }
17148 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000017149
Evan Cheng536e6672009-03-12 05:59:15 +000017150 if (!Ld || !ISD::isNormalLoad(Ld))
17151 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017152
Evan Cheng536e6672009-03-12 05:59:15 +000017153 // If this is not the MMX case, i.e. we are just turning i64 load/store
17154 // into f64 load/store, avoid the transformation if there are multiple
17155 // uses of the loaded value.
17156 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
17157 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017158
Andrew Trickac6d9be2013-05-25 02:42:55 +000017159 SDLoc LdDL(Ld);
17160 SDLoc StDL(N);
Evan Cheng536e6672009-03-12 05:59:15 +000017161 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
17162 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
17163 // pair instead.
17164 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017165 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000017166 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
17167 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017168 Ld->isNonTemporal(), Ld->isInvariant(),
17169 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017170 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000017171 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000017172 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000017173 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000017174 Ops.size());
17175 }
Evan Cheng536e6672009-03-12 05:59:15 +000017176 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000017177 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017178 St->isVolatile(), St->isNonTemporal(),
17179 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000017180 }
Evan Cheng536e6672009-03-12 05:59:15 +000017181
17182 // Otherwise, lower to two pairs of 32-bit loads / stores.
17183 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017184 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
17185 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017186
Owen Anderson825b72b2009-08-11 20:47:22 +000017187 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017188 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017189 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017190 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000017191 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017192 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000017193 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017194 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000017195 MinAlign(Ld->getAlignment(), 4));
17196
17197 SDValue NewChain = LoLd.getValue(1);
17198 if (TokenFactorIndex != -1) {
17199 Ops.push_back(LoLd);
17200 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000017201 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000017202 Ops.size());
17203 }
17204
17205 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017206 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
17207 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017208
17209 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017210 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017211 St->isVolatile(), St->isNonTemporal(),
17212 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017213 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017214 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000017215 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000017216 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000017217 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000017218 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000017219 }
Dan Gohman475871a2008-07-27 21:46:04 +000017220 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000017221}
17222
Duncan Sands17470be2011-09-22 20:15:48 +000017223/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
17224/// and return the operands for the horizontal operation in LHS and RHS. A
17225/// horizontal operation performs the binary operation on successive elements
17226/// of its first operand, then on successive elements of its second operand,
17227/// returning the resulting values in a vector. For example, if
17228/// A = < float a0, float a1, float a2, float a3 >
17229/// and
17230/// B = < float b0, float b1, float b2, float b3 >
17231/// then the result of doing a horizontal operation on A and B is
17232/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
17233/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
17234/// A horizontal-op B, for some already available A and B, and if so then LHS is
17235/// set to A, RHS to B, and the routine returns 'true'.
17236/// Note that the binary operation should have the property that if one of the
17237/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017238static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000017239 // Look for the following pattern: if
17240 // A = < float a0, float a1, float a2, float a3 >
17241 // B = < float b0, float b1, float b2, float b3 >
17242 // and
17243 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17244 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17245 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17246 // which is A horizontal-op B.
17247
17248 // At least one of the operands should be a vector shuffle.
17249 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17250 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17251 return false;
17252
17253 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017254
17255 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17256 "Unsupported vector type for horizontal add/sub");
17257
17258 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17259 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017260 unsigned NumElts = VT.getVectorNumElements();
17261 unsigned NumLanes = VT.getSizeInBits()/128;
17262 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017263 assert((NumLaneElts % 2 == 0) &&
17264 "Vector type should have an even number of elements in each lane");
17265 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017266
17267 // View LHS in the form
17268 // LHS = VECTOR_SHUFFLE A, B, LMask
17269 // If LHS is not a shuffle then pretend it is the shuffle
17270 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17271 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17272 // type VT.
17273 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017274 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017275 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17276 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17277 A = LHS.getOperand(0);
17278 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17279 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017280 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17281 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017282 } else {
17283 if (LHS.getOpcode() != ISD::UNDEF)
17284 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017285 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017286 LMask[i] = i;
17287 }
17288
17289 // Likewise, view RHS in the form
17290 // RHS = VECTOR_SHUFFLE C, D, RMask
17291 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017292 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017293 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17294 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17295 C = RHS.getOperand(0);
17296 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17297 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017298 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17299 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017300 } else {
17301 if (RHS.getOpcode() != ISD::UNDEF)
17302 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017303 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017304 RMask[i] = i;
17305 }
17306
17307 // Check that the shuffles are both shuffling the same vectors.
17308 if (!(A == C && B == D) && !(A == D && B == C))
17309 return false;
17310
17311 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17312 if (!A.getNode() && !B.getNode())
17313 return false;
17314
17315 // If A and B occur in reverse order in RHS, then "swap" them (which means
17316 // rewriting the mask).
17317 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017318 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017319
17320 // At this point LHS and RHS are equivalent to
17321 // LHS = VECTOR_SHUFFLE A, B, LMask
17322 // RHS = VECTOR_SHUFFLE A, B, RMask
17323 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017324 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017325 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017326
Craig Topperf8363302011-12-02 08:18:41 +000017327 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017328 if (LIdx < 0 || RIdx < 0 ||
17329 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17330 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017331 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017332
Craig Topperf8363302011-12-02 08:18:41 +000017333 // Check that successive elements are being operated on. If not, this is
17334 // not a horizontal operation.
17335 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17336 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017337 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017338 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017339 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017340 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017341 }
17342
17343 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17344 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17345 return true;
17346}
17347
17348/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17349static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17350 const X86Subtarget *Subtarget) {
17351 EVT VT = N->getValueType(0);
17352 SDValue LHS = N->getOperand(0);
17353 SDValue RHS = N->getOperand(1);
17354
17355 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017356 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017357 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017358 isHorizontalBinOp(LHS, RHS, true))
Andrew Trickac6d9be2013-05-25 02:42:55 +000017359 return DAG.getNode(X86ISD::FHADD, SDLoc(N), VT, LHS, RHS);
Duncan Sands17470be2011-09-22 20:15:48 +000017360 return SDValue();
17361}
17362
17363/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17364static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17365 const X86Subtarget *Subtarget) {
17366 EVT VT = N->getValueType(0);
17367 SDValue LHS = N->getOperand(0);
17368 SDValue RHS = N->getOperand(1);
17369
17370 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017371 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017372 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017373 isHorizontalBinOp(LHS, RHS, false))
Andrew Trickac6d9be2013-05-25 02:42:55 +000017374 return DAG.getNode(X86ISD::FHSUB, SDLoc(N), VT, LHS, RHS);
Duncan Sands17470be2011-09-22 20:15:48 +000017375 return SDValue();
17376}
17377
Chris Lattner6cf73262008-01-25 06:14:17 +000017378/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17379/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017380static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017381 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17382 // F[X]OR(0.0, x) -> x
17383 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017384 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17385 if (C->getValueAPF().isPosZero())
17386 return N->getOperand(1);
17387 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17388 if (C->getValueAPF().isPosZero())
17389 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017390 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017391}
17392
Nadav Rotemd60cb112012-08-19 13:06:16 +000017393/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17394/// X86ISD::FMAX nodes.
17395static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17396 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17397
17398 // Only perform optimizations if UnsafeMath is used.
17399 if (!DAG.getTarget().Options.UnsafeFPMath)
17400 return SDValue();
17401
17402 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017403 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017404 unsigned NewOp = 0;
17405 switch (N->getOpcode()) {
17406 default: llvm_unreachable("unknown opcode");
17407 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17408 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17409 }
17410
Andrew Trickac6d9be2013-05-25 02:42:55 +000017411 return DAG.getNode(NewOp, SDLoc(N), N->getValueType(0),
Nadav Rotemd60cb112012-08-19 13:06:16 +000017412 N->getOperand(0), N->getOperand(1));
17413}
17414
Chris Lattneraf723b92008-01-25 05:46:26 +000017415/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017416static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017417 // FAND(0.0, x) -> 0.0
17418 // FAND(x, 0.0) -> 0.0
17419 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17420 if (C->getValueAPF().isPosZero())
17421 return N->getOperand(0);
17422 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17423 if (C->getValueAPF().isPosZero())
17424 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017425 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017426}
17427
Dan Gohmane5af2d32009-01-29 01:59:02 +000017428static SDValue PerformBTCombine(SDNode *N,
17429 SelectionDAG &DAG,
17430 TargetLowering::DAGCombinerInfo &DCI) {
17431 // BT ignores high bits in the bit index operand.
17432 SDValue Op1 = N->getOperand(1);
17433 if (Op1.hasOneUse()) {
17434 unsigned BitWidth = Op1.getValueSizeInBits();
17435 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17436 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017437 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17438 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017439 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017440 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17441 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17442 DCI.CommitTargetLoweringOpt(TLO);
17443 }
17444 return SDValue();
17445}
Chris Lattner83e6c992006-10-04 06:57:07 +000017446
Eli Friedman7a5e5552009-06-07 06:52:44 +000017447static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17448 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017449 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017450 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017451 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017452 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017453 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017454 OpVT.getVectorElementType().getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017455 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017456 }
17457 return SDValue();
17458}
17459
Matt Arsenault225ed702013-05-18 00:21:46 +000017460static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017461 const X86Subtarget *Subtarget) {
17462 EVT VT = N->getValueType(0);
17463 if (!VT.isVector())
17464 return SDValue();
17465
17466 SDValue N0 = N->getOperand(0);
17467 SDValue N1 = N->getOperand(1);
17468 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +000017469 SDLoc dl(N);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017470
17471 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17472 // both SSE and AVX2 since there is no sign-extended shift right
17473 // operation on a vector with 64-bit elements.
17474 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17475 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17476 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17477 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17478 SDValue N00 = N0.getOperand(0);
17479
Matt Arsenault225ed702013-05-18 00:21:46 +000017480 // EXTLOAD has a better solution on AVX2,
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017481 // it may be replaced with X86ISD::VSEXT node.
17482 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17483 if (!ISD::isNormalLoad(N00.getNode()))
17484 return SDValue();
17485
17486 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
Matt Arsenault225ed702013-05-18 00:21:46 +000017487 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017488 N00, N1);
17489 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17490 }
17491 }
17492 return SDValue();
17493}
17494
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017495static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17496 TargetLowering::DAGCombinerInfo &DCI,
17497 const X86Subtarget *Subtarget) {
17498 if (!DCI.isBeforeLegalizeOps())
17499 return SDValue();
17500
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017501 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017502 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017503
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017504 EVT VT = N->getValueType(0);
17505 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017506 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17507 if (R.getNode())
17508 return R;
17509 }
17510
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017511 return SDValue();
17512}
17513
Michael Liaof6c24ee2012-08-10 14:39:24 +000017514static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017515 const X86Subtarget* Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017516 SDLoc dl(N);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017517 EVT VT = N->getValueType(0);
17518
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017519 // Let legalize expand this if it isn't a legal type yet.
17520 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17521 return SDValue();
17522
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017523 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017524 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17525 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017526 return SDValue();
17527
17528 SDValue A = N->getOperand(0);
17529 SDValue B = N->getOperand(1);
17530 SDValue C = N->getOperand(2);
17531
17532 bool NegA = (A.getOpcode() == ISD::FNEG);
17533 bool NegB = (B.getOpcode() == ISD::FNEG);
17534 bool NegC = (C.getOpcode() == ISD::FNEG);
17535
Michael Liaof6c24ee2012-08-10 14:39:24 +000017536 // Negative multiplication when NegA xor NegB
17537 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017538 if (NegA)
17539 A = A.getOperand(0);
17540 if (NegB)
17541 B = B.getOperand(0);
17542 if (NegC)
17543 C = C.getOperand(0);
17544
17545 unsigned Opcode;
17546 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017547 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017548 else
Craig Topperbf404372012-08-31 15:40:30 +000017549 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17550
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017551 return DAG.getNode(Opcode, dl, VT, A, B, C);
17552}
17553
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017554static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017555 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017556 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017557 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17558 // (and (i32 x86isd::setcc_carry), 1)
17559 // This eliminates the zext. This transformation is necessary because
17560 // ISD::SETCC is always legalized to i8.
Andrew Trickac6d9be2013-05-25 02:42:55 +000017561 SDLoc dl(N);
Evan Cheng2e489c42009-12-16 00:53:11 +000017562 SDValue N0 = N->getOperand(0);
17563 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017564
Evan Cheng2e489c42009-12-16 00:53:11 +000017565 if (N0.getOpcode() == ISD::AND &&
17566 N0.hasOneUse() &&
17567 N0.getOperand(0).hasOneUse()) {
17568 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017569 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17570 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17571 if (!C || C->getZExtValue() != 1)
17572 return SDValue();
17573 return DAG.getNode(ISD::AND, dl, VT,
17574 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17575 N00.getOperand(0), N00.getOperand(1)),
17576 DAG.getConstant(1, VT));
17577 }
17578 }
17579
Craig Topper5a529e42013-01-18 06:44:29 +000017580 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017581 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17582 if (R.getNode())
17583 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017584 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017585
Evan Cheng2e489c42009-12-16 00:53:11 +000017586 return SDValue();
17587}
17588
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017589// Optimize x == -y --> x+y == 0
17590// x != -y --> x+y != 0
17591static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17592 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17593 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017594 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017595
17596 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17597 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17598 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017599 SDValue addV = DAG.getNode(ISD::ADD, SDLoc(N),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017600 LHS.getValueType(), RHS, LHS.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +000017601 return DAG.getSetCC(SDLoc(N), N->getValueType(0),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017602 addV, DAG.getConstant(0, addV.getValueType()), CC);
17603 }
17604 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17606 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017607 SDValue addV = DAG.getNode(ISD::ADD, SDLoc(N),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017608 RHS.getValueType(), LHS, RHS.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +000017609 return DAG.getSetCC(SDLoc(N), N->getValueType(0),
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017610 addV, DAG.getConstant(0, addV.getValueType()), CC);
17611 }
17612 return SDValue();
17613}
17614
Eric Christophere187e252013-01-31 00:50:48 +000017615// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17616// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017617// an all-ones bit which is more useful than 0/1 in some cases.
Andrew Trickac6d9be2013-05-25 02:42:55 +000017618static SDValue MaterializeSETB(SDLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
Shuxin Yanga5526a92012-10-31 23:11:48 +000017619 return DAG.getNode(ISD::AND, DL, MVT::i8,
17620 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17621 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17622 DAG.getConstant(1, MVT::i8));
17623}
17624
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017625// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017626static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17627 TargetLowering::DAGCombinerInfo &DCI,
17628 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017629 SDLoc DL(N);
Michael Liao2a33cec2012-08-10 19:58:13 +000017630 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17631 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017632
Shuxin Yanga5526a92012-10-31 23:11:48 +000017633 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017634 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017635 // materializing "setb reg".
17636 //
17637 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17638 // cannot take an immediate as its first operand.
17639 //
Eric Christophere187e252013-01-31 00:50:48 +000017640 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017641 EFLAGS.getValueType().isInteger() &&
17642 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017643 SDValue NewSub = DAG.getNode(X86ISD::SUB, SDLoc(EFLAGS),
Shuxin Yanga5526a92012-10-31 23:11:48 +000017644 EFLAGS.getNode()->getVTList(),
17645 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17646 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17647 return MaterializeSETB(DL, NewEFLAGS, DAG);
17648 }
17649 }
17650
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017651 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17652 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17653 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017654 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017655 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017656
Michael Liao2a33cec2012-08-10 19:58:13 +000017657 SDValue Flags;
17658
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017659 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17660 if (Flags.getNode()) {
17661 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17662 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17663 }
17664
Michael Liao2a33cec2012-08-10 19:58:13 +000017665 return SDValue();
17666}
17667
17668// Optimize branch condition evaluation.
17669//
17670static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17671 TargetLowering::DAGCombinerInfo &DCI,
17672 const X86Subtarget *Subtarget) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017673 SDLoc DL(N);
Michael Liao2a33cec2012-08-10 19:58:13 +000017674 SDValue Chain = N->getOperand(0);
17675 SDValue Dest = N->getOperand(1);
17676 SDValue EFLAGS = N->getOperand(3);
17677 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17678
17679 SDValue Flags;
17680
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017681 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17682 if (Flags.getNode()) {
17683 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17684 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17685 Flags);
17686 }
17687
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017688 return SDValue();
17689}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017690
Benjamin Kramer1396c402011-06-18 11:09:41 +000017691static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17692 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017693 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017694 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017695
17696 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017697 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017698 SDLoc dl(N);
Craig Topper7fd5e162012-04-24 06:02:29 +000017699 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017700 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17701 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17702 }
17703
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017704 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17705 // a 32-bit target where SSE doesn't support i64->FP operations.
17706 if (Op0.getOpcode() == ISD::LOAD) {
17707 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17708 EVT VT = Ld->getValueType(0);
17709 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17710 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17711 !XTLI->getSubtarget()->is64Bit() &&
17712 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017713 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17714 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017715 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17716 return FILDChain;
17717 }
17718 }
17719 return SDValue();
17720}
17721
Chris Lattner23a01992010-12-20 01:37:09 +000017722// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17723static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17724 X86TargetLowering::DAGCombinerInfo &DCI) {
17725 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17726 // the result is either zero or one (depending on the input carry bit).
17727 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17728 if (X86::isZeroNode(N->getOperand(0)) &&
17729 X86::isZeroNode(N->getOperand(1)) &&
17730 // We don't have a good way to replace an EFLAGS use, so only do this when
17731 // dead right now.
17732 SDValue(N, 1).use_empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017733 SDLoc DL(N);
Chris Lattner23a01992010-12-20 01:37:09 +000017734 EVT VT = N->getValueType(0);
17735 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17736 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17737 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17738 DAG.getConstant(X86::COND_B,MVT::i8),
17739 N->getOperand(2)),
17740 DAG.getConstant(1, VT));
17741 return DCI.CombineTo(N, Res1, CarryOut);
17742 }
17743
17744 return SDValue();
17745}
17746
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017747// fold (add Y, (sete X, 0)) -> adc 0, Y
17748// (add Y, (setne X, 0)) -> sbb -1, Y
17749// (sub (sete X, 0), Y) -> sbb 0, Y
17750// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017751static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000017752 SDLoc DL(N);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017753
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017754 // Look through ZExts.
17755 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17756 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17757 return SDValue();
17758
17759 SDValue SetCC = Ext.getOperand(0);
17760 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17761 return SDValue();
17762
17763 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17764 if (CC != X86::COND_E && CC != X86::COND_NE)
17765 return SDValue();
17766
17767 SDValue Cmp = SetCC.getOperand(1);
17768 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017769 !X86::isZeroNode(Cmp.getOperand(1)) ||
17770 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017771 return SDValue();
17772
17773 SDValue CmpOp0 = Cmp.getOperand(0);
17774 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17775 DAG.getConstant(1, CmpOp0.getValueType()));
17776
17777 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17778 if (CC == X86::COND_NE)
17779 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17780 DL, OtherVal.getValueType(), OtherVal,
17781 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17782 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17783 DL, OtherVal.getValueType(), OtherVal,
17784 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17785}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017786
Craig Topper54f952a2011-11-19 09:02:40 +000017787/// PerformADDCombine - Do target-specific dag combines on integer adds.
17788static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17789 const X86Subtarget *Subtarget) {
17790 EVT VT = N->getValueType(0);
17791 SDValue Op0 = N->getOperand(0);
17792 SDValue Op1 = N->getOperand(1);
17793
17794 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017795 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017796 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017797 isHorizontalBinOp(Op0, Op1, true))
Andrew Trickac6d9be2013-05-25 02:42:55 +000017798 return DAG.getNode(X86ISD::HADD, SDLoc(N), VT, Op0, Op1);
Craig Topper54f952a2011-11-19 09:02:40 +000017799
17800 return OptimizeConditionalInDecrement(N, DAG);
17801}
17802
17803static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17804 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017805 SDValue Op0 = N->getOperand(0);
17806 SDValue Op1 = N->getOperand(1);
17807
17808 // X86 can't encode an immediate LHS of a sub. See if we can push the
17809 // negation into a preceding instruction.
17810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017811 // If the RHS of the sub is a XOR with one use and a constant, invert the
17812 // immediate. Then add one to the LHS of the sub so we can turn
17813 // X-Y -> X+~Y+1, saving one register.
17814 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17815 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017816 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017817 EVT VT = Op0.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000017818 SDValue NewXor = DAG.getNode(ISD::XOR, SDLoc(Op1), VT,
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017819 Op1.getOperand(0),
17820 DAG.getConstant(~XorC, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +000017821 return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017822 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017823 }
17824 }
17825
Craig Topper54f952a2011-11-19 09:02:40 +000017826 // Try to synthesize horizontal adds from adds of shuffles.
17827 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017828 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017829 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017830 isHorizontalBinOp(Op0, Op1, true))
Andrew Trickac6d9be2013-05-25 02:42:55 +000017831 return DAG.getNode(X86ISD::HSUB, SDLoc(N), VT, Op0, Op1);
Craig Topper54f952a2011-11-19 09:02:40 +000017832
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017833 return OptimizeConditionalInDecrement(N, DAG);
17834}
17835
Michael Liaod9d09602012-10-23 17:34:00 +000017836/// performVZEXTCombine - Performs build vector combines
17837static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17838 TargetLowering::DAGCombinerInfo &DCI,
17839 const X86Subtarget *Subtarget) {
17840 // (vzext (bitcast (vzext (x)) -> (vzext x)
17841 SDValue In = N->getOperand(0);
17842 while (In.getOpcode() == ISD::BITCAST)
17843 In = In.getOperand(0);
17844
17845 if (In.getOpcode() != X86ISD::VZEXT)
17846 return SDValue();
17847
Andrew Trickac6d9be2013-05-25 02:42:55 +000017848 return DAG.getNode(X86ISD::VZEXT, SDLoc(N), N->getValueType(0),
Nadav Rotemb39a5522013-02-14 18:20:48 +000017849 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017850}
17851
Dan Gohman475871a2008-07-27 21:46:04 +000017852SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017853 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017854 SelectionDAG &DAG = DCI.DAG;
17855 switch (N->getOpcode()) {
17856 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017857 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017858 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017859 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017860 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017861 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017862 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17863 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017864 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017865 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017866 case ISD::SHL:
17867 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017868 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017869 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017870 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017871 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017872 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017873 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017874 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017875 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17876 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017877 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017878 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017879 case X86ISD::FMIN:
17880 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017881 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017882 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017883 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017884 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017885 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017886 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017887 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017888 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017889 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017890 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017891 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017892 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017893 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017894 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017895 case X86ISD::UNPCKH:
17896 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017897 case X86ISD::MOVHLPS:
17898 case X86ISD::MOVLHPS:
17899 case X86ISD::PSHUFD:
17900 case X86ISD::PSHUFHW:
17901 case X86ISD::PSHUFLW:
17902 case X86ISD::MOVSS:
17903 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017904 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017905 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017906 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017907 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017908 }
17909
Dan Gohman475871a2008-07-27 21:46:04 +000017910 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017911}
17912
Evan Chenge5b51ac2010-04-17 06:13:15 +000017913/// isTypeDesirableForOp - Return true if the target has native support for
17914/// the specified value type and it is 'desirable' to use the type for the
17915/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17916/// instruction encodings are longer and some i16 instructions are slow.
17917bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17918 if (!isTypeLegal(VT))
17919 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017920 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017921 return true;
17922
17923 switch (Opc) {
17924 default:
17925 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017926 case ISD::LOAD:
17927 case ISD::SIGN_EXTEND:
17928 case ISD::ZERO_EXTEND:
17929 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017930 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017931 case ISD::SRL:
17932 case ISD::SUB:
17933 case ISD::ADD:
17934 case ISD::MUL:
17935 case ISD::AND:
17936 case ISD::OR:
17937 case ISD::XOR:
17938 return false;
17939 }
17940}
17941
17942/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017943/// beneficial for dag combiner to promote the specified node. If true, it
17944/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017945bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017946 EVT VT = Op.getValueType();
17947 if (VT != MVT::i16)
17948 return false;
17949
Evan Cheng4c26e932010-04-19 19:29:22 +000017950 bool Promote = false;
17951 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017952 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017953 default: break;
17954 case ISD::LOAD: {
17955 LoadSDNode *LD = cast<LoadSDNode>(Op);
17956 // If the non-extending load has a single use and it's not live out, then it
17957 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017958 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17959 Op.hasOneUse()*/) {
17960 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17961 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17962 // The only case where we'd want to promote LOAD (rather then it being
17963 // promoted as an operand is when it's only use is liveout.
17964 if (UI->getOpcode() != ISD::CopyToReg)
17965 return false;
17966 }
17967 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017968 Promote = true;
17969 break;
17970 }
17971 case ISD::SIGN_EXTEND:
17972 case ISD::ZERO_EXTEND:
17973 case ISD::ANY_EXTEND:
17974 Promote = true;
17975 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017976 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017977 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017978 SDValue N0 = Op.getOperand(0);
17979 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017980 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017981 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017982 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017983 break;
17984 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017985 case ISD::ADD:
17986 case ISD::MUL:
17987 case ISD::AND:
17988 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017989 case ISD::XOR:
17990 Commute = true;
17991 // fallthrough
17992 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017993 SDValue N0 = Op.getOperand(0);
17994 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017995 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017996 return false;
17997 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017998 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017999 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000018000 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000018001 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000018002 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000018003 }
18004 }
18005
18006 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000018007 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000018008}
18009
Evan Cheng60c07e12006-07-05 22:17:51 +000018010//===----------------------------------------------------------------------===//
18011// X86 Inline Assembly Support
18012//===----------------------------------------------------------------------===//
18013
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018014namespace {
18015 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000018016 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018017 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018018
Benjamin Kramer0581ed72011-12-18 20:51:31 +000018019 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018020 StringRef piece(*args[i]);
18021 if (!s.startswith(piece)) // Check if the piece matches.
18022 return false;
18023
18024 s = s.substr(piece.size());
18025 StringRef::size_type pos = s.find_first_not_of(" \t");
18026 if (pos == 0) // We matched a prefix.
18027 return false;
18028
18029 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018030 }
18031
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018032 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018033 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000018034 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018035}
18036
Chris Lattnerb8105652009-07-20 17:51:36 +000018037bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
18038 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000018039
18040 std::string AsmStr = IA->getAsmString();
18041
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018042 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
18043 if (!Ty || Ty->getBitWidth() % 16 != 0)
18044 return false;
18045
Chris Lattnerb8105652009-07-20 17:51:36 +000018046 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000018047 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000018048 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000018049
18050 switch (AsmPieces.size()) {
18051 default: return false;
18052 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000018053 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018054 // we will turn this bswap into something that will be lowered to logical
18055 // ops instead of emitting the bswap asm. For now, we don't support 486 or
18056 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000018057 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018058 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
18059 matchAsm(AsmPieces[0], "bswapl", "$0") ||
18060 matchAsm(AsmPieces[0], "bswapq", "$0") ||
18061 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
18062 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
18063 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000018064 // No need to check constraints, nothing other than the equivalent of
18065 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000018066 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018067 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018068
Chris Lattnerb8105652009-07-20 17:51:36 +000018069 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000018070 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018071 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018072 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
18073 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000018074 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000018075 const std::string &ConstraintsStr = IA->getConstraintString();
18076 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000018077 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000018078 if (AsmPieces.size() == 4 &&
18079 AsmPieces[0] == "~{cc}" &&
18080 AsmPieces[1] == "~{dirflag}" &&
18081 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018082 AsmPieces[3] == "~{fpsr}")
18083 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018084 }
18085 break;
18086 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000018087 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018088 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018089 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
18090 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
18091 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018092 AsmPieces.clear();
18093 const std::string &ConstraintsStr = IA->getConstraintString();
18094 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000018095 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018096 if (AsmPieces.size() == 4 &&
18097 AsmPieces[0] == "~{cc}" &&
18098 AsmPieces[1] == "~{dirflag}" &&
18099 AsmPieces[2] == "~{flags}" &&
18100 AsmPieces[3] == "~{fpsr}")
18101 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000018102 }
Evan Cheng55d42002011-01-08 01:24:27 +000018103
18104 if (CI->getType()->isIntegerTy(64)) {
18105 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
18106 if (Constraints.size() >= 2 &&
18107 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
18108 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
18109 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000018110 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
18111 matchAsm(AsmPieces[1], "bswap", "%edx") &&
18112 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000018113 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000018114 }
18115 }
18116 break;
18117 }
18118 return false;
18119}
18120
Chris Lattnerf4dff842006-07-11 02:54:03 +000018121/// getConstraintType - Given a constraint letter, return the type of
18122/// constraint it is for this target.
18123X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000018124X86TargetLowering::getConstraintType(const std::string &Constraint) const {
18125 if (Constraint.size() == 1) {
18126 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000018127 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000018128 case 'q':
18129 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000018130 case 'f':
18131 case 't':
18132 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000018133 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000018134 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000018135 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000018136 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000018137 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000018138 case 'a':
18139 case 'b':
18140 case 'c':
18141 case 'd':
18142 case 'S':
18143 case 'D':
18144 case 'A':
18145 return C_Register;
18146 case 'I':
18147 case 'J':
18148 case 'K':
18149 case 'L':
18150 case 'M':
18151 case 'N':
18152 case 'G':
18153 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000018154 case 'e':
18155 case 'Z':
18156 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000018157 default:
18158 break;
18159 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000018160 }
Chris Lattner4234f572007-03-25 02:14:49 +000018161 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000018162}
18163
John Thompson44ab89e2010-10-29 17:29:13 +000018164/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000018165/// This object must already have been set up with the operand type
18166/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000018167TargetLowering::ConstraintWeight
18168 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000018169 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000018170 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018171 Value *CallOperandVal = info.CallOperandVal;
18172 // If we don't have a value, we can't do a match,
18173 // but allow it at the lowest weight.
18174 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000018175 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000018176 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000018177 // Look at the constraint type.
18178 switch (*constraint) {
18179 default:
John Thompson44ab89e2010-10-29 17:29:13 +000018180 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
18181 case 'R':
18182 case 'q':
18183 case 'Q':
18184 case 'a':
18185 case 'b':
18186 case 'c':
18187 case 'd':
18188 case 'S':
18189 case 'D':
18190 case 'A':
18191 if (CallOperandVal->getType()->isIntegerTy())
18192 weight = CW_SpecificReg;
18193 break;
18194 case 'f':
18195 case 't':
18196 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018197 if (type->isFloatingPointTy())
18198 weight = CW_SpecificReg;
18199 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018200 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018201 if (type->isX86_MMXTy() && Subtarget->hasMMX())
18202 weight = CW_SpecificReg;
18203 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018204 case 'x':
18205 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000018206 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018207 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000018208 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018209 break;
18210 case 'I':
18211 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
18212 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000018213 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018214 }
18215 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018216 case 'J':
18217 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18218 if (C->getZExtValue() <= 63)
18219 weight = CW_Constant;
18220 }
18221 break;
18222 case 'K':
18223 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18224 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
18225 weight = CW_Constant;
18226 }
18227 break;
18228 case 'L':
18229 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18230 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
18231 weight = CW_Constant;
18232 }
18233 break;
18234 case 'M':
18235 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18236 if (C->getZExtValue() <= 3)
18237 weight = CW_Constant;
18238 }
18239 break;
18240 case 'N':
18241 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18242 if (C->getZExtValue() <= 0xff)
18243 weight = CW_Constant;
18244 }
18245 break;
18246 case 'G':
18247 case 'C':
18248 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18249 weight = CW_Constant;
18250 }
18251 break;
18252 case 'e':
18253 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18254 if ((C->getSExtValue() >= -0x80000000LL) &&
18255 (C->getSExtValue() <= 0x7fffffffLL))
18256 weight = CW_Constant;
18257 }
18258 break;
18259 case 'Z':
18260 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18261 if (C->getZExtValue() <= 0xffffffff)
18262 weight = CW_Constant;
18263 }
18264 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018265 }
18266 return weight;
18267}
18268
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018269/// LowerXConstraint - try to replace an X constraint, which matches anything,
18270/// with another that has more specific requirements based on the type of the
18271/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018272const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018273LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018274 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18275 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018276 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018277 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018278 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018279 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018280 return "x";
18281 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018282
Chris Lattner5e764232008-04-26 23:02:14 +000018283 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018284}
18285
Chris Lattner48884cd2007-08-25 00:47:38 +000018286/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18287/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018288void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018289 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018290 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018291 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018292 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018293
Eric Christopher100c8332011-06-02 23:16:42 +000018294 // Only support length 1 constraints for now.
18295 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018296
Eric Christopher100c8332011-06-02 23:16:42 +000018297 char ConstraintLetter = Constraint[0];
18298 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018299 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018300 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018302 if (C->getZExtValue() <= 31) {
18303 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018304 break;
18305 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018306 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018307 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018308 case 'J':
18309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018310 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018311 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18312 break;
18313 }
18314 }
18315 return;
18316 case 'K':
18317 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018318 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018319 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18320 break;
18321 }
18322 }
18323 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018324 case 'N':
18325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018326 if (C->getZExtValue() <= 255) {
18327 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018328 break;
18329 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018330 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018331 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018332 case 'e': {
18333 // 32-bit signed value
18334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018335 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18336 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018337 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018338 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018339 break;
18340 }
18341 // FIXME gcc accepts some relocatable values here too, but only in certain
18342 // memory models; it's complicated.
18343 }
18344 return;
18345 }
18346 case 'Z': {
18347 // 32-bit unsigned value
18348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018349 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18350 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018351 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18352 break;
18353 }
18354 }
18355 // FIXME gcc accepts some relocatable values here too, but only in certain
18356 // memory models; it's complicated.
18357 return;
18358 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018359 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018360 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018361 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018362 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018363 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018364 break;
18365 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018366
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018367 // In any sort of PIC mode addresses need to be computed at runtime by
18368 // adding in a register or some sort of table lookup. These can't
18369 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018370 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018371 return;
18372
Chris Lattnerdc43a882007-05-03 16:52:29 +000018373 // If we are in non-pic codegen mode, we allow the address of a global (with
18374 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018375 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018376 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018377
Chris Lattner49921962009-05-08 18:23:14 +000018378 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18379 while (1) {
18380 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18381 Offset += GA->getOffset();
18382 break;
18383 } else if (Op.getOpcode() == ISD::ADD) {
18384 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18385 Offset += C->getZExtValue();
18386 Op = Op.getOperand(0);
18387 continue;
18388 }
18389 } else if (Op.getOpcode() == ISD::SUB) {
18390 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18391 Offset += -C->getZExtValue();
18392 Op = Op.getOperand(0);
18393 continue;
18394 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018395 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018396
Chris Lattner49921962009-05-08 18:23:14 +000018397 // Otherwise, this isn't something we can handle, reject it.
18398 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018399 }
Eric Christopherfd179292009-08-27 18:07:15 +000018400
Dan Gohman46510a72010-04-15 01:51:59 +000018401 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018402 // If we require an extra load to get this address, as in PIC mode, we
18403 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018404 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18405 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018406 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018407
Andrew Trickac6d9be2013-05-25 02:42:55 +000018408 Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
Devang Patel0d881da2010-07-06 22:08:15 +000018409 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018410 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018411 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018412 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018413
Gabor Greifba36cb52008-08-28 21:40:38 +000018414 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018415 Ops.push_back(Result);
18416 return;
18417 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018418 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018419}
18420
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018421std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018422X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier5b3fca52013-06-22 18:37:38 +000018423 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018424 // First, see if this is a constraint that directly corresponds to an LLVM
18425 // register class.
18426 if (Constraint.size() == 1) {
18427 // GCC Constraint Letters
18428 switch (Constraint[0]) {
18429 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018430 // TODO: Slight differences here in allocation order and leaving
18431 // RIP in the class. Do they matter any more here than they do
18432 // in the normal allocation?
18433 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18434 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018435 if (VT == MVT::i32 || VT == MVT::f32)
18436 return std::make_pair(0U, &X86::GR32RegClass);
18437 if (VT == MVT::i16)
18438 return std::make_pair(0U, &X86::GR16RegClass);
18439 if (VT == MVT::i8 || VT == MVT::i1)
18440 return std::make_pair(0U, &X86::GR8RegClass);
18441 if (VT == MVT::i64 || VT == MVT::f64)
18442 return std::make_pair(0U, &X86::GR64RegClass);
18443 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018444 }
18445 // 32-bit fallthrough
18446 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018447 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018448 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18449 if (VT == MVT::i16)
18450 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18451 if (VT == MVT::i8 || VT == MVT::i1)
18452 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18453 if (VT == MVT::i64)
18454 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018455 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018456 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018457 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018458 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018459 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018460 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018461 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018462 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018463 return std::make_pair(0U, &X86::GR32RegClass);
18464 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018465 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018466 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018467 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018468 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018469 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018470 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018471 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18472 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018473 case 'f': // FP Stack registers.
18474 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18475 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018476 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018477 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018478 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018479 return std::make_pair(0U, &X86::RFP64RegClass);
18480 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018481 case 'y': // MMX_REGS if MMX allowed.
18482 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018483 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018484 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018485 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018486 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018487 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018488 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018489
Chad Rosier5b3fca52013-06-22 18:37:38 +000018490 switch (VT.SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018491 default: break;
18492 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018493 case MVT::f32:
18494 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018495 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018496 case MVT::f64:
18497 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018498 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018499 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018500 case MVT::v16i8:
18501 case MVT::v8i16:
18502 case MVT::v4i32:
18503 case MVT::v2i64:
18504 case MVT::v4f32:
18505 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018506 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018507 // AVX types.
18508 case MVT::v32i8:
18509 case MVT::v16i16:
18510 case MVT::v8i32:
18511 case MVT::v4i64:
18512 case MVT::v8f32:
18513 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018514 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018515 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018516 break;
18517 }
18518 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018519
Chris Lattnerf76d1802006-07-31 23:26:50 +000018520 // Use the default implementation in TargetLowering to convert the register
18521 // constraint into a member of a register class.
18522 std::pair<unsigned, const TargetRegisterClass*> Res;
18523 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018524
18525 // Not found as a standard register?
18526 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018527 // Map st(0) -> st(7) -> ST0
18528 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18529 tolower(Constraint[1]) == 's' &&
18530 tolower(Constraint[2]) == 't' &&
18531 Constraint[3] == '(' &&
18532 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18533 Constraint[5] == ')' &&
18534 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018535
Chris Lattner56d77c72009-09-13 22:41:48 +000018536 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018537 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018538 return Res;
18539 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018540
Chris Lattner56d77c72009-09-13 22:41:48 +000018541 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018542 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018543 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018544 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018545 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018546 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018547
18548 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018549 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018550 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018551 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018552 return Res;
18553 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018554
Dale Johannesen330169f2008-11-13 21:52:36 +000018555 // 'A' means EAX + EDX.
18556 if (Constraint == "A") {
18557 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018558 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018559 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018560 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018561 return Res;
18562 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018563
Chris Lattnerf76d1802006-07-31 23:26:50 +000018564 // Otherwise, check to see if this is a register class of the wrong value
18565 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18566 // turn into {ax},{dx}.
18567 if (Res.second->hasType(VT))
18568 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018569
Chris Lattnerf76d1802006-07-31 23:26:50 +000018570 // All of the single-register GCC register classes map their values onto
18571 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18572 // really want an 8-bit or 32-bit register, map to the appropriate register
18573 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018574 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018575 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018576 unsigned DestReg = 0;
18577 switch (Res.first) {
18578 default: break;
18579 case X86::AX: DestReg = X86::AL; break;
18580 case X86::DX: DestReg = X86::DL; break;
18581 case X86::CX: DestReg = X86::CL; break;
18582 case X86::BX: DestReg = X86::BL; break;
18583 }
18584 if (DestReg) {
18585 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018586 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018587 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018588 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018589 unsigned DestReg = 0;
18590 switch (Res.first) {
18591 default: break;
18592 case X86::AX: DestReg = X86::EAX; break;
18593 case X86::DX: DestReg = X86::EDX; break;
18594 case X86::CX: DestReg = X86::ECX; break;
18595 case X86::BX: DestReg = X86::EBX; break;
18596 case X86::SI: DestReg = X86::ESI; break;
18597 case X86::DI: DestReg = X86::EDI; break;
18598 case X86::BP: DestReg = X86::EBP; break;
18599 case X86::SP: DestReg = X86::ESP; break;
18600 }
18601 if (DestReg) {
18602 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018603 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018604 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018605 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018606 unsigned DestReg = 0;
18607 switch (Res.first) {
18608 default: break;
18609 case X86::AX: DestReg = X86::RAX; break;
18610 case X86::DX: DestReg = X86::RDX; break;
18611 case X86::CX: DestReg = X86::RCX; break;
18612 case X86::BX: DestReg = X86::RBX; break;
18613 case X86::SI: DestReg = X86::RSI; break;
18614 case X86::DI: DestReg = X86::RDI; break;
18615 case X86::BP: DestReg = X86::RBP; break;
18616 case X86::SP: DestReg = X86::RSP; break;
18617 }
18618 if (DestReg) {
18619 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018620 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018621 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018622 }
Craig Topperc9099502012-04-20 06:31:50 +000018623 } else if (Res.second == &X86::FR32RegClass ||
18624 Res.second == &X86::FR64RegClass ||
18625 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018626 // Handle references to XMM physical registers that got mapped into the
18627 // wrong class. This can happen with constraints like {xmm0} where the
18628 // target independent register mapper will just pick the first match it can
18629 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018630
18631 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018632 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018633 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018634 Res.second = &X86::FR64RegClass;
18635 else if (X86::VR128RegClass.hasType(VT))
18636 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018637 else if (X86::VR256RegClass.hasType(VT))
18638 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018639 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018640
Chris Lattnerf76d1802006-07-31 23:26:50 +000018641 return Res;
18642}