blob: 71288c9f04c3672064c02173a850ba93f3671abe [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.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
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,
David Greenea5f26012011-02-07 19:36:54 +0000107 DebugLoc 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,
137 DebugLoc 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();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000166
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000167 RegInfo = TM.getRegisterInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +0000168 TD = getDataLayout();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000169
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000170 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000171 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000172
173 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000174 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000175 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
176 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000177
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 // For 64-bit since we have so many registers use the ILP scheduler, for
179 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000180 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000181 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000182 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000183 else if (Subtarget->is64Bit())
184 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000185 else
186 setSchedulingPreference(Sched::RegPressure);
Michael Liaoc5c970e2012-10-31 04:14:09 +0000187 setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
Evan Cheng714554d2006-03-16 21:47:42 +0000188
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000189 // Bypass expensive divides on Atom when compiling with O2
190 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default) {
Preston Gurd8d662b52012-10-04 21:33:40 +0000191 addBypassSlowDiv(32, 8);
Preston Gurd9a2cfff2013-03-04 18:13:57 +0000192 if (Subtarget->is64Bit())
193 addBypassSlowDiv(64, 16);
194 }
Preston Gurd2e2efd92012-09-04 18:22:17 +0000195
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000196 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000197 // Setup Windows compiler runtime calls.
198 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000199 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000200 setLibcallName(RTLIB::SREM_I64, "_allrem");
201 setLibcallName(RTLIB::UREM_I64, "_aullrem");
202 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000203 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000204 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000205 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
206 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
207 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000208
209 // The _ftol2 runtime function has an unusual calling conv, which
210 // is modeled by a special pseudo-instruction.
211 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
212 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
213 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
214 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000215 }
216
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000217 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000218 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000219 setUseUnderscoreSetJmp(false);
220 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000221 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000222 // MS runtime is weird: it exports _setjmp, but longjmp!
223 setUseUnderscoreSetJmp(true);
224 setUseUnderscoreLongJmp(false);
225 } else {
226 setUseUnderscoreSetJmp(true);
227 setUseUnderscoreLongJmp(true);
228 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000229
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000230 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000231 addRegisterClass(MVT::i8, &X86::GR8RegClass);
232 addRegisterClass(MVT::i16, &X86::GR16RegClass);
233 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000235 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000238
Scott Michelfdc40a02009-02-17 22:15:04 +0000239 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000241 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000243 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
245 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000246
247 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
249 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
250 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
251 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
252 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
253 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000254
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000255 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
256 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
258 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
259 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000260
Evan Cheng25ab6902006-09-08 06:48:29 +0000261 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000264 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000265 // We have an algorithm for SSE2->double, and we turn this into a
266 // 64-bit FILD followed by conditional FADD for other targets.
267 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000268 // We have an algorithm for SSE2, and we turn this into a 64-bit
269 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000270 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000272
273 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
274 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
276 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000278 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000279 // SSE has no i16 to fp conversion, only i32
280 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000282 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000284 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
286 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000287 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000288 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
290 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000291 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000292
Dale Johannesen73328d12007-09-19 23:55:34 +0000293 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
294 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
296 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000297
Evan Cheng02568ff2006-01-30 22:13:22 +0000298 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
299 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
301 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000302
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000303 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000305 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000307 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
309 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310 }
311
312 // Handle FP_TO_UINT by promoting the destination to a larger signed
313 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
315 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
316 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000317
Evan Cheng25ab6902006-09-08 06:48:29 +0000318 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000321 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000322 // Since AVX is a superset of SSE3, only check for SSE here.
323 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 // Expand FP_TO_UINT into a select.
325 // FIXME: We would like to use a Custom expander here eventually to do
326 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000329 // With SSE3 we can use fisttpll to convert to a signed i64; without
330 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000332 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000333
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000334 if (isTargetFTOL()) {
335 // Use the _ftol2 runtime function, which has a pseudo-instruction
336 // to handle its weird calling convention.
337 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
338 }
339
Chris Lattner399610a2006-12-05 18:22:22 +0000340 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000341 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000342 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
343 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000344 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000345 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000346 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000347 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000348 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000349 }
Chris Lattner21f66852005-12-23 05:15:23 +0000350
Dan Gohmanb00ee212008-02-18 19:34:53 +0000351 // Scalar integer divide and remainder are lowered to use operations that
352 // produce two results, to match the available instructions. This exposes
353 // the two-result form to trivial CSE, which is able to combine x/y and x%y
354 // into a single instruction.
355 //
356 // Scalar integer multiply-high is also lowered to use two-result
357 // operations, to match the available instructions. However, plain multiply
358 // (low) operations are left as Legal, as there are single-result
359 // instructions for this in x86. Using the two-result multiply instructions
360 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000361 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000362 MVT VT = IntVTs[i];
363 setOperationAction(ISD::MULHS, VT, Expand);
364 setOperationAction(ISD::MULHU, VT, Expand);
365 setOperationAction(ISD::SDIV, VT, Expand);
366 setOperationAction(ISD::UDIV, VT, Expand);
367 setOperationAction(ISD::SREM, VT, Expand);
368 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000369
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000370 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000371 setOperationAction(ISD::ADDC, VT, Custom);
372 setOperationAction(ISD::ADDE, VT, Custom);
373 setOperationAction(ISD::SUBC, VT, Custom);
374 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000375 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000376
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
378 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Tom Stellard3ef53832013-03-08 15:36:57 +0000379 setOperationAction(ISD::BR_CC , MVT::f32, Expand);
380 setOperationAction(ISD::BR_CC , MVT::f64, Expand);
381 setOperationAction(ISD::BR_CC , MVT::f80, Expand);
382 setOperationAction(ISD::BR_CC , MVT::i8, Expand);
383 setOperationAction(ISD::BR_CC , MVT::i16, Expand);
384 setOperationAction(ISD::BR_CC , MVT::i32, Expand);
385 setOperationAction(ISD::BR_CC , MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
389 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
390 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
391 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
392 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
393 setOperationAction(ISD::FREM , MVT::f32 , Expand);
394 setOperationAction(ISD::FREM , MVT::f64 , Expand);
395 setOperationAction(ISD::FREM , MVT::f80 , Expand);
396 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000397
Chandler Carruth77821022011-12-24 12:12:34 +0000398 // Promote the i8 variants and force them on up to i32 which has a shorter
399 // encoding.
400 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
401 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
402 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
403 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000404 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000405 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
406 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
407 if (Subtarget->is64Bit())
408 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000409 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000410 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
411 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
414 }
Craig Topper37f21672011-10-11 06:44:02 +0000415
416 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000417 // When promoting the i8 variants, force them to i32 for a shorter
418 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000419 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000420 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
421 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
422 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
424 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
425 if (Subtarget->is64Bit())
426 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000427 } else {
428 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
429 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
430 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000431 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
432 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
433 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
434 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000435 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000436 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
437 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000438 }
439
Benjamin Kramer1292c222010-12-04 20:32:23 +0000440 if (Subtarget->hasPOPCNT()) {
441 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
442 } else {
443 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
444 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
445 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
446 if (Subtarget->is64Bit())
447 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
448 }
449
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
451 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000452
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000453 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000454 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000455 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000456 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000457 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
459 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
460 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
461 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
462 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000463 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
465 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
466 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
467 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000468 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000470 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000471 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Michael Liao6c0e04c2012-10-15 22:39:43 +0000473 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intened to support
474 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
Michael Liao281ae5a2012-10-17 02:22:27 +0000475 // support continuation, user-level threading, and etc.. As a result, no
Michael Liao6c0e04c2012-10-15 22:39:43 +0000476 // other SjLj exception interfaces are implemented and please don't build
477 // your own exception handling based on them.
478 // LLVM/Clang supports zero-cost DWARF exception handling.
479 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
480 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000481
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000482 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
484 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
485 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
486 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000487 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
489 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000490 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000491 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
493 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
494 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
495 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000496 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000497 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000498 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
500 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
501 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000502 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000503 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
504 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
505 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000506 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507
Craig Topper1accb7e2012-01-10 06:54:16 +0000508 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000510
Eric Christopher9a9d2752010-07-22 02:48:34 +0000511 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000512 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000513
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000514 // On X86 and X86-64, atomic operations are lowered to locked instructions.
515 // Locked instructions, in turn, have implicit fence semantics (all memory
516 // operations are flushed before issuing the locked instruction, and they
517 // are not buffered), so we can fold away the common pattern of
518 // fence-atomic-fence.
519 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000520
Mon P Wang63307c32008-05-05 19:05:59 +0000521 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000522 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000523 MVT VT = IntVTs[i];
524 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
525 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000526 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000527 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000528
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000529 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000530 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
532 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
533 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
534 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
535 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
536 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
537 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Michael Liaoe5e8f762012-09-25 18:08:13 +0000538 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
539 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
540 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
541 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000542 }
543
Eli Friedman43f51ae2011-08-26 21:21:21 +0000544 if (Subtarget->hasCmpxchg16b()) {
545 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
546 }
547
Evan Cheng3c992d22006-03-07 02:02:57 +0000548 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000549 if (!Subtarget->isTargetDarwin() &&
550 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000551 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000553 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000554
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
556 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
557 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
558 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000559 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000560 setExceptionPointerRegister(X86::RAX);
561 setExceptionSelectorRegister(X86::RDX);
562 } else {
563 setExceptionPointerRegister(X86::EAX);
564 setExceptionSelectorRegister(X86::EDX);
565 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
567 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000568
Duncan Sands4a544a72011-09-06 13:37:06 +0000569 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
570 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Shuxin Yang970755e2012-10-19 20:11:16 +0000573 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000574
Nate Begemanacc398c2006-01-25 18:21:52 +0000575 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::VASTART , MVT::Other, Custom);
577 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000578 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setOperationAction(ISD::VAARG , MVT::Other, Custom);
580 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000581 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setOperationAction(ISD::VAARG , MVT::Other, Expand);
583 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000584 }
Evan Chengae642192007-03-02 23:16:35 +0000585
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
587 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000588
589 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
590 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
591 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000592 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000593 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
594 MVT::i64 : MVT::i32, Custom);
595 else
596 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
597 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000598
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000599 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000600 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000601 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000602 addRegisterClass(MVT::f32, &X86::FR32RegClass);
603 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000604
Evan Cheng223547a2006-01-31 22:28:30 +0000605 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::FABS , MVT::f64, Custom);
607 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000608
609 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::FNEG , MVT::f64, Custom);
611 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000612
Evan Cheng68c47cb2007-01-05 07:55:56 +0000613 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
615 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000616
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000617 // Lower this to FGETSIGNx86 plus an AND.
618 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
619 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
620
Evan Chengd25e9e82006-02-02 00:28:23 +0000621 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000622 setOperationAction(ISD::FSIN , MVT::f64, Expand);
623 setOperationAction(ISD::FCOS , MVT::f64, Expand);
624 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
625 setOperationAction(ISD::FSIN , MVT::f32, Expand);
626 setOperationAction(ISD::FCOS , MVT::f32, Expand);
627 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000628
Chris Lattnera54aa942006-01-29 06:26:08 +0000629 // Expand FP immediates into loads from the stack, except for the special
630 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000631 addLegalFPImmediate(APFloat(+0.0)); // xorpd
632 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000633 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000634 // Use SSE for f32, x87 for f64.
635 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000636 addRegisterClass(MVT::f32, &X86::FR32RegClass);
637 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000638
639 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641
642 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000644
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000646
647 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
649 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000650
651 // We don't support sin/cos/fmod
Evan Cheng8688a582013-01-29 02:32:37 +0000652 setOperationAction(ISD::FSIN , MVT::f32, Expand);
653 setOperationAction(ISD::FCOS , MVT::f32, Expand);
654 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000655
Nate Begemane1795842008-02-14 08:57:00 +0000656 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657 addLegalFPImmediate(APFloat(+0.0f)); // xorps
658 addLegalFPImmediate(APFloat(+0.0)); // FLD0
659 addLegalFPImmediate(APFloat(+1.0)); // FLD1
660 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
661 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
662
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000663 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000664 setOperationAction(ISD::FSIN , MVT::f64, Expand);
665 setOperationAction(ISD::FCOS , MVT::f64, Expand);
666 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000667 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000669 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000670 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000671 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
672 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
675 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
676 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
677 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000678
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000679 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000680 setOperationAction(ISD::FSIN , MVT::f64, Expand);
681 setOperationAction(ISD::FSIN , MVT::f32, Expand);
682 setOperationAction(ISD::FCOS , MVT::f64, Expand);
683 setOperationAction(ISD::FCOS , MVT::f32, Expand);
684 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
685 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000686 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000687 addLegalFPImmediate(APFloat(+0.0)); // FLD0
688 addLegalFPImmediate(APFloat(+1.0)); // FLD1
689 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
690 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000691 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
692 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
693 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
694 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000695 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000696
Cameron Zwarich33390842011-07-08 21:39:21 +0000697 // We don't support FMA.
698 setOperationAction(ISD::FMA, MVT::f64, Expand);
699 setOperationAction(ISD::FMA, MVT::f32, Expand);
700
Dale Johannesen59a58732007-08-05 18:49:15 +0000701 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000702 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000703 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
705 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000706 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000707 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000708 addLegalFPImmediate(TmpFlt); // FLD0
709 TmpFlt.changeSign();
710 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000711
712 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000713 APFloat TmpFlt2(+1.0);
714 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
715 &ignored);
716 addLegalFPImmediate(TmpFlt2); // FLD1
717 TmpFlt2.changeSign();
718 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
719 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000720
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000721 if (!TM.Options.UnsafeFPMath) {
Evan Cheng8688a582013-01-29 02:32:37 +0000722 setOperationAction(ISD::FSIN , MVT::f80, Expand);
723 setOperationAction(ISD::FCOS , MVT::f80, Expand);
724 setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000725 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000726
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000727 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
728 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
729 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
730 setOperationAction(ISD::FRINT, MVT::f80, Expand);
731 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000732 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000733 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000734
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000735 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
737 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
738 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000739
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::FLOG, MVT::f80, Expand);
741 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
742 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
743 setOperationAction(ISD::FEXP, MVT::f80, Expand);
744 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000745
Mon P Wangf007a8b2008-11-06 05:31:54 +0000746 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000747 // (for widening) or expand (for scalarization). Then we will selectively
748 // turn on ones that can be effectively codegen'd.
Craig Topper55de3392012-11-14 06:41:09 +0000749 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
750 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper49010472012-11-15 06:51:10 +0000751 MVT VT = (MVT::SimpleValueType)i;
Craig Topper55de3392012-11-14 06:41:09 +0000752 setOperationAction(ISD::ADD , VT, Expand);
753 setOperationAction(ISD::SUB , VT, Expand);
754 setOperationAction(ISD::FADD, VT, Expand);
755 setOperationAction(ISD::FNEG, VT, Expand);
756 setOperationAction(ISD::FSUB, VT, Expand);
757 setOperationAction(ISD::MUL , VT, Expand);
758 setOperationAction(ISD::FMUL, VT, Expand);
759 setOperationAction(ISD::SDIV, VT, Expand);
760 setOperationAction(ISD::UDIV, VT, Expand);
761 setOperationAction(ISD::FDIV, VT, Expand);
762 setOperationAction(ISD::SREM, VT, Expand);
763 setOperationAction(ISD::UREM, VT, Expand);
764 setOperationAction(ISD::LOAD, VT, Expand);
765 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
767 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
768 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
769 setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
770 setOperationAction(ISD::FABS, VT, Expand);
771 setOperationAction(ISD::FSIN, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000772 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000773 setOperationAction(ISD::FCOS, VT, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000774 setOperationAction(ISD::FSINCOS, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000775 setOperationAction(ISD::FREM, VT, Expand);
776 setOperationAction(ISD::FMA, VT, Expand);
777 setOperationAction(ISD::FPOWI, VT, Expand);
778 setOperationAction(ISD::FSQRT, VT, Expand);
779 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
780 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000781 setOperationAction(ISD::FCEIL, VT, Expand);
782 setOperationAction(ISD::FTRUNC, VT, Expand);
783 setOperationAction(ISD::FRINT, VT, Expand);
784 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000785 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
786 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
787 setOperationAction(ISD::SDIVREM, VT, Expand);
788 setOperationAction(ISD::UDIVREM, VT, Expand);
789 setOperationAction(ISD::FPOW, VT, Expand);
790 setOperationAction(ISD::CTPOP, VT, Expand);
791 setOperationAction(ISD::CTTZ, VT, Expand);
792 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
793 setOperationAction(ISD::CTLZ, VT, Expand);
794 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
795 setOperationAction(ISD::SHL, VT, Expand);
796 setOperationAction(ISD::SRA, VT, Expand);
797 setOperationAction(ISD::SRL, VT, Expand);
798 setOperationAction(ISD::ROTL, VT, Expand);
799 setOperationAction(ISD::ROTR, VT, Expand);
800 setOperationAction(ISD::BSWAP, VT, Expand);
801 setOperationAction(ISD::SETCC, VT, Expand);
802 setOperationAction(ISD::FLOG, VT, Expand);
803 setOperationAction(ISD::FLOG2, VT, Expand);
804 setOperationAction(ISD::FLOG10, VT, Expand);
805 setOperationAction(ISD::FEXP, VT, Expand);
806 setOperationAction(ISD::FEXP2, VT, Expand);
807 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
808 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
809 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
810 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
811 setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
812 setOperationAction(ISD::TRUNCATE, VT, Expand);
813 setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
814 setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
815 setOperationAction(ISD::ANY_EXTEND, VT, Expand);
816 setOperationAction(ISD::VSELECT, VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000817 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
818 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Craig Topper55de3392012-11-14 06:41:09 +0000819 setTruncStoreAction(VT,
Dan Gohman2e141d72009-12-14 23:40:38 +0000820 (MVT::SimpleValueType)InnerVT, Expand);
Craig Topper55de3392012-11-14 06:41:09 +0000821 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
822 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
823 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000824 }
825
Evan Chengc7ce29b2009-02-13 22:36:38 +0000826 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
827 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000828 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000829 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000830 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000831 }
832
Dale Johannesen0488fb62010-09-30 23:57:10 +0000833 // MMX-sized vectors (other than x86mmx) are expected to be expanded
834 // into smaller operations.
835 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
836 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
837 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
838 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
839 setOperationAction(ISD::AND, MVT::v8i8, Expand);
840 setOperationAction(ISD::AND, MVT::v4i16, Expand);
841 setOperationAction(ISD::AND, MVT::v2i32, Expand);
842 setOperationAction(ISD::AND, MVT::v1i64, Expand);
843 setOperationAction(ISD::OR, MVT::v8i8, Expand);
844 setOperationAction(ISD::OR, MVT::v4i16, Expand);
845 setOperationAction(ISD::OR, MVT::v2i32, Expand);
846 setOperationAction(ISD::OR, MVT::v1i64, Expand);
847 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
848 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
849 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
850 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
851 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
852 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
853 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
854 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
856 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
857 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
858 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
859 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000860 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
861 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
862 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
863 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000864
Craig Topper1accb7e2012-01-10 06:54:16 +0000865 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000866 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000867
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
869 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
870 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
871 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
872 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
873 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000874 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
876 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
877 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
878 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
879 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000880 }
881
Craig Topper1accb7e2012-01-10 06:54:16 +0000882 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000883 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000884
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000885 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
886 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000887 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
888 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
889 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
890 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000891
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
893 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
894 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
895 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +0000896 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
898 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
899 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
900 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
901 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
902 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
903 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
904 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
905 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
906 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
907 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
908 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000909 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000910
Nadav Rotem354efd82011-09-18 14:57:03 +0000911 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000912 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
913 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
914 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000915
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
917 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
918 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
919 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
920 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000921
Evan Cheng2c3ae372006-04-12 21:21:57 +0000922 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000923 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000924 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000925 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000926 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000927 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000928 // Do not attempt to custom lower non-128-bit vectors
929 if (!VT.is128BitVector())
930 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000931 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
932 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
933 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000934 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
937 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
938 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
939 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000942
Nate Begemancdd1eec2008-02-12 22:51:28 +0000943 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
945 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000946 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000947
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000948 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000949 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000950 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000951
952 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000953 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000954 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000955
Craig Topper0d1f1762012-08-12 00:34:56 +0000956 setOperationAction(ISD::AND, VT, Promote);
957 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
958 setOperationAction(ISD::OR, VT, Promote);
959 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
960 setOperationAction(ISD::XOR, VT, Promote);
961 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
962 setOperationAction(ISD::LOAD, VT, Promote);
963 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
964 setOperationAction(ISD::SELECT, VT, Promote);
965 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000966 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000967
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000969
Evan Cheng2c3ae372006-04-12 21:21:57 +0000970 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
972 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
973 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
974 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000975
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
977 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000978
Michael Liaoa7554632012-10-23 17:36:08 +0000979 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom);
980 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Michael Liao991b6a22012-10-24 04:09:32 +0000981 // As there is no 64-bit GPR available, we need build a special custom
982 // sequence to convert from v2i32 to v2f32.
983 if (!Subtarget->is64Bit())
984 setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom);
Michael Liaoa7554632012-10-23 17:36:08 +0000985
Michael Liao9d796db2012-10-10 16:32:15 +0000986 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom);
Michael Liao44c2d612012-10-10 16:53:28 +0000987 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom);
Michael Liao9d796db2012-10-10 16:32:15 +0000988
Michael Liaob8150d82012-09-10 18:33:51 +0000989 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000990 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000991
Craig Topperd0a31172012-01-10 06:37:29 +0000992 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000993 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
994 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
995 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
996 setOperationAction(ISD::FRINT, MVT::f32, Legal);
997 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
998 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
999 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1000 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1001 setOperationAction(ISD::FRINT, MVT::f64, Legal);
1002 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1003
Craig Topper12fb5c62012-09-08 17:42:27 +00001004 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001005 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
1006 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
1007 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
1008 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001009 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001010 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
1011 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
1012 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
1013 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001014
Nate Begeman14d12ca2008-02-11 04:19:36 +00001015 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +00001016 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001017
Nadav Rotemfbad25e2011-09-11 15:02:23 +00001018 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
1019 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
1020 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
1021 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
1022 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +00001023
Nate Begeman14d12ca2008-02-11 04:19:36 +00001024 // i8 and i16 vectors are custom , because the source register and source
1025 // source memory operand types are not the same width. f32 vectors are
1026 // custom since the immediate controlling the insert encodes additional
1027 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
1029 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
1030 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
1031 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001032
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1034 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1035 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1036 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001037
Pete Coopera77214a2011-11-14 19:38:42 +00001038 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +00001039 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +00001040 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +00001041 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
1042 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +00001043 }
1044 }
Evan Cheng470a6ad2006-02-22 02:26:30 +00001045
Craig Topper1accb7e2012-01-10 06:54:16 +00001046 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001047 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001048 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001049
Nadav Rotem43012222011-05-11 08:12:09 +00001050 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001051 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001052
Nadav Rotem43012222011-05-11 08:12:09 +00001053 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +00001054 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001055
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001056 if (Subtarget->hasInt256()) {
Craig Topper7be5dfd2011-11-12 09:58:49 +00001057 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
1058 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1059
1060 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1061 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1062
1063 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1064 } else {
1065 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1066 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1067
1068 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1069 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1070
1071 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1072 }
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);
1121 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001122 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001123
Michael Liaoa7554632012-10-23 17:36:08 +00001124 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1125 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1126 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1127
Michael Liaob8150d82012-09-10 18:33:51 +00001128 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1129
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001130 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1131 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1132
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001133 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1134 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1135
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001136 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001137 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001138
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001139 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1140
Duncan Sands28b77e92011-09-06 19:07:46 +00001141 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1142 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1143 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1144 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001145
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001146 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1147 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1148 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1149
Craig Topperaaa643c2011-11-09 07:28:55 +00001150 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1151 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1152 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1153 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001154
Nadav Rotem0509db22012-12-28 05:45:24 +00001155 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1156 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1157 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1158 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1159 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1160 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001161
Craig Topperbf404372012-08-31 15:40:30 +00001162 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001163 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1164 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1165 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1166 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1167 setOperationAction(ISD::FMA, MVT::f32, Legal);
1168 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001169 }
Craig Topper880ef452012-08-11 22:34:26 +00001170
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001171 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001172 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1173 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1174 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1175 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001176
Craig Topperaaa643c2011-11-09 07:28:55 +00001177 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1178 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1179 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1180 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001181
Craig Topperaaa643c2011-11-09 07:28:55 +00001182 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1183 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1184 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001185 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001186
1187 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001188
1189 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1190 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1191
1192 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1193 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1194
1195 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001196
1197 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001198 } else {
1199 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1200 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1201 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1202 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1203
1204 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1205 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1206 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1207 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1208
1209 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1210 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1211 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1212 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001213
1214 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1215 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1216
1217 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1218 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1219
1220 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001221 }
Craig Topper13894fa2011-08-24 06:14:18 +00001222
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001223 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001224 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1225 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001226 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001227
1228 // Extract subvector is special because the value type
1229 // (result) is 128-bit but the source is 256-bit wide.
1230 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001231 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001232
1233 // Do not attempt to custom lower other non-256-bit vectors
1234 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001235 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001236
Craig Topper0d1f1762012-08-12 00:34:56 +00001237 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1238 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1239 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1240 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1241 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1242 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1243 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001244 }
1245
David Greene54d8eba2011-01-27 22:38:56 +00001246 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001247 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001248 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001249
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001250 // Do not attempt to promote non-256-bit vectors
1251 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001252 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001253
Craig Topper0d1f1762012-08-12 00:34:56 +00001254 setOperationAction(ISD::AND, VT, Promote);
1255 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1256 setOperationAction(ISD::OR, VT, Promote);
1257 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1258 setOperationAction(ISD::XOR, VT, Promote);
1259 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1260 setOperationAction(ISD::LOAD, VT, Promote);
1261 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1262 setOperationAction(ISD::SELECT, VT, Promote);
1263 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001264 }
David Greene9b9838d2009-06-29 16:47:10 +00001265 }
1266
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001267 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1268 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001269 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1270 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001271 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1272 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001273 }
1274
Evan Cheng6be2c582006-04-05 23:38:46 +00001275 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001277 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001278
Eli Friedman962f5492010-06-02 19:35:46 +00001279 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1280 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001281 //
Eli Friedman962f5492010-06-02 19:35:46 +00001282 // FIXME: We really should do custom legalization for addition and
1283 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1284 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001285 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1286 // Add/Sub/Mul with overflow operations are custom lowered.
1287 MVT VT = IntVTs[i];
1288 setOperationAction(ISD::SADDO, VT, Custom);
1289 setOperationAction(ISD::UADDO, VT, Custom);
1290 setOperationAction(ISD::SSUBO, VT, Custom);
1291 setOperationAction(ISD::USUBO, VT, Custom);
1292 setOperationAction(ISD::SMULO, VT, Custom);
1293 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001294 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001295
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001296 // There are no 8-bit 3-address imul/mul instructions
1297 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1298 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001299
Evan Chengd54f2d52009-03-31 19:38:51 +00001300 if (!Subtarget->is64Bit()) {
1301 // These libcalls are not available in 32-bit.
1302 setLibcallName(RTLIB::SHL_I128, 0);
1303 setLibcallName(RTLIB::SRL_I128, 0);
1304 setLibcallName(RTLIB::SRA_I128, 0);
1305 }
1306
Evan Cheng8688a582013-01-29 02:32:37 +00001307 // Combine sin / cos into one node or libcall if possible.
1308 if (Subtarget->hasSinCos()) {
1309 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1310 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001311 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001312 // For MacOSX, we don't want to the normal expansion of a libcall to
1313 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1314 // traffic.
1315 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1316 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1317 }
1318 }
1319
Evan Cheng206ee9d2006-07-07 08:33:52 +00001320 // We have target-specific dag combine patterns for the following nodes:
1321 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001322 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001323 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001324 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001325 setTargetDAGCombine(ISD::SHL);
1326 setTargetDAGCombine(ISD::SRA);
1327 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001328 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001329 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001330 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001331 setTargetDAGCombine(ISD::FADD);
1332 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001333 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001334 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001335 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001336 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001337 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001338 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001339 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001340 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001341 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001342 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001343 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001344 if (Subtarget->is64Bit())
1345 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001346 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001347
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001348 computeRegisterProperties();
1349
Evan Cheng05219282011-01-06 06:52:41 +00001350 // On Darwin, -Os means optimize for size without hurting performance,
1351 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001352 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1353 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1354 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1355 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1356 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1357 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001358 setPrefLoopAlignment(4); // 2^4 bytes.
Jim Grosbach3450f802013-02-20 21:13:59 +00001359 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001360
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001361 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001362 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001363
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001364 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001365}
1366
Duncan Sands28b77e92011-09-06 19:07:46 +00001367EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1368 if (!VT.isVector()) return MVT::i8;
1369 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001370}
1371
Evan Cheng29286502008-01-23 23:17:41 +00001372/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1373/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001374static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001375 if (MaxAlign == 16)
1376 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001377 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001378 if (VTy->getBitWidth() == 128)
1379 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001380 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001381 unsigned EltAlign = 0;
1382 getMaxByValAlign(ATy->getElementType(), EltAlign);
1383 if (EltAlign > MaxAlign)
1384 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001385 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001386 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1387 unsigned EltAlign = 0;
1388 getMaxByValAlign(STy->getElementType(i), EltAlign);
1389 if (EltAlign > MaxAlign)
1390 MaxAlign = EltAlign;
1391 if (MaxAlign == 16)
1392 break;
1393 }
1394 }
Evan Cheng29286502008-01-23 23:17:41 +00001395}
1396
1397/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1398/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001399/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1400/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001401unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001402 if (Subtarget->is64Bit()) {
1403 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001404 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001405 if (TyAlign > 8)
1406 return TyAlign;
1407 return 8;
1408 }
1409
Evan Cheng29286502008-01-23 23:17:41 +00001410 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001411 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001412 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001413 return Align;
1414}
Chris Lattner2b02a442007-02-25 08:29:00 +00001415
Evan Chengf0df0312008-05-15 08:39:06 +00001416/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001417/// and store operations as a result of memset, memcpy, and memmove
1418/// lowering. If DstAlign is zero that means it's safe to destination
1419/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1420/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001421/// probably because the source does not need to be loaded. If 'IsMemset' is
1422/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1423/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1424/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001425/// It returns EVT::Other if the type should be determined using generic
1426/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001427EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001428X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1429 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001430 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001431 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001432 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001433 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001434 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001435 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1436 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001437 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001438 (Subtarget->isUnalignedMemAccessFast() ||
1439 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001440 (SrcAlign == 0 || SrcAlign >= 16)))) {
1441 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001442 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001443 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001444 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001445 return MVT::v8f32;
1446 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001447 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001448 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001449 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001450 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001451 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001452 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001453 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001454 // Do not use f64 to lower memcpy if source is string constant. It's
1455 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001456 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001457 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001458 }
Evan Chengf0df0312008-05-15 08:39:06 +00001459 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001460 return MVT::i64;
1461 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001462}
1463
Evan Cheng7d342672012-12-12 01:32:07 +00001464bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001465 if (VT == MVT::f32)
1466 return X86ScalarSSEf32;
1467 else if (VT == MVT::f64)
1468 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001469 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001470}
1471
Evan Cheng376642e2012-12-10 23:21:26 +00001472bool
1473X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1474 if (Fast)
1475 *Fast = Subtarget->isUnalignedMemAccessFast();
1476 return true;
1477}
1478
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001479/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1480/// current function. The returned value is a member of the
1481/// MachineJumpTableInfo::JTEntryKind enum.
1482unsigned X86TargetLowering::getJumpTableEncoding() const {
1483 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1484 // symbol.
1485 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1486 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001487 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001488
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001489 // Otherwise, use the normal jump table encoding heuristics.
1490 return TargetLowering::getJumpTableEncoding();
1491}
1492
Chris Lattnerc64daab2010-01-26 05:02:42 +00001493const MCExpr *
1494X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1495 const MachineBasicBlock *MBB,
1496 unsigned uid,MCContext &Ctx) const{
1497 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1498 Subtarget->isPICStyleGOT());
1499 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1500 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001501 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1502 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001503}
1504
Evan Chengcc415862007-11-09 01:32:10 +00001505/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1506/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001507SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001508 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001509 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001510 // This doesn't have DebugLoc associated with it, but is not really the
1511 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001512 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001513 return Table;
1514}
1515
Chris Lattner589c6f62010-01-26 06:28:43 +00001516/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1517/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1518/// MCExpr.
1519const MCExpr *X86TargetLowering::
1520getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1521 MCContext &Ctx) const {
1522 // X86-64 uses RIP relative addressing based on the jump table label.
1523 if (Subtarget->isPICStyleRIPRel())
1524 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1525
1526 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001527 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001528}
1529
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001530// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001531std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001532X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001533 const TargetRegisterClass *RRC = 0;
1534 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001535 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001536 default:
1537 return TargetLowering::findRepresentativeClass(VT);
1538 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001539 RRC = Subtarget->is64Bit() ?
1540 (const TargetRegisterClass*)&X86::GR64RegClass :
1541 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001542 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001543 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001544 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001545 break;
1546 case MVT::f32: case MVT::f64:
1547 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1548 case MVT::v4f32: case MVT::v2f64:
1549 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1550 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001551 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001552 break;
1553 }
1554 return std::make_pair(RRC, Cost);
1555}
1556
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001557bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1558 unsigned &Offset) const {
1559 if (!Subtarget->isTargetLinux())
1560 return false;
1561
1562 if (Subtarget->is64Bit()) {
1563 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1564 Offset = 0x28;
1565 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1566 AddressSpace = 256;
1567 else
1568 AddressSpace = 257;
1569 } else {
1570 // %gs:0x14 on i386
1571 Offset = 0x14;
1572 AddressSpace = 256;
1573 }
1574 return true;
1575}
1576
Chris Lattner2b02a442007-02-25 08:29:00 +00001577//===----------------------------------------------------------------------===//
1578// Return Value Calling Convention Implementation
1579//===----------------------------------------------------------------------===//
1580
Chris Lattner59ed56b2007-02-28 04:55:35 +00001581#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001582
Michael J. Spencerec38de22010-10-10 22:04:20 +00001583bool
Eric Christopher471e4222011-06-08 23:55:35 +00001584X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001585 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001586 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001587 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001588 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001589 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001590 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001591 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001592}
1593
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594SDValue
1595X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001596 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001598 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001599 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001600 MachineFunction &MF = DAG.getMachineFunction();
1601 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
Chris Lattner9774c912007-02-27 05:28:59 +00001603 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001604 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001605 RVLocs, *DAG.getContext());
1606 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001607
Dan Gohman475871a2008-07-27 21:46:04 +00001608 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001609 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001610 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1611 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001612 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1613 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001614
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001615 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001616 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1617 CCValAssign &VA = RVLocs[i];
1618 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001619 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001620 EVT ValVT = ValToCopy.getValueType();
1621
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001622 // Promote values to the appropriate types
1623 if (VA.getLocInfo() == CCValAssign::SExt)
1624 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1625 else if (VA.getLocInfo() == CCValAssign::ZExt)
1626 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1627 else if (VA.getLocInfo() == CCValAssign::AExt)
1628 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1629 else if (VA.getLocInfo() == CCValAssign::BCvt)
1630 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1631
Dale Johannesenc4510512010-09-24 19:05:48 +00001632 // If this is x86-64, and we disabled SSE, we can't return FP values,
1633 // or SSE or MMX vectors.
1634 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1635 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001636 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001637 report_fatal_error("SSE register return with SSE disabled");
1638 }
1639 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1640 // llvm-gcc has never done it right and no one has noticed, so this
1641 // should be OK for now.
1642 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001643 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001644 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001645
Chris Lattner447ff682008-03-11 03:23:40 +00001646 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1647 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001648 if (VA.getLocReg() == X86::ST0 ||
1649 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001650 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1651 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001652 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001654 RetOps.push_back(ValToCopy);
1655 // Don't emit a copytoreg.
1656 continue;
1657 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001658
Evan Cheng242b38b2009-02-23 09:03:22 +00001659 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1660 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001661 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001662 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001663 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001664 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001665 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1666 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001667 // If we don't have SSE2 available, convert to v4f32 so the generated
1668 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001669 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001670 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001671 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001672 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001673 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001674
Dale Johannesendd64c412009-02-04 00:33:20 +00001675 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001676 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001677 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001678 }
Dan Gohman61a92132008-04-21 23:59:07 +00001679
Eli Benderskya5597f02013-01-25 22:07:43 +00001680 // The x86-64 ABIs require that for returning structs by value we copy
1681 // the sret argument into %rax/%eax (depending on ABI) for the return.
1682 // We saved the argument into a virtual register in the entry block,
1683 // so now we copy the value out and into %rax/%eax.
Dan Gohman61a92132008-04-21 23:59:07 +00001684 if (Subtarget->is64Bit() &&
1685 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1686 MachineFunction &MF = DAG.getMachineFunction();
1687 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1688 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001689 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001690 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001691 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001692
Eli Benderskya5597f02013-01-25 22:07:43 +00001693 unsigned RetValReg = Subtarget->isTarget64BitILP32() ? X86::EAX : X86::RAX;
1694 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001695 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001696
Eli Benderskya5597f02013-01-25 22:07:43 +00001697 // RAX/EAX now acts like a return value.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001698 RetOps.push_back(DAG.getRegister(RetValReg, MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Chris Lattner447ff682008-03-11 03:23:40 +00001701 RetOps[0] = Chain; // Update chain.
1702
1703 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001705 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001706
1707 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001709}
1710
Evan Chengbf010eb2012-04-10 01:51:00 +00001711bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001712 if (N->getNumValues() != 1)
1713 return false;
1714 if (!N->hasNUsesOfValue(1, 0))
1715 return false;
1716
Evan Chengbf010eb2012-04-10 01:51:00 +00001717 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001718 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001719 if (Copy->getOpcode() == ISD::CopyToReg) {
1720 // If the copy has a glue operand, we conservatively assume it isn't safe to
1721 // perform a tail call.
1722 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1723 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001724 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001725 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001726 return false;
1727
Evan Cheng1bf891a2010-12-01 22:59:46 +00001728 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001729 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001730 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001731 if (UI->getOpcode() != X86ISD::RET_FLAG)
1732 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001733 HasRet = true;
1734 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001735
Evan Chengbf010eb2012-04-10 01:51:00 +00001736 if (!HasRet)
1737 return false;
1738
1739 Chain = TCChain;
1740 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001741}
1742
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001743MVT
1744X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001745 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001746 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001747 // TODO: Is this also valid on 32-bit?
1748 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001749 ReturnMVT = MVT::i8;
1750 else
1751 ReturnMVT = MVT::i32;
1752
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001753 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001754 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001755}
1756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757/// LowerCallResult - Lower the result values of a call into the
1758/// appropriate copies out of appropriate physical registers.
1759///
1760SDValue
1761X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001762 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001763 const SmallVectorImpl<ISD::InputArg> &Ins,
1764 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001765 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001766
Chris Lattnere32bbf62007-02-28 07:09:55 +00001767 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001768 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001769 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001770 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001771 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001773
Chris Lattner3085e152007-02-25 08:59:22 +00001774 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001775 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001776 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001777 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001778
Torok Edwin3f142c32009-02-01 18:15:56 +00001779 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001781 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001782 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001783 }
1784
Evan Cheng79fb3b42009-02-20 20:43:02 +00001785 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001786
1787 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001788 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001789 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001790 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001791 // instead.
1792 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1793 // If we prefer to use the value in xmm registers, copy it out as f80 and
1794 // use a truncate to move it from fp stack reg to xmm reg.
1795 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001796 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001797 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1798 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001799 Val = Chain.getValue(0);
1800
1801 // Round the f80 to the right size, which also moves it to the appropriate
1802 // xmm register.
1803 if (CopyVT != VA.getValVT())
1804 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1805 // This truncation won't change the value.
1806 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001807 } else {
1808 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1809 CopyVT, InFlag).getValue(1);
1810 Val = Chain.getValue(0);
1811 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001812 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001814 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001815
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001817}
1818
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001819//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001820// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001821//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001822// StdCall calling convention seems to be standard for many Windows' API
1823// routines and around. It differs from C calling convention just a little:
1824// callee should clean up the stack, not caller. Symbols should be also
1825// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001826// For info on fast calling convention see Fast Calling Convention (tail call)
1827// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001828
Dan Gohman98ca4f22009-08-05 01:29:28 +00001829/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001830/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001831enum StructReturnType {
1832 NotStructReturn,
1833 RegStructReturn,
1834 StackStructReturn
1835};
1836static StructReturnType
1837callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001838 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001839 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001840
Rafael Espindola1cee7102012-07-25 13:41:10 +00001841 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1842 if (!Flags.isSRet())
1843 return NotStructReturn;
1844 if (Flags.isInReg())
1845 return RegStructReturn;
1846 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001847}
1848
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001849/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001850/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001851static StructReturnType
1852argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001854 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001855
Rafael Espindola1cee7102012-07-25 13:41:10 +00001856 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1857 if (!Flags.isSRet())
1858 return NotStructReturn;
1859 if (Flags.isInReg())
1860 return RegStructReturn;
1861 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001862}
1863
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001864/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1865/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001866/// the specific parameter attribute. The copy will be passed as a byval
1867/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001868static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001869CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001870 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1871 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001872 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001873
Dale Johannesendd64c412009-02-04 00:33:20 +00001874 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001875 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001876 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001877}
1878
Chris Lattner29689432010-03-11 00:22:57 +00001879/// IsTailCallConvention - Return true if the calling convention is one that
1880/// supports tail call optimization.
1881static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001882 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1883 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001884}
1885
Evan Cheng485fafc2011-03-21 01:19:09 +00001886bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001887 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001888 return false;
1889
1890 CallSite CS(CI);
1891 CallingConv::ID CalleeCC = CS.getCallingConv();
1892 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1893 return false;
1894
1895 return true;
1896}
1897
Evan Cheng0c439eb2010-01-27 00:07:07 +00001898/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1899/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001900static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1901 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001902 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001903}
1904
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905SDValue
1906X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001907 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908 const SmallVectorImpl<ISD::InputArg> &Ins,
1909 DebugLoc dl, SelectionDAG &DAG,
1910 const CCValAssign &VA,
1911 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001912 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001913 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001915 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1916 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001917 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001918 EVT ValVT;
1919
1920 // If value is passed by pointer we have address passed instead of the value
1921 // itself.
1922 if (VA.getLocInfo() == CCValAssign::Indirect)
1923 ValVT = VA.getLocVT();
1924 else
1925 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001926
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001927 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001928 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001929 // In case of tail call optimization mark all arguments mutable. Since they
1930 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001931 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001932 unsigned Bytes = Flags.getByValSize();
1933 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1934 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001935 return DAG.getFrameIndex(FI, getPointerTy());
1936 } else {
1937 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001938 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001939 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1940 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001941 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001942 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001943 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001944}
1945
Dan Gohman475871a2008-07-27 21:46:04 +00001946SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001948 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001949 bool isVarArg,
1950 const SmallVectorImpl<ISD::InputArg> &Ins,
1951 DebugLoc dl,
1952 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001953 SmallVectorImpl<SDValue> &InVals)
1954 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001955 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001956 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Gordon Henriksen86737662008-01-05 16:56:59 +00001958 const Function* Fn = MF.getFunction();
1959 if (Fn->hasExternalLinkage() &&
1960 Subtarget->isTargetCygMing() &&
1961 Fn->getName() == "main")
1962 FuncInfo->setForceFramePointer(true);
1963
Evan Cheng1bc78042006-04-26 01:20:17 +00001964 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001966 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001967 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001968
Chris Lattner29689432010-03-11 00:22:57 +00001969 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001970 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971
Chris Lattner638402b2007-02-28 07:00:42 +00001972 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001973 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001974 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001976
1977 // Allocate shadow area for Win64
1978 if (IsWin64) {
1979 CCInfo.AllocateStack(32, 8);
1980 }
1981
Duncan Sands45907662010-10-31 13:21:44 +00001982 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Chris Lattnerf39f7712007-02-28 05:46:49 +00001984 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001985 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1987 CCValAssign &VA = ArgLocs[i];
1988 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1989 // places.
1990 assert(VA.getValNo() != LastVal &&
1991 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001992 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001993 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Chris Lattnerf39f7712007-02-28 05:46:49 +00001995 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001996 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001997 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001999 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00002001 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00002003 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00002005 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002006 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002007 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00002008 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00002009 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00002010 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002011 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002012 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002013 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002014
Devang Patel68e6bee2011-02-21 23:21:26 +00002015 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Chris Lattnerf39f7712007-02-28 05:46:49 +00002018 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2019 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2020 // right size.
2021 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002022 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002023 DAG.getValueType(VA.getValVT()));
2024 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002025 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002026 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002027 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002028 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002030 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002031 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002032 if (RegVT.isVector())
2033 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2034 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002035 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002036 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002037 } else {
2038 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002040 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002041
2042 // If value is passed via pointer - do a load.
2043 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002044 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002045 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002046
Dan Gohman98ca4f22009-08-05 01:29:28 +00002047 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002048 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002049
Eli Benderskya5597f02013-01-25 22:07:43 +00002050 // The x86-64 ABIs require that for returning structs by value we copy
2051 // the sret argument into %rax/%eax (depending on ABI) for the return.
2052 // Save the argument into a virtual register so that we can access it
2053 // from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002054 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00002055 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2056 unsigned Reg = FuncInfo->getSRetReturnReg();
2057 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002058 MVT PtrTy = getPointerTy();
2059 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002060 FuncInfo->setSRetReturnReg(Reg);
2061 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002062 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002064 }
2065
Chris Lattnerf39f7712007-02-28 05:46:49 +00002066 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002067 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002068 if (FuncIsMadeTailCallSafe(CallConv,
2069 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002070 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002071
Evan Cheng1bc78042006-04-26 01:20:17 +00002072 // If the function takes variable number of arguments, make a frame index for
2073 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002074 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002075 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2076 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002077 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 }
2079 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002080 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2081
2082 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002083 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002084 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002086 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002087 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2088 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002089 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2091 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2092 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002093 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002094 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002095
2096 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002097 // The XMM registers which might contain var arg parameters are shadowed
2098 // in their paired GPR. So we only need to save the GPR to their home
2099 // slots.
2100 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002101 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002102 } else {
2103 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2104 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002105
Chad Rosier30450e82011-12-22 22:35:21 +00002106 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2107 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002108 }
2109 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2110 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002111
Bill Wendling831737d2012-12-30 10:32:01 +00002112 bool NoImplicitFloatOps = Fn->getAttributes().
2113 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002114 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002115 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002116 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2117 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002118 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002119 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002120 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002121 // Kernel mode asks for SSE to be disabled, so don't push them
2122 // on the stack.
2123 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002124
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002125 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002126 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002127 // Get to the caller-allocated home save location. Add 8 to account
2128 // for the return address.
2129 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002130 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002131 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002132 // Fixup to set vararg frame on shadow area (4 x i64).
2133 if (NumIntRegs < 4)
2134 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002135 } else {
2136 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002137 // registers, then we must store them to their spots on the stack so
2138 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002139 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2140 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2141 FuncInfo->setRegSaveFrameIndex(
2142 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002143 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002144 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002145
Gordon Henriksen86737662008-01-05 16:56:59 +00002146 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002147 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002148 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2149 getPointerTy());
2150 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002151 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002152 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2153 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002154 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002155 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002156 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002157 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002158 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002159 MachinePointerInfo::getFixedStack(
2160 FuncInfo->getRegSaveFrameIndex(), Offset),
2161 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002162 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002163 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002165
Dan Gohmanface41a2009-08-16 21:24:25 +00002166 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2167 // Now store the XMM (fp + vector) parameter registers.
2168 SmallVector<SDValue, 11> SaveXMMOps;
2169 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002170
Craig Topperc9099502012-04-20 06:31:50 +00002171 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002172 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2173 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002174
Dan Gohman1e93df62010-04-17 14:41:14 +00002175 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2176 FuncInfo->getRegSaveFrameIndex()));
2177 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2178 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002179
Dan Gohmanface41a2009-08-16 21:24:25 +00002180 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002181 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002182 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002183 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2184 SaveXMMOps.push_back(Val);
2185 }
2186 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2187 MVT::Other,
2188 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002190
2191 if (!MemOps.empty())
2192 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2193 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002194 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002196
Gordon Henriksen86737662008-01-05 16:56:59 +00002197 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002198 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2199 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002200 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002201 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002202 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002203 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002204 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002205 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002206 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002207 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002208
Gordon Henriksen86737662008-01-05 16:56:59 +00002209 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002210 // RegSaveFrameIndex is X86-64 only.
2211 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002212 if (CallConv == CallingConv::X86_FastCall ||
2213 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002214 // fastcc functions can't have varargs.
2215 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002216 }
Evan Cheng25caf632006-05-23 21:06:34 +00002217
Rafael Espindola76927d752011-08-30 19:39:58 +00002218 FuncInfo->setArgumentStackSize(StackSize);
2219
Dan Gohman98ca4f22009-08-05 01:29:28 +00002220 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002221}
2222
Dan Gohman475871a2008-07-27 21:46:04 +00002223SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2225 SDValue StackPtr, SDValue Arg,
2226 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002227 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002228 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002229 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002230 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002231 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002232 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002233 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002234
2235 return DAG.getStore(Chain, dl, Arg, PtrOff,
2236 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002237 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002238}
2239
Bill Wendling64e87322009-01-16 19:25:27 +00002240/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002241/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002242SDValue
2243X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002244 SDValue &OutRetAddr, SDValue Chain,
2245 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002246 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002247 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002248 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002249 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002250
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002251 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002252 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002253 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002254 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002255}
2256
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002257/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002258/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002259static SDValue
2260EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002261 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2262 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002263 // Store the return address to the appropriate stack slot.
2264 if (!FPDiff) return Chain;
2265 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002266 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002267 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002268 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002269 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002270 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002271 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002272 return Chain;
2273}
2274
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002276X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002277 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002278 SelectionDAG &DAG = CLI.DAG;
2279 DebugLoc &dl = CLI.DL;
2280 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2281 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2282 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2283 SDValue Chain = CLI.Chain;
2284 SDValue Callee = CLI.Callee;
2285 CallingConv::ID CallConv = CLI.CallConv;
2286 bool &isTailCall = CLI.IsTailCall;
2287 bool isVarArg = CLI.IsVarArg;
2288
Dan Gohman98ca4f22009-08-05 01:29:28 +00002289 MachineFunction &MF = DAG.getMachineFunction();
2290 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002291 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002292 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002293 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002294 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002295
Nick Lewycky22de16d2012-01-19 00:34:10 +00002296 if (MF.getTarget().Options.DisableTailCalls)
2297 isTailCall = false;
2298
Evan Cheng5f941932010-02-05 02:21:12 +00002299 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002300 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002301 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002302 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002303 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002304 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002305
2306 // Sibcalls are automatically detected tailcalls which do not require
2307 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002308 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002309 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002310
2311 if (isTailCall)
2312 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002313 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002314
Chris Lattner29689432010-03-11 00:22:57 +00002315 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002316 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002317
Chris Lattner638402b2007-02-28 07:00:42 +00002318 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002319 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002320 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002321 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002322
2323 // Allocate shadow area for Win64
2324 if (IsWin64) {
2325 CCInfo.AllocateStack(32, 8);
2326 }
2327
Duncan Sands45907662010-10-31 13:21:44 +00002328 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002329
Chris Lattner423c5f42007-02-28 05:31:48 +00002330 // Get a count of how many bytes are to be pushed on the stack.
2331 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002332 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002333 // This is a sibcall. The memory operands are available in caller's
2334 // own caller's stack.
2335 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002336 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2337 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002338 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002339
Gordon Henriksen86737662008-01-05 16:56:59 +00002340 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002341 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002342 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002343 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2344 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2345
Gordon Henriksen86737662008-01-05 16:56:59 +00002346 FPDiff = NumBytesCallerPushed - NumBytes;
2347
2348 // Set the delta of movement of the returnaddr stackslot.
2349 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002350 if (FPDiff < X86Info->getTCReturnAddrDelta())
2351 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002352 }
2353
Evan Chengf22f9b32010-02-06 03:28:46 +00002354 if (!IsSibcall)
2355 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
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.
Chris Lattner423c5f42007-02-28 05:31:48 +00002369 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2370 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002371 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002372 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002373 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002374 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002375
Chris Lattner423c5f42007-02-28 05:31:48 +00002376 // Promote the value if needed.
2377 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002378 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002379 case CCValAssign::Full: break;
2380 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002381 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002382 break;
2383 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002384 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002385 break;
2386 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002387 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002388 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002389 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002390 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2391 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002392 } else
2393 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2394 break;
2395 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002396 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002397 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002398 case CCValAssign::Indirect: {
2399 // Store the argument.
2400 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002401 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002402 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002403 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002404 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002405 Arg = SpillSlot;
2406 break;
2407 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002409
Chris Lattner423c5f42007-02-28 05:31:48 +00002410 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002411 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2412 if (isVarArg && IsWin64) {
2413 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2414 // shadow reg if callee is a varargs function.
2415 unsigned ShadowReg = 0;
2416 switch (VA.getLocReg()) {
2417 case X86::XMM0: ShadowReg = X86::RCX; break;
2418 case X86::XMM1: ShadowReg = X86::RDX; break;
2419 case X86::XMM2: ShadowReg = X86::R8; break;
2420 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002421 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002422 if (ShadowReg)
2423 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002424 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002425 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002426 assert(VA.isMemLoc());
2427 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002428 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2429 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002430 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2431 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002432 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002433 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002434
Evan Cheng32fe1032006-05-25 00:59:30 +00002435 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002436 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002437 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002438
Chris Lattner88e1fd52009-07-09 04:24:46 +00002439 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002440 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2441 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002442 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002443 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2444 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002445 } else {
2446 // If we are tail calling and generating PIC/GOT style code load the
2447 // address of the callee into ECX. The value in ecx is used as target of
2448 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2449 // for tail calls on PIC/GOT architectures. Normally we would just put the
2450 // address of GOT into ebx and then call target@PLT. But for tail calls
2451 // ebx would be restored (since ebx is callee saved) before jumping to the
2452 // target@PLT.
2453
2454 // Note: The actual moving to ECX is done further down.
2455 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2456 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2457 !G->getGlobal()->hasProtectedVisibility())
2458 Callee = LowerGlobalAddress(Callee, DAG);
2459 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002460 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002461 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002462 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002463
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002464 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002465 // From AMD64 ABI document:
2466 // For calls that may call functions that use varargs or stdargs
2467 // (prototype-less calls or calls to functions containing ellipsis (...) in
2468 // the declaration) %al is used as hidden argument to specify the number
2469 // of SSE registers used. The contents of %al do not need to match exactly
2470 // the number of registers, but must be an ubound on the number of SSE
2471 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002472
Gordon Henriksen86737662008-01-05 16:56:59 +00002473 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002474 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002475 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2476 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2477 };
2478 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002479 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002480 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002481
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002482 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2483 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002484 }
2485
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002486 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 if (isTailCall) {
2488 // Force all the incoming stack arguments to be loaded from the stack
2489 // before any new outgoing arguments are stored to the stack, because the
2490 // outgoing stack slots may alias the incoming argument stack slots, and
2491 // the alias isn't otherwise explicit. This is slightly more conservative
2492 // than necessary, because it means that each store effectively depends
2493 // on every argument instead of just those arguments it would clobber.
2494 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2495
Dan Gohman475871a2008-07-27 21:46:04 +00002496 SmallVector<SDValue, 8> MemOpChains2;
2497 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002498 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002499 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002500 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2501 CCValAssign &VA = ArgLocs[i];
2502 if (VA.isRegLoc())
2503 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002504 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002505 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002506 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002507 // Create frame index.
2508 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002509 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002510 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002511 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002512
Duncan Sands276dcbd2008-03-21 09:14:45 +00002513 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002514 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002515 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002516 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002517 StackPtr = DAG.getCopyFromReg(Chain, dl,
2518 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002519 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002520 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002521
Dan Gohman98ca4f22009-08-05 01:29:28 +00002522 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2523 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002524 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002525 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002526 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002527 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002528 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002529 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002530 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002531 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002532 }
2533 }
2534
2535 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002536 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002537 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002538
2539 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002540 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2541 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002542 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002543 }
2544
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002545 // Build a sequence of copy-to-reg nodes chained together with token chain
2546 // and flag operands which copy the outgoing args into registers.
2547 SDValue InFlag;
2548 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2549 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2550 RegsToPass[i].second, InFlag);
2551 InFlag = Chain.getValue(1);
2552 }
2553
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002554 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2555 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2556 // In the 64-bit large code model, we have to make all calls
2557 // through a register, since the call instruction's 32-bit
2558 // pc-relative offset may not be large enough to hold the whole
2559 // address.
2560 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002561 // If the callee is a GlobalAddress node (quite common, every direct call
2562 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2563 // it.
2564
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002565 // We should use extra load for direct calls to dllimported functions in
2566 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002567 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002568 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002569 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002570 bool ExtraLoad = false;
2571 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002572
Chris Lattner48a7d022009-07-09 05:02:21 +00002573 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2574 // external symbols most go through the PLT in PIC mode. If the symbol
2575 // has hidden or protected visibility, or if it is static or local, then
2576 // we don't need to use the PLT - we can directly call it.
2577 if (Subtarget->isTargetELF() &&
2578 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002579 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002580 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002581 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002582 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002583 (!Subtarget->getTargetTriple().isMacOSX() ||
2584 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002585 // PC-relative references to external symbols should go through $stub,
2586 // unless we're building with the leopard linker or later, which
2587 // automatically synthesizes these stubs.
2588 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002589 } else if (Subtarget->isPICStyleRIPRel() &&
2590 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002591 cast<Function>(GV)->getAttributes().
2592 hasAttribute(AttributeSet::FunctionIndex,
2593 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002594 // If the function is marked as non-lazy, generate an indirect call
2595 // which loads from the GOT directly. This avoids runtime overhead
2596 // at the cost of eager binding (and one extra byte of encoding).
2597 OpFlags = X86II::MO_GOTPCREL;
2598 WrapperKind = X86ISD::WrapperRIP;
2599 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002600 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002601
Devang Patel0d881da2010-07-06 22:08:15 +00002602 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002603 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002604
2605 // Add a wrapper if needed.
2606 if (WrapperKind != ISD::DELETED_NODE)
2607 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2608 // Add extra indirection if needed.
2609 if (ExtraLoad)
2610 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2611 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002612 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002613 }
Bill Wendling056292f2008-09-16 21:48:12 +00002614 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002615 unsigned char OpFlags = 0;
2616
Evan Cheng1bf891a2010-12-01 22:59:46 +00002617 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2618 // external symbols should go through the PLT.
2619 if (Subtarget->isTargetELF() &&
2620 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2621 OpFlags = X86II::MO_PLT;
2622 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002623 (!Subtarget->getTargetTriple().isMacOSX() ||
2624 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002625 // PC-relative references to external symbols should go through $stub,
2626 // unless we're building with the leopard linker or later, which
2627 // automatically synthesizes these stubs.
2628 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002629 }
Eric Christopherfd179292009-08-27 18:07:15 +00002630
Chris Lattner48a7d022009-07-09 05:02:21 +00002631 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2632 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002633 }
2634
Chris Lattnerd96d0722007-02-25 06:40:16 +00002635 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002636 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002637 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002638
Evan Chengf22f9b32010-02-06 03:28:46 +00002639 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002640 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2641 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002642 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002643 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002644
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002645 Ops.push_back(Chain);
2646 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002647
Dan Gohman98ca4f22009-08-05 01:29:28 +00002648 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002649 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002650
Gordon Henriksen86737662008-01-05 16:56:59 +00002651 // Add argument registers to the end of the list so that they are known live
2652 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002653 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2654 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2655 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002656
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002657 // Add a register mask operand representing the call-preserved registers.
2658 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2659 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2660 assert(Mask && "Missing call preserved mask for calling convention");
2661 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002662
Gabor Greifba36cb52008-08-28 21:40:38 +00002663 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002664 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002665
Dan Gohman98ca4f22009-08-05 01:29:28 +00002666 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002667 // We used to do:
2668 //// If this is the first return lowered for this function, add the regs
2669 //// to the liveout set for the function.
2670 // This isn't right, although it's probably harmless on x86; liveouts
2671 // should be computed from returns not tail calls. Consider a void
2672 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002673 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002674 }
2675
Dale Johannesenace16102009-02-03 19:33:06 +00002676 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002677 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002678
Chris Lattner2d297092006-05-23 18:50:38 +00002679 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002680 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002681 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2682 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002683 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002684 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002685 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002686 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002687 // pops the hidden struct pointer, so we have to push it back.
2688 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002689 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002690 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002691 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002692 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002693
Gordon Henriksenae636f82008-01-03 16:47:34 +00002694 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002695 if (!IsSibcall) {
2696 Chain = DAG.getCALLSEQ_END(Chain,
2697 DAG.getIntPtrConstant(NumBytes, true),
2698 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2699 true),
2700 InFlag);
2701 InFlag = Chain.getValue(1);
2702 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002703
Chris Lattner3085e152007-02-25 08:59:22 +00002704 // Handle result values, copying them out of physregs into vregs that we
2705 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2707 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002708}
2709
Evan Cheng25ab6902006-09-08 06:48:29 +00002710//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002711// Fast Calling Convention (tail call) implementation
2712//===----------------------------------------------------------------------===//
2713
2714// Like std call, callee cleans arguments, convention except that ECX is
2715// reserved for storing the tail called function address. Only 2 registers are
2716// free for argument passing (inreg). Tail call optimization is performed
2717// provided:
2718// * tailcallopt is enabled
2719// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002720// On X86_64 architecture with GOT-style position independent code only local
2721// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002722// To keep the stack aligned according to platform abi the function
2723// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2724// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002725// If a tail called function callee has more arguments than the caller the
2726// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002727// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002728// original REtADDR, but before the saved framepointer or the spilled registers
2729// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2730// stack layout:
2731// arg1
2732// arg2
2733// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002734// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002735// move area ]
2736// (possible EBP)
2737// ESI
2738// EDI
2739// local1 ..
2740
2741/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2742/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002743unsigned
2744X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2745 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002746 MachineFunction &MF = DAG.getMachineFunction();
2747 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002748 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002749 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002750 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002751 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002752 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002753 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2754 // Number smaller than 12 so just add the difference.
2755 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2756 } else {
2757 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002758 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002759 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002760 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002761 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002762}
2763
Evan Cheng5f941932010-02-05 02:21:12 +00002764/// MatchingStackOffset - Return true if the given stack call argument is
2765/// already available in the same position (relatively) of the caller's
2766/// incoming argument stack.
2767static
2768bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2769 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2770 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002771 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2772 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002773 if (Arg.getOpcode() == ISD::CopyFromReg) {
2774 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002775 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002776 return false;
2777 MachineInstr *Def = MRI->getVRegDef(VR);
2778 if (!Def)
2779 return false;
2780 if (!Flags.isByVal()) {
2781 if (!TII->isLoadFromStackSlot(Def, FI))
2782 return false;
2783 } else {
2784 unsigned Opcode = Def->getOpcode();
2785 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2786 Def->getOperand(1).isFI()) {
2787 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002788 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002789 } else
2790 return false;
2791 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002792 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2793 if (Flags.isByVal())
2794 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002795 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002796 // define @foo(%struct.X* %A) {
2797 // tail call @bar(%struct.X* byval %A)
2798 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002799 return false;
2800 SDValue Ptr = Ld->getBasePtr();
2801 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2802 if (!FINode)
2803 return false;
2804 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002805 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002806 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002807 FI = FINode->getIndex();
2808 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002809 } else
2810 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002811
Evan Cheng4cae1332010-03-05 08:38:04 +00002812 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002813 if (!MFI->isFixedObjectIndex(FI))
2814 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002815 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002816}
2817
Dan Gohman98ca4f22009-08-05 01:29:28 +00002818/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2819/// for tail call optimization. Targets which want to do tail call
2820/// optimization should implement this function.
2821bool
2822X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002823 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002824 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002825 bool isCalleeStructRet,
2826 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002827 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002828 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002829 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002830 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002831 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002832 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002833 CalleeCC != CallingConv::C)
2834 return false;
2835
Evan Cheng7096ae42010-01-29 06:45:59 +00002836 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002837 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002838 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002839
2840 // If the function return type is x86_fp80 and the callee return type is not,
2841 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2842 // perform a tailcall optimization here.
2843 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2844 return false;
2845
Evan Cheng13617962010-04-30 01:12:32 +00002846 CallingConv::ID CallerCC = CallerF->getCallingConv();
2847 bool CCMatch = CallerCC == CalleeCC;
2848
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002849 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002850 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002851 return true;
2852 return false;
2853 }
2854
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002855 // Look for obvious safe cases to perform tail call optimization that do not
2856 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002857
Evan Cheng2c12cb42010-03-26 16:26:03 +00002858 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2859 // emit a special epilogue.
2860 if (RegInfo->needsStackRealignment(MF))
2861 return false;
2862
Evan Chenga375d472010-03-15 18:54:48 +00002863 // Also avoid sibcall optimization if either caller or callee uses struct
2864 // return semantics.
2865 if (isCalleeStructRet || isCallerStructRet)
2866 return false;
2867
Chad Rosier2416da32011-06-24 21:15:36 +00002868 // An stdcall caller is expected to clean up its arguments; the callee
2869 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002870 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002871 return false;
2872
Chad Rosier871f6642011-05-18 19:59:50 +00002873 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002874 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002875 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002876
2877 // Optimizing for varargs on Win64 is unlikely to be safe without
2878 // additional testing.
2879 if (Subtarget->isTargetWin64())
2880 return false;
2881
Chad Rosier871f6642011-05-18 19:59:50 +00002882 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002883 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002884 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002885
Chad Rosier871f6642011-05-18 19:59:50 +00002886 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2887 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2888 if (!ArgLocs[i].isRegLoc())
2889 return false;
2890 }
2891
Chad Rosier30450e82011-12-22 22:35:21 +00002892 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2893 // stack. Therefore, if it's not used by the call it is not safe to optimize
2894 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002895 bool Unused = false;
2896 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2897 if (!Ins[i].Used) {
2898 Unused = true;
2899 break;
2900 }
2901 }
2902 if (Unused) {
2903 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002904 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002905 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002906 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002907 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002908 CCValAssign &VA = RVLocs[i];
2909 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2910 return false;
2911 }
2912 }
2913
Evan Cheng13617962010-04-30 01:12:32 +00002914 // If the calling conventions do not match, then we'd better make sure the
2915 // results are returned in the same way as what the caller expects.
2916 if (!CCMatch) {
2917 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002918 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002919 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002920 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2921
2922 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002923 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002924 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002925 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2926
2927 if (RVLocs1.size() != RVLocs2.size())
2928 return false;
2929 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2930 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2931 return false;
2932 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2933 return false;
2934 if (RVLocs1[i].isRegLoc()) {
2935 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2936 return false;
2937 } else {
2938 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2939 return false;
2940 }
2941 }
2942 }
2943
Evan Chenga6bff982010-01-30 01:22:00 +00002944 // If the callee takes no arguments then go on to check the results of the
2945 // call.
2946 if (!Outs.empty()) {
2947 // Check if stack adjustment is needed. For now, do not do this if any
2948 // argument is passed on the stack.
2949 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002950 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002951 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002952
2953 // Allocate shadow area for Win64
2954 if (Subtarget->isTargetWin64()) {
2955 CCInfo.AllocateStack(32, 8);
2956 }
2957
Duncan Sands45907662010-10-31 13:21:44 +00002958 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002959 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002960 MachineFunction &MF = DAG.getMachineFunction();
2961 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2962 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002963
2964 // Check if the arguments are already laid out in the right way as
2965 // the caller's fixed stack objects.
2966 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002967 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2968 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002969 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002970 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2971 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002972 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002973 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002974 if (VA.getLocInfo() == CCValAssign::Indirect)
2975 return false;
2976 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002977 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2978 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002979 return false;
2980 }
2981 }
2982 }
Evan Cheng9c044672010-05-29 01:35:22 +00002983
2984 // If the tailcall address may be in a register, then make sure it's
2985 // possible to register allocate for it. In 32-bit, the call address can
2986 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002987 // callee-saved registers are restored. These happen to be the same
2988 // registers used to pass 'inreg' arguments so watch out for those.
2989 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002990 ((!isa<GlobalAddressSDNode>(Callee) &&
2991 !isa<ExternalSymbolSDNode>(Callee)) ||
2992 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002993 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002994 // In PIC we need an extra register to formulate the address computation
2995 // for the callee.
2996 unsigned MaxInRegs =
2997 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
2998
Evan Cheng9c044672010-05-29 01:35:22 +00002999 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3000 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00003001 if (!VA.isRegLoc())
3002 continue;
3003 unsigned Reg = VA.getLocReg();
3004 switch (Reg) {
3005 default: break;
3006 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00003007 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00003008 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00003009 break;
Evan Cheng9c044672010-05-29 01:35:22 +00003010 }
3011 }
3012 }
Evan Chenga6bff982010-01-30 01:22:00 +00003013 }
Evan Chengb1712452010-01-27 06:25:16 +00003014
Evan Cheng86809cc2010-02-03 03:28:02 +00003015 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003016}
3017
Dan Gohman3df24e62008-09-03 23:12:08 +00003018FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003019X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3020 const TargetLibraryInfo *libInfo) const {
3021 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003022}
3023
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003024//===----------------------------------------------------------------------===//
3025// Other Lowering Hooks
3026//===----------------------------------------------------------------------===//
3027
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003028static bool MayFoldLoad(SDValue Op) {
3029 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3030}
3031
3032static bool MayFoldIntoStore(SDValue Op) {
3033 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3034}
3035
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003036static bool isTargetShuffle(unsigned Opcode) {
3037 switch(Opcode) {
3038 default: return false;
3039 case X86ISD::PSHUFD:
3040 case X86ISD::PSHUFHW:
3041 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003042 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003043 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003044 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003045 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003046 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003047 case X86ISD::MOVLPS:
3048 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003049 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003050 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003051 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003052 case X86ISD::MOVSS:
3053 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003054 case X86ISD::UNPCKL:
3055 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003056 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003057 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003058 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003059 return true;
3060 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003061}
3062
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003063static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003064 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003065 switch(Opc) {
3066 default: llvm_unreachable("Unknown x86 shuffle node");
3067 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003068 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003069 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003070 return DAG.getNode(Opc, dl, VT, V1);
3071 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003072}
3073
3074static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003075 SDValue V1, unsigned TargetMask,
3076 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003077 switch(Opc) {
3078 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003079 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003080 case X86ISD::PSHUFHW:
3081 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003082 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003083 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003084 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3085 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003086}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003087
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003088static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003089 SDValue V1, SDValue V2, unsigned TargetMask,
3090 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003091 switch(Opc) {
3092 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003093 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003094 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003095 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003096 return DAG.getNode(Opc, dl, VT, V1, V2,
3097 DAG.getConstant(TargetMask, MVT::i8));
3098 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003099}
3100
3101static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3102 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3103 switch(Opc) {
3104 default: llvm_unreachable("Unknown x86 shuffle node");
3105 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003106 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003107 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003108 case X86ISD::MOVLPS:
3109 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003110 case X86ISD::MOVSS:
3111 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003112 case X86ISD::UNPCKL:
3113 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003114 return DAG.getNode(Opc, dl, VT, V1, V2);
3115 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003116}
3117
Dan Gohmand858e902010-04-17 15:26:15 +00003118SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003119 MachineFunction &MF = DAG.getMachineFunction();
3120 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3121 int ReturnAddrIndex = FuncInfo->getRAIndex();
3122
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003123 if (ReturnAddrIndex == 0) {
3124 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003125 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003126 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003127 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003128 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003129 }
3130
Evan Cheng25ab6902006-09-08 06:48:29 +00003131 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003132}
3133
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003134bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3135 bool hasSymbolicDisplacement) {
3136 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003137 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003138 return false;
3139
3140 // If we don't have a symbolic displacement - we don't have any extra
3141 // restrictions.
3142 if (!hasSymbolicDisplacement)
3143 return true;
3144
3145 // FIXME: Some tweaks might be needed for medium code model.
3146 if (M != CodeModel::Small && M != CodeModel::Kernel)
3147 return false;
3148
3149 // For small code model we assume that latest object is 16MB before end of 31
3150 // bits boundary. We may also accept pretty large negative constants knowing
3151 // that all objects are in the positive half of address space.
3152 if (M == CodeModel::Small && Offset < 16*1024*1024)
3153 return true;
3154
3155 // For kernel code model we know that all object resist in the negative half
3156 // of 32bits address space. We may not accept negative offsets, since they may
3157 // be just off and we may accept pretty large positive ones.
3158 if (M == CodeModel::Kernel && Offset > 0)
3159 return true;
3160
3161 return false;
3162}
3163
Evan Chengef41ff62011-06-23 17:54:54 +00003164/// isCalleePop - Determines whether the callee is required to pop its
3165/// own arguments. Callee pop is necessary to support tail calls.
3166bool X86::isCalleePop(CallingConv::ID CallingConv,
3167 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3168 if (IsVarArg)
3169 return false;
3170
3171 switch (CallingConv) {
3172 default:
3173 return false;
3174 case CallingConv::X86_StdCall:
3175 return !is64Bit;
3176 case CallingConv::X86_FastCall:
3177 return !is64Bit;
3178 case CallingConv::X86_ThisCall:
3179 return !is64Bit;
3180 case CallingConv::Fast:
3181 return TailCallOpt;
3182 case CallingConv::GHC:
3183 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003184 case CallingConv::HiPE:
3185 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003186 }
3187}
3188
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003189/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3190/// specific condition code, returning the condition code and the LHS/RHS of the
3191/// comparison to make.
3192static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3193 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003194 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003195 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3196 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3197 // X > -1 -> X == 0, jump !sign.
3198 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003199 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003200 }
3201 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003202 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003203 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003204 }
3205 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003206 // X < 1 -> X <= 0
3207 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003208 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003209 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003210 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003211
Evan Chengd9558e02006-01-06 00:43:03 +00003212 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003213 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003214 case ISD::SETEQ: return X86::COND_E;
3215 case ISD::SETGT: return X86::COND_G;
3216 case ISD::SETGE: return X86::COND_GE;
3217 case ISD::SETLT: return X86::COND_L;
3218 case ISD::SETLE: return X86::COND_LE;
3219 case ISD::SETNE: return X86::COND_NE;
3220 case ISD::SETULT: return X86::COND_B;
3221 case ISD::SETUGT: return X86::COND_A;
3222 case ISD::SETULE: return X86::COND_BE;
3223 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003224 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003226
Chris Lattner4c78e022008-12-23 23:42:27 +00003227 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003228
Chris Lattner4c78e022008-12-23 23:42:27 +00003229 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003230 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3231 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003232 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3233 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003234 }
3235
Chris Lattner4c78e022008-12-23 23:42:27 +00003236 switch (SetCCOpcode) {
3237 default: break;
3238 case ISD::SETOLT:
3239 case ISD::SETOLE:
3240 case ISD::SETUGT:
3241 case ISD::SETUGE:
3242 std::swap(LHS, RHS);
3243 break;
3244 }
3245
3246 // On a floating point condition, the flags are set as follows:
3247 // ZF PF CF op
3248 // 0 | 0 | 0 | X > Y
3249 // 0 | 0 | 1 | X < Y
3250 // 1 | 0 | 0 | X == Y
3251 // 1 | 1 | 1 | unordered
3252 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003253 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003254 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003255 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003256 case ISD::SETOLT: // flipped
3257 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003258 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003259 case ISD::SETOLE: // flipped
3260 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003261 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003262 case ISD::SETUGT: // flipped
3263 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003264 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003265 case ISD::SETUGE: // flipped
3266 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003267 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003268 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003269 case ISD::SETNE: return X86::COND_NE;
3270 case ISD::SETUO: return X86::COND_P;
3271 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003272 case ISD::SETOEQ:
3273 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003274 }
Evan Chengd9558e02006-01-06 00:43:03 +00003275}
3276
Evan Cheng4a460802006-01-11 00:33:36 +00003277/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3278/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003279/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003280static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003281 switch (X86CC) {
3282 default:
3283 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003284 case X86::COND_B:
3285 case X86::COND_BE:
3286 case X86::COND_E:
3287 case X86::COND_P:
3288 case X86::COND_A:
3289 case X86::COND_AE:
3290 case X86::COND_NE:
3291 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003292 return true;
3293 }
3294}
3295
Evan Chengeb2f9692009-10-27 19:56:55 +00003296/// isFPImmLegal - Returns true if the target can instruction select the
3297/// specified FP immediate natively. If false, the legalizer will
3298/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003299bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003300 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3301 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3302 return true;
3303 }
3304 return false;
3305}
3306
Nate Begeman9008ca62009-04-27 18:41:29 +00003307/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3308/// the specified range (L, H].
3309static bool isUndefOrInRange(int Val, int Low, int Hi) {
3310 return (Val < 0) || (Val >= Low && Val < Hi);
3311}
3312
3313/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3314/// specified value.
3315static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003316 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003317}
3318
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003319/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003320/// from position Pos and ending in Pos+Size, falls within the specified
3321/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003322static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003323 unsigned Pos, unsigned Size, int Low) {
3324 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003325 if (!isUndefOrEqual(Mask[i], Low))
3326 return false;
3327 return true;
3328}
3329
Nate Begeman9008ca62009-04-27 18:41:29 +00003330/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3331/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3332/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003333static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003334 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003335 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003337 return (Mask[0] < 2 && Mask[1] < 2);
3338 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003339}
3340
Nate Begeman9008ca62009-04-27 18:41:29 +00003341/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3342/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003343static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3344 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003345 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003346
Nate Begeman9008ca62009-04-27 18:41:29 +00003347 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003348 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3349 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003350
Evan Cheng506d3df2006-03-29 23:07:14 +00003351 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003352 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003353 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003354 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003355
Craig Toppera9a568a2012-05-02 08:03:44 +00003356 if (VT == MVT::v16i16) {
3357 // Lower quadword copied in order or undef.
3358 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3359 return false;
3360
3361 // Upper quadword shuffled.
3362 for (unsigned i = 12; i != 16; ++i)
3363 if (!isUndefOrInRange(Mask[i], 12, 16))
3364 return false;
3365 }
3366
Evan Cheng506d3df2006-03-29 23:07:14 +00003367 return true;
3368}
3369
Nate Begeman9008ca62009-04-27 18:41:29 +00003370/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3371/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003372static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3373 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003374 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003375
Rafael Espindola15684b22009-04-24 12:40:33 +00003376 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003377 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3378 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003379
Rafael Espindola15684b22009-04-24 12:40:33 +00003380 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003381 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003382 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003383 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003384
Craig Toppera9a568a2012-05-02 08:03:44 +00003385 if (VT == MVT::v16i16) {
3386 // Upper quadword copied in order.
3387 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3388 return false;
3389
3390 // Lower quadword shuffled.
3391 for (unsigned i = 8; i != 12; ++i)
3392 if (!isUndefOrInRange(Mask[i], 8, 12))
3393 return false;
3394 }
3395
Rafael Espindola15684b22009-04-24 12:40:33 +00003396 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003397}
3398
Nate Begemana09008b2009-10-19 02:17:23 +00003399/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3400/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003401static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3402 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003403 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3404 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003405 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003406
Craig Topper0e2037b2012-01-20 05:53:00 +00003407 unsigned NumElts = VT.getVectorNumElements();
3408 unsigned NumLanes = VT.getSizeInBits()/128;
3409 unsigned NumLaneElts = NumElts/NumLanes;
3410
3411 // Do not handle 64-bit element shuffles with palignr.
3412 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003413 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003414
Craig Topper0e2037b2012-01-20 05:53:00 +00003415 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3416 unsigned i;
3417 for (i = 0; i != NumLaneElts; ++i) {
3418 if (Mask[i+l] >= 0)
3419 break;
3420 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003421
Craig Topper0e2037b2012-01-20 05:53:00 +00003422 // Lane is all undef, go to next lane
3423 if (i == NumLaneElts)
3424 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003425
Craig Topper0e2037b2012-01-20 05:53:00 +00003426 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003427
Craig Topper0e2037b2012-01-20 05:53:00 +00003428 // Make sure its in this lane in one of the sources
3429 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3430 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003431 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003432
3433 // If not lane 0, then we must match lane 0
3434 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3435 return false;
3436
3437 // Correct second source to be contiguous with first source
3438 if (Start >= (int)NumElts)
3439 Start -= NumElts - NumLaneElts;
3440
3441 // Make sure we're shifting in the right direction.
3442 if (Start <= (int)(i+l))
3443 return false;
3444
3445 Start -= i;
3446
3447 // Check the rest of the elements to see if they are consecutive.
3448 for (++i; i != NumLaneElts; ++i) {
3449 int Idx = Mask[i+l];
3450
3451 // Make sure its in this lane
3452 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3453 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3454 return false;
3455
3456 // If not lane 0, then we must match lane 0
3457 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3458 return false;
3459
3460 if (Idx >= (int)NumElts)
3461 Idx -= NumElts - NumLaneElts;
3462
3463 if (!isUndefOrEqual(Idx, Start+i))
3464 return false;
3465
3466 }
Nate Begemana09008b2009-10-19 02:17:23 +00003467 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003468
Nate Begemana09008b2009-10-19 02:17:23 +00003469 return true;
3470}
3471
Craig Topper1a7700a2012-01-19 08:19:12 +00003472/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3473/// the two vector operands have swapped position.
3474static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3475 unsigned NumElems) {
3476 for (unsigned i = 0; i != NumElems; ++i) {
3477 int idx = Mask[i];
3478 if (idx < 0)
3479 continue;
3480 else if (idx < (int)NumElems)
3481 Mask[i] = idx + NumElems;
3482 else
3483 Mask[i] = idx - NumElems;
3484 }
3485}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003486
Craig Topper1a7700a2012-01-19 08:19:12 +00003487/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3488/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3489/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3490/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003491static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003492 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003493 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003494 return false;
3495
Craig Topper1a7700a2012-01-19 08:19:12 +00003496 unsigned NumElems = VT.getVectorNumElements();
3497 unsigned NumLanes = VT.getSizeInBits()/128;
3498 unsigned NumLaneElems = NumElems/NumLanes;
3499
3500 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003501 return false;
3502
3503 // VSHUFPSY divides the resulting vector into 4 chunks.
3504 // The sources are also splitted into 4 chunks, and each destination
3505 // chunk must come from a different source chunk.
3506 //
3507 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3508 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3509 //
3510 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3511 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3512 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003513 // VSHUFPDY divides the resulting vector into 4 chunks.
3514 // The sources are also splitted into 4 chunks, and each destination
3515 // chunk must come from a different source chunk.
3516 //
3517 // SRC1 => X3 X2 X1 X0
3518 // SRC2 => Y3 Y2 Y1 Y0
3519 //
3520 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3521 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003522 unsigned HalfLaneElems = NumLaneElems/2;
3523 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3524 for (unsigned i = 0; i != NumLaneElems; ++i) {
3525 int Idx = Mask[i+l];
3526 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3527 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3528 return false;
3529 // For VSHUFPSY, the mask of the second half must be the same as the
3530 // first but with the appropriate offsets. This works in the same way as
3531 // VPERMILPS works with masks.
3532 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3533 continue;
3534 if (!isUndefOrEqual(Idx, Mask[i]+l))
3535 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003536 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003537 }
3538
3539 return true;
3540}
3541
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003542/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3543/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003544static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003545 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003546 return false;
3547
Craig Topper7a9a28b2012-08-12 02:23:29 +00003548 unsigned NumElems = VT.getVectorNumElements();
3549
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003550 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003551 return false;
3552
Evan Cheng2064a2b2006-03-28 06:50:32 +00003553 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003554 return isUndefOrEqual(Mask[0], 6) &&
3555 isUndefOrEqual(Mask[1], 7) &&
3556 isUndefOrEqual(Mask[2], 2) &&
3557 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003558}
3559
Nate Begeman0b10b912009-11-07 23:17:15 +00003560/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3561/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3562/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003563static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003564 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003565 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003566
Craig Topper7a9a28b2012-08-12 02:23:29 +00003567 unsigned NumElems = VT.getVectorNumElements();
3568
Nate Begeman0b10b912009-11-07 23:17:15 +00003569 if (NumElems != 4)
3570 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003571
Craig Topperdd637ae2012-02-19 05:41:45 +00003572 return isUndefOrEqual(Mask[0], 2) &&
3573 isUndefOrEqual(Mask[1], 3) &&
3574 isUndefOrEqual(Mask[2], 2) &&
3575 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003576}
3577
Evan Cheng5ced1d82006-04-06 23:23:56 +00003578/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3579/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003580static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003581 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003582 return false;
3583
Craig Topperdd637ae2012-02-19 05:41:45 +00003584 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003585
Evan Cheng5ced1d82006-04-06 23:23:56 +00003586 if (NumElems != 2 && NumElems != 4)
3587 return false;
3588
Chad Rosier238ae312012-04-30 17:47:15 +00003589 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003590 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003591 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003592
Chad Rosier238ae312012-04-30 17:47:15 +00003593 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003594 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003595 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003596
3597 return true;
3598}
3599
Nate Begeman0b10b912009-11-07 23:17:15 +00003600/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3601/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003602static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003603 if (!VT.is128BitVector())
3604 return false;
3605
Craig Topperdd637ae2012-02-19 05:41:45 +00003606 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003607
Craig Topper7a9a28b2012-08-12 02:23:29 +00003608 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003609 return false;
3610
Chad Rosier238ae312012-04-30 17:47:15 +00003611 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003612 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003613 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003614
Chad Rosier238ae312012-04-30 17:47:15 +00003615 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3616 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003617 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003618
3619 return true;
3620}
3621
Elena Demikhovsky15963732012-06-26 08:04:10 +00003622//
3623// Some special combinations that can be optimized.
3624//
3625static
3626SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3627 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003628 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003629 DebugLoc dl = SVOp->getDebugLoc();
3630
3631 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3632 return SDValue();
3633
3634 ArrayRef<int> Mask = SVOp->getMask();
3635
3636 // These are the special masks that may be optimized.
3637 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3638 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3639 bool MatchEvenMask = true;
3640 bool MatchOddMask = true;
3641 for (int i=0; i<8; ++i) {
3642 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3643 MatchEvenMask = false;
3644 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3645 MatchOddMask = false;
3646 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003647
Elena Demikhovsky32510202012-09-04 12:49:02 +00003648 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003649 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003650
Elena Demikhovsky15963732012-06-26 08:04:10 +00003651 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3652
Elena Demikhovsky32510202012-09-04 12:49:02 +00003653 SDValue Op0 = SVOp->getOperand(0);
3654 SDValue Op1 = SVOp->getOperand(1);
3655
3656 if (MatchEvenMask) {
3657 // Shift the second operand right to 32 bits.
3658 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3659 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3660 } else {
3661 // Shift the first operand left to 32 bits.
3662 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3663 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3664 }
3665 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3666 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003667}
3668
Evan Cheng0038e592006-03-28 00:39:58 +00003669/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3670/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003671static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003672 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003673 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003674
3675 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3676 "Unsupported vector type for unpckh");
3677
Craig Topper5a529e42013-01-18 06:44:29 +00003678 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003679 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003680 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003681
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003682 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3683 // independently on 128-bit lanes.
3684 unsigned NumLanes = VT.getSizeInBits()/128;
3685 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003686
Craig Topper94438ba2011-12-16 08:06:31 +00003687 for (unsigned l = 0; l != NumLanes; ++l) {
3688 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3689 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003690 i += 2, ++j) {
3691 int BitI = Mask[i];
3692 int BitI1 = Mask[i+1];
3693 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003694 return false;
David Greenea20244d2011-03-02 17:23:43 +00003695 if (V2IsSplat) {
3696 if (!isUndefOrEqual(BitI1, NumElts))
3697 return false;
3698 } else {
3699 if (!isUndefOrEqual(BitI1, j + NumElts))
3700 return false;
3701 }
Evan Cheng39623da2006-04-20 08:58:49 +00003702 }
Evan Cheng0038e592006-03-28 00:39:58 +00003703 }
David Greenea20244d2011-03-02 17:23:43 +00003704
Evan Cheng0038e592006-03-28 00:39:58 +00003705 return true;
3706}
3707
Evan Cheng4fcb9222006-03-28 02:43:26 +00003708/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3709/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003710static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003711 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003712 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003713
3714 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3715 "Unsupported vector type for unpckh");
3716
Craig Topper5a529e42013-01-18 06:44:29 +00003717 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003718 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003719 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003720
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003721 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3722 // independently on 128-bit lanes.
3723 unsigned NumLanes = VT.getSizeInBits()/128;
3724 unsigned NumLaneElts = NumElts/NumLanes;
3725
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003726 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003727 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3728 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003729 int BitI = Mask[i];
3730 int BitI1 = Mask[i+1];
3731 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003732 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003733 if (V2IsSplat) {
3734 if (isUndefOrEqual(BitI1, NumElts))
3735 return false;
3736 } else {
3737 if (!isUndefOrEqual(BitI1, j+NumElts))
3738 return false;
3739 }
Evan Cheng39623da2006-04-20 08:58:49 +00003740 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003741 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003742 return true;
3743}
3744
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003745/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3746/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3747/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003748static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003749 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003750 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003751
3752 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3753 "Unsupported vector type for unpckh");
3754
Craig Topper5a529e42013-01-18 06:44:29 +00003755 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003756 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003757 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003758
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003759 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3760 // FIXME: Need a better way to get rid of this, there's no latency difference
3761 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3762 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003763 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003764 return false;
3765
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003766 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3767 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003768 unsigned NumLanes = VT.getSizeInBits()/128;
3769 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003770
Craig Topper94438ba2011-12-16 08:06:31 +00003771 for (unsigned l = 0; l != NumLanes; ++l) {
3772 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3773 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003774 i += 2, ++j) {
3775 int BitI = Mask[i];
3776 int BitI1 = Mask[i+1];
3777
3778 if (!isUndefOrEqual(BitI, j))
3779 return false;
3780 if (!isUndefOrEqual(BitI1, j))
3781 return false;
3782 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003783 }
David Greenea20244d2011-03-02 17:23:43 +00003784
Rafael Espindola15684b22009-04-24 12:40:33 +00003785 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003786}
3787
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003788/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3789/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3790/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003791static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003792 unsigned NumElts = VT.getVectorNumElements();
3793
3794 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3795 "Unsupported vector type for unpckh");
3796
Craig Topper5a529e42013-01-18 06:44:29 +00003797 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003798 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003799 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003800
Craig Topper94438ba2011-12-16 08:06:31 +00003801 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3802 // independently on 128-bit lanes.
3803 unsigned NumLanes = VT.getSizeInBits()/128;
3804 unsigned NumLaneElts = NumElts/NumLanes;
3805
3806 for (unsigned l = 0; l != NumLanes; ++l) {
3807 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3808 i != (l+1)*NumLaneElts; i += 2, ++j) {
3809 int BitI = Mask[i];
3810 int BitI1 = Mask[i+1];
3811 if (!isUndefOrEqual(BitI, j))
3812 return false;
3813 if (!isUndefOrEqual(BitI1, j))
3814 return false;
3815 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003816 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003817 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003818}
3819
Evan Cheng017dcc62006-04-21 01:05:10 +00003820/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3821/// specifies a shuffle of elements that is suitable for input to MOVSS,
3822/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003823static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003824 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003825 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003826 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003827 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003828
Craig Topperc612d792012-01-02 09:17:37 +00003829 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003830
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003832 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003833
Craig Topperc612d792012-01-02 09:17:37 +00003834 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003835 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003836 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003837
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003838 return true;
3839}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003840
Craig Topper70b883b2011-11-28 10:14:51 +00003841/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003842/// as permutations between 128-bit chunks or halves. As an example: this
3843/// shuffle bellow:
3844/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3845/// The first half comes from the second half of V1 and the second half from the
3846/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003847static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3848 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003849 return false;
3850
3851 // The shuffle result is divided into half A and half B. In total the two
3852 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3853 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003854 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003855 bool MatchA = false, MatchB = false;
3856
3857 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003858 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003859 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3860 MatchA = true;
3861 break;
3862 }
3863 }
3864
3865 // Check if B 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, HalfSize, HalfSize, Half*HalfSize)) {
3868 MatchB = true;
3869 break;
3870 }
3871 }
3872
3873 return MatchA && MatchB;
3874}
3875
Craig Topper70b883b2011-11-28 10:14:51 +00003876/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3877/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003878static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003879 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003880
Craig Topperc612d792012-01-02 09:17:37 +00003881 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003882
Craig Topperc612d792012-01-02 09:17:37 +00003883 unsigned FstHalf = 0, SndHalf = 0;
3884 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003885 if (SVOp->getMaskElt(i) > 0) {
3886 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3887 break;
3888 }
3889 }
Craig Topperc612d792012-01-02 09:17:37 +00003890 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003891 if (SVOp->getMaskElt(i) > 0) {
3892 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3893 break;
3894 }
3895 }
3896
3897 return (FstHalf | (SndHalf << 4));
3898}
3899
Craig Topper70b883b2011-11-28 10:14:51 +00003900/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003901/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3902/// Note that VPERMIL mask matching is different depending whether theunderlying
3903/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3904/// to the same elements of the low, but to the higher half of the source.
3905/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003906/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003907static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3908 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003909 return false;
3910
Craig Topperc612d792012-01-02 09:17:37 +00003911 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003912 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003913 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003914 return false;
3915
Craig Topperc612d792012-01-02 09:17:37 +00003916 unsigned NumLanes = VT.getSizeInBits()/128;
3917 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003918 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003919 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003920 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003921 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003922 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003923 continue;
3924 // VPERMILPS handling
3925 if (Mask[i] < 0)
3926 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003927 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003928 return false;
3929 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003930 }
3931
3932 return true;
3933}
3934
Craig Topper5aaffa82012-02-19 02:53:47 +00003935/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003936/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003937/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003938static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003940 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003941 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003942
3943 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003944 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003945 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003946
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003948 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003949
Craig Topperc612d792012-01-02 09:17:37 +00003950 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3952 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3953 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003954 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003955
Evan Cheng39623da2006-04-20 08:58:49 +00003956 return true;
3957}
3958
Evan Chengd9539472006-04-14 21:59:03 +00003959/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3960/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003961/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003962static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003963 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003964 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003965 return false;
3966
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003967 unsigned NumElems = VT.getVectorNumElements();
3968
Craig Topper5a529e42013-01-18 06:44:29 +00003969 if ((VT.is128BitVector() && NumElems != 4) ||
3970 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003971 return false;
3972
3973 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003974 for (unsigned i = 0; i != NumElems; i += 2)
3975 if (!isUndefOrEqual(Mask[i], i+1) ||
3976 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003978
3979 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003980}
3981
3982/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3983/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003984/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003985static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003986 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003987 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003988 return false;
3989
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003990 unsigned NumElems = VT.getVectorNumElements();
3991
Craig Topper5a529e42013-01-18 06:44:29 +00003992 if ((VT.is128BitVector() && NumElems != 4) ||
3993 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003994 return false;
3995
3996 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003997 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003998 if (!isUndefOrEqual(Mask[i], i) ||
3999 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00004000 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00004001
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00004002 return true;
Evan Chengd9539472006-04-14 21:59:03 +00004003}
4004
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004005/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
4006/// specifies a shuffle of elements that is suitable for input to 256-bit
4007/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004008static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4009 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00004010 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004011
Craig Topper7a9a28b2012-08-12 02:23:29 +00004012 unsigned NumElts = VT.getVectorNumElements();
4013 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004014 return false;
4015
Craig Topperc612d792012-01-02 09:17:37 +00004016 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004017 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004018 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004019 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004020 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004021 return false;
4022 return true;
4023}
4024
Evan Cheng0b457f02008-09-25 20:50:48 +00004025/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004026/// specifies a shuffle of elements that is suitable for input to 128-bit
4027/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004028static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004029 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004030 return false;
4031
Craig Topperc612d792012-01-02 09:17:37 +00004032 unsigned e = VT.getVectorNumElements() / 2;
4033 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004034 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004035 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004036 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004037 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004038 return false;
4039 return true;
4040}
4041
David Greenec38a03e2011-02-03 15:50:00 +00004042/// isVEXTRACTF128Index - Return true if the specified
4043/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4044/// suitable for input to VEXTRACTF128.
4045bool X86::isVEXTRACTF128Index(SDNode *N) {
4046 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4047 return false;
4048
4049 // The index should be aligned on a 128-bit boundary.
4050 uint64_t Index =
4051 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4052
Craig Topper5141d972013-01-18 08:41:28 +00004053 MVT VT = N->getValueType(0).getSimpleVT();
4054 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004055 bool Result = (Index * ElSize) % 128 == 0;
4056
4057 return Result;
4058}
4059
David Greeneccacdc12011-02-04 16:08:29 +00004060/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4061/// operand specifies a subvector insert that is suitable for input to
4062/// VINSERTF128.
4063bool X86::isVINSERTF128Index(SDNode *N) {
4064 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4065 return false;
4066
4067 // The index should be aligned on a 128-bit boundary.
4068 uint64_t Index =
4069 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4070
Craig Topper5141d972013-01-18 08:41:28 +00004071 MVT VT = N->getValueType(0).getSimpleVT();
4072 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004073 bool Result = (Index * ElSize) % 128 == 0;
4074
4075 return Result;
4076}
4077
Evan Cheng63d33002006-03-22 08:01:21 +00004078/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004079/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004080/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004081static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004082 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004083
Craig Topper1a7700a2012-01-19 08:19:12 +00004084 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4085 "Unsupported vector type for PSHUF/SHUFP");
4086
4087 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4088 // independently on 128-bit lanes.
4089 unsigned NumElts = VT.getVectorNumElements();
4090 unsigned NumLanes = VT.getSizeInBits()/128;
4091 unsigned NumLaneElts = NumElts/NumLanes;
4092
4093 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4094 "Only supports 2 or 4 elements per lane");
4095
4096 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004097 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004098 for (unsigned i = 0; i != NumElts; ++i) {
4099 int Elt = N->getMaskElt(i);
4100 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004101 Elt &= NumLaneElts - 1;
4102 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004103 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004104 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004105
Evan Cheng63d33002006-03-22 08:01:21 +00004106 return Mask;
4107}
4108
Evan Cheng506d3df2006-03-29 23:07:14 +00004109/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004110/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004111static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004112 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004113
4114 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4115 "Unsupported vector type for PSHUFHW");
4116
4117 unsigned NumElts = VT.getVectorNumElements();
4118
Evan Cheng506d3df2006-03-29 23:07:14 +00004119 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004120 for (unsigned l = 0; l != NumElts; l += 8) {
4121 // 8 nodes per lane, but we only care about the last 4.
4122 for (unsigned i = 0; i < 4; ++i) {
4123 int Elt = N->getMaskElt(l+i+4);
4124 if (Elt < 0) continue;
4125 Elt &= 0x3; // only 2-bits.
4126 Mask |= Elt << (i * 2);
4127 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004128 }
Craig Topper6b28d352012-05-03 07:12:59 +00004129
Evan Cheng506d3df2006-03-29 23:07:14 +00004130 return Mask;
4131}
4132
4133/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004134/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004135static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004136 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004137
4138 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4139 "Unsupported vector type for PSHUFHW");
4140
4141 unsigned NumElts = VT.getVectorNumElements();
4142
Evan Cheng506d3df2006-03-29 23:07:14 +00004143 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004144 for (unsigned l = 0; l != NumElts; l += 8) {
4145 // 8 nodes per lane, but we only care about the first 4.
4146 for (unsigned i = 0; i < 4; ++i) {
4147 int Elt = N->getMaskElt(l+i);
4148 if (Elt < 0) continue;
4149 Elt &= 0x3; // only 2-bits
4150 Mask |= Elt << (i * 2);
4151 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004152 }
Craig Topper6b28d352012-05-03 07:12:59 +00004153
Evan Cheng506d3df2006-03-29 23:07:14 +00004154 return Mask;
4155}
4156
Nate Begemana09008b2009-10-19 02:17:23 +00004157/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4158/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004159static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004160 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004161 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004162
Craig Topper0e2037b2012-01-20 05:53:00 +00004163 unsigned NumElts = VT.getVectorNumElements();
4164 unsigned NumLanes = VT.getSizeInBits()/128;
4165 unsigned NumLaneElts = NumElts/NumLanes;
4166
4167 int Val = 0;
4168 unsigned i;
4169 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004170 Val = SVOp->getMaskElt(i);
4171 if (Val >= 0)
4172 break;
4173 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004174 if (Val >= (int)NumElts)
4175 Val -= NumElts - NumLaneElts;
4176
Eli Friedman63f8dde2011-07-25 21:36:45 +00004177 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004178 return (Val - i) * EltSize;
4179}
4180
David Greenec38a03e2011-02-03 15:50:00 +00004181/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4182/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4183/// instructions.
4184unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4185 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4186 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4187
4188 uint64_t Index =
4189 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4190
Craig Toppercfcab212013-01-19 08:27:45 +00004191 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4192 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004193
4194 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004195 return Index / NumElemsPerChunk;
4196}
4197
David Greeneccacdc12011-02-04 16:08:29 +00004198/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4199/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4200/// instructions.
4201unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4202 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4203 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4204
4205 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004206 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004207
Craig Toppercfcab212013-01-19 08:27:45 +00004208 MVT VecVT = N->getValueType(0).getSimpleVT();
4209 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004210
4211 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004212 return Index / NumElemsPerChunk;
4213}
4214
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004215/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4216/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4217/// Handles 256-bit.
4218static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004219 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004220
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004221 unsigned NumElts = VT.getVectorNumElements();
4222
Craig Topper095c5282012-04-15 23:48:57 +00004223 assert((VT.is256BitVector() && NumElts == 4) &&
4224 "Unsupported vector type for VPERMQ/VPERMPD");
4225
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004226 unsigned Mask = 0;
4227 for (unsigned i = 0; i != NumElts; ++i) {
4228 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004229 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004230 continue;
4231 Mask |= Elt << (i*2);
4232 }
4233
4234 return Mask;
4235}
Evan Cheng37b73872009-07-30 08:33:02 +00004236/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4237/// constant +0.0.
4238bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004239 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4240 return CN->isNullValue();
4241 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4242 return CFP->getValueAPF().isPosZero();
4243 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004244}
4245
Nate Begeman9008ca62009-04-27 18:41:29 +00004246/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4247/// their permute mask.
4248static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4249 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004250 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004251 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004252 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004253
Nate Begeman5a5ca152009-04-29 05:20:52 +00004254 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004255 int Idx = SVOp->getMaskElt(i);
4256 if (Idx >= 0) {
4257 if (Idx < (int)NumElems)
4258 Idx += NumElems;
4259 else
4260 Idx -= NumElems;
4261 }
4262 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004263 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4265 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004266}
4267
Evan Cheng533a0aa2006-04-19 20:35:22 +00004268/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4269/// match movhlps. The lower half elements should come from upper half of
4270/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004271/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004272static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004273 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004274 return false;
4275 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004276 return false;
4277 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004278 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004279 return false;
4280 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004281 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004282 return false;
4283 return true;
4284}
4285
Evan Cheng5ced1d82006-04-06 23:23:56 +00004286/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004287/// is promoted to a vector. It also returns the LoadSDNode by reference if
4288/// required.
4289static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004290 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4291 return false;
4292 N = N->getOperand(0).getNode();
4293 if (!ISD::isNON_EXTLoad(N))
4294 return false;
4295 if (LD)
4296 *LD = cast<LoadSDNode>(N);
4297 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004298}
4299
Dan Gohman65fd6562011-11-03 21:49:52 +00004300// Test whether the given value is a vector value which will be legalized
4301// into a load.
4302static bool WillBeConstantPoolLoad(SDNode *N) {
4303 if (N->getOpcode() != ISD::BUILD_VECTOR)
4304 return false;
4305
4306 // Check for any non-constant elements.
4307 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4308 switch (N->getOperand(i).getNode()->getOpcode()) {
4309 case ISD::UNDEF:
4310 case ISD::ConstantFP:
4311 case ISD::Constant:
4312 break;
4313 default:
4314 return false;
4315 }
4316
4317 // Vectors of all-zeros and all-ones are materialized with special
4318 // instructions rather than being loaded.
4319 return !ISD::isBuildVectorAllZeros(N) &&
4320 !ISD::isBuildVectorAllOnes(N);
4321}
4322
Evan Cheng533a0aa2006-04-19 20:35:22 +00004323/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4324/// match movlp{s|d}. The lower half elements should come from lower half of
4325/// V1 (and in order), and the upper half elements should come from the upper
4326/// half of V2 (and in order). And since V1 will become the source of the
4327/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004328static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004329 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004330 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004331 return false;
4332
Evan Cheng466685d2006-10-09 20:57:25 +00004333 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004334 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004335 // Is V2 is a vector load, don't do this transformation. We will try to use
4336 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004337 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004338 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004339
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004340 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004341
Evan Cheng533a0aa2006-04-19 20:35:22 +00004342 if (NumElems != 2 && NumElems != 4)
4343 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004344 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004345 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004346 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004347 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004348 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004349 return false;
4350 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004351}
4352
Evan Cheng39623da2006-04-20 08:58:49 +00004353/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4354/// all the same.
4355static bool isSplatVector(SDNode *N) {
4356 if (N->getOpcode() != ISD::BUILD_VECTOR)
4357 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004358
Dan Gohman475871a2008-07-27 21:46:04 +00004359 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004360 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4361 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004362 return false;
4363 return true;
4364}
4365
Evan Cheng213d2cf2007-05-17 18:45:50 +00004366/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004367/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004368/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004369static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004370 SDValue V1 = N->getOperand(0);
4371 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004372 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4373 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004375 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004377 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4378 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004379 if (Opc != ISD::BUILD_VECTOR ||
4380 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 return false;
4382 } else if (Idx >= 0) {
4383 unsigned Opc = V1.getOpcode();
4384 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4385 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004386 if (Opc != ISD::BUILD_VECTOR ||
4387 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004388 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004389 }
4390 }
4391 return true;
4392}
4393
4394/// getZeroVector - Returns a vector of specified type with all zero elements.
4395///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004396static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004397 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004398 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004399
Dale Johannesen0488fb62010-09-30 23:57:10 +00004400 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004401 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004402 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004403 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004404 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004405 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4406 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4407 } else { // SSE1
4408 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4409 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4410 }
Craig Topper5a529e42013-01-18 06:44:29 +00004411 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004412 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004413 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4414 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4415 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4416 } else {
4417 // 256-bit logic and arithmetic instructions in AVX are all
4418 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4419 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4420 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4421 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4422 }
Craig Topper9d352402012-04-23 07:24:41 +00004423 } else
4424 llvm_unreachable("Unexpected vector type");
4425
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004426 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004427}
4428
Chris Lattner8a594482007-11-25 00:24:49 +00004429/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004430/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4431/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4432/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004433static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004434 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004435 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004436
Owen Anderson825b72b2009-08-11 20:47:22 +00004437 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004438 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004439 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004440 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004441 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4442 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4443 } else { // AVX
4444 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004445 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004446 }
Craig Topper5a529e42013-01-18 06:44:29 +00004447 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004448 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004449 } else
4450 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004451
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004452 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004453}
4454
Evan Cheng39623da2006-04-20 08:58:49 +00004455/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4456/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004457static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004458 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004459 if (Mask[i] > (int)NumElems) {
4460 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004461 }
Evan Cheng39623da2006-04-20 08:58:49 +00004462 }
Evan Cheng39623da2006-04-20 08:58:49 +00004463}
4464
Evan Cheng017dcc62006-04-21 01:05:10 +00004465/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4466/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004467static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 SDValue V2) {
4469 unsigned NumElems = VT.getVectorNumElements();
4470 SmallVector<int, 8> Mask;
4471 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004472 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 Mask.push_back(i);
4474 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004475}
4476
Nate Begeman9008ca62009-04-27 18:41:29 +00004477/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004478static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc 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;
Evan Chengc575ca22006-04-17 20:43:08 +00004482 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004483 Mask.push_back(i);
4484 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004485 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004487}
4488
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004489/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004490static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 SDValue V2) {
4492 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004494 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004495 Mask.push_back(i + Half);
4496 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004497 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004499}
4500
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004501// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004502// a generic shuffle instruction because the target has no such instructions.
4503// Generate shuffles which repeat i16 and i8 several times until they can be
4504// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004505static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004506 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004507 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004508 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004509
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 while (NumElems > 4) {
4511 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004512 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004514 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004515 EltNo -= NumElems/2;
4516 }
4517 NumElems >>= 1;
4518 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004519 return V;
4520}
Eric Christopherfd179292009-08-27 18:07:15 +00004521
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004522/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4523static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4524 EVT VT = V.getValueType();
4525 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004526
Craig Topper5a529e42013-01-18 06:44:29 +00004527 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004528 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004529 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004530 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4531 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004532 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004533 // To use VPERMILPS to splat scalars, the second half of indicies must
4534 // refer to the higher part, which is a duplication of the lower one,
4535 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004536 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4537 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004538
4539 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4540 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4541 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004542 } else
4543 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004544
4545 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4546}
4547
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004548/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004549static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4550 EVT SrcVT = SV->getValueType(0);
4551 SDValue V1 = SV->getOperand(0);
4552 DebugLoc dl = SV->getDebugLoc();
4553
4554 int EltNo = SV->getSplatIndex();
4555 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004556 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004557
Craig Topper5a529e42013-01-18 06:44:29 +00004558 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4559 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004560
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004561 // Extract the 128-bit part containing the splat element and update
4562 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004563 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004564 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4565 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004566 EltNo -= NumElems/2;
4567 }
4568
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004569 // All i16 and i8 vector types can't be used directly by a generic shuffle
4570 // instruction because the target has no such instruction. Generate shuffles
4571 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004572 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004573 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004574 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004575 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004576
4577 // Recreate the 256-bit vector and place the same 128-bit vector
4578 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004579 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004580 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004581 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004582 }
4583
4584 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004585}
4586
Evan Chengba05f722006-04-21 23:03:30 +00004587/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004588/// vector of zero or undef vector. This produces a shuffle where the low
4589/// element of V2 is swizzled into the zero/undef vector, landing at element
4590/// 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 +00004591static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004592 bool IsZero,
4593 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004594 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004595 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004596 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004597 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 unsigned NumElems = VT.getVectorNumElements();
4599 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004600 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 // If this is the insertion idx, put the low elt of V2 here.
4602 MaskVec.push_back(i == Idx ? NumElems : i);
4603 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004604}
4605
Craig Toppera1ffc682012-03-20 06:42:26 +00004606/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4607/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004608/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004609static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004610 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004611 unsigned NumElems = VT.getVectorNumElements();
4612 SDValue ImmN;
4613
Craig Topper89f4e662012-03-20 07:17:59 +00004614 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004615 switch(N->getOpcode()) {
4616 case X86ISD::SHUFP:
4617 ImmN = N->getOperand(N->getNumOperands()-1);
4618 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4619 break;
4620 case X86ISD::UNPCKH:
4621 DecodeUNPCKHMask(VT, Mask);
4622 break;
4623 case X86ISD::UNPCKL:
4624 DecodeUNPCKLMask(VT, Mask);
4625 break;
4626 case X86ISD::MOVHLPS:
4627 DecodeMOVHLPSMask(NumElems, Mask);
4628 break;
4629 case X86ISD::MOVLHPS:
4630 DecodeMOVLHPSMask(NumElems, Mask);
4631 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004632 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004633 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004634 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004635 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004636 case X86ISD::PSHUFD:
4637 case X86ISD::VPERMILP:
4638 ImmN = N->getOperand(N->getNumOperands()-1);
4639 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004640 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004641 break;
4642 case X86ISD::PSHUFHW:
4643 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004644 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004645 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004646 break;
4647 case X86ISD::PSHUFLW:
4648 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004649 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004650 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004651 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004652 case X86ISD::VPERMI:
4653 ImmN = N->getOperand(N->getNumOperands()-1);
4654 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4655 IsUnary = true;
4656 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004657 case X86ISD::MOVSS:
4658 case X86ISD::MOVSD: {
4659 // The index 0 always comes from the first element of the second source,
4660 // this is why MOVSS and MOVSD are used in the first place. The other
4661 // elements come from the other positions of the first source vector
4662 Mask.push_back(NumElems);
4663 for (unsigned i = 1; i != NumElems; ++i) {
4664 Mask.push_back(i);
4665 }
4666 break;
4667 }
4668 case X86ISD::VPERM2X128:
4669 ImmN = N->getOperand(N->getNumOperands()-1);
4670 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004671 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004672 break;
4673 case X86ISD::MOVDDUP:
4674 case X86ISD::MOVLHPD:
4675 case X86ISD::MOVLPD:
4676 case X86ISD::MOVLPS:
4677 case X86ISD::MOVSHDUP:
4678 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004679 // Not yet implemented
4680 return false;
4681 default: llvm_unreachable("unknown target shuffle node");
4682 }
4683
4684 return true;
4685}
4686
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004687/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4688/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004689static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004690 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004691 if (Depth == 6)
4692 return SDValue(); // Limit search depth.
4693
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694 SDValue V = SDValue(N, 0);
4695 EVT VT = V.getValueType();
4696 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004697
4698 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4699 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004700 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701
Craig Topper3d092db2012-03-21 02:14:01 +00004702 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004703 return DAG.getUNDEF(VT.getVectorElementType());
4704
Craig Topperd156dc12012-02-06 07:17:51 +00004705 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004706 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4707 : SV->getOperand(1);
4708 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004709 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004710
4711 // Recurse into target specific vector shuffles to find scalars.
4712 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004713 MVT ShufVT = V.getValueType().getSimpleVT();
4714 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004715 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004716 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004717
Craig Topperd978c542012-05-06 19:46:21 +00004718 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004719 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004720
Craig Topper3d092db2012-03-21 02:14:01 +00004721 int Elt = ShuffleMask[Index];
4722 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004723 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004724
Craig Topper3d092db2012-03-21 02:14:01 +00004725 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004726 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004727 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004728 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004729 }
4730
4731 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004732 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004733 V = V.getOperand(0);
4734 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004735 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004736
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004737 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004738 return SDValue();
4739 }
4740
4741 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4742 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004743 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004744
4745 if (V.getOpcode() == ISD::BUILD_VECTOR)
4746 return V.getOperand(Index);
4747
4748 return SDValue();
4749}
4750
4751/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4752/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004753/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004754static
Craig Topper3d092db2012-03-21 02:14:01 +00004755unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004756 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004757 unsigned i;
4758 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004759 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004760 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004761 if (!(Elt.getNode() &&
4762 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4763 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004764 }
4765
4766 return i;
4767}
4768
Craig Topper3d092db2012-03-21 02:14:01 +00004769/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4770/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004771/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4772static
Craig Topper3d092db2012-03-21 02:14:01 +00004773bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4774 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4775 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004776 bool SeenV1 = false;
4777 bool SeenV2 = false;
4778
Craig Topper3d092db2012-03-21 02:14:01 +00004779 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004780 int Idx = SVOp->getMaskElt(i);
4781 // Ignore undef indicies
4782 if (Idx < 0)
4783 continue;
4784
Craig Topper3d092db2012-03-21 02:14:01 +00004785 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004786 SeenV1 = true;
4787 else
4788 SeenV2 = true;
4789
4790 // Only accept consecutive elements from the same vector
4791 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4792 return false;
4793 }
4794
4795 OpNum = SeenV1 ? 0 : 1;
4796 return true;
4797}
4798
4799/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4800/// logical left shift of a vector.
4801static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4802 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4803 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4804 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4805 false /* check zeros from right */, DAG);
4806 unsigned OpSrc;
4807
4808 if (!NumZeros)
4809 return false;
4810
4811 // Considering the elements in the mask that are not consecutive zeros,
4812 // check if they consecutively come from only one of the source vectors.
4813 //
4814 // V1 = {X, A, B, C} 0
4815 // \ \ \ /
4816 // vector_shuffle V1, V2 <1, 2, 3, X>
4817 //
4818 if (!isShuffleMaskConsecutive(SVOp,
4819 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004820 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004821 NumZeros, // Where to start looking in the src vector
4822 NumElems, // Number of elements in vector
4823 OpSrc)) // Which source operand ?
4824 return false;
4825
4826 isLeft = false;
4827 ShAmt = NumZeros;
4828 ShVal = SVOp->getOperand(OpSrc);
4829 return true;
4830}
4831
4832/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4833/// logical left shift of a vector.
4834static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4835 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4836 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4837 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4838 true /* check zeros from left */, DAG);
4839 unsigned OpSrc;
4840
4841 if (!NumZeros)
4842 return false;
4843
4844 // Considering the elements in the mask that are not consecutive zeros,
4845 // check if they consecutively come from only one of the source vectors.
4846 //
4847 // 0 { A, B, X, X } = V2
4848 // / \ / /
4849 // vector_shuffle V1, V2 <X, X, 4, 5>
4850 //
4851 if (!isShuffleMaskConsecutive(SVOp,
4852 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004853 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004854 0, // Where to start looking in the src vector
4855 NumElems, // Number of elements in vector
4856 OpSrc)) // Which source operand ?
4857 return false;
4858
4859 isLeft = true;
4860 ShAmt = NumZeros;
4861 ShVal = SVOp->getOperand(OpSrc);
4862 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004863}
4864
4865/// isVectorShift - Returns true if the shuffle can be implemented as a
4866/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004867static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004868 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004869 // Although the logic below support any bitwidth size, there are no
4870 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004871 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004872 return false;
4873
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004874 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4875 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4876 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004877
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004878 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004879}
4880
Evan Chengc78d3b42006-04-24 18:01:45 +00004881/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4882///
Dan Gohman475871a2008-07-27 21:46:04 +00004883static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004884 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004885 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004886 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004887 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004888 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004889 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004890
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004891 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004893 bool First = true;
4894 for (unsigned i = 0; i < 16; ++i) {
4895 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4896 if (ThisIsNonZero && First) {
4897 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004898 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004899 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004901 First = false;
4902 }
4903
4904 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004905 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004906 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4907 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004908 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004910 }
4911 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004912 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4913 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4914 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004915 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004916 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004917 } else
4918 ThisElt = LastElt;
4919
Gabor Greifba36cb52008-08-28 21:40:38 +00004920 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004921 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004922 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004923 }
4924 }
4925
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004926 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004927}
4928
Bill Wendlinga348c562007-03-22 18:42:45 +00004929/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004930///
Dan Gohman475871a2008-07-27 21:46:04 +00004931static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004932 unsigned NumNonZero, unsigned NumZero,
4933 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004934 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004935 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004936 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004937 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004938
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004939 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004940 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004941 bool First = true;
4942 for (unsigned i = 0; i < 8; ++i) {
4943 bool isNonZero = (NonZeros & (1 << i)) != 0;
4944 if (isNonZero) {
4945 if (First) {
4946 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004947 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004948 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004949 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004950 First = false;
4951 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004952 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004953 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004954 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004955 }
4956 }
4957
4958 return V;
4959}
4960
Evan Chengf26ffe92008-05-29 08:22:04 +00004961/// getVShift - Return a vector logical shift node.
4962///
Owen Andersone50ed302009-08-10 22:56:29 +00004963static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004964 unsigned NumBits, SelectionDAG &DAG,
4965 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004966 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004967 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004968 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004969 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4970 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004971 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004972 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004973 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004974}
4975
Dan Gohman475871a2008-07-27 21:46:04 +00004976SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004977X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004978 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004979
Evan Chengc3630942009-12-09 21:00:30 +00004980 // Check if the scalar load can be widened into a vector load. And if
4981 // the address is "base + cst" see if the cst can be "absorbed" into
4982 // the shuffle mask.
4983 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4984 SDValue Ptr = LD->getBasePtr();
4985 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4986 return SDValue();
4987 EVT PVT = LD->getValueType(0);
4988 if (PVT != MVT::i32 && PVT != MVT::f32)
4989 return SDValue();
4990
4991 int FI = -1;
4992 int64_t Offset = 0;
4993 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4994 FI = FINode->getIndex();
4995 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004996 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004997 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4998 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4999 Offset = Ptr.getConstantOperandVal(1);
5000 Ptr = Ptr.getOperand(0);
5001 } else {
5002 return SDValue();
5003 }
5004
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005005 // FIXME: 256-bit vector instructions don't require a strict alignment,
5006 // improve this code to support it better.
5007 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005008 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005009 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005010 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005011 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005012 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005013 // Can't change the alignment. FIXME: It's possible to compute
5014 // the exact stack offset and reference FI + adjust offset instead.
5015 // If someone *really* cares about this. That's the way to implement it.
5016 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005017 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005018 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005019 }
5020 }
5021
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005022 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005023 // Ptr + (Offset & ~15).
5024 if (Offset < 0)
5025 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005026 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005027 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005028 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005029 if (StartOffset)
5030 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5031 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5032
5033 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005034 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005035
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005036 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5037 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005038 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005039 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005040
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005041 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005042 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005043 Mask.push_back(EltNo);
5044
Craig Toppercc3000632012-01-30 07:50:31 +00005045 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005046 }
5047
5048 return SDValue();
5049}
5050
Michael J. Spencerec38de22010-10-10 22:04:20 +00005051/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5052/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005053/// load which has the same value as a build_vector whose operands are 'elts'.
5054///
5055/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005056///
Nate Begeman1449f292010-03-24 22:19:06 +00005057/// FIXME: we'd also like to handle the case where the last elements are zero
5058/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5059/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005060static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005061 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005062 EVT EltVT = VT.getVectorElementType();
5063 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005064
Nate Begemanfdea31a2010-03-24 20:49:50 +00005065 LoadSDNode *LDBase = NULL;
5066 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005067
Nate Begeman1449f292010-03-24 22:19:06 +00005068 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005069 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005070 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005071 for (unsigned i = 0; i < NumElems; ++i) {
5072 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005073
Nate Begemanfdea31a2010-03-24 20:49:50 +00005074 if (!Elt.getNode() ||
5075 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5076 return SDValue();
5077 if (!LDBase) {
5078 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5079 return SDValue();
5080 LDBase = cast<LoadSDNode>(Elt.getNode());
5081 LastLoadedElt = i;
5082 continue;
5083 }
5084 if (Elt.getOpcode() == ISD::UNDEF)
5085 continue;
5086
5087 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5088 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5089 return SDValue();
5090 LastLoadedElt = i;
5091 }
Nate Begeman1449f292010-03-24 22:19:06 +00005092
5093 // If we have found an entire vector of loads and undefs, then return a large
5094 // load of the entire vector width starting at the base pointer. If we found
5095 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005096 if (LastLoadedElt == NumElems - 1) {
5097 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005098 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005099 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005100 LDBase->isVolatile(), LDBase->isNonTemporal(),
5101 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005102 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005103 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005104 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005105 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005106 }
5107 if (NumElems == 4 && LastLoadedElt == 1 &&
5108 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005109 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5110 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005111 SDValue ResNode =
5112 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5113 LDBase->getPointerInfo(),
5114 LDBase->getAlignment(),
5115 false/*isVolatile*/, true/*ReadMem*/,
5116 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005117
5118 // Make sure the newly-created LOAD is in the same position as LDBase in
5119 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5120 // update uses of LDBase's output chain to use the TokenFactor.
5121 if (LDBase->hasAnyUseOfValue(1)) {
5122 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5123 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5124 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5125 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5126 SDValue(ResNode.getNode(), 1));
5127 }
5128
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005129 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005130 }
5131 return SDValue();
5132}
5133
Nadav Rotem9d68b062012-04-08 12:54:54 +00005134/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5135/// to generate a splat value for the following cases:
5136/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005137/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005138/// a scalar load, or a constant.
5139/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005140/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005141SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005142X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005143 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005144 return SDValue();
5145
Craig Topper45e1c752013-01-20 00:38:18 +00005146 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005147 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005148
Craig Topper5da8a802012-05-04 05:49:51 +00005149 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5150 "Unsupported vector type for broadcast.");
5151
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005152 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005153 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154
Nadav Rotem9d68b062012-04-08 12:54:54 +00005155 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005156 default:
5157 // Unknown pattern found.
5158 return SDValue();
5159
5160 case ISD::BUILD_VECTOR: {
5161 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005162 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005163 return SDValue();
5164
Nadav Rotem9d68b062012-04-08 12:54:54 +00005165 Ld = Op.getOperand(0);
5166 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5167 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005168
5169 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005170 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005171 // Constants may have multiple users.
5172 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005173 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005174 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005175 }
5176
5177 case ISD::VECTOR_SHUFFLE: {
5178 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5179
5180 // Shuffles must have a splat mask where the first element is
5181 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005182 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005183 return SDValue();
5184
5185 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005186 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005187 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5188
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005189 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005190 return SDValue();
5191
5192 // Use the register form of the broadcast instruction available on AVX2.
5193 if (VT.is256BitVector())
5194 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5195 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5196 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005197
5198 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005199 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005200 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005201
5202 // The scalar_to_vector node and the suspected
5203 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005204 // Constants may have multiple users.
5205 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005206 return SDValue();
5207 break;
5208 }
5209 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005210
Craig Topper7a9a28b2012-08-12 02:23:29 +00005211 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005212
5213 // Handle the broadcasting a single constant scalar from the constant pool
5214 // into a vector. On Sandybridge it is still better to load a constant vector
5215 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005216 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005217 EVT CVT = Ld.getValueType();
5218 assert(!CVT.isVector() && "Must not broadcast a vector type");
5219 unsigned ScalarSize = CVT.getSizeInBits();
5220
Craig Topper5da8a802012-05-04 05:49:51 +00005221 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005222 const Constant *C = 0;
5223 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5224 C = CI->getConstantIntValue();
5225 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5226 C = CF->getConstantFPValue();
5227
5228 assert(C && "Invalid constant type");
5229
Nadav Rotem154819d2012-04-09 07:45:58 +00005230 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005231 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005232 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005233 MachinePointerInfo::getConstantPool(),
5234 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005235
Nadav Rotem9d68b062012-04-08 12:54:54 +00005236 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5237 }
5238 }
5239
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005240 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005241 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5242
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005243 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005244 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005245 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5246 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5247
5248 // The scalar source must be a normal load.
5249 if (!IsLoad)
5250 return SDValue();
5251
Craig Topper5da8a802012-05-04 05:49:51 +00005252 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005253 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005254
Craig Toppera9376332012-01-10 08:23:59 +00005255 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005256 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005257 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005258 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005259 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005260 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005261
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005262 // Unsupported broadcast.
5263 return SDValue();
5264}
5265
Evan Chengc3630942009-12-09 21:00:30 +00005266SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005267X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5268 EVT VT = Op.getValueType();
5269
5270 // Skip if insert_vec_elt is not supported.
5271 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5272 return SDValue();
5273
5274 DebugLoc DL = Op.getDebugLoc();
5275 unsigned NumElems = Op.getNumOperands();
5276
5277 SDValue VecIn1;
5278 SDValue VecIn2;
5279 SmallVector<unsigned, 4> InsertIndices;
5280 SmallVector<int, 8> Mask(NumElems, -1);
5281
5282 for (unsigned i = 0; i != NumElems; ++i) {
5283 unsigned Opc = Op.getOperand(i).getOpcode();
5284
5285 if (Opc == ISD::UNDEF)
5286 continue;
5287
5288 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5289 // Quit if more than 1 elements need inserting.
5290 if (InsertIndices.size() > 1)
5291 return SDValue();
5292
5293 InsertIndices.push_back(i);
5294 continue;
5295 }
5296
5297 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5298 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5299
5300 // Quit if extracted from vector of different type.
5301 if (ExtractedFromVec.getValueType() != VT)
5302 return SDValue();
5303
5304 // Quit if non-constant index.
5305 if (!isa<ConstantSDNode>(ExtIdx))
5306 return SDValue();
5307
5308 if (VecIn1.getNode() == 0)
5309 VecIn1 = ExtractedFromVec;
5310 else if (VecIn1 != ExtractedFromVec) {
5311 if (VecIn2.getNode() == 0)
5312 VecIn2 = ExtractedFromVec;
5313 else if (VecIn2 != ExtractedFromVec)
5314 // Quit if more than 2 vectors to shuffle
5315 return SDValue();
5316 }
5317
5318 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5319
5320 if (ExtractedFromVec == VecIn1)
5321 Mask[i] = Idx;
5322 else if (ExtractedFromVec == VecIn2)
5323 Mask[i] = Idx + NumElems;
5324 }
5325
5326 if (VecIn1.getNode() == 0)
5327 return SDValue();
5328
5329 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5330 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5331 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5332 unsigned Idx = InsertIndices[i];
5333 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5334 DAG.getIntPtrConstant(Idx));
5335 }
5336
5337 return NV;
5338}
5339
5340SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005341X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005342 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005343
Craig Topper45e1c752013-01-20 00:38:18 +00005344 MVT VT = Op.getValueType().getSimpleVT();
5345 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005346 unsigned NumElems = Op.getNumOperands();
5347
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005348 // Vectors containing all zeros can be matched by pxor and xorps later
5349 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5350 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5351 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005352 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005353 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005354
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005355 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005356 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005357
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005358 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005359 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5360 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005361 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005362 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005363 return Op;
5364
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005365 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005366 }
5367
Nadav Rotem154819d2012-04-09 07:45:58 +00005368 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005369 if (Broadcast.getNode())
5370 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005371
Owen Andersone50ed302009-08-10 22:56:29 +00005372 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373
Evan Cheng0db9fe62006-04-25 20:13:52 +00005374 unsigned NumZero = 0;
5375 unsigned NumNonZero = 0;
5376 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005377 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005378 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005380 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005381 if (Elt.getOpcode() == ISD::UNDEF)
5382 continue;
5383 Values.insert(Elt);
5384 if (Elt.getOpcode() != ISD::Constant &&
5385 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005386 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005387 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005388 NumZero++;
5389 else {
5390 NonZeros |= (1 << i);
5391 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005392 }
5393 }
5394
Chris Lattner97a2a562010-08-26 05:24:29 +00005395 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5396 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005397 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005398
Chris Lattner67f453a2008-03-09 05:42:06 +00005399 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005400 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005401 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005402 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005403
Chris Lattner62098042008-03-09 01:05:04 +00005404 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5405 // the value are obviously zero, truncate the value to i32 and do the
5406 // insertion that way. Only do this if the value is non-constant or if the
5407 // value is a constant being inserted into element 0. It is cheaper to do
5408 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005409 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005410 (!IsAllConstants || Idx == 0)) {
5411 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005412 // Handle SSE only.
5413 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5414 EVT VecVT = MVT::v4i32;
5415 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005416
Chris Lattner62098042008-03-09 01:05:04 +00005417 // Truncate the value (which may itself be a constant) to i32, and
5418 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005420 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005421 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005422
Chris Lattner62098042008-03-09 01:05:04 +00005423 // Now we have our 32-bit value zero extended in the low element of
5424 // a vector. If Idx != 0, swizzle it into place.
5425 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005426 SmallVector<int, 4> Mask;
5427 Mask.push_back(Idx);
5428 for (unsigned i = 1; i != VecElts; ++i)
5429 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005430 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005431 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005432 }
Craig Topper07a27622012-01-22 03:07:48 +00005433 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005434 }
5435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005436
Chris Lattner19f79692008-03-08 22:59:52 +00005437 // If we have a constant or non-constant insertion into the low element of
5438 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5439 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005440 // depending on what the source datatype is.
5441 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005442 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005443 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005444
5445 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005446 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005447 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005448 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005449 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5450 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005451 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005452 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005453 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5454 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005455 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005456 }
5457
5458 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005459 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005460 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005461 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005462 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005463 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005464 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005465 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005466 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005467 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005468 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005469 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005470 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005471
5472 // Is it a vector logical left shift?
5473 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005474 X86::isZeroNode(Op.getOperand(0)) &&
5475 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005476 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005477 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005478 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005479 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005480 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005481 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005482
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005483 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005484 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485
Chris Lattner19f79692008-03-08 22:59:52 +00005486 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5487 // is a non-constant being inserted into an element other than the low one,
5488 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5489 // movd/movss) to move this into the low element, then shuffle it into
5490 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005491 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005492 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005493
Evan Cheng0db9fe62006-04-25 20:13:52 +00005494 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005495 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005496 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005497 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005498 MaskVec.push_back(i == Idx ? 0 : 1);
5499 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500 }
5501 }
5502
Chris Lattner67f453a2008-03-09 05:42:06 +00005503 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005504 if (Values.size() == 1) {
5505 if (EVTBits == 32) {
5506 // Instead of a shuffle like this:
5507 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5508 // Check if it's possible to issue this instead.
5509 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5510 unsigned Idx = CountTrailingZeros_32(NonZeros);
5511 SDValue Item = Op.getOperand(Idx);
5512 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5513 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5514 }
Dan Gohman475871a2008-07-27 21:46:04 +00005515 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005517
Dan Gohmana3941172007-07-24 22:55:08 +00005518 // A vector full of immediates; various special cases are already
5519 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005520 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005521 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005522
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005523 // For AVX-length vectors, build the individual 128-bit pieces and use
5524 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005525 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005526 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005527 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005528 V.push_back(Op.getOperand(i));
5529
5530 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5531
5532 // Build both the lower and upper subvector.
5533 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5534 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5535 NumElems/2);
5536
5537 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005538 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005539 }
5540
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005541 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005542 if (EVTBits == 64) {
5543 if (NumNonZero == 1) {
5544 // One half is zero or undef.
5545 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005546 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005547 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005548 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005549 }
Dan Gohman475871a2008-07-27 21:46:04 +00005550 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005551 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005552
5553 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005554 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005555 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005556 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005557 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005558 }
5559
Bill Wendling826f36f2007-03-28 00:57:11 +00005560 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005561 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005562 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005563 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005564 }
5565
5566 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005567 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005568 if (NumElems == 4 && NumZero > 0) {
5569 for (unsigned i = 0; i < 4; ++i) {
5570 bool isZero = !(NonZeros & (1 << i));
5571 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005572 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005573 else
Dale Johannesenace16102009-02-03 19:33:06 +00005574 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005575 }
5576
5577 for (unsigned i = 0; i < 2; ++i) {
5578 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5579 default: break;
5580 case 0:
5581 V[i] = V[i*2]; // Must be a zero vector.
5582 break;
5583 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005584 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005585 break;
5586 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005587 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005588 break;
5589 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005590 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005591 break;
5592 }
5593 }
5594
Benjamin Kramer9c683542012-01-30 15:16:21 +00005595 bool Reverse1 = (NonZeros & 0x3) == 2;
5596 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5597 int MaskVec[] = {
5598 Reverse1 ? 1 : 0,
5599 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005600 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5601 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005602 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005603 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005604 }
5605
Craig Topper7a9a28b2012-08-12 02:23:29 +00005606 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005607 // Check for a build vector of consecutive loads.
5608 for (unsigned i = 0; i < NumElems; ++i)
5609 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005610
Nate Begemanfdea31a2010-03-24 20:49:50 +00005611 // Check for elements which are consecutive loads.
5612 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5613 if (LD.getNode())
5614 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005615
Michael Liaofacace82012-10-19 17:15:18 +00005616 // Check for a build vector from mostly shuffle plus few inserting.
5617 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5618 if (Sh.getNode())
5619 return Sh;
5620
Michael J. Spencerec38de22010-10-10 22:04:20 +00005621 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005622 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005623 SDValue Result;
5624 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5625 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5626 else
5627 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005628
Chris Lattner24faf612010-08-28 17:59:08 +00005629 for (unsigned i = 1; i < NumElems; ++i) {
5630 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5631 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005632 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005633 }
5634 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005635 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005636
Chris Lattner6e80e442010-08-28 17:15:43 +00005637 // Otherwise, expand into a number of unpckl*, start by extending each of
5638 // our (non-undef) elements to the full vector width with the element in the
5639 // bottom slot of the vector (which generates no code for SSE).
5640 for (unsigned i = 0; i < NumElems; ++i) {
5641 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5642 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5643 else
5644 V[i] = DAG.getUNDEF(VT);
5645 }
5646
5647 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005648 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5649 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5650 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005651 unsigned EltStride = NumElems >> 1;
5652 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005653 for (unsigned i = 0; i < EltStride; ++i) {
5654 // If V[i+EltStride] is undef and this is the first round of mixing,
5655 // then it is safe to just drop this shuffle: V[i] is already in the
5656 // right place, the one element (since it's the first round) being
5657 // inserted as undef can be dropped. This isn't safe for successive
5658 // rounds because they will permute elements within both vectors.
5659 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5660 EltStride == NumElems/2)
5661 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005662
Chris Lattner6e80e442010-08-28 17:15:43 +00005663 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005664 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005665 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005666 }
5667 return V[0];
5668 }
Dan Gohman475871a2008-07-27 21:46:04 +00005669 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005670}
5671
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005672// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5673// to create 256-bit vectors from two other 128-bit ones.
5674static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5675 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005676 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005677
Craig Topper7a9a28b2012-08-12 02:23:29 +00005678 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005679
5680 SDValue V1 = Op.getOperand(0);
5681 SDValue V2 = Op.getOperand(1);
5682 unsigned NumElems = ResVT.getVectorNumElements();
5683
Craig Topper4c7972d2012-04-22 18:15:59 +00005684 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005685}
5686
Craig Topper55b24052012-09-11 06:15:32 +00005687static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005688 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005689
5690 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5691 // from two other 128-bit ones.
5692 return LowerAVXCONCAT_VECTORS(Op, DAG);
5693}
5694
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005695// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005696static SDValue
5697LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5698 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005699 SDValue V1 = SVOp->getOperand(0);
5700 SDValue V2 = SVOp->getOperand(1);
5701 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005702 MVT VT = SVOp->getValueType(0).getSimpleVT();
5703 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005704 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005705
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005706 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5707 return SDValue();
5708 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005709 return SDValue();
5710
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005711 // Check the mask for BLEND and build the value.
5712 unsigned MaskValue = 0;
5713 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005714 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005715 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005716
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005717 // Blend for v16i16 should be symetric for the both lanes.
5718 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005719
Craig Topper9b33ef72013-01-21 06:57:59 +00005720 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005721 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005722 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005723
Craig Topper04f74a12013-01-21 07:25:16 +00005724 if ((EltIdx < 0 || EltIdx == (int)i) &&
5725 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005726 continue;
5727
Craig Topper9b33ef72013-01-21 06:57:59 +00005728 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005729 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005730 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5731 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005732 else
Craig Topper1842ba02012-04-23 06:38:28 +00005733 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005734 }
5735
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005736 // Convert i32 vectors to floating point if it is not AVX2.
5737 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005738 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005739 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005740 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5741 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005742 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5743 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5744 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005745
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005746 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5747 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005748 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005749}
5750
Nate Begemanb9a47b82009-02-23 08:49:38 +00005751// v8i16 shuffles - Prefer shuffles in the following order:
5752// 1. [all] pshuflw, pshufhw, optional move
5753// 2. [ssse3] 1 x pshufb
5754// 3. [ssse3] 2 x pshufb + 1 x por
5755// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005756static SDValue
5757LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5758 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005759 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005760 SDValue V1 = SVOp->getOperand(0);
5761 SDValue V2 = SVOp->getOperand(1);
5762 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005764
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 // Determine if more than 1 of the words in each of the low and high quadwords
5766 // of the result come from the same quadword of one of the two inputs. Undef
5767 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005768 unsigned LoQuad[] = { 0, 0, 0, 0 };
5769 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005770 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005771 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005772 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005773 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005774 MaskVals.push_back(EltIdx);
5775 if (EltIdx < 0) {
5776 ++Quad[0];
5777 ++Quad[1];
5778 ++Quad[2];
5779 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005780 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 }
5782 ++Quad[EltIdx / 4];
5783 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005784 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005785
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005787 unsigned MaxQuad = 1;
5788 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 if (LoQuad[i] > MaxQuad) {
5790 BestLoQuad = i;
5791 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005792 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005793 }
5794
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005796 MaxQuad = 1;
5797 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 if (HiQuad[i] > MaxQuad) {
5799 BestHiQuad = i;
5800 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005801 }
5802 }
5803
Nate Begemanb9a47b82009-02-23 08:49:38 +00005804 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005805 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 // single pshufb instruction is necessary. If There are more than 2 input
5807 // quads, disable the next transformation since it does not help SSSE3.
5808 bool V1Used = InputQuads[0] || InputQuads[1];
5809 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005810 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005811 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005812 BestLoQuad = InputQuads[0] ? 0 : 1;
5813 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 }
5815 if (InputQuads.count() > 2) {
5816 BestLoQuad = -1;
5817 BestHiQuad = -1;
5818 }
5819 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005820
Nate Begemanb9a47b82009-02-23 08:49:38 +00005821 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5822 // the shuffle mask. If a quad is scored as -1, that means that it contains
5823 // words from all 4 input quadwords.
5824 SDValue NewV;
5825 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005826 int MaskV[] = {
5827 BestLoQuad < 0 ? 0 : BestLoQuad,
5828 BestHiQuad < 0 ? 1 : BestHiQuad
5829 };
Eric Christopherfd179292009-08-27 18:07:15 +00005830 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005831 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5832 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5833 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005834
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5836 // source words for the shuffle, to aid later transformations.
5837 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005838 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005841 if (idx != (int)i)
5842 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005843 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005844 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 AllWordsInNewV = false;
5846 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005847 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005848
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5850 if (AllWordsInNewV) {
5851 for (int i = 0; i != 8; ++i) {
5852 int idx = MaskVals[i];
5853 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005854 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005855 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 if ((idx != i) && idx < 4)
5857 pshufhw = false;
5858 if ((idx != i) && idx > 3)
5859 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005860 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 V1 = NewV;
5862 V2Used = false;
5863 BestLoQuad = 0;
5864 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005865 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005866
Nate Begemanb9a47b82009-02-23 08:49:38 +00005867 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5868 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005869 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005870 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5871 unsigned TargetMask = 0;
5872 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005874 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5875 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5876 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005877 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005878 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005879 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005880 }
Eric Christopherfd179292009-08-27 18:07:15 +00005881
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005882 // Promote splats to a larger type which usually leads to more efficient code.
5883 // FIXME: Is this true if pshufb is available?
5884 if (SVOp->isSplat())
5885 return PromoteSplat(SVOp, DAG);
5886
Nate Begemanb9a47b82009-02-23 08:49:38 +00005887 // If we have SSSE3, and all words of the result are from 1 input vector,
5888 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5889 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005890 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005892
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005894 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 // mask, and elements that come from V1 in the V2 mask, so that the two
5896 // results can be OR'd together.
5897 bool TwoInputs = V1Used && V2Used;
5898 for (unsigned i = 0; i != 8; ++i) {
5899 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005900 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5901 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005902 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005903 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005905 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005906 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005907 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005908 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005909 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005911
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 // Calculate the shuffle mask for the second input, shuffle it, and
5913 // OR it with the first shuffled input.
5914 pshufbMask.clear();
5915 for (unsigned i = 0; i != 8; ++i) {
5916 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005917 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5918 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5919 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5920 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005922 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005923 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005924 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005925 MVT::v16i8, &pshufbMask[0], 16));
5926 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005927 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005928 }
5929
5930 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5931 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005932 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005934 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005935 for (int i = 0; i != 4; ++i) {
5936 int idx = MaskVals[i];
5937 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005938 InOrder.set(i);
5939 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005940 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005941 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005942 }
5943 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005944 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005945 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005946
Craig Topperdd637ae2012-02-19 05:41:45 +00005947 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5948 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005949 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005950 NewV.getOperand(0),
5951 getShufflePSHUFLWImmediate(SVOp), DAG);
5952 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 }
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5956 // and update MaskVals with the new element order.
5957 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005958 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005959 for (unsigned i = 4; i != 8; ++i) {
5960 int idx = MaskVals[i];
5961 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005962 InOrder.set(i);
5963 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005964 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 }
5967 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005968 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005969 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005970
Craig Topperdd637ae2012-02-19 05:41:45 +00005971 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5972 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005973 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005974 NewV.getOperand(0),
5975 getShufflePSHUFHWImmediate(SVOp), DAG);
5976 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005977 }
Eric Christopherfd179292009-08-27 18:07:15 +00005978
Nate Begemanb9a47b82009-02-23 08:49:38 +00005979 // In case BestHi & BestLo were both -1, which means each quadword has a word
5980 // from each of the four input quadwords, calculate the InOrder bitvector now
5981 // before falling through to the insert/extract cleanup.
5982 if (BestLoQuad == -1 && BestHiQuad == -1) {
5983 NewV = V1;
5984 for (int i = 0; i != 8; ++i)
5985 if (MaskVals[i] < 0 || MaskVals[i] == i)
5986 InOrder.set(i);
5987 }
Eric Christopherfd179292009-08-27 18:07:15 +00005988
Nate Begemanb9a47b82009-02-23 08:49:38 +00005989 // The other elements are put in the right place using pextrw and pinsrw.
5990 for (unsigned i = 0; i != 8; ++i) {
5991 if (InOrder[i])
5992 continue;
5993 int EltIdx = MaskVals[i];
5994 if (EltIdx < 0)
5995 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005996 SDValue ExtOp = (EltIdx < 8) ?
5997 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5998 DAG.getIntPtrConstant(EltIdx)) :
5999 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006002 DAG.getIntPtrConstant(i));
6003 }
6004 return NewV;
6005}
6006
6007// v16i8 shuffles - Prefer shuffles in the following order:
6008// 1. [ssse3] 1 x pshufb
6009// 2. [ssse3] 2 x pshufb + 1 x por
6010// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6011static
Nate Begeman9008ca62009-04-27 18:41:29 +00006012SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006013 SelectionDAG &DAG,
6014 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006015 SDValue V1 = SVOp->getOperand(0);
6016 SDValue V2 = SVOp->getOperand(1);
6017 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006018 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006019
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006020 // Promote splats to a larger type which usually leads to more efficient code.
6021 // FIXME: Is this true if pshufb is available?
6022 if (SVOp->isSplat())
6023 return PromoteSplat(SVOp, DAG);
6024
Nate Begemanb9a47b82009-02-23 08:49:38 +00006025 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006026 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006027 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006028
Nate Begemanb9a47b82009-02-23 08:49:38 +00006029 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006030 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006032
Nate Begemanb9a47b82009-02-23 08:49:38 +00006033 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006034 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006035 //
6036 // Otherwise, we have elements from both input vectors, and must zero out
6037 // elements that come from V2 in the first mask, and V1 in the second mask
6038 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006039 for (unsigned i = 0; i != 16; ++i) {
6040 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006041 if (EltIdx < 0 || EltIdx >= 16)
6042 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006043 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006044 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006045 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006046 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006047 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006048
6049 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6050 // the 2nd operand if it's undefined or zero.
6051 if (V2.getOpcode() == ISD::UNDEF ||
6052 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006053 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006054
Nate Begemanb9a47b82009-02-23 08:49:38 +00006055 // Calculate the shuffle mask for the second input, shuffle it, and
6056 // OR it with the first shuffled input.
6057 pshufbMask.clear();
6058 for (unsigned i = 0; i != 16; ++i) {
6059 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006060 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006061 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006062 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006063 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006064 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006065 MVT::v16i8, &pshufbMask[0], 16));
6066 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006067 }
Eric Christopherfd179292009-08-27 18:07:15 +00006068
Nate Begemanb9a47b82009-02-23 08:49:38 +00006069 // No SSSE3 - Calculate in place words and then fix all out of place words
6070 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6071 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006072 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6073 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006074 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006075 for (int i = 0; i != 8; ++i) {
6076 int Elt0 = MaskVals[i*2];
6077 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006078
Nate Begemanb9a47b82009-02-23 08:49:38 +00006079 // This word of the result is all undef, skip it.
6080 if (Elt0 < 0 && Elt1 < 0)
6081 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006082
Nate Begemanb9a47b82009-02-23 08:49:38 +00006083 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006084 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006085 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006086
Nate Begemanb9a47b82009-02-23 08:49:38 +00006087 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6088 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6089 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006090
6091 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6092 // using a single extract together, load it and store it.
6093 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006095 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006096 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006097 DAG.getIntPtrConstant(i));
6098 continue;
6099 }
6100
Nate Begemanb9a47b82009-02-23 08:49:38 +00006101 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006102 // source byte is not also odd, shift the extracted word left 8 bits
6103 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006104 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006105 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006106 DAG.getIntPtrConstant(Elt1 / 2));
6107 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006108 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006109 DAG.getConstant(8,
6110 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006111 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006112 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6113 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006114 }
6115 // If Elt0 is defined, extract it from the appropriate source. If the
6116 // source byte is not also even, shift the extracted word right 8 bits. If
6117 // Elt1 was also defined, OR the extracted values together before
6118 // inserting them in the result.
6119 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006120 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006121 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6122 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006123 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006124 DAG.getConstant(8,
6125 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006126 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006127 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6128 DAG.getConstant(0x00FF, MVT::i16));
6129 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006130 : InsElt0;
6131 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006132 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006133 DAG.getIntPtrConstant(i));
6134 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006135 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006136}
6137
Elena Demikhovsky41789462012-09-06 12:42:01 +00006138// v32i8 shuffles - Translate to VPSHUFB if possible.
6139static
6140SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006141 const X86Subtarget *Subtarget,
6142 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006143 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006144 SDValue V1 = SVOp->getOperand(0);
6145 SDValue V2 = SVOp->getOperand(1);
6146 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006147 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006148
6149 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006150 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6151 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006152
Michael Liao471b9172012-10-03 23:43:52 +00006153 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006154 // (1) one of input vector is undefined or zeroinitializer.
6155 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6156 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006157 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006158 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006159 return SDValue();
6160
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006161 if (V1IsAllZero && !V2IsAllZero) {
6162 CommuteVectorShuffleMask(MaskVals, 32);
6163 V1 = V2;
6164 }
6165 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006166 for (unsigned i = 0; i != 32; i++) {
6167 int EltIdx = MaskVals[i];
6168 if (EltIdx < 0 || EltIdx >= 32)
6169 EltIdx = 0x80;
6170 else {
6171 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6172 // Cross lane is not allowed.
6173 return SDValue();
6174 EltIdx &= 0xf;
6175 }
6176 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6177 }
6178 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6179 DAG.getNode(ISD::BUILD_VECTOR, dl,
6180 MVT::v32i8, &pshufbMask[0], 32));
6181}
6182
Evan Cheng7a831ce2007-12-15 03:00:47 +00006183/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006184/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006185/// done when every pair / quad of shuffle mask elements point to elements in
6186/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006187/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006188static
Nate Begeman9008ca62009-04-27 18:41:29 +00006189SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006190 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006191 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006192 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006193 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006194 MVT NewVT;
6195 unsigned Scale;
6196 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006197 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006198 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6199 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6200 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6201 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6202 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6203 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006204 }
6205
Nate Begeman9008ca62009-04-27 18:41:29 +00006206 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006207 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006208 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006209 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006210 int EltIdx = SVOp->getMaskElt(i+j);
6211 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006212 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006213 if (StartIdx < 0)
6214 StartIdx = (EltIdx / Scale);
6215 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006216 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006217 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006218 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006219 }
6220
Craig Topper11ac1f82012-05-04 04:08:44 +00006221 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6222 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006223 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006224}
6225
Evan Chengd880b972008-05-09 21:53:03 +00006226/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006227///
Craig Topperf84b7502013-01-20 00:50:58 +00006228static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006229 SDValue SrcOp, SelectionDAG &DAG,
6230 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006231 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006232 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006233 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006234 LD = dyn_cast<LoadSDNode>(SrcOp);
6235 if (!LD) {
6236 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6237 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006238 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006239 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006240 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006241 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006242 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006243 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006244 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006245 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006246 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6247 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6248 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006249 SrcOp.getOperand(0)
6250 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006251 }
6252 }
6253 }
6254
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006255 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006256 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006257 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006258 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006259}
6260
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006261/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6262/// which could not be matched by any known target speficic shuffle
6263static SDValue
6264LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006265
6266 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6267 if (NewOp.getNode())
6268 return NewOp;
6269
Craig Topper657a99c2013-01-19 23:36:09 +00006270 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006271
Craig Topper8f35c132012-01-20 09:29:03 +00006272 unsigned NumElems = VT.getVectorNumElements();
6273 unsigned NumLaneElems = NumElems / 2;
6274
Craig Topper8f35c132012-01-20 09:29:03 +00006275 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006276 MVT EltVT = VT.getVectorElementType();
6277 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006278 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006279
Craig Topper9a2b6e12012-04-06 07:45:23 +00006280 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006281 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006282 // Build a shuffle mask for the output, discovering on the fly which
6283 // input vectors to use as shuffle operands (recorded in InputUsed).
6284 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006285 // out with UseBuildVector set.
6286 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006287 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006288 unsigned LaneStart = l * NumLaneElems;
6289 for (unsigned i = 0; i != NumLaneElems; ++i) {
6290 // The mask element. This indexes into the input.
6291 int Idx = SVOp->getMaskElt(i+LaneStart);
6292 if (Idx < 0) {
6293 // the mask element does not index into any input vector.
6294 Mask.push_back(-1);
6295 continue;
6296 }
Craig Topper8f35c132012-01-20 09:29:03 +00006297
Craig Topper9a2b6e12012-04-06 07:45:23 +00006298 // The input vector this mask element indexes into.
6299 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006300
Craig Topper9a2b6e12012-04-06 07:45:23 +00006301 // Turn the index into an offset from the start of the input vector.
6302 Idx -= Input * NumLaneElems;
6303
6304 // Find or create a shuffle vector operand to hold this input.
6305 unsigned OpNo;
6306 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6307 if (InputUsed[OpNo] == Input)
6308 // This input vector is already an operand.
6309 break;
6310 if (InputUsed[OpNo] < 0) {
6311 // Create a new operand for this input vector.
6312 InputUsed[OpNo] = Input;
6313 break;
6314 }
6315 }
6316
6317 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006318 // More than two input vectors used! Give up on trying to create a
6319 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6320 UseBuildVector = true;
6321 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006322 }
6323
6324 // Add the mask index for the new shuffle vector.
6325 Mask.push_back(Idx + OpNo * NumLaneElems);
6326 }
6327
Craig Topper8ae97ba2012-05-21 06:40:16 +00006328 if (UseBuildVector) {
6329 SmallVector<SDValue, 16> SVOps;
6330 for (unsigned i = 0; i != NumLaneElems; ++i) {
6331 // The mask element. This indexes into the input.
6332 int Idx = SVOp->getMaskElt(i+LaneStart);
6333 if (Idx < 0) {
6334 SVOps.push_back(DAG.getUNDEF(EltVT));
6335 continue;
6336 }
6337
6338 // The input vector this mask element indexes into.
6339 int Input = Idx / NumElems;
6340
6341 // Turn the index into an offset from the start of the input vector.
6342 Idx -= Input * NumElems;
6343
6344 // Extract the vector element by hand.
6345 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6346 SVOp->getOperand(Input),
6347 DAG.getIntPtrConstant(Idx)));
6348 }
6349
6350 // Construct the output using a BUILD_VECTOR.
6351 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6352 SVOps.size());
6353 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006354 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006355 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006356 } else {
6357 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006358 (InputUsed[0] % 2) * NumLaneElems,
6359 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006360 // If only one input was used, use an undefined vector for the other.
6361 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6362 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006363 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006364 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006365 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006366 }
6367
6368 Mask.clear();
6369 }
Craig Topper8f35c132012-01-20 09:29:03 +00006370
6371 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006372 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006373}
6374
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006375/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6376/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006377static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006378LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006379 SDValue V1 = SVOp->getOperand(0);
6380 SDValue V2 = SVOp->getOperand(1);
6381 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006382 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006383
Craig Topper7a9a28b2012-08-12 02:23:29 +00006384 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006385
Benjamin Kramer9c683542012-01-30 15:16:21 +00006386 std::pair<int, int> Locs[4];
6387 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006388 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006389
Evan Chengace3c172008-07-22 21:13:36 +00006390 unsigned NumHi = 0;
6391 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006392 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006393 int Idx = PermMask[i];
6394 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006395 Locs[i] = std::make_pair(-1, -1);
6396 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006397 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6398 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006399 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006400 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006401 NumLo++;
6402 } else {
6403 Locs[i] = std::make_pair(1, NumHi);
6404 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006405 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006406 NumHi++;
6407 }
6408 }
6409 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006410
Evan Chengace3c172008-07-22 21:13:36 +00006411 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006412 // If no more than two elements come from either vector. This can be
6413 // implemented with two shuffles. First shuffle gather the elements.
6414 // The second shuffle, which takes the first shuffle as both of its
6415 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006417
Benjamin Kramer9c683542012-01-30 15:16:21 +00006418 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006419
Benjamin Kramer9c683542012-01-30 15:16:21 +00006420 for (unsigned i = 0; i != 4; ++i)
6421 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006422 unsigned Idx = (i < 2) ? 0 : 4;
6423 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006424 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006425 }
Evan Chengace3c172008-07-22 21:13:36 +00006426
Nate Begeman9008ca62009-04-27 18:41:29 +00006427 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006428 }
6429
6430 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006431 // Otherwise, we must have three elements from one vector, call it X, and
6432 // one element from the other, call it Y. First, use a shufps to build an
6433 // intermediate vector with the one element from Y and the element from X
6434 // that will be in the same half in the final destination (the indexes don't
6435 // matter). Then, use a shufps to build the final vector, taking the half
6436 // containing the element from Y from the intermediate, and the other half
6437 // from X.
6438 if (NumHi == 3) {
6439 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006440 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006441 std::swap(V1, V2);
6442 }
6443
6444 // Find the element from V2.
6445 unsigned HiIndex;
6446 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006447 int Val = PermMask[HiIndex];
6448 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006449 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006450 if (Val >= 4)
6451 break;
6452 }
6453
Nate Begeman9008ca62009-04-27 18:41:29 +00006454 Mask1[0] = PermMask[HiIndex];
6455 Mask1[1] = -1;
6456 Mask1[2] = PermMask[HiIndex^1];
6457 Mask1[3] = -1;
6458 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006459
6460 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006461 Mask1[0] = PermMask[0];
6462 Mask1[1] = PermMask[1];
6463 Mask1[2] = HiIndex & 1 ? 6 : 4;
6464 Mask1[3] = HiIndex & 1 ? 4 : 6;
6465 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006466 }
Craig Topper69947b92012-04-23 06:57:04 +00006467
6468 Mask1[0] = HiIndex & 1 ? 2 : 0;
6469 Mask1[1] = HiIndex & 1 ? 0 : 2;
6470 Mask1[2] = PermMask[2];
6471 Mask1[3] = PermMask[3];
6472 if (Mask1[2] >= 0)
6473 Mask1[2] += 4;
6474 if (Mask1[3] >= 0)
6475 Mask1[3] += 4;
6476 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006477 }
6478
6479 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006480 int LoMask[] = { -1, -1, -1, -1 };
6481 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006482
Benjamin Kramer9c683542012-01-30 15:16:21 +00006483 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006484 unsigned MaskIdx = 0;
6485 unsigned LoIdx = 0;
6486 unsigned HiIdx = 2;
6487 for (unsigned i = 0; i != 4; ++i) {
6488 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006489 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006490 MaskIdx = 1;
6491 LoIdx = 0;
6492 HiIdx = 2;
6493 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006494 int Idx = PermMask[i];
6495 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006496 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006497 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006498 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006499 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006500 LoIdx++;
6501 } else {
6502 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006503 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006504 HiIdx++;
6505 }
6506 }
6507
Nate Begeman9008ca62009-04-27 18:41:29 +00006508 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6509 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006510 int MaskOps[] = { -1, -1, -1, -1 };
6511 for (unsigned i = 0; i != 4; ++i)
6512 if (Locs[i].first != -1)
6513 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006514 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006515}
6516
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006517static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006518 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006519 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006520
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006521 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6522 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006523 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6524 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6525 // BUILD_VECTOR (load), undef
6526 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006527
6528 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006529}
6530
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006531static
Evan Cheng835580f2010-10-07 20:50:20 +00006532SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6533 EVT VT = Op.getValueType();
6534
6535 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006536 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6537 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006538 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6539 V1, DAG));
6540}
6541
6542static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006543SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006544 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006545 SDValue V1 = Op.getOperand(0);
6546 SDValue V2 = Op.getOperand(1);
6547 EVT VT = Op.getValueType();
6548
6549 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6550
Craig Topper1accb7e2012-01-10 06:54:16 +00006551 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006552 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6553
Evan Cheng0899f5c2011-08-31 02:05:24 +00006554 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6555 return DAG.getNode(ISD::BITCAST, dl, VT,
6556 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6557 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6558 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006559}
6560
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006561static
6562SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6563 SDValue V1 = Op.getOperand(0);
6564 SDValue V2 = Op.getOperand(1);
6565 EVT VT = Op.getValueType();
6566
6567 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6568 "unsupported shuffle type");
6569
6570 if (V2.getOpcode() == ISD::UNDEF)
6571 V2 = V1;
6572
6573 // v4i32 or v4f32
6574 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6575}
6576
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006577static
Craig Topper1accb7e2012-01-10 06:54:16 +00006578SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006579 SDValue V1 = Op.getOperand(0);
6580 SDValue V2 = Op.getOperand(1);
6581 EVT VT = Op.getValueType();
6582 unsigned NumElems = VT.getVectorNumElements();
6583
6584 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6585 // operand of these instructions is only memory, so check if there's a
6586 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6587 // same masks.
6588 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006589
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006590 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006591 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592 CanFoldLoad = true;
6593
6594 // When V1 is a load, it can be folded later into a store in isel, example:
6595 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6596 // turns into:
6597 // (MOVLPSmr addr:$src1, VR128:$src2)
6598 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006599 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006600 CanFoldLoad = true;
6601
Dan Gohman65fd6562011-11-03 21:49:52 +00006602 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006603 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006604 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006605 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6606
6607 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006608 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006609 if (SVOp->getMaskElt(1) != -1)
6610 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006611 }
6612
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006613 // movl and movlp will both match v2i64, but v2i64 is never matched by
6614 // movl earlier because we make it strict to avoid messing with the movlp load
6615 // folding logic (see the code above getMOVLP call). Match it here then,
6616 // this is horrible, but will stay like this until we move all shuffle
6617 // matching to x86 specific nodes. Note that for the 1st condition all
6618 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006619 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006620 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6621 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006622 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006623 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006624 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006625 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006626
6627 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6628
6629 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006630 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006631 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006632}
6633
Michael Liaod9d09602012-10-23 17:34:00 +00006634// Reduce a vector shuffle to zext.
6635SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006636X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006637 // PMOVZX is only available from SSE41.
6638 if (!Subtarget->hasSSE41())
6639 return SDValue();
6640
6641 EVT VT = Op.getValueType();
6642
6643 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006644 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006645 return SDValue();
6646
6647 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6648 DebugLoc DL = Op.getDebugLoc();
6649 SDValue V1 = Op.getOperand(0);
6650 SDValue V2 = Op.getOperand(1);
6651 unsigned NumElems = VT.getVectorNumElements();
6652
6653 // Extending is an unary operation and the element type of the source vector
6654 // won't be equal to or larger than i64.
6655 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6656 VT.getVectorElementType() == MVT::i64)
6657 return SDValue();
6658
6659 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6660 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006661 while ((1U << Shift) < NumElems) {
6662 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006663 break;
6664 Shift += 1;
6665 // The maximal ratio is 8, i.e. from i8 to i64.
6666 if (Shift > 3)
6667 return SDValue();
6668 }
6669
6670 // Check the shuffle mask.
6671 unsigned Mask = (1U << Shift) - 1;
6672 for (unsigned i = 0; i != NumElems; ++i) {
6673 int EltIdx = SVOp->getMaskElt(i);
6674 if ((i & Mask) != 0 && EltIdx != -1)
6675 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006676 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006677 return SDValue();
6678 }
6679
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006680 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006681 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006682 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6683 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006684
6685 if (!isTypeLegal(NVT))
6686 return SDValue();
6687
6688 // Simplify the operand as it's prepared to be fed into shuffle.
6689 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6690 if (V1.getOpcode() == ISD::BITCAST &&
6691 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6692 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6693 V1.getOperand(0)
6694 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6695 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6696 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006697 ConstantSDNode *CIdx =
6698 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006699 // If it's foldable, i.e. normal load with single use, we will let code
6700 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006701 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006702 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6703 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6704 // The "ext_vec_elt" node is wider than the result node.
6705 // In this case we should extract subvector from V.
6706 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6707 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6708 EVT FullVT = V.getValueType();
6709 EVT SubVecVT = EVT::getVectorVT(*Context,
6710 FullVT.getVectorElementType(),
6711 FullVT.getVectorNumElements()/Ratio);
6712 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
6713 DAG.getIntPtrConstant(0));
6714 }
Michael Liaod9d09602012-10-23 17:34:00 +00006715 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006716 }
Michael Liaod9d09602012-10-23 17:34:00 +00006717 }
6718
6719 return DAG.getNode(ISD::BITCAST, DL, VT,
6720 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6721}
6722
Nadav Rotem154819d2012-04-09 07:45:58 +00006723SDValue
6724X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006725 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006726 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006727 DebugLoc dl = Op.getDebugLoc();
6728 SDValue V1 = Op.getOperand(0);
6729 SDValue V2 = Op.getOperand(1);
6730
6731 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006732 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006733
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006734 // Handle splat operations
6735 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006736 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006737 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006738 if (Broadcast.getNode())
6739 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006740 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006741
Michael Liaod9d09602012-10-23 17:34:00 +00006742 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006743 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006744 if (NewOp.getNode())
6745 return NewOp;
6746
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006747 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6748 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006749 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6750 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006751 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006752 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006753 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006754 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006755 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006756 // FIXME: Figure out a cleaner way to do this.
6757 // Try to make use of movq to zero out the top part.
6758 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006759 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006760 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006761 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006762 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6763 NewVT, true, false))
6764 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006765 DAG, Subtarget, dl);
6766 }
6767 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006768 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006769 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006770 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006771 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6772 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6773 DAG, Subtarget, dl);
6774 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006775 }
6776 }
6777 return SDValue();
6778}
6779
Dan Gohman475871a2008-07-27 21:46:04 +00006780SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006781X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006782 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006783 SDValue V1 = Op.getOperand(0);
6784 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006785 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006786 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006787 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006788 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006790 bool V1IsSplat = false;
6791 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006792 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006793 bool HasFp256 = Subtarget->hasFp256();
6794 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006795 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006796 bool OptForSize = MF.getFunction()->getAttributes().
6797 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006798
Craig Topper3426a3e2011-11-14 06:46:21 +00006799 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006800
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006801 if (V1IsUndef && V2IsUndef)
6802 return DAG.getUNDEF(VT);
6803
6804 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006805
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006806 // Vector shuffle lowering takes 3 steps:
6807 //
6808 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6809 // narrowing and commutation of operands should be handled.
6810 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6811 // shuffle nodes.
6812 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6813 // so the shuffle can be broken into other shuffles and the legalizer can
6814 // try the lowering again.
6815 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006816 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006817 // be matched during isel, all of them must be converted to a target specific
6818 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006819
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006820 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6821 // narrowing and commutation of operands should be handled. The actual code
6822 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006823 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006824 if (NewOp.getNode())
6825 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006826
Craig Topper5aaffa82012-02-19 02:53:47 +00006827 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6828
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006829 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6830 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006831 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006832 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006833 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006834 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006835
Craig Topperdd637ae2012-02-19 05:41:45 +00006836 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006837 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006838 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006839
Craig Topperdd637ae2012-02-19 05:41:45 +00006840 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006841 return getMOVHighToLow(Op, dl, DAG);
6842
6843 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006844 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006845 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006846 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006847
Craig Topper5aaffa82012-02-19 02:53:47 +00006848 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006849 // The actual implementation will match the mask in the if above and then
6850 // during isel it can match several different instructions, not only pshufd
6851 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006852 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6853 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006854
Craig Topper5aaffa82012-02-19 02:53:47 +00006855 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006856
Craig Topper1accb7e2012-01-10 06:54:16 +00006857 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006858 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6859
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006860 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6861 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6862 DAG);
6863
Craig Topperb3982da2011-12-31 23:50:21 +00006864 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006865 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006866 }
Eric Christopherfd179292009-08-27 18:07:15 +00006867
Evan Chengf26ffe92008-05-29 08:22:04 +00006868 // Check if this can be converted into a logical shift.
6869 bool isLeft = false;
6870 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006871 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006872 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006873 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006874 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006875 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006876 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006877 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006878 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006879 }
Eric Christopherfd179292009-08-27 18:07:15 +00006880
Craig Topper5aaffa82012-02-19 02:53:47 +00006881 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006882 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006883 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006884 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006885 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006886 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6887
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006888 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006889 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6890 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006891 }
Eric Christopherfd179292009-08-27 18:07:15 +00006892
Nate Begeman9008ca62009-04-27 18:41:29 +00006893 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006894 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006895 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006896
Craig Topperdd637ae2012-02-19 05:41:45 +00006897 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006898 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006899
Craig Topperdd637ae2012-02-19 05:41:45 +00006900 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006901 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006902
Craig Topperdd637ae2012-02-19 05:41:45 +00006903 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006904 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006905
Craig Topperdd637ae2012-02-19 05:41:45 +00006906 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006907 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006908
Craig Topperdd637ae2012-02-19 05:41:45 +00006909 if (ShouldXformToMOVHLPS(M, VT) ||
6910 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006911 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006912
Evan Chengf26ffe92008-05-29 08:22:04 +00006913 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006914 // No better options. Use a vshldq / vsrldq.
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
Evan Cheng9eca5e82006-10-25 21:49:50 +00006920 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006921 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6922 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006923 V1IsSplat = isSplatVector(V1.getNode());
6924 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006925
Chris Lattner8a594482007-11-25 00:24:49 +00006926 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006927 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6928 CommuteVectorShuffleMask(M, NumElems);
6929 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006930 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006931 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006932 }
6933
Craig Topperbeabc6c2011-12-05 06:56:46 +00006934 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006935 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006936 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006937 return V1;
6938 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6939 // the instruction selector will not match, so get a canonical MOVL with
6940 // swapped operands to undo the commute.
6941 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006942 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006943
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006944 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006945 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006946
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006947 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006948 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006949
Evan Cheng9bbbb982006-10-25 20:48:19 +00006950 if (V2IsSplat) {
6951 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006952 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006953 // new vector_shuffle with the corrected mask.p
6954 SmallVector<int, 8> NewMask(M.begin(), M.end());
6955 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006956 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006957 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006958 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006959 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006960 }
6961
Evan Cheng9eca5e82006-10-25 21:49:50 +00006962 if (Commuted) {
6963 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006964 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006965 CommuteVectorShuffleMask(M, NumElems);
6966 std::swap(V1, V2);
6967 std::swap(V1IsSplat, V2IsSplat);
6968 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006969
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006970 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006971 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006972
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006973 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006974 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006975 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006976
Nate Begeman9008ca62009-04-27 18:41:29 +00006977 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006978 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006979 return CommuteVectorShuffle(SVOp, DAG);
6980
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006981 // The checks below are all present in isShuffleMaskLegal, but they are
6982 // inlined here right now to enable us to directly emit target specific
6983 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006984
Craig Topper0e2037b2012-01-20 05:53:00 +00006985 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006986 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006987 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006988 DAG);
6989
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006990 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6991 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006992 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006993 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006994 }
6995
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006996 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006997 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006998 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006999 DAG);
7000
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007001 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007002 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007003 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00007004 DAG);
7005
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007006 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00007007 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00007008 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00007009
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007010 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007011 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007012 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007013 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007014
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007015 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007016 // Generate target specific nodes for 128 or 256-bit shuffles only
7017 // supported in the AVX instruction set.
7018 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007019
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007020 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007021 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007022 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7023
Craig Topper70b883b2011-11-28 10:14:51 +00007024 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007025 if (isVPERMILPMask(M, VT, HasFp256)) {
7026 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007027 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007028 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007029 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007030 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007031 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007032
Craig Topper70b883b2011-11-28 10:14:51 +00007033 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007034 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007035 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007036 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007037
Craig Topper1842ba02012-04-23 06:38:28 +00007038 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007039 if (BlendOp.getNode())
7040 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007041
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007042 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007043 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007044 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007045 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007046 }
Craig Topper92040742012-04-16 06:43:40 +00007047 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7048 &permclMask[0], 8);
7049 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007050 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007051 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007052 }
Craig Topper095c5282012-04-15 23:48:57 +00007053
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007054 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007055 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007056 getShuffleCLImmediate(SVOp), DAG);
7057
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007058 //===--------------------------------------------------------------------===//
7059 // Since no target specific shuffle was selected for this generic one,
7060 // lower it into other known shuffles. FIXME: this isn't true yet, but
7061 // this is the plan.
7062 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007063
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007064 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7065 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007066 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007067 if (NewOp.getNode())
7068 return NewOp;
7069 }
7070
7071 if (VT == MVT::v16i8) {
7072 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7073 if (NewOp.getNode())
7074 return NewOp;
7075 }
7076
Elena Demikhovsky41789462012-09-06 12:42:01 +00007077 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007078 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007079 if (NewOp.getNode())
7080 return NewOp;
7081 }
7082
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007083 // Handle all 128-bit wide vectors with 4 elements, and match them with
7084 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007085 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007086 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7087
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007088 // Handle general 256-bit shuffles
7089 if (VT.is256BitVector())
7090 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7091
Dan Gohman475871a2008-07-27 21:46:04 +00007092 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007093}
7094
Craig Topperf84b7502013-01-20 00:50:58 +00007095static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007096 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007097 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007098
Craig Topper45e1c752013-01-20 00:38:18 +00007099 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007100 return SDValue();
7101
Duncan Sands83ec4b62008-06-06 12:08:01 +00007102 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007103 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007104 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007105 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007106 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007107 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007108 }
7109
7110 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007111 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7112 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7113 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007114 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7115 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007116 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007117 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007118 Op.getOperand(0)),
7119 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007120 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007121 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007122 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007123 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007124 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007125 }
7126
7127 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007128 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7129 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007130 // result has a single use which is a store or a bitcast to i32. And in
7131 // the case of a store, it's not worth it if the index is a constant 0,
7132 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007133 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007134 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007135 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007136 if ((User->getOpcode() != ISD::STORE ||
7137 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7138 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007139 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007141 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007142 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007143 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007144 Op.getOperand(0)),
7145 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007146 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007147 }
7148
7149 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007150 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007151 if (isa<ConstantSDNode>(Op.getOperand(1)))
7152 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007153 }
Dan Gohman475871a2008-07-27 21:46:04 +00007154 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007155}
7156
Dan Gohman475871a2008-07-27 21:46:04 +00007157SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007158X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7159 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007160 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007161 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007162
David Greene74a579d2011-02-10 16:57:36 +00007163 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007164 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007165
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007166 // If this is a 256-bit vector result, first extract the 128-bit vector and
7167 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007168 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007169 DebugLoc dl = Op.getNode()->getDebugLoc();
7170 unsigned NumElems = VecVT.getVectorNumElements();
7171 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007172 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7173
7174 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007175 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007176
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007177 if (IdxVal >= NumElems/2)
7178 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007179 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007180 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007181 }
7182
Craig Topper7a9a28b2012-08-12 02:23:29 +00007183 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007184
Craig Topperd0a31172012-01-10 06:37:29 +00007185 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007186 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007187 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007188 return Res;
7189 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007190
Craig Topper45e1c752013-01-20 00:38:18 +00007191 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007192 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007193 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007194 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007196 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007197 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7199 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007200 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007201 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007202 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007203 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007204 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007205 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007206 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007207 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007208 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007209 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007210 }
7211
7212 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007213 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007214 if (Idx == 0)
7215 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007216
Evan Cheng0db9fe62006-04-25 20:13:52 +00007217 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007218 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007219 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007220 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007221 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007222 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007223 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007224 }
7225
7226 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007227 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7228 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7229 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007230 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007231 if (Idx == 0)
7232 return Op;
7233
7234 // UNPCKHPD the element to the lowest double word, then movsd.
7235 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7236 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007237 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007238 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007239 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007240 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007241 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007242 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007243 }
7244
Dan Gohman475871a2008-07-27 21:46:04 +00007245 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007246}
7247
Craig Topperf84b7502013-01-20 00:50:58 +00007248static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007249 MVT VT = Op.getValueType().getSimpleVT();
7250 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007251 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007252
Dan Gohman475871a2008-07-27 21:46:04 +00007253 SDValue N0 = Op.getOperand(0);
7254 SDValue N1 = Op.getOperand(1);
7255 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007256
Craig Topper7a9a28b2012-08-12 02:23:29 +00007257 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007258 return SDValue();
7259
Dan Gohman8a55ce42009-09-23 21:02:20 +00007260 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007261 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007262 unsigned Opc;
7263 if (VT == MVT::v8i16)
7264 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007265 else if (VT == MVT::v16i8)
7266 Opc = X86ISD::PINSRB;
7267 else
7268 Opc = X86ISD::PINSRB;
7269
Nate Begeman14d12ca2008-02-11 04:19:36 +00007270 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7271 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007272 if (N1.getValueType() != MVT::i32)
7273 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7274 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007275 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007276 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007277 }
7278
7279 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007280 // Bits [7:6] of the constant are the source select. This will always be
7281 // zero here. The DAG Combiner may combine an extract_elt index into these
7282 // bits. For example (insert (extract, 3), 2) could be matched by putting
7283 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007284 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007285 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007286 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007287 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007288 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007289 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007290 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007291 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007292 }
7293
7294 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007295 // PINSR* works with constant index.
7296 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007297 }
Dan Gohman475871a2008-07-27 21:46:04 +00007298 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007299}
7300
Dan Gohman475871a2008-07-27 21:46:04 +00007301SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007302X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007303 MVT VT = Op.getValueType().getSimpleVT();
7304 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007305
David Greene6b381262011-02-09 15:32:06 +00007306 DebugLoc dl = Op.getDebugLoc();
7307 SDValue N0 = Op.getOperand(0);
7308 SDValue N1 = Op.getOperand(1);
7309 SDValue N2 = Op.getOperand(2);
7310
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007311 // If this is a 256-bit vector result, first extract the 128-bit vector,
7312 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007313 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007314 if (!isa<ConstantSDNode>(N2))
7315 return SDValue();
7316
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007317 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007318 unsigned NumElems = VT.getVectorNumElements();
7319 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007320 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007321
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007322 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007323 bool Upper = IdxVal >= NumElems/2;
7324 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7325 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007326
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007327 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007328 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007329 }
7330
Craig Topperd0a31172012-01-10 06:37:29 +00007331 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007332 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7333
Dan Gohman8a55ce42009-09-23 21:02:20 +00007334 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007335 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007336
Dan Gohman8a55ce42009-09-23 21:02:20 +00007337 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007338 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7339 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007340 if (N1.getValueType() != MVT::i32)
7341 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7342 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007343 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007344 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007345 }
Dan Gohman475871a2008-07-27 21:46:04 +00007346 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007347}
7348
Craig Topper55b24052012-09-11 06:15:32 +00007349static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007350 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007351 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007352 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007353
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007354 // If this is a 256-bit vector result, first insert into a 128-bit
7355 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007356 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007357 // Insert into a 128-bit vector.
7358 EVT VT128 = EVT::getVectorVT(*Context,
7359 OpVT.getVectorElementType(),
7360 OpVT.getVectorNumElements() / 2);
7361
7362 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7363
7364 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007365 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007366 }
7367
Craig Topperd77d2fe2012-04-29 20:22:05 +00007368 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007369 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007370 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007371
Owen Anderson825b72b2009-08-11 20:47:22 +00007372 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007373 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007374 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007375 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007376}
7377
David Greene91585092011-01-26 15:38:49 +00007378// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7379// a simple subregister reference or explicit instructions to grab
7380// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007381static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7382 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007383 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007384 DebugLoc dl = Op.getNode()->getDebugLoc();
7385 SDValue Vec = Op.getNode()->getOperand(0);
7386 SDValue Idx = Op.getNode()->getOperand(1);
7387
Craig Topper7a9a28b2012-08-12 02:23:29 +00007388 if (Op.getNode()->getValueType(0).is128BitVector() &&
7389 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007390 isa<ConstantSDNode>(Idx)) {
7391 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7392 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007393 }
David Greene91585092011-01-26 15:38:49 +00007394 }
7395 return SDValue();
7396}
7397
David Greenecfe33c42011-01-26 19:13:22 +00007398// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7399// simple superregister reference or explicit instructions to insert
7400// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007401static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7402 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007403 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007404 DebugLoc dl = Op.getNode()->getDebugLoc();
7405 SDValue Vec = Op.getNode()->getOperand(0);
7406 SDValue SubVec = Op.getNode()->getOperand(1);
7407 SDValue Idx = Op.getNode()->getOperand(2);
7408
Craig Topper7a9a28b2012-08-12 02:23:29 +00007409 if (Op.getNode()->getValueType(0).is256BitVector() &&
7410 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007411 isa<ConstantSDNode>(Idx)) {
7412 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7413 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007414 }
7415 }
7416 return SDValue();
7417}
7418
Bill Wendling056292f2008-09-16 21:48:12 +00007419// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7420// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7421// one of the above mentioned nodes. It has to be wrapped because otherwise
7422// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7423// be used to form addressing mode. These wrapped nodes will be selected
7424// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007425SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007426X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007427 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007428
Chris Lattner41621a22009-06-26 19:22:52 +00007429 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7430 // global base reg.
7431 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007432 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007433 CodeModel::Model M = getTargetMachine().getCodeModel();
7434
Chris Lattner4f066492009-07-11 20:29:19 +00007435 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007436 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007437 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007438 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007439 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007440 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007441 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007442
Evan Cheng1606e8e2009-03-13 07:51:59 +00007443 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007444 CP->getAlignment(),
7445 CP->getOffset(), OpFlag);
7446 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007447 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007448 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007449 if (OpFlag) {
7450 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007451 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007452 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007453 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007454 }
7455
7456 return Result;
7457}
7458
Dan Gohmand858e902010-04-17 15:26:15 +00007459SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007460 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007461
Chris Lattner18c59872009-06-27 04:16:01 +00007462 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7463 // global base reg.
7464 unsigned char OpFlag = 0;
7465 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007466 CodeModel::Model M = getTargetMachine().getCodeModel();
7467
Chris Lattner4f066492009-07-11 20:29:19 +00007468 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007469 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007470 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007471 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007472 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007473 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007474 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007475
Chris Lattner18c59872009-06-27 04:16:01 +00007476 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7477 OpFlag);
7478 DebugLoc DL = JT->getDebugLoc();
7479 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007480
Chris Lattner18c59872009-06-27 04:16:01 +00007481 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007482 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007483 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7484 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007485 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007486 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007487
Chris Lattner18c59872009-06-27 04:16:01 +00007488 return Result;
7489}
7490
7491SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007492X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007493 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007494
Chris Lattner18c59872009-06-27 04:16:01 +00007495 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7496 // global base reg.
7497 unsigned char OpFlag = 0;
7498 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007499 CodeModel::Model M = getTargetMachine().getCodeModel();
7500
Chris Lattner4f066492009-07-11 20:29:19 +00007501 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007502 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7503 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7504 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007505 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007506 } else if (Subtarget->isPICStyleGOT()) {
7507 OpFlag = X86II::MO_GOT;
7508 } else if (Subtarget->isPICStyleStubPIC()) {
7509 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7510 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7511 OpFlag = X86II::MO_DARWIN_NONLAZY;
7512 }
Eric Christopherfd179292009-08-27 18:07:15 +00007513
Chris Lattner18c59872009-06-27 04:16:01 +00007514 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007515
Chris Lattner18c59872009-06-27 04:16:01 +00007516 DebugLoc DL = Op.getDebugLoc();
7517 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007518
Chris Lattner18c59872009-06-27 04:16:01 +00007519 // With PIC, the address is actually $g + Offset.
7520 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007521 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007522 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7523 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007524 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007525 Result);
7526 }
Eric Christopherfd179292009-08-27 18:07:15 +00007527
Eli Friedman586272d2011-08-11 01:48:05 +00007528 // For symbols that require a load from a stub to get the address, emit the
7529 // load.
7530 if (isGlobalStubReference(OpFlag))
7531 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007532 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007533
Chris Lattner18c59872009-06-27 04:16:01 +00007534 return Result;
7535}
7536
Dan Gohman475871a2008-07-27 21:46:04 +00007537SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007538X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007539 // Create the TargetBlockAddressAddress node.
7540 unsigned char OpFlags =
7541 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007542 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007543 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007544 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007545 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007546 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7547 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007548
Dan Gohmanf705adb2009-10-30 01:28:02 +00007549 if (Subtarget->isPICStyleRIPRel() &&
7550 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007551 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7552 else
7553 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007554
Dan Gohman29cbade2009-11-20 23:18:13 +00007555 // With PIC, the address is actually $g + Offset.
7556 if (isGlobalRelativeToPICBase(OpFlags)) {
7557 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7558 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7559 Result);
7560 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007561
7562 return Result;
7563}
7564
7565SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007566X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007567 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007568 // Create the TargetGlobalAddress node, folding in the constant
7569 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007570 unsigned char OpFlags =
7571 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007572 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007573 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007574 if (OpFlags == X86II::MO_NO_FLAG &&
7575 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007576 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007577 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007578 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007579 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007580 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007581 }
Eric Christopherfd179292009-08-27 18:07:15 +00007582
Chris Lattner4f066492009-07-11 20:29:19 +00007583 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007584 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007585 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7586 else
7587 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007588
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007589 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007590 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007591 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7592 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007593 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007595
Chris Lattner36c25012009-07-10 07:34:39 +00007596 // For globals that require a load from a stub to get the address, emit the
7597 // load.
7598 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007599 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007600 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007601
Dan Gohman6520e202008-10-18 02:06:02 +00007602 // If there was a non-zero offset that we didn't fold, create an explicit
7603 // addition for it.
7604 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007605 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007606 DAG.getConstant(Offset, getPointerTy()));
7607
Evan Cheng0db9fe62006-04-25 20:13:52 +00007608 return Result;
7609}
7610
Evan Chengda43bcf2008-09-24 00:05:32 +00007611SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007612X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007613 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007614 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007615 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007616}
7617
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007618static SDValue
7619GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007620 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007621 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007622 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007623 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007624 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007625 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007626 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007627 GA->getOffset(),
7628 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007629
7630 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7631 : X86ISD::TLSADDR;
7632
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007633 if (InFlag) {
7634 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007635 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007636 } else {
7637 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007638 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007639 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007640
7641 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007642 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007643
Rafael Espindola15f1b662009-04-24 12:59:40 +00007644 SDValue Flag = Chain.getValue(1);
7645 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007646}
7647
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007648// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007649static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007650LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007651 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007652 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007653 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7654 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007655 DAG.getNode(X86ISD::GlobalBaseReg,
7656 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007657 InFlag = Chain.getValue(1);
7658
Chris Lattnerb903bed2009-06-26 21:20:29 +00007659 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007660}
7661
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007662// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007663static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007664LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007665 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007666 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7667 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007668}
7669
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007670static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7671 SelectionDAG &DAG,
7672 const EVT PtrVT,
7673 bool is64Bit) {
7674 DebugLoc dl = GA->getDebugLoc();
7675
7676 // Get the start address of the TLS block for this module.
7677 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7678 .getInfo<X86MachineFunctionInfo>();
7679 MFI->incNumLocalDynamicTLSAccesses();
7680
7681 SDValue Base;
7682 if (is64Bit) {
7683 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7684 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7685 } else {
7686 SDValue InFlag;
7687 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7688 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7689 InFlag = Chain.getValue(1);
7690 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7691 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7692 }
7693
7694 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7695 // of Base.
7696
7697 // Build x@dtpoff.
7698 unsigned char OperandFlags = X86II::MO_DTPOFF;
7699 unsigned WrapperKind = X86ISD::Wrapper;
7700 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7701 GA->getValueType(0),
7702 GA->getOffset(), OperandFlags);
7703 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7704
7705 // Add x@dtpoff with the base.
7706 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7707}
7708
Hans Wennborg228756c2012-05-11 10:11:01 +00007709// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007710static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007711 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007712 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007713 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007714
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007715 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7716 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7717 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007718
Michael J. Spencerec38de22010-10-10 22:04:20 +00007719 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007720 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007721 MachinePointerInfo(Ptr),
7722 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007723
Chris Lattnerb903bed2009-06-26 21:20:29 +00007724 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007725 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7726 // initialexec.
7727 unsigned WrapperKind = X86ISD::Wrapper;
7728 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007729 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007730 } else if (model == TLSModel::InitialExec) {
7731 if (is64Bit) {
7732 OperandFlags = X86II::MO_GOTTPOFF;
7733 WrapperKind = X86ISD::WrapperRIP;
7734 } else {
7735 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7736 }
Chris Lattner18c59872009-06-27 04:16:01 +00007737 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007738 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007739 }
Eric Christopherfd179292009-08-27 18:07:15 +00007740
Hans Wennborg228756c2012-05-11 10:11:01 +00007741 // emit "addl x@ntpoff,%eax" (local exec)
7742 // or "addl x@indntpoff,%eax" (initial exec)
7743 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007744 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007745 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007746 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007747 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007748
Hans Wennborg228756c2012-05-11 10:11:01 +00007749 if (model == TLSModel::InitialExec) {
7750 if (isPIC && !is64Bit) {
7751 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7752 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7753 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007754 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007755
7756 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7757 MachinePointerInfo::getGOT(), false, false, false,
7758 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007759 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007760
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007761 // The address of the thread local variable is the add of the thread
7762 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007763 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007764}
7765
Dan Gohman475871a2008-07-27 21:46:04 +00007766SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007767X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007768
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007769 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007770 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007771
Eric Christopher30ef0e52010-06-03 04:07:48 +00007772 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007773 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007774
Eric Christopher30ef0e52010-06-03 04:07:48 +00007775 switch (model) {
7776 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007777 if (Subtarget->is64Bit())
7778 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7779 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007780 case TLSModel::LocalDynamic:
7781 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7782 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007783 case TLSModel::InitialExec:
7784 case TLSModel::LocalExec:
7785 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007786 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007787 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007788 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007789 llvm_unreachable("Unknown TLS model.");
7790 }
7791
7792 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007793 // Darwin only has one model of TLS. Lower to that.
7794 unsigned char OpFlag = 0;
7795 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7796 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007797
Eric Christopher30ef0e52010-06-03 04:07:48 +00007798 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7799 // global base reg.
7800 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7801 !Subtarget->is64Bit();
7802 if (PIC32)
7803 OpFlag = X86II::MO_TLVP_PIC_BASE;
7804 else
7805 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007806 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007807 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007808 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007809 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007810 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007811
Eric Christopher30ef0e52010-06-03 04:07:48 +00007812 // With PIC32, the address is actually $g + Offset.
7813 if (PIC32)
7814 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7815 DAG.getNode(X86ISD::GlobalBaseReg,
7816 DebugLoc(), getPointerTy()),
7817 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007818
Eric Christopher30ef0e52010-06-03 04:07:48 +00007819 // Lowering the machine isd will make sure everything is in the right
7820 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007821 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007822 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007823 SDValue Args[] = { Chain, Offset };
7824 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007825
Eric Christopher30ef0e52010-06-03 04:07:48 +00007826 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7827 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7828 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007829
Eric Christopher30ef0e52010-06-03 04:07:48 +00007830 // And our return value (tls address) is in the standard call return value
7831 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007832 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007833 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7834 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007835 }
7836
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007837 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007838 // Just use the implicit TLS architecture
7839 // Need to generate someting similar to:
7840 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7841 // ; from TEB
7842 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7843 // mov rcx, qword [rdx+rcx*8]
7844 // mov eax, .tls$:tlsvar
7845 // [rax+rcx] contains the address
7846 // Windows 64bit: gs:0x58
7847 // Windows 32bit: fs:__tls_array
7848
7849 // If GV is an alias then use the aliasee for determining
7850 // thread-localness.
7851 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7852 GV = GA->resolveAliasedGlobal(false);
7853 DebugLoc dl = GA->getDebugLoc();
7854 SDValue Chain = DAG.getEntryNode();
7855
7856 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007857 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
7858 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007859 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7860 ? Type::getInt8PtrTy(*DAG.getContext(),
7861 256)
7862 : Type::getInt32PtrTy(*DAG.getContext(),
7863 257));
7864
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007865 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
7866 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
7867 DAG.getExternalSymbol("_tls_array", getPointerTy()));
7868
7869 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007870 MachinePointerInfo(Ptr),
7871 false, false, false, 0);
7872
7873 // Load the _tls_index variable
7874 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7875 if (Subtarget->is64Bit())
7876 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7877 IDX, MachinePointerInfo(), MVT::i32,
7878 false, false, 0);
7879 else
7880 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7881 false, false, false, 0);
7882
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007883 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007884 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007885 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7886
7887 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7888 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7889 false, false, false, 0);
7890
7891 // Get the offset of start of .tls section
7892 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7893 GA->getValueType(0),
7894 GA->getOffset(), X86II::MO_SECREL);
7895 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7896
7897 // The address of the thread local variable is the add of the thread
7898 // pointer with the offset of the variable.
7899 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007900 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007901
David Blaikie4d6ccb52012-01-20 21:51:11 +00007902 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007903}
7904
Chad Rosierb90d2a92012-01-03 23:19:12 +00007905/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7906/// and take a 2 x i32 value to shift plus a shift amount.
7907SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007908 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007909 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007910 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007911 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007912 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007913 SDValue ShOpLo = Op.getOperand(0);
7914 SDValue ShOpHi = Op.getOperand(1);
7915 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007916 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007918 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007919
Dan Gohman475871a2008-07-27 21:46:04 +00007920 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007921 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007922 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7923 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007924 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007925 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7926 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007927 }
Evan Chenge3413162006-01-09 18:33:28 +00007928
Owen Anderson825b72b2009-08-11 20:47:22 +00007929 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7930 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007931 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007932 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007933
Dan Gohman475871a2008-07-27 21:46:04 +00007934 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007936 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7937 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007938
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007939 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007940 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7941 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007942 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007943 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7944 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007945 }
7946
Dan Gohman475871a2008-07-27 21:46:04 +00007947 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007948 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007949}
Evan Chenga3195e82006-01-12 22:54:21 +00007950
Dan Gohmand858e902010-04-17 15:26:15 +00007951SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7952 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007953 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007954
Dale Johannesen0488fb62010-09-30 23:57:10 +00007955 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007956 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007957
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007959 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007960
Eli Friedman36df4992009-05-27 00:47:34 +00007961 // These are really Legal; return the operand so the caller accepts it as
7962 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007964 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007965 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007966 Subtarget->is64Bit()) {
7967 return Op;
7968 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007969
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007970 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007971 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007972 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007973 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007974 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007975 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007976 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007977 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007978 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007979 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7980}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007981
Owen Andersone50ed302009-08-10 22:56:29 +00007982SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007983 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007984 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007985 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007986 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007987 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007988 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007989 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007990 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007991 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007992 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007993
Chris Lattner492a43e2010-09-22 01:28:21 +00007994 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007995
Stuart Hastings84be9582011-06-02 15:57:11 +00007996 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7997 MachineMemOperand *MMO;
7998 if (FI) {
7999 int SSFI = FI->getIndex();
8000 MMO =
8001 DAG.getMachineFunction()
8002 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8003 MachineMemOperand::MOLoad, ByteSize, ByteSize);
8004 } else {
8005 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
8006 StackSlot = StackSlot.getOperand(1);
8007 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008008 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008009 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
8010 X86ISD::FILD, DL,
8011 Tys, Ops, array_lengthof(Ops),
8012 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008013
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008014 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008015 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008016 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008017
8018 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8019 // shouldn't be necessary except that RFP cannot be live across
8020 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008021 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008022 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8023 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008024 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008025 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008026 SDValue Ops[] = {
8027 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8028 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008029 MachineMemOperand *MMO =
8030 DAG.getMachineFunction()
8031 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008032 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008033
Chris Lattner492a43e2010-09-22 01:28:21 +00008034 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8035 Ops, array_lengthof(Ops),
8036 Op.getValueType(), MMO);
8037 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008038 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008039 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008040 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008041
Evan Cheng0db9fe62006-04-25 20:13:52 +00008042 return Result;
8043}
8044
Bill Wendling8b8a6362009-01-17 03:56:04 +00008045// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008046SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8047 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008048 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008049 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008050 movq %rax, %xmm0
8051 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8052 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8053 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008054 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008055 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008056 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008057 addpd %xmm1, %xmm0
8058 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008059 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008060
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008061 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008062 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008063
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008064 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008065 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8066 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008067 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008068
Chris Lattner97484792012-01-25 09:56:22 +00008069 SmallVector<Constant*,2> CV1;
8070 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008071 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8072 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008073 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008074 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8075 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008076 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008077 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008078
Bill Wendling397ae212012-01-05 02:13:20 +00008079 // Load the 64-bit value into an XMM register.
8080 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8081 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008082 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008083 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008084 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008085 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8086 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8087 CLod0);
8088
Owen Anderson825b72b2009-08-11 20:47:22 +00008089 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008090 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008091 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008092 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008093 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008094 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008095
Craig Topperd0a31172012-01-10 06:37:29 +00008096 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008097 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8098 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8099 } else {
8100 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8101 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8102 S2F, 0x4E, DAG);
8103 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8104 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8105 Sub);
8106 }
8107
8108 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008109 DAG.getIntPtrConstant(0));
8110}
8111
Bill Wendling8b8a6362009-01-17 03:56:04 +00008112// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008113SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8114 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008115 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008116 // FP constant to bias correct the final result.
8117 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008118 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008119
8120 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008121 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008122 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008123
Eli Friedmanf3704762011-08-29 21:15:46 +00008124 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008125 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008126
Owen Anderson825b72b2009-08-11 20:47:22 +00008127 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008128 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008129 DAG.getIntPtrConstant(0));
8130
8131 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008132 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008133 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008134 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008135 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008136 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008137 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008138 MVT::v2f64, Bias)));
8139 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008140 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008141 DAG.getIntPtrConstant(0));
8142
8143 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008144 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008145
8146 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008147 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008148
Craig Topper69947b92012-04-23 06:57:04 +00008149 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008150 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008151 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008152 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008153 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008154
8155 // Handle final rounding.
8156 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008157}
8158
Michael Liaoa7554632012-10-23 17:36:08 +00008159SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8160 SelectionDAG &DAG) const {
8161 SDValue N0 = Op.getOperand(0);
8162 EVT SVT = N0.getValueType();
8163 DebugLoc dl = Op.getDebugLoc();
8164
8165 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8166 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8167 "Custom UINT_TO_FP is not supported!");
8168
Craig Topperb99bafe2013-01-21 06:21:54 +00008169 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8170 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008171 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8172 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8173}
8174
Dan Gohmand858e902010-04-17 15:26:15 +00008175SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8176 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008177 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008178 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008179
Michael Liaoa7554632012-10-23 17:36:08 +00008180 if (Op.getValueType().isVector())
8181 return lowerUINT_TO_FP_vec(Op, DAG);
8182
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008183 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008184 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8185 // the optimization here.
8186 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008187 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008188
Owen Andersone50ed302009-08-10 22:56:29 +00008189 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008190 EVT DstVT = Op.getValueType();
8191 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008192 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008193 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008194 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008195 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008196 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008197
8198 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008199 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008200 if (SrcVT == MVT::i32) {
8201 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8202 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8203 getPointerTy(), StackSlot, WordOff);
8204 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008205 StackSlot, MachinePointerInfo(),
8206 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008207 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008208 OffsetSlot, MachinePointerInfo(),
8209 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008210 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8211 return Fild;
8212 }
8213
8214 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8215 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008216 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008217 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008218 // For i64 source, we need to add the appropriate power of 2 if the input
8219 // was negative. This is the same as the optimization in
8220 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8221 // we must be careful to do the computation in x87 extended precision, not
8222 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008223 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8224 MachineMemOperand *MMO =
8225 DAG.getMachineFunction()
8226 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8227 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008228
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008229 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8230 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008231 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8232 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008233
8234 APInt FF(32, 0x5F800000ULL);
8235
8236 // Check whether the sign bit is set.
8237 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8238 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8239 ISD::SETLT);
8240
8241 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8242 SDValue FudgePtr = DAG.getConstantPool(
8243 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8244 getPointerTy());
8245
8246 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8247 SDValue Zero = DAG.getIntPtrConstant(0);
8248 SDValue Four = DAG.getIntPtrConstant(4);
8249 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8250 Zero, Four);
8251 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8252
8253 // Load the value out, extending it from f32 to f80.
8254 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008255 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008256 FudgePtr, MachinePointerInfo::getConstantPool(),
8257 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008258 // Extend everything to 80 bits to force it to be done on x87.
8259 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8260 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008261}
8262
Craig Topperb99bafe2013-01-21 06:21:54 +00008263std::pair<SDValue,SDValue>
8264X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8265 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008266 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008267
Owen Andersone50ed302009-08-10 22:56:29 +00008268 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008269
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008270 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008271 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8272 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008273 }
8274
Owen Anderson825b72b2009-08-11 20:47:22 +00008275 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8276 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008277 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008278
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008279 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008281 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008282 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008283 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008284 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008285 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008286 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008287
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008288 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8289 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008290 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008291 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008292 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008293 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008294
Evan Cheng0db9fe62006-04-25 20:13:52 +00008295 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008296 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8297 Opc = X86ISD::WIN_FTOL;
8298 else
8299 switch (DstTy.getSimpleVT().SimpleTy) {
8300 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8301 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8302 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8303 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8304 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008305
Dan Gohman475871a2008-07-27 21:46:04 +00008306 SDValue Chain = DAG.getEntryNode();
8307 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008308 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008309 // FIXME This causes a redundant load/store if the SSE-class value is already
8310 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008311 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008312 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008313 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008314 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008315 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008316 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008317 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008318 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008319 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008320
Chris Lattner492a43e2010-09-22 01:28:21 +00008321 MachineMemOperand *MMO =
8322 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8323 MachineMemOperand::MOLoad, MemSize, MemSize);
8324 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8325 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008326 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008327 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008328 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8329 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008330
Chris Lattner07290932010-09-22 01:05:16 +00008331 MachineMemOperand *MMO =
8332 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8333 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008334
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008335 if (Opc != X86ISD::WIN_FTOL) {
8336 // Build the FP_TO_INT*_IN_MEM
8337 SDValue Ops[] = { Chain, Value, StackSlot };
8338 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8339 Ops, 3, DstTy, MMO);
8340 return std::make_pair(FIST, StackSlot);
8341 } else {
8342 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8343 DAG.getVTList(MVT::Other, MVT::Glue),
8344 Chain, Value);
8345 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8346 MVT::i32, ftol.getValue(1));
8347 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8348 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008349 SDValue Ops[] = { eax, edx };
8350 SDValue pair = IsReplace
8351 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8352 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008353 return std::make_pair(pair, SDValue());
8354 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008355}
8356
Nadav Rotem0509db22012-12-28 05:45:24 +00008357static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8358 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008359 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008360 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008361 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008362 DebugLoc dl = Op->getDebugLoc();
8363
8364 // Optimize vectors in AVX mode:
8365 //
8366 // v8i16 -> v8i32
8367 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8368 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8369 // Concat upper and lower parts.
8370 //
8371 // v4i32 -> v4i64
8372 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8373 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8374 // Concat upper and lower parts.
8375 //
8376
8377 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8378 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8379 return SDValue();
8380
8381 if (Subtarget->hasInt256())
8382 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8383
8384 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8385 SDValue Undef = DAG.getUNDEF(InVT);
8386 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8387 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8388 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8389
Craig Toppera080daf2013-01-20 21:50:27 +00008390 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008391 VT.getVectorNumElements()/2);
8392
8393 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8394 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8395
8396 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8397}
8398
8399SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8400 SelectionDAG &DAG) const {
8401 if (Subtarget->hasFp256()) {
8402 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8403 if (Res.getNode())
8404 return Res;
8405 }
8406
8407 return SDValue();
8408}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008409SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8410 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008411 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008412 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008413 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008414 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008415
Nadav Rotem0509db22012-12-28 05:45:24 +00008416 if (Subtarget->hasFp256()) {
8417 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8418 if (Res.getNode())
8419 return Res;
8420 }
8421
Michael Liaoa7554632012-10-23 17:36:08 +00008422 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8423 VT.getVectorNumElements() != SVT.getVectorNumElements())
8424 return SDValue();
8425
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008426 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008427
8428 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008429 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008430 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8431
8432 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8433 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8434 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008435 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8436 DAG.getUNDEF(MVT::v8i16),
8437 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008438
8439 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8440}
8441
Craig Topperd713c0f2013-01-20 21:34:37 +00008442SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008443 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008444 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008445 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008446 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008447
Nadav Rotem3c22a442012-12-27 07:45:10 +00008448 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8449 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8450 if (Subtarget->hasInt256()) {
8451 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8452 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8453 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8454 ShufMask);
8455 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8456 DAG.getIntPtrConstant(0));
8457 }
8458
8459 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8460 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8461 DAG.getIntPtrConstant(0));
8462 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8463 DAG.getIntPtrConstant(2));
8464
8465 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8466 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8467
8468 // The PSHUFD mask:
8469 static const int ShufMask1[] = {0, 2, 0, 0};
8470 SDValue Undef = DAG.getUNDEF(VT);
8471 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8472 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8473
8474 // The MOVLHPS mask:
8475 static const int ShufMask2[] = {0, 1, 4, 5};
8476 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8477 }
8478
8479 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8480 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8481 if (Subtarget->hasInt256()) {
8482 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8483
8484 SmallVector<SDValue,32> pshufbMask;
8485 for (unsigned i = 0; i < 2; ++i) {
8486 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8487 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8488 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8489 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8490 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8491 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8492 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8493 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8494 for (unsigned j = 0; j < 8; ++j)
8495 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8496 }
8497 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8498 &pshufbMask[0], 32);
8499 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8500 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8501
8502 static const int ShufMask[] = {0, 2, -1, -1};
8503 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8504 &ShufMask[0]);
8505 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8506 DAG.getIntPtrConstant(0));
8507 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8508 }
8509
8510 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8511 DAG.getIntPtrConstant(0));
8512
8513 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8514 DAG.getIntPtrConstant(4));
8515
8516 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8517 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8518
8519 // The PSHUFB mask:
8520 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8521 -1, -1, -1, -1, -1, -1, -1, -1};
8522
8523 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8524 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8525 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8526
8527 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8528 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8529
8530 // The MOVLHPS Mask:
8531 static const int ShufMask2[] = {0, 1, 4, 5};
8532 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8533 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8534 }
8535
8536 // Handle truncation of V256 to V128 using shuffles.
8537 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008538 return SDValue();
8539
Nadav Rotem3c22a442012-12-27 07:45:10 +00008540 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8541 "Invalid op");
8542 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008543
8544 unsigned NumElems = VT.getVectorNumElements();
8545 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8546 NumElems * 2);
8547
Michael Liaobedcbd42012-10-16 18:14:11 +00008548 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8549 // Prepare truncation shuffle mask
8550 for (unsigned i = 0; i != NumElems; ++i)
8551 MaskVec[i] = i * 2;
8552 SDValue V = DAG.getVectorShuffle(NVT, DL,
8553 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8554 DAG.getUNDEF(NVT), &MaskVec[0]);
8555 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8556 DAG.getIntPtrConstant(0));
8557}
8558
Dan Gohmand858e902010-04-17 15:26:15 +00008559SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8560 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008561 MVT VT = Op.getValueType().getSimpleVT();
8562 if (VT.isVector()) {
8563 if (VT == MVT::v8i16)
8564 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008565 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8566 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008567 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008568 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008569
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008570 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8571 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008572 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008573 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8574 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008575
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008576 if (StackSlot.getNode())
8577 // Load the result.
8578 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8579 FIST, StackSlot, MachinePointerInfo(),
8580 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008581
8582 // The node is the result.
8583 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008584}
8585
Dan Gohmand858e902010-04-17 15:26:15 +00008586SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8587 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008588 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8589 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008590 SDValue FIST = Vals.first, StackSlot = Vals.second;
8591 assert(FIST.getNode() && "Unexpected failure");
8592
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008593 if (StackSlot.getNode())
8594 // Load the result.
8595 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8596 FIST, StackSlot, MachinePointerInfo(),
8597 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008598
8599 // The node is the result.
8600 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008601}
8602
Craig Topperb84b4232013-01-21 06:13:28 +00008603static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008604 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008605 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008606 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008607 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008608
8609 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8610
8611 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8612 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8613 In, DAG.getUNDEF(SVT)));
8614}
8615
Craig Topper43620672012-09-08 07:31:51 +00008616SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008617 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008618 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008619 MVT VT = Op.getValueType().getSimpleVT();
8620 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008621 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8622 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008623 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008624 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008625 }
Craig Topper43620672012-09-08 07:31:51 +00008626 Constant *C;
8627 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008628 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8629 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008630 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008631 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8632 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008633 C = ConstantVector::getSplat(NumElts, C);
8634 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8635 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008636 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008637 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008638 false, false, false, Alignment);
8639 if (VT.isVector()) {
8640 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8641 return DAG.getNode(ISD::BITCAST, dl, VT,
8642 DAG.getNode(ISD::AND, dl, ANDVT,
8643 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8644 Op.getOperand(0)),
8645 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8646 }
Dale Johannesenace16102009-02-03 19:33:06 +00008647 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008648}
8649
Dan Gohmand858e902010-04-17 15:26:15 +00008650SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008651 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008652 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008653 MVT VT = Op.getValueType().getSimpleVT();
8654 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008655 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8656 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008657 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008658 NumElts = VT.getVectorNumElements();
8659 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008660 Constant *C;
8661 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008662 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8663 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008664 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008665 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8666 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008667 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008668 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8669 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008670 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008671 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008672 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008673 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008674 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008675 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008676 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008677 DAG.getNode(ISD::BITCAST, dl, XORVT,
8678 Op.getOperand(0)),
8679 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008680 }
Craig Topper69947b92012-04-23 06:57:04 +00008681
8682 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008683}
8684
Dan Gohmand858e902010-04-17 15:26:15 +00008685SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008686 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008687 SDValue Op0 = Op.getOperand(0);
8688 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008689 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008690 MVT VT = Op.getValueType().getSimpleVT();
8691 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008692
8693 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008694 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008695 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008696 SrcVT = VT;
8697 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008698 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008699 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008700 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008701 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008702 }
8703
8704 // At this point the operands and the result should have the same
8705 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008706
Evan Cheng68c47cb2007-01-05 07:55:56 +00008707 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008708 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008709 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008710 const fltSemantics &Sem = APFloat::IEEEdouble;
8711 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8712 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008713 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008714 const fltSemantics &Sem = APFloat::IEEEsingle;
8715 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8716 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8717 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8718 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008719 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008720 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008721 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008722 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008723 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008724 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008725 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008726
8727 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008728 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008729 // Op0 is MVT::f32, Op1 is MVT::f64.
8730 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8731 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8732 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008733 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008734 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008735 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008736 }
8737
Evan Cheng73d6cf12007-01-05 21:37:56 +00008738 // Clear first operand sign bit.
8739 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008740 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008741 const fltSemantics &Sem = APFloat::IEEEdouble;
8742 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8743 APInt(64, ~(1ULL << 63)))));
8744 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008745 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008746 const fltSemantics &Sem = APFloat::IEEEsingle;
8747 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8748 APInt(32, ~(1U << 31)))));
8749 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8750 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8751 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008752 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008753 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008754 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008755 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008756 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008757 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008758 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008759
8760 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008761 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008762}
8763
Craig Topper55b24052012-09-11 06:15:32 +00008764static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008765 SDValue N0 = Op.getOperand(0);
8766 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008767 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008768
8769 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8770 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8771 DAG.getConstant(1, VT));
8772 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8773}
8774
Michael Liaof966e4e2012-09-13 20:24:54 +00008775// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8776//
Craig Topperb99bafe2013-01-21 06:21:54 +00008777SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8778 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008779 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8780
8781 if (!Subtarget->hasSSE41())
8782 return SDValue();
8783
8784 if (!Op->hasOneUse())
8785 return SDValue();
8786
8787 SDNode *N = Op.getNode();
8788 DebugLoc DL = N->getDebugLoc();
8789
8790 SmallVector<SDValue, 8> Opnds;
8791 DenseMap<SDValue, unsigned> VecInMap;
8792 EVT VT = MVT::Other;
8793
8794 // Recognize a special case where a vector is casted into wide integer to
8795 // test all 0s.
8796 Opnds.push_back(N->getOperand(0));
8797 Opnds.push_back(N->getOperand(1));
8798
8799 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8800 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8801 // BFS traverse all OR'd operands.
8802 if (I->getOpcode() == ISD::OR) {
8803 Opnds.push_back(I->getOperand(0));
8804 Opnds.push_back(I->getOperand(1));
8805 // Re-evaluate the number of nodes to be traversed.
8806 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8807 continue;
8808 }
8809
8810 // Quit if a non-EXTRACT_VECTOR_ELT
8811 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8812 return SDValue();
8813
8814 // Quit if without a constant index.
8815 SDValue Idx = I->getOperand(1);
8816 if (!isa<ConstantSDNode>(Idx))
8817 return SDValue();
8818
8819 SDValue ExtractedFromVec = I->getOperand(0);
8820 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8821 if (M == VecInMap.end()) {
8822 VT = ExtractedFromVec.getValueType();
8823 // Quit if not 128/256-bit vector.
8824 if (!VT.is128BitVector() && !VT.is256BitVector())
8825 return SDValue();
8826 // Quit if not the same type.
8827 if (VecInMap.begin() != VecInMap.end() &&
8828 VT != VecInMap.begin()->first.getValueType())
8829 return SDValue();
8830 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8831 }
8832 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8833 }
8834
8835 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008836 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008837
8838 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8839 SmallVector<SDValue, 8> VecIns;
8840
8841 for (DenseMap<SDValue, unsigned>::const_iterator
8842 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8843 // Quit if not all elements are used.
8844 if (I->second != FullMask)
8845 return SDValue();
8846 VecIns.push_back(I->first);
8847 }
8848
8849 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8850
8851 // Cast all vectors into TestVT for PTEST.
8852 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8853 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8854
8855 // If more than one full vectors are evaluated, OR them first before PTEST.
8856 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8857 // Each iteration will OR 2 nodes and append the result until there is only
8858 // 1 node left, i.e. the final OR'd value of all vectors.
8859 SDValue LHS = VecIns[Slot];
8860 SDValue RHS = VecIns[Slot + 1];
8861 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8862 }
8863
8864 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8865 VecIns.back(), VecIns.back());
8866}
8867
Dan Gohman076aee32009-03-04 19:44:21 +00008868/// Emit nodes that will be selected as "test Op0,Op0", or something
8869/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008870SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008871 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008872 DebugLoc dl = Op.getDebugLoc();
8873
Dan Gohman31125812009-03-07 01:58:32 +00008874 // CF and OF aren't always set the way we want. Determine which
8875 // of these we need.
8876 bool NeedCF = false;
8877 bool NeedOF = false;
8878 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008879 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008880 case X86::COND_A: case X86::COND_AE:
8881 case X86::COND_B: case X86::COND_BE:
8882 NeedCF = true;
8883 break;
8884 case X86::COND_G: case X86::COND_GE:
8885 case X86::COND_L: case X86::COND_LE:
8886 case X86::COND_O: case X86::COND_NO:
8887 NeedOF = true;
8888 break;
Dan Gohman31125812009-03-07 01:58:32 +00008889 }
8890
Dan Gohman076aee32009-03-04 19:44:21 +00008891 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008892 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8893 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008894 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8895 // Emit a CMP with 0, which is the TEST pattern.
8896 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8897 DAG.getConstant(0, Op.getValueType()));
8898
8899 unsigned Opcode = 0;
8900 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008901
8902 // Truncate operations may prevent the merge of the SETCC instruction
8903 // and the arithmetic intruction before it. Attempt to truncate the operands
8904 // of the arithmetic instruction and use a reduced bit-width instruction.
8905 bool NeedTruncation = false;
8906 SDValue ArithOp = Op;
8907 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8908 SDValue Arith = Op->getOperand(0);
8909 // Both the trunc and the arithmetic op need to have one user each.
8910 if (Arith->hasOneUse())
8911 switch (Arith.getOpcode()) {
8912 default: break;
8913 case ISD::ADD:
8914 case ISD::SUB:
8915 case ISD::AND:
8916 case ISD::OR:
8917 case ISD::XOR: {
8918 NeedTruncation = true;
8919 ArithOp = Arith;
8920 }
8921 }
8922 }
8923
8924 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8925 // which may be the result of a CAST. We use the variable 'Op', which is the
8926 // non-casted variable when we check for possible users.
8927 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008928 case ISD::ADD:
8929 // Due to an isel shortcoming, be conservative if this add is likely to be
8930 // selected as part of a load-modify-store instruction. When the root node
8931 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8932 // uses of other nodes in the match, such as the ADD in this case. This
8933 // leads to the ADD being left around and reselected, with the result being
8934 // two adds in the output. Alas, even if none our users are stores, that
8935 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8936 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8937 // climbing the DAG back to the root, and it doesn't seem to be worth the
8938 // effort.
8939 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008940 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8941 if (UI->getOpcode() != ISD::CopyToReg &&
8942 UI->getOpcode() != ISD::SETCC &&
8943 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008944 goto default_case;
8945
8946 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008947 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008948 // An add of one will be selected as an INC.
8949 if (C->getAPIntValue() == 1) {
8950 Opcode = X86ISD::INC;
8951 NumOperands = 1;
8952 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008953 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008954
8955 // An add of negative one (subtract of one) will be selected as a DEC.
8956 if (C->getAPIntValue().isAllOnesValue()) {
8957 Opcode = X86ISD::DEC;
8958 NumOperands = 1;
8959 break;
8960 }
Dan Gohman076aee32009-03-04 19:44:21 +00008961 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008962
8963 // Otherwise use a regular EFLAGS-setting add.
8964 Opcode = X86ISD::ADD;
8965 NumOperands = 2;
8966 break;
8967 case ISD::AND: {
8968 // If the primary and result isn't used, don't bother using X86ISD::AND,
8969 // because a TEST instruction will be better.
8970 bool NonFlagUse = false;
8971 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8972 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8973 SDNode *User = *UI;
8974 unsigned UOpNo = UI.getOperandNo();
8975 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8976 // Look pass truncate.
8977 UOpNo = User->use_begin().getOperandNo();
8978 User = *User->use_begin();
8979 }
8980
8981 if (User->getOpcode() != ISD::BRCOND &&
8982 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008983 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008984 NonFlagUse = true;
8985 break;
8986 }
Dan Gohman076aee32009-03-04 19:44:21 +00008987 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008988
8989 if (!NonFlagUse)
8990 break;
8991 }
8992 // FALL THROUGH
8993 case ISD::SUB:
8994 case ISD::OR:
8995 case ISD::XOR:
8996 // Due to the ISEL shortcoming noted above, be conservative if this op is
8997 // likely to be selected as part of a load-modify-store instruction.
8998 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8999 UE = Op.getNode()->use_end(); UI != UE; ++UI)
9000 if (UI->getOpcode() == ISD::STORE)
9001 goto default_case;
9002
9003 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009004 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009005 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009006 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009007 case ISD::XOR: Opcode = X86ISD::XOR; break;
9008 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00009009 case ISD::OR: {
9010 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
9011 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9012 if (EFLAGS.getNode())
9013 return EFLAGS;
9014 }
9015 Opcode = X86ISD::OR;
9016 break;
9017 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009018 }
9019
9020 NumOperands = 2;
9021 break;
9022 case X86ISD::ADD:
9023 case X86ISD::SUB:
9024 case X86ISD::INC:
9025 case X86ISD::DEC:
9026 case X86ISD::OR:
9027 case X86ISD::XOR:
9028 case X86ISD::AND:
9029 return SDValue(Op.getNode(), 1);
9030 default:
9031 default_case:
9032 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009033 }
9034
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009035 // If we found that truncation is beneficial, perform the truncation and
9036 // update 'Op'.
9037 if (NeedTruncation) {
9038 EVT VT = Op.getValueType();
9039 SDValue WideVal = Op->getOperand(0);
9040 EVT WideVT = WideVal.getValueType();
9041 unsigned ConvertedOp = 0;
9042 // Use a target machine opcode to prevent further DAGCombine
9043 // optimizations that may separate the arithmetic operations
9044 // from the setcc node.
9045 switch (WideVal.getOpcode()) {
9046 default: break;
9047 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9048 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9049 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9050 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9051 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9052 }
9053
9054 if (ConvertedOp) {
9055 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9056 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9057 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9058 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9059 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9060 }
9061 }
9062 }
9063
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009064 if (Opcode == 0)
9065 // Emit a CMP with 0, which is the TEST pattern.
9066 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9067 DAG.getConstant(0, Op.getValueType()));
9068
9069 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9070 SmallVector<SDValue, 4> Ops;
9071 for (unsigned i = 0; i != NumOperands; ++i)
9072 Ops.push_back(Op.getOperand(i));
9073
9074 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9075 DAG.ReplaceAllUsesWith(Op, New);
9076 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009077}
9078
9079/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9080/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009081SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009082 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9084 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009085 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009086
9087 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009088 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9089 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9090 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9091 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9092 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9093 Op0, Op1);
9094 return SDValue(Sub.getNode(), 1);
9095 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009096 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009097}
9098
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009099/// Convert a comparison if required by the subtarget.
9100SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9101 SelectionDAG &DAG) const {
9102 // If the subtarget does not support the FUCOMI instruction, floating-point
9103 // comparisons have to be converted.
9104 if (Subtarget->hasCMov() ||
9105 Cmp.getOpcode() != X86ISD::CMP ||
9106 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9107 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9108 return Cmp;
9109
9110 // The instruction selector will select an FUCOM instruction instead of
9111 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9112 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9113 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9114 DebugLoc dl = Cmp.getDebugLoc();
9115 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9116 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9117 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9118 DAG.getConstant(8, MVT::i8));
9119 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9120 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9121}
9122
Evan Cheng4e544802012-12-05 00:10:38 +00009123static bool isAllOnes(SDValue V) {
9124 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9125 return C && C->isAllOnesValue();
9126}
9127
Evan Chengd40d03e2010-01-06 19:38:29 +00009128/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9129/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009130SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9131 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009132 SDValue Op0 = And.getOperand(0);
9133 SDValue Op1 = And.getOperand(1);
9134 if (Op0.getOpcode() == ISD::TRUNCATE)
9135 Op0 = Op0.getOperand(0);
9136 if (Op1.getOpcode() == ISD::TRUNCATE)
9137 Op1 = Op1.getOperand(0);
9138
Evan Chengd40d03e2010-01-06 19:38:29 +00009139 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009140 if (Op1.getOpcode() == ISD::SHL)
9141 std::swap(Op0, Op1);
9142 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009143 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9144 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009145 // If we looked past a truncate, check that it's only truncating away
9146 // known zeros.
9147 unsigned BitWidth = Op0.getValueSizeInBits();
9148 unsigned AndBitWidth = And.getValueSizeInBits();
9149 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009150 APInt Zeros, Ones;
9151 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009152 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9153 return SDValue();
9154 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009155 LHS = Op1;
9156 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009157 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009158 } else if (Op1.getOpcode() == ISD::Constant) {
9159 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009160 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009161 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009162
9163 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009164 LHS = AndLHS.getOperand(0);
9165 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009166 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009167
9168 // Use BT if the immediate can't be encoded in a TEST instruction.
9169 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9170 LHS = AndLHS;
9171 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9172 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009173 }
Evan Cheng0488db92007-09-25 01:57:46 +00009174
Evan Chengd40d03e2010-01-06 19:38:29 +00009175 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009176 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9177 // the condition code later.
9178 bool Invert = false;
9179 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9180 Invert = true;
9181 LHS = LHS.getOperand(0);
9182 }
9183
Evan Chenge5b51ac2010-04-17 06:13:15 +00009184 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009185 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009186 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009187 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009188 // Also promote i16 to i32 for performance / code size reason.
9189 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009190 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009191 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009192
Evan Chengd40d03e2010-01-06 19:38:29 +00009193 // If the operand types disagree, extend the shift amount to match. Since
9194 // BT ignores high bits (like shifts) we can use anyextend.
9195 if (LHS.getValueType() != RHS.getValueType())
9196 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009197
Evan Chengd40d03e2010-01-06 19:38:29 +00009198 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009199 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9200 // Flip the condition if the LHS was a not instruction
9201 if (Invert)
9202 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009203 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9204 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009205 }
9206
Evan Cheng54de3ea2010-01-05 06:52:31 +00009207 return SDValue();
9208}
9209
Craig Topper89af15e2011-09-18 08:03:58 +00009210// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009211// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009212static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009213 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009214
Craig Topper7a9a28b2012-08-12 02:23:29 +00009215 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009216 "Unsupported value type for operation");
9217
Craig Topper66ddd152012-04-27 22:54:43 +00009218 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009219 DebugLoc dl = Op.getDebugLoc();
9220 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009221
9222 // Extract the LHS vectors
9223 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009224 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9225 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009226
9227 // Extract the RHS vectors
9228 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009229 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9230 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009231
9232 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009233 MVT EltVT = VT.getVectorElementType();
9234 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009235 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9236 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9237 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9238}
9239
Craig Topper26827f32013-01-20 09:02:22 +00009240static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9241 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009242 SDValue Cond;
9243 SDValue Op0 = Op.getOperand(0);
9244 SDValue Op1 = Op.getOperand(1);
9245 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009246 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009247 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009248 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009249 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009250
9251 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009252#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009253 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009254 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9255#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009256
Craig Topper523908d2012-08-13 02:34:03 +00009257 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009258 bool Swap = false;
9259
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009260 // SSE Condition code mapping:
9261 // 0 - EQ
9262 // 1 - LT
9263 // 2 - LE
9264 // 3 - UNORD
9265 // 4 - NEQ
9266 // 5 - NLT
9267 // 6 - NLE
9268 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009269 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009270 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009271 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009272 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009273 case ISD::SETOGT:
9274 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009275 case ISD::SETLT:
9276 case ISD::SETOLT: SSECC = 1; break;
9277 case ISD::SETOGE:
9278 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009279 case ISD::SETLE:
9280 case ISD::SETOLE: SSECC = 2; break;
9281 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009282 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009283 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009284 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009285 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009286 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009287 case ISD::SETUGT: SSECC = 6; break;
9288 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009289 case ISD::SETUEQ:
9290 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009291 }
9292 if (Swap)
9293 std::swap(Op0, Op1);
9294
Nate Begemanfb8ead02008-07-25 19:05:58 +00009295 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009296 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009297 unsigned CC0, CC1;
9298 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009299 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009300 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9301 } else {
9302 assert(SetCCOpcode == ISD::SETONE);
9303 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009304 }
Craig Topper523908d2012-08-13 02:34:03 +00009305
9306 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9307 DAG.getConstant(CC0, MVT::i8));
9308 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9309 DAG.getConstant(CC1, MVT::i8));
9310 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009311 }
9312 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009313 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9314 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009315 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009316
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009317 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009318 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009319 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009320
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 // We are handling one of the integer comparisons here. Since SSE only has
9322 // GT and EQ comparisons for integer, swapping operands and multiple
9323 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009324 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009325 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009326
Nate Begeman30a0de92008-07-17 16:51:19 +00009327 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009328 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009329 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009330 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009331 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009332 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009333 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009334 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009335 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009336 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009337 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009338 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009339 }
9340 if (Swap)
9341 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009342
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009343 // Check that the operation in question is available (most are plain SSE2,
9344 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009345 if (VT == MVT::v2i64) {
9346 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9347 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009348 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9349 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009350 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009351 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9352
9353 // First cast everything to the right type,
9354 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9355 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9356
9357 // Do the compare.
9358 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9359
9360 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009361 const int Mask[] = { 1, 0, 3, 2 };
9362 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9363 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009364
9365 if (Invert)
9366 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9367
9368 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9369 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009370 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009371
Nate Begeman30a0de92008-07-17 16:51:19 +00009372 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9373 // bits of the inputs before performing those operations.
9374 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009375 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009376 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9377 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009378 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009379 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9380 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009381 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9382 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009384
Dale Johannesenace16102009-02-03 19:33:06 +00009385 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009386
9387 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009388 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009389 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009390
Nate Begeman30a0de92008-07-17 16:51:19 +00009391 return Result;
9392}
Evan Cheng0488db92007-09-25 01:57:46 +00009393
Craig Topper26827f32013-01-20 09:02:22 +00009394SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9395
9396 MVT VT = Op.getValueType().getSimpleVT();
9397
9398 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9399
9400 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9401 SDValue Op0 = Op.getOperand(0);
9402 SDValue Op1 = Op.getOperand(1);
9403 DebugLoc dl = Op.getDebugLoc();
9404 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9405
9406 // Optimize to BT if possible.
9407 // Lower (X & (1 << N)) == 0 to BT(X, N).
9408 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9409 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9410 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9411 Op1.getOpcode() == ISD::Constant &&
9412 cast<ConstantSDNode>(Op1)->isNullValue() &&
9413 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9414 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9415 if (NewSetCC.getNode())
9416 return NewSetCC;
9417 }
9418
9419 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9420 // these.
9421 if (Op1.getOpcode() == ISD::Constant &&
9422 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9423 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9424 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9425
9426 // If the input is a setcc, then reuse the input setcc or use a new one with
9427 // the inverted condition.
9428 if (Op0.getOpcode() == X86ISD::SETCC) {
9429 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9430 bool Invert = (CC == ISD::SETNE) ^
9431 cast<ConstantSDNode>(Op1)->isNullValue();
9432 if (!Invert) return Op0;
9433
9434 CCode = X86::GetOppositeBranchCondition(CCode);
9435 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9436 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9437 }
9438 }
9439
9440 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9441 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9442 if (X86CC == X86::COND_INVALID)
9443 return SDValue();
9444
9445 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9446 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9447 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9448 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9449}
9450
Evan Cheng370e5342008-12-03 08:38:43 +00009451// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009452static bool isX86LogicalCmp(SDValue Op) {
9453 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009454 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9455 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009456 return true;
9457 if (Op.getResNo() == 1 &&
9458 (Opc == X86ISD::ADD ||
9459 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009460 Opc == X86ISD::ADC ||
9461 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009462 Opc == X86ISD::SMUL ||
9463 Opc == X86ISD::UMUL ||
9464 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009465 Opc == X86ISD::DEC ||
9466 Opc == X86ISD::OR ||
9467 Opc == X86ISD::XOR ||
9468 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009469 return true;
9470
Chris Lattner9637d5b2010-12-05 07:49:54 +00009471 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9472 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009473
Dan Gohman076aee32009-03-04 19:44:21 +00009474 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009475}
9476
Chris Lattnera2b56002010-12-05 01:23:24 +00009477static bool isZero(SDValue V) {
9478 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9479 return C && C->isNullValue();
9480}
9481
Evan Chengb64dd5f2012-08-07 22:21:00 +00009482static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9483 if (V.getOpcode() != ISD::TRUNCATE)
9484 return false;
9485
9486 SDValue VOp0 = V.getOperand(0);
9487 unsigned InBits = VOp0.getValueSizeInBits();
9488 unsigned Bits = V.getValueSizeInBits();
9489 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9490}
9491
Dan Gohmand858e902010-04-17 15:26:15 +00009492SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009493 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009494 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009495 SDValue Op1 = Op.getOperand(1);
9496 SDValue Op2 = Op.getOperand(2);
9497 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009498 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009499
Dan Gohman1a492952009-10-20 16:22:37 +00009500 if (Cond.getOpcode() == ISD::SETCC) {
9501 SDValue NewCond = LowerSETCC(Cond, DAG);
9502 if (NewCond.getNode())
9503 Cond = NewCond;
9504 }
Evan Cheng734503b2006-09-11 02:19:56 +00009505
Chris Lattnera2b56002010-12-05 01:23:24 +00009506 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009507 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009508 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009509 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009510 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009511 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9512 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009513 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009514
Chris Lattnera2b56002010-12-05 01:23:24 +00009515 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009516
9517 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009518 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9519 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009520
9521 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009522 // Apply further optimizations for special cases
9523 // (select (x != 0), -1, 0) -> neg & sbb
9524 // (select (x == 0), 0, -1) -> neg & sbb
9525 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009526 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009527 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9528 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009529 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9530 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009531 CmpOp0);
9532 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9533 DAG.getConstant(X86::COND_B, MVT::i8),
9534 SDValue(Neg.getNode(), 1));
9535 return Res;
9536 }
9537
Chris Lattnera2b56002010-12-05 01:23:24 +00009538 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9539 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009540 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009541
Chris Lattner96908b12010-12-05 02:00:51 +00009542 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009543 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9544 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009545
Chris Lattner96908b12010-12-05 02:00:51 +00009546 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9547 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009548
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009549 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009550 if (N2C == 0 || !N2C->isNullValue())
9551 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9552 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009553 }
9554 }
9555
Chris Lattnera2b56002010-12-05 01:23:24 +00009556 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009557 if (Cond.getOpcode() == ISD::AND &&
9558 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9559 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009560 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009561 Cond = Cond.getOperand(0);
9562 }
9563
Evan Cheng3f41d662007-10-08 22:16:29 +00009564 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9565 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009566 unsigned CondOpcode = Cond.getOpcode();
9567 if (CondOpcode == X86ISD::SETCC ||
9568 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009569 CC = Cond.getOperand(0);
9570
Dan Gohman475871a2008-07-27 21:46:04 +00009571 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009572 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009573 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009574
Evan Cheng3f41d662007-10-08 22:16:29 +00009575 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009576 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009577 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009578 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009579
Chris Lattnerd1980a52009-03-12 06:52:53 +00009580 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9581 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009582 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009583 addTest = false;
9584 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009585 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9586 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9587 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9588 Cond.getOperand(0).getValueType() != MVT::i8)) {
9589 SDValue LHS = Cond.getOperand(0);
9590 SDValue RHS = Cond.getOperand(1);
9591 unsigned X86Opcode;
9592 unsigned X86Cond;
9593 SDVTList VTs;
9594 switch (CondOpcode) {
9595 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9596 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9597 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9598 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9599 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9600 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9601 default: llvm_unreachable("unexpected overflowing operator");
9602 }
9603 if (CondOpcode == ISD::UMULO)
9604 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9605 MVT::i32);
9606 else
9607 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9608
9609 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9610
9611 if (CondOpcode == ISD::UMULO)
9612 Cond = X86Op.getValue(2);
9613 else
9614 Cond = X86Op.getValue(1);
9615
9616 CC = DAG.getConstant(X86Cond, MVT::i8);
9617 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009618 }
9619
9620 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009621 // Look pass the truncate if the high bits are known zero.
9622 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9623 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009624
9625 // We know the result of AND is compared against zero. Try to match
9626 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009627 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009628 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009629 if (NewSetCC.getNode()) {
9630 CC = NewSetCC.getOperand(0);
9631 Cond = NewSetCC.getOperand(1);
9632 addTest = false;
9633 }
9634 }
9635 }
9636
9637 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009638 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009639 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009640 }
9641
Benjamin Kramere915ff32010-12-22 23:09:28 +00009642 // a < b ? -1 : 0 -> RES = ~setcc_carry
9643 // a < b ? 0 : -1 -> RES = setcc_carry
9644 // a >= b ? -1 : 0 -> RES = setcc_carry
9645 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009646 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009647 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009648 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9649
9650 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9651 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9652 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9653 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9654 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9655 return DAG.getNOT(DL, Res, Res.getValueType());
9656 return Res;
9657 }
9658 }
9659
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009660 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9661 // widen the cmov and push the truncate through. This avoids introducing a new
9662 // branch during isel and doesn't add any extensions.
9663 if (Op.getValueType() == MVT::i8 &&
9664 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9665 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9666 if (T1.getValueType() == T2.getValueType() &&
9667 // Blacklist CopyFromReg to avoid partial register stalls.
9668 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9669 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009670 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009671 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9672 }
9673 }
9674
Evan Cheng0488db92007-09-25 01:57:46 +00009675 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9676 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009677 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009678 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009679 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009680}
9681
Nadav Rotem1a330af2012-12-27 22:47:16 +00009682SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9683 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009684 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009685 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009686 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009687 DebugLoc dl = Op->getDebugLoc();
9688
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009689 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9690 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9691 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009692
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009693 if (Subtarget->hasInt256())
9694 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009695
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009696 // Optimize vectors in AVX mode
9697 // Sign extend v8i16 to v8i32 and
9698 // v4i32 to v4i64
9699 //
9700 // Divide input vector into two parts
9701 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9702 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9703 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009704
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009705 unsigned NumElems = InVT.getVectorNumElements();
9706 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009707
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009708 SmallVector<int,8> ShufMask1(NumElems, -1);
9709 for (unsigned i = 0; i != NumElems/2; ++i)
9710 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009711
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009712 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009713
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009714 SmallVector<int,8> ShufMask2(NumElems, -1);
9715 for (unsigned i = 0; i != NumElems/2; ++i)
9716 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009717
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009718 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009719
Craig Toppera080daf2013-01-20 21:50:27 +00009720 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009721 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009722
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009723 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9724 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009725
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009726 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009727}
9728
Evan Cheng370e5342008-12-03 08:38:43 +00009729// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9730// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9731// from the AND / OR.
9732static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9733 Opc = Op.getOpcode();
9734 if (Opc != ISD::OR && Opc != ISD::AND)
9735 return false;
9736 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9737 Op.getOperand(0).hasOneUse() &&
9738 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9739 Op.getOperand(1).hasOneUse());
9740}
9741
Evan Cheng961d6d42009-02-02 08:19:07 +00009742// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9743// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009744static bool isXor1OfSetCC(SDValue Op) {
9745 if (Op.getOpcode() != ISD::XOR)
9746 return false;
9747 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9748 if (N1C && N1C->getAPIntValue() == 1) {
9749 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9750 Op.getOperand(0).hasOneUse();
9751 }
9752 return false;
9753}
9754
Dan Gohmand858e902010-04-17 15:26:15 +00009755SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009756 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009757 SDValue Chain = Op.getOperand(0);
9758 SDValue Cond = Op.getOperand(1);
9759 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009760 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009761 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009762 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009763
Dan Gohman1a492952009-10-20 16:22:37 +00009764 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009765 // Check for setcc([su]{add,sub,mul}o == 0).
9766 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9767 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9768 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9769 Cond.getOperand(0).getResNo() == 1 &&
9770 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9771 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9772 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9773 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9774 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9775 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9776 Inverted = true;
9777 Cond = Cond.getOperand(0);
9778 } else {
9779 SDValue NewCond = LowerSETCC(Cond, DAG);
9780 if (NewCond.getNode())
9781 Cond = NewCond;
9782 }
Dan Gohman1a492952009-10-20 16:22:37 +00009783 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009784#if 0
9785 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009786 else if (Cond.getOpcode() == X86ISD::ADD ||
9787 Cond.getOpcode() == X86ISD::SUB ||
9788 Cond.getOpcode() == X86ISD::SMUL ||
9789 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009790 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009791#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009792
Evan Chengad9c0a32009-12-15 00:53:42 +00009793 // Look pass (and (setcc_carry (cmp ...)), 1).
9794 if (Cond.getOpcode() == ISD::AND &&
9795 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9796 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009797 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009798 Cond = Cond.getOperand(0);
9799 }
9800
Evan Cheng3f41d662007-10-08 22:16:29 +00009801 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9802 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009803 unsigned CondOpcode = Cond.getOpcode();
9804 if (CondOpcode == X86ISD::SETCC ||
9805 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009806 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009807
Dan Gohman475871a2008-07-27 21:46:04 +00009808 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009809 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009810 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009811 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009812 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009813 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009814 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009815 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009816 default: break;
9817 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009818 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009819 // These can only come from an arithmetic instruction with overflow,
9820 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009821 Cond = Cond.getNode()->getOperand(1);
9822 addTest = false;
9823 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009824 }
Evan Cheng0488db92007-09-25 01:57:46 +00009825 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009826 }
9827 CondOpcode = Cond.getOpcode();
9828 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9829 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9830 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9831 Cond.getOperand(0).getValueType() != MVT::i8)) {
9832 SDValue LHS = Cond.getOperand(0);
9833 SDValue RHS = Cond.getOperand(1);
9834 unsigned X86Opcode;
9835 unsigned X86Cond;
9836 SDVTList VTs;
9837 switch (CondOpcode) {
9838 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9839 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9840 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9841 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9842 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9843 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9844 default: llvm_unreachable("unexpected overflowing operator");
9845 }
9846 if (Inverted)
9847 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9848 if (CondOpcode == ISD::UMULO)
9849 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9850 MVT::i32);
9851 else
9852 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9853
9854 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9855
9856 if (CondOpcode == ISD::UMULO)
9857 Cond = X86Op.getValue(2);
9858 else
9859 Cond = X86Op.getValue(1);
9860
9861 CC = DAG.getConstant(X86Cond, MVT::i8);
9862 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009863 } else {
9864 unsigned CondOpc;
9865 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9866 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009867 if (CondOpc == ISD::OR) {
9868 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9869 // two branches instead of an explicit OR instruction with a
9870 // separate test.
9871 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009872 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009873 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009874 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009875 Chain, Dest, CC, Cmp);
9876 CC = Cond.getOperand(1).getOperand(0);
9877 Cond = Cmp;
9878 addTest = false;
9879 }
9880 } else { // ISD::AND
9881 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9882 // two branches instead of an explicit AND instruction with a
9883 // separate test. However, we only do this if this block doesn't
9884 // have a fall-through edge, because this requires an explicit
9885 // jmp when the condition is false.
9886 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009887 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009888 Op.getNode()->hasOneUse()) {
9889 X86::CondCode CCode =
9890 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9891 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009892 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009893 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009894 // Look for an unconditional branch following this conditional branch.
9895 // We need this because we need to reverse the successors in order
9896 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009897 if (User->getOpcode() == ISD::BR) {
9898 SDValue FalseBB = User->getOperand(1);
9899 SDNode *NewBR =
9900 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009901 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009902 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009903 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009904
Dale Johannesene4d209d2009-02-03 20:21:25 +00009905 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009906 Chain, Dest, CC, Cmp);
9907 X86::CondCode CCode =
9908 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9909 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009910 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009911 Cond = Cmp;
9912 addTest = false;
9913 }
9914 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009915 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009916 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9917 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9918 // It should be transformed during dag combiner except when the condition
9919 // is set by a arithmetics with overflow node.
9920 X86::CondCode CCode =
9921 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9922 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009923 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009924 Cond = Cond.getOperand(0).getOperand(1);
9925 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009926 } else if (Cond.getOpcode() == ISD::SETCC &&
9927 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9928 // For FCMP_OEQ, we can emit
9929 // two branches instead of an explicit AND instruction with a
9930 // separate test. However, we only do this if this block doesn't
9931 // have a fall-through edge, because this requires an explicit
9932 // jmp when the condition is false.
9933 if (Op.getNode()->hasOneUse()) {
9934 SDNode *User = *Op.getNode()->use_begin();
9935 // Look for an unconditional branch following this conditional branch.
9936 // We need this because we need to reverse the successors in order
9937 // to implement FCMP_OEQ.
9938 if (User->getOpcode() == ISD::BR) {
9939 SDValue FalseBB = User->getOperand(1);
9940 SDNode *NewBR =
9941 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9942 assert(NewBR == User);
9943 (void)NewBR;
9944 Dest = FalseBB;
9945
9946 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9947 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009948 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009949 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9950 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9951 Chain, Dest, CC, Cmp);
9952 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9953 Cond = Cmp;
9954 addTest = false;
9955 }
9956 }
9957 } else if (Cond.getOpcode() == ISD::SETCC &&
9958 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9959 // For FCMP_UNE, we can emit
9960 // two branches instead of an explicit AND instruction with a
9961 // separate test. However, we only do this if this block doesn't
9962 // have a fall-through edge, because this requires an explicit
9963 // jmp when the condition is false.
9964 if (Op.getNode()->hasOneUse()) {
9965 SDNode *User = *Op.getNode()->use_begin();
9966 // Look for an unconditional branch following this conditional branch.
9967 // We need this because we need to reverse the successors in order
9968 // to implement FCMP_UNE.
9969 if (User->getOpcode() == ISD::BR) {
9970 SDValue FalseBB = User->getOperand(1);
9971 SDNode *NewBR =
9972 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9973 assert(NewBR == User);
9974 (void)NewBR;
9975
9976 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9977 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009978 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009979 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9980 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9981 Chain, Dest, CC, Cmp);
9982 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9983 Cond = Cmp;
9984 addTest = false;
9985 Dest = FalseBB;
9986 }
9987 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009988 }
Evan Cheng0488db92007-09-25 01:57:46 +00009989 }
9990
9991 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009992 // Look pass the truncate if the high bits are known zero.
9993 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9994 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009995
9996 // We know the result of AND is compared against zero. Try to match
9997 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009998 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009999 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
10000 if (NewSetCC.getNode()) {
10001 CC = NewSetCC.getOperand(0);
10002 Cond = NewSetCC.getOperand(1);
10003 addTest = false;
10004 }
10005 }
10006 }
10007
10008 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010009 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +000010010 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +000010011 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010012 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010013 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010014 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010015}
10016
Anton Korobeynikove060b532007-04-17 19:34:00 +000010017// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10018// Calls to _alloca is needed to probe the stack when allocating more than 4k
10019// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10020// that the guard pages used by the OS virtual memory manager are allocated in
10021// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010022SDValue
10023X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010024 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010025 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010026 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010027 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010028 "are being used");
10029 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010030 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010031
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010032 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010033 SDValue Chain = Op.getOperand(0);
10034 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010035 // FIXME: Ensure alignment here
10036
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010037 bool Is64Bit = Subtarget->is64Bit();
10038 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010039
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010040 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010041 MachineFunction &MF = DAG.getMachineFunction();
10042 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010043
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010044 if (Is64Bit) {
10045 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010046 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010047 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010048
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010049 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010050 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010051 if (I->hasNestAttr())
10052 report_fatal_error("Cannot use segmented stacks with functions that "
10053 "have nested arguments.");
10054 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010055
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010056 const TargetRegisterClass *AddrRegClass =
10057 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10058 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10059 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10060 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10061 DAG.getRegister(Vreg, SPTy));
10062 SDValue Ops1[2] = { Value, Chain };
10063 return DAG.getMergeValues(Ops1, 2, dl);
10064 } else {
10065 SDValue Flag;
10066 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010067
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010068 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10069 Flag = Chain.getValue(1);
10070 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010071
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010072 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10073 Flag = Chain.getValue(1);
10074
Michael Liaoc5c970e2012-10-31 04:14:09 +000010075 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10076 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010077
10078 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10079 return DAG.getMergeValues(Ops1, 2, dl);
10080 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010081}
10082
Dan Gohmand858e902010-04-17 15:26:15 +000010083SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010084 MachineFunction &MF = DAG.getMachineFunction();
10085 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10086
Dan Gohman69de1932008-02-06 22:27:42 +000010087 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010088 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010089
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010090 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010091 // vastart just stores the address of the VarArgsFrameIndex slot into the
10092 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010093 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10094 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010095 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10096 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010097 }
10098
10099 // __va_list_tag:
10100 // gp_offset (0 - 6 * 8)
10101 // fp_offset (48 - 48 + 8 * 16)
10102 // overflow_arg_area (point to parameters coming in memory).
10103 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010104 SmallVector<SDValue, 8> MemOps;
10105 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010106 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010107 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010108 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10109 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010110 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010111 MemOps.push_back(Store);
10112
10113 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010114 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010115 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010116 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010117 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10118 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010119 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010120 MemOps.push_back(Store);
10121
10122 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010123 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010124 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010125 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10126 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010127 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10128 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010129 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010130 MemOps.push_back(Store);
10131
10132 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010133 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010134 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010135 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10136 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010137 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10138 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010139 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010140 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010141 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010142}
10143
Dan Gohmand858e902010-04-17 15:26:15 +000010144SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010145 assert(Subtarget->is64Bit() &&
10146 "LowerVAARG only handles 64-bit va_arg!");
10147 assert((Subtarget->isTargetLinux() ||
10148 Subtarget->isTargetDarwin()) &&
10149 "Unhandled target in LowerVAARG");
10150 assert(Op.getNode()->getNumOperands() == 4);
10151 SDValue Chain = Op.getOperand(0);
10152 SDValue SrcPtr = Op.getOperand(1);
10153 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10154 unsigned Align = Op.getConstantOperandVal(3);
10155 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010156
Dan Gohman320afb82010-10-12 18:00:49 +000010157 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010158 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010159 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010160 uint8_t ArgMode;
10161
10162 // Decide which area this value should be read from.
10163 // TODO: Implement the AMD64 ABI in its entirety. This simple
10164 // selection mechanism works only for the basic types.
10165 if (ArgVT == MVT::f80) {
10166 llvm_unreachable("va_arg for f80 not yet implemented");
10167 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10168 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10169 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10170 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10171 } else {
10172 llvm_unreachable("Unhandled argument type in LowerVAARG");
10173 }
10174
10175 if (ArgMode == 2) {
10176 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010177 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010178 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010179 .getFunction()->getAttributes()
10180 .hasAttribute(AttributeSet::FunctionIndex,
10181 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010182 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010183 }
10184
10185 // Insert VAARG_64 node into the DAG
10186 // VAARG_64 returns two values: Variable Argument Address, Chain
10187 SmallVector<SDValue, 11> InstOps;
10188 InstOps.push_back(Chain);
10189 InstOps.push_back(SrcPtr);
10190 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10191 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10192 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10193 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10194 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10195 VTs, &InstOps[0], InstOps.size(),
10196 MVT::i64,
10197 MachinePointerInfo(SV),
10198 /*Align=*/0,
10199 /*Volatile=*/false,
10200 /*ReadMem=*/true,
10201 /*WriteMem=*/true);
10202 Chain = VAARG.getValue(1);
10203
10204 // Load the next argument and return it
10205 return DAG.getLoad(ArgVT, dl,
10206 Chain,
10207 VAARG,
10208 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010209 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010210}
10211
Craig Topper55b24052012-09-11 06:15:32 +000010212static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10213 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010214 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010215 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010216 SDValue Chain = Op.getOperand(0);
10217 SDValue DstPtr = Op.getOperand(1);
10218 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010219 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10220 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010221 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010222
Chris Lattnere72f2022010-09-21 05:40:29 +000010223 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010224 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010225 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010226 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010227}
10228
Craig Topperff3139f2013-02-19 07:43:59 +000010229// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010230// may or may not be a constant. Takes immediate version of shift as input.
10231static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10232 SDValue SrcOp, SDValue ShAmt,
10233 SelectionDAG &DAG) {
10234 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10235
10236 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010237 // Constant may be a TargetConstant. Use a regular constant.
10238 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010239 switch (Opc) {
10240 default: llvm_unreachable("Unknown target vector shift node");
10241 case X86ISD::VSHLI:
10242 case X86ISD::VSRLI:
10243 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010244 return DAG.getNode(Opc, dl, VT, SrcOp,
10245 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010246 }
10247 }
10248
10249 // Change opcode to non-immediate version
10250 switch (Opc) {
10251 default: llvm_unreachable("Unknown target vector shift node");
10252 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10253 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10254 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10255 }
10256
10257 // Need to build a vector containing shift amount
10258 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10259 SDValue ShOps[4];
10260 ShOps[0] = ShAmt;
10261 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010262 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010263 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010264
10265 // The return type has to be a 128-bit type with the same element
10266 // type as the input type.
10267 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10268 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10269
10270 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010271 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10272}
10273
Craig Topper55b24052012-09-11 06:15:32 +000010274static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010275 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010276 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010277 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010278 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010279 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010280 case Intrinsic::x86_sse_comieq_ss:
10281 case Intrinsic::x86_sse_comilt_ss:
10282 case Intrinsic::x86_sse_comile_ss:
10283 case Intrinsic::x86_sse_comigt_ss:
10284 case Intrinsic::x86_sse_comige_ss:
10285 case Intrinsic::x86_sse_comineq_ss:
10286 case Intrinsic::x86_sse_ucomieq_ss:
10287 case Intrinsic::x86_sse_ucomilt_ss:
10288 case Intrinsic::x86_sse_ucomile_ss:
10289 case Intrinsic::x86_sse_ucomigt_ss:
10290 case Intrinsic::x86_sse_ucomige_ss:
10291 case Intrinsic::x86_sse_ucomineq_ss:
10292 case Intrinsic::x86_sse2_comieq_sd:
10293 case Intrinsic::x86_sse2_comilt_sd:
10294 case Intrinsic::x86_sse2_comile_sd:
10295 case Intrinsic::x86_sse2_comigt_sd:
10296 case Intrinsic::x86_sse2_comige_sd:
10297 case Intrinsic::x86_sse2_comineq_sd:
10298 case Intrinsic::x86_sse2_ucomieq_sd:
10299 case Intrinsic::x86_sse2_ucomilt_sd:
10300 case Intrinsic::x86_sse2_ucomile_sd:
10301 case Intrinsic::x86_sse2_ucomigt_sd:
10302 case Intrinsic::x86_sse2_ucomige_sd:
10303 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010304 unsigned Opc;
10305 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010306 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010307 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010308 case Intrinsic::x86_sse_comieq_ss:
10309 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010310 Opc = X86ISD::COMI;
10311 CC = ISD::SETEQ;
10312 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010313 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010314 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010315 Opc = X86ISD::COMI;
10316 CC = ISD::SETLT;
10317 break;
10318 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010319 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010320 Opc = X86ISD::COMI;
10321 CC = ISD::SETLE;
10322 break;
10323 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010324 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010325 Opc = X86ISD::COMI;
10326 CC = ISD::SETGT;
10327 break;
10328 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010329 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010330 Opc = X86ISD::COMI;
10331 CC = ISD::SETGE;
10332 break;
10333 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010334 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010335 Opc = X86ISD::COMI;
10336 CC = ISD::SETNE;
10337 break;
10338 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010339 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010340 Opc = X86ISD::UCOMI;
10341 CC = ISD::SETEQ;
10342 break;
10343 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010344 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010345 Opc = X86ISD::UCOMI;
10346 CC = ISD::SETLT;
10347 break;
10348 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010349 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010350 Opc = X86ISD::UCOMI;
10351 CC = ISD::SETLE;
10352 break;
10353 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010354 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010355 Opc = X86ISD::UCOMI;
10356 CC = ISD::SETGT;
10357 break;
10358 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010359 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010360 Opc = X86ISD::UCOMI;
10361 CC = ISD::SETGE;
10362 break;
10363 case Intrinsic::x86_sse_ucomineq_ss:
10364 case Intrinsic::x86_sse2_ucomineq_sd:
10365 Opc = X86ISD::UCOMI;
10366 CC = ISD::SETNE;
10367 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010368 }
Evan Cheng734503b2006-09-11 02:19:56 +000010369
Dan Gohman475871a2008-07-27 21:46:04 +000010370 SDValue LHS = Op.getOperand(1);
10371 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010372 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010373 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010374 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10375 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10376 DAG.getConstant(X86CC, MVT::i8), Cond);
10377 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010378 }
Craig Topper6d688152012-08-14 07:43:25 +000010379
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010380 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010381 case Intrinsic::x86_sse2_pmulu_dq:
10382 case Intrinsic::x86_avx2_pmulu_dq:
10383 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10384 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010385
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010386 // SSE2/AVX2 sub with unsigned saturation intrinsics
10387 case Intrinsic::x86_sse2_psubus_b:
10388 case Intrinsic::x86_sse2_psubus_w:
10389 case Intrinsic::x86_avx2_psubus_b:
10390 case Intrinsic::x86_avx2_psubus_w:
10391 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10392 Op.getOperand(1), Op.getOperand(2));
10393
Craig Topper6d688152012-08-14 07:43:25 +000010394 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010395 case Intrinsic::x86_sse3_hadd_ps:
10396 case Intrinsic::x86_sse3_hadd_pd:
10397 case Intrinsic::x86_avx_hadd_ps_256:
10398 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010399 case Intrinsic::x86_sse3_hsub_ps:
10400 case Intrinsic::x86_sse3_hsub_pd:
10401 case Intrinsic::x86_avx_hsub_ps_256:
10402 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010403 case Intrinsic::x86_ssse3_phadd_w_128:
10404 case Intrinsic::x86_ssse3_phadd_d_128:
10405 case Intrinsic::x86_avx2_phadd_w:
10406 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010407 case Intrinsic::x86_ssse3_phsub_w_128:
10408 case Intrinsic::x86_ssse3_phsub_d_128:
10409 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010410 case Intrinsic::x86_avx2_phsub_d: {
10411 unsigned Opcode;
10412 switch (IntNo) {
10413 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10414 case Intrinsic::x86_sse3_hadd_ps:
10415 case Intrinsic::x86_sse3_hadd_pd:
10416 case Intrinsic::x86_avx_hadd_ps_256:
10417 case Intrinsic::x86_avx_hadd_pd_256:
10418 Opcode = X86ISD::FHADD;
10419 break;
10420 case Intrinsic::x86_sse3_hsub_ps:
10421 case Intrinsic::x86_sse3_hsub_pd:
10422 case Intrinsic::x86_avx_hsub_ps_256:
10423 case Intrinsic::x86_avx_hsub_pd_256:
10424 Opcode = X86ISD::FHSUB;
10425 break;
10426 case Intrinsic::x86_ssse3_phadd_w_128:
10427 case Intrinsic::x86_ssse3_phadd_d_128:
10428 case Intrinsic::x86_avx2_phadd_w:
10429 case Intrinsic::x86_avx2_phadd_d:
10430 Opcode = X86ISD::HADD;
10431 break;
10432 case Intrinsic::x86_ssse3_phsub_w_128:
10433 case Intrinsic::x86_ssse3_phsub_d_128:
10434 case Intrinsic::x86_avx2_phsub_w:
10435 case Intrinsic::x86_avx2_phsub_d:
10436 Opcode = X86ISD::HSUB;
10437 break;
10438 }
10439 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010440 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010441 }
10442
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010443 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10444 case Intrinsic::x86_sse2_pmaxu_b:
10445 case Intrinsic::x86_sse41_pmaxuw:
10446 case Intrinsic::x86_sse41_pmaxud:
10447 case Intrinsic::x86_avx2_pmaxu_b:
10448 case Intrinsic::x86_avx2_pmaxu_w:
10449 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010450 case Intrinsic::x86_sse2_pminu_b:
10451 case Intrinsic::x86_sse41_pminuw:
10452 case Intrinsic::x86_sse41_pminud:
10453 case Intrinsic::x86_avx2_pminu_b:
10454 case Intrinsic::x86_avx2_pminu_w:
10455 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010456 case Intrinsic::x86_sse41_pmaxsb:
10457 case Intrinsic::x86_sse2_pmaxs_w:
10458 case Intrinsic::x86_sse41_pmaxsd:
10459 case Intrinsic::x86_avx2_pmaxs_b:
10460 case Intrinsic::x86_avx2_pmaxs_w:
10461 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010462 case Intrinsic::x86_sse41_pminsb:
10463 case Intrinsic::x86_sse2_pmins_w:
10464 case Intrinsic::x86_sse41_pminsd:
10465 case Intrinsic::x86_avx2_pmins_b:
10466 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010467 case Intrinsic::x86_avx2_pmins_d: {
10468 unsigned Opcode;
10469 switch (IntNo) {
10470 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10471 case Intrinsic::x86_sse2_pmaxu_b:
10472 case Intrinsic::x86_sse41_pmaxuw:
10473 case Intrinsic::x86_sse41_pmaxud:
10474 case Intrinsic::x86_avx2_pmaxu_b:
10475 case Intrinsic::x86_avx2_pmaxu_w:
10476 case Intrinsic::x86_avx2_pmaxu_d:
10477 Opcode = X86ISD::UMAX;
10478 break;
10479 case Intrinsic::x86_sse2_pminu_b:
10480 case Intrinsic::x86_sse41_pminuw:
10481 case Intrinsic::x86_sse41_pminud:
10482 case Intrinsic::x86_avx2_pminu_b:
10483 case Intrinsic::x86_avx2_pminu_w:
10484 case Intrinsic::x86_avx2_pminu_d:
10485 Opcode = X86ISD::UMIN;
10486 break;
10487 case Intrinsic::x86_sse41_pmaxsb:
10488 case Intrinsic::x86_sse2_pmaxs_w:
10489 case Intrinsic::x86_sse41_pmaxsd:
10490 case Intrinsic::x86_avx2_pmaxs_b:
10491 case Intrinsic::x86_avx2_pmaxs_w:
10492 case Intrinsic::x86_avx2_pmaxs_d:
10493 Opcode = X86ISD::SMAX;
10494 break;
10495 case Intrinsic::x86_sse41_pminsb:
10496 case Intrinsic::x86_sse2_pmins_w:
10497 case Intrinsic::x86_sse41_pminsd:
10498 case Intrinsic::x86_avx2_pmins_b:
10499 case Intrinsic::x86_avx2_pmins_w:
10500 case Intrinsic::x86_avx2_pmins_d:
10501 Opcode = X86ISD::SMIN;
10502 break;
10503 }
10504 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010505 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010506 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010507
Craig Topper6d183e42012-12-29 16:44:25 +000010508 // SSE/SSE2/AVX floating point max/min intrinsics.
10509 case Intrinsic::x86_sse_max_ps:
10510 case Intrinsic::x86_sse2_max_pd:
10511 case Intrinsic::x86_avx_max_ps_256:
10512 case Intrinsic::x86_avx_max_pd_256:
10513 case Intrinsic::x86_sse_min_ps:
10514 case Intrinsic::x86_sse2_min_pd:
10515 case Intrinsic::x86_avx_min_ps_256:
10516 case Intrinsic::x86_avx_min_pd_256: {
10517 unsigned Opcode;
10518 switch (IntNo) {
10519 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10520 case Intrinsic::x86_sse_max_ps:
10521 case Intrinsic::x86_sse2_max_pd:
10522 case Intrinsic::x86_avx_max_ps_256:
10523 case Intrinsic::x86_avx_max_pd_256:
10524 Opcode = X86ISD::FMAX;
10525 break;
10526 case Intrinsic::x86_sse_min_ps:
10527 case Intrinsic::x86_sse2_min_pd:
10528 case Intrinsic::x86_avx_min_ps_256:
10529 case Intrinsic::x86_avx_min_pd_256:
10530 Opcode = X86ISD::FMIN;
10531 break;
10532 }
10533 return DAG.getNode(Opcode, dl, Op.getValueType(),
10534 Op.getOperand(1), Op.getOperand(2));
10535 }
10536
Craig Topper6d688152012-08-14 07:43:25 +000010537 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010538 case Intrinsic::x86_avx2_psllv_d:
10539 case Intrinsic::x86_avx2_psllv_q:
10540 case Intrinsic::x86_avx2_psllv_d_256:
10541 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010542 case Intrinsic::x86_avx2_psrlv_d:
10543 case Intrinsic::x86_avx2_psrlv_q:
10544 case Intrinsic::x86_avx2_psrlv_d_256:
10545 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010546 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010547 case Intrinsic::x86_avx2_psrav_d_256: {
10548 unsigned Opcode;
10549 switch (IntNo) {
10550 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10551 case Intrinsic::x86_avx2_psllv_d:
10552 case Intrinsic::x86_avx2_psllv_q:
10553 case Intrinsic::x86_avx2_psllv_d_256:
10554 case Intrinsic::x86_avx2_psllv_q_256:
10555 Opcode = ISD::SHL;
10556 break;
10557 case Intrinsic::x86_avx2_psrlv_d:
10558 case Intrinsic::x86_avx2_psrlv_q:
10559 case Intrinsic::x86_avx2_psrlv_d_256:
10560 case Intrinsic::x86_avx2_psrlv_q_256:
10561 Opcode = ISD::SRL;
10562 break;
10563 case Intrinsic::x86_avx2_psrav_d:
10564 case Intrinsic::x86_avx2_psrav_d_256:
10565 Opcode = ISD::SRA;
10566 break;
10567 }
10568 return DAG.getNode(Opcode, dl, Op.getValueType(),
10569 Op.getOperand(1), Op.getOperand(2));
10570 }
10571
Craig Topper969ba282012-01-25 06:43:11 +000010572 case Intrinsic::x86_ssse3_pshuf_b_128:
10573 case Intrinsic::x86_avx2_pshuf_b:
10574 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10575 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010576
Craig Topper969ba282012-01-25 06:43:11 +000010577 case Intrinsic::x86_ssse3_psign_b_128:
10578 case Intrinsic::x86_ssse3_psign_w_128:
10579 case Intrinsic::x86_ssse3_psign_d_128:
10580 case Intrinsic::x86_avx2_psign_b:
10581 case Intrinsic::x86_avx2_psign_w:
10582 case Intrinsic::x86_avx2_psign_d:
10583 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10584 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010585
Craig Toppere566cd02012-01-26 07:18:03 +000010586 case Intrinsic::x86_sse41_insertps:
10587 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10588 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010589
Craig Toppere566cd02012-01-26 07:18:03 +000010590 case Intrinsic::x86_avx_vperm2f128_ps_256:
10591 case Intrinsic::x86_avx_vperm2f128_pd_256:
10592 case Intrinsic::x86_avx_vperm2f128_si_256:
10593 case Intrinsic::x86_avx2_vperm2i128:
10594 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10595 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010596
Craig Topperffa6c402012-04-16 07:13:00 +000010597 case Intrinsic::x86_avx2_permd:
10598 case Intrinsic::x86_avx2_permps:
10599 // Operands intentionally swapped. Mask is last operand to intrinsic,
10600 // but second operand for node/intruction.
10601 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10602 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010603
Craig Topper22d8f0d2012-12-29 18:18:20 +000010604 case Intrinsic::x86_sse_sqrt_ps:
10605 case Intrinsic::x86_sse2_sqrt_pd:
10606 case Intrinsic::x86_avx_sqrt_ps_256:
10607 case Intrinsic::x86_avx_sqrt_pd_256:
10608 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10609
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010610 // ptest and testp intrinsics. The intrinsic these come from are designed to
10611 // return an integer value, not just an instruction so lower it to the ptest
10612 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010613 case Intrinsic::x86_sse41_ptestz:
10614 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010615 case Intrinsic::x86_sse41_ptestnzc:
10616 case Intrinsic::x86_avx_ptestz_256:
10617 case Intrinsic::x86_avx_ptestc_256:
10618 case Intrinsic::x86_avx_ptestnzc_256:
10619 case Intrinsic::x86_avx_vtestz_ps:
10620 case Intrinsic::x86_avx_vtestc_ps:
10621 case Intrinsic::x86_avx_vtestnzc_ps:
10622 case Intrinsic::x86_avx_vtestz_pd:
10623 case Intrinsic::x86_avx_vtestc_pd:
10624 case Intrinsic::x86_avx_vtestnzc_pd:
10625 case Intrinsic::x86_avx_vtestz_ps_256:
10626 case Intrinsic::x86_avx_vtestc_ps_256:
10627 case Intrinsic::x86_avx_vtestnzc_ps_256:
10628 case Intrinsic::x86_avx_vtestz_pd_256:
10629 case Intrinsic::x86_avx_vtestc_pd_256:
10630 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10631 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010632 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010633 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010634 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010635 case Intrinsic::x86_avx_vtestz_ps:
10636 case Intrinsic::x86_avx_vtestz_pd:
10637 case Intrinsic::x86_avx_vtestz_ps_256:
10638 case Intrinsic::x86_avx_vtestz_pd_256:
10639 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010640 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010641 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010642 // ZF = 1
10643 X86CC = X86::COND_E;
10644 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010645 case Intrinsic::x86_avx_vtestc_ps:
10646 case Intrinsic::x86_avx_vtestc_pd:
10647 case Intrinsic::x86_avx_vtestc_ps_256:
10648 case Intrinsic::x86_avx_vtestc_pd_256:
10649 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010650 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010651 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010652 // CF = 1
10653 X86CC = X86::COND_B;
10654 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010655 case Intrinsic::x86_avx_vtestnzc_ps:
10656 case Intrinsic::x86_avx_vtestnzc_pd:
10657 case Intrinsic::x86_avx_vtestnzc_ps_256:
10658 case Intrinsic::x86_avx_vtestnzc_pd_256:
10659 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010660 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010661 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010662 // ZF and CF = 0
10663 X86CC = X86::COND_A;
10664 break;
10665 }
Eric Christopherfd179292009-08-27 18:07:15 +000010666
Eric Christopher71c67532009-07-29 00:28:05 +000010667 SDValue LHS = Op.getOperand(1);
10668 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010669 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10670 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010671 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10672 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10673 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010674 }
Evan Cheng5759f972008-05-04 09:15:50 +000010675
Craig Topper80e46362012-01-23 06:16:53 +000010676 // SSE/AVX shift intrinsics
10677 case Intrinsic::x86_sse2_psll_w:
10678 case Intrinsic::x86_sse2_psll_d:
10679 case Intrinsic::x86_sse2_psll_q:
10680 case Intrinsic::x86_avx2_psll_w:
10681 case Intrinsic::x86_avx2_psll_d:
10682 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010683 case Intrinsic::x86_sse2_psrl_w:
10684 case Intrinsic::x86_sse2_psrl_d:
10685 case Intrinsic::x86_sse2_psrl_q:
10686 case Intrinsic::x86_avx2_psrl_w:
10687 case Intrinsic::x86_avx2_psrl_d:
10688 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010689 case Intrinsic::x86_sse2_psra_w:
10690 case Intrinsic::x86_sse2_psra_d:
10691 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010692 case Intrinsic::x86_avx2_psra_d: {
10693 unsigned Opcode;
10694 switch (IntNo) {
10695 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10696 case Intrinsic::x86_sse2_psll_w:
10697 case Intrinsic::x86_sse2_psll_d:
10698 case Intrinsic::x86_sse2_psll_q:
10699 case Intrinsic::x86_avx2_psll_w:
10700 case Intrinsic::x86_avx2_psll_d:
10701 case Intrinsic::x86_avx2_psll_q:
10702 Opcode = X86ISD::VSHL;
10703 break;
10704 case Intrinsic::x86_sse2_psrl_w:
10705 case Intrinsic::x86_sse2_psrl_d:
10706 case Intrinsic::x86_sse2_psrl_q:
10707 case Intrinsic::x86_avx2_psrl_w:
10708 case Intrinsic::x86_avx2_psrl_d:
10709 case Intrinsic::x86_avx2_psrl_q:
10710 Opcode = X86ISD::VSRL;
10711 break;
10712 case Intrinsic::x86_sse2_psra_w:
10713 case Intrinsic::x86_sse2_psra_d:
10714 case Intrinsic::x86_avx2_psra_w:
10715 case Intrinsic::x86_avx2_psra_d:
10716 Opcode = X86ISD::VSRA;
10717 break;
10718 }
10719 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010720 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010721 }
10722
10723 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010724 case Intrinsic::x86_sse2_pslli_w:
10725 case Intrinsic::x86_sse2_pslli_d:
10726 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010727 case Intrinsic::x86_avx2_pslli_w:
10728 case Intrinsic::x86_avx2_pslli_d:
10729 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010730 case Intrinsic::x86_sse2_psrli_w:
10731 case Intrinsic::x86_sse2_psrli_d:
10732 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010733 case Intrinsic::x86_avx2_psrli_w:
10734 case Intrinsic::x86_avx2_psrli_d:
10735 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010736 case Intrinsic::x86_sse2_psrai_w:
10737 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010738 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010739 case Intrinsic::x86_avx2_psrai_d: {
10740 unsigned Opcode;
10741 switch (IntNo) {
10742 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10743 case Intrinsic::x86_sse2_pslli_w:
10744 case Intrinsic::x86_sse2_pslli_d:
10745 case Intrinsic::x86_sse2_pslli_q:
10746 case Intrinsic::x86_avx2_pslli_w:
10747 case Intrinsic::x86_avx2_pslli_d:
10748 case Intrinsic::x86_avx2_pslli_q:
10749 Opcode = X86ISD::VSHLI;
10750 break;
10751 case Intrinsic::x86_sse2_psrli_w:
10752 case Intrinsic::x86_sse2_psrli_d:
10753 case Intrinsic::x86_sse2_psrli_q:
10754 case Intrinsic::x86_avx2_psrli_w:
10755 case Intrinsic::x86_avx2_psrli_d:
10756 case Intrinsic::x86_avx2_psrli_q:
10757 Opcode = X86ISD::VSRLI;
10758 break;
10759 case Intrinsic::x86_sse2_psrai_w:
10760 case Intrinsic::x86_sse2_psrai_d:
10761 case Intrinsic::x86_avx2_psrai_w:
10762 case Intrinsic::x86_avx2_psrai_d:
10763 Opcode = X86ISD::VSRAI;
10764 break;
10765 }
10766 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010767 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010768 }
10769
Craig Topper4feb6472012-08-06 06:22:36 +000010770 case Intrinsic::x86_sse42_pcmpistria128:
10771 case Intrinsic::x86_sse42_pcmpestria128:
10772 case Intrinsic::x86_sse42_pcmpistric128:
10773 case Intrinsic::x86_sse42_pcmpestric128:
10774 case Intrinsic::x86_sse42_pcmpistrio128:
10775 case Intrinsic::x86_sse42_pcmpestrio128:
10776 case Intrinsic::x86_sse42_pcmpistris128:
10777 case Intrinsic::x86_sse42_pcmpestris128:
10778 case Intrinsic::x86_sse42_pcmpistriz128:
10779 case Intrinsic::x86_sse42_pcmpestriz128: {
10780 unsigned Opcode;
10781 unsigned X86CC;
10782 switch (IntNo) {
10783 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10784 case Intrinsic::x86_sse42_pcmpistria128:
10785 Opcode = X86ISD::PCMPISTRI;
10786 X86CC = X86::COND_A;
10787 break;
10788 case Intrinsic::x86_sse42_pcmpestria128:
10789 Opcode = X86ISD::PCMPESTRI;
10790 X86CC = X86::COND_A;
10791 break;
10792 case Intrinsic::x86_sse42_pcmpistric128:
10793 Opcode = X86ISD::PCMPISTRI;
10794 X86CC = X86::COND_B;
10795 break;
10796 case Intrinsic::x86_sse42_pcmpestric128:
10797 Opcode = X86ISD::PCMPESTRI;
10798 X86CC = X86::COND_B;
10799 break;
10800 case Intrinsic::x86_sse42_pcmpistrio128:
10801 Opcode = X86ISD::PCMPISTRI;
10802 X86CC = X86::COND_O;
10803 break;
10804 case Intrinsic::x86_sse42_pcmpestrio128:
10805 Opcode = X86ISD::PCMPESTRI;
10806 X86CC = X86::COND_O;
10807 break;
10808 case Intrinsic::x86_sse42_pcmpistris128:
10809 Opcode = X86ISD::PCMPISTRI;
10810 X86CC = X86::COND_S;
10811 break;
10812 case Intrinsic::x86_sse42_pcmpestris128:
10813 Opcode = X86ISD::PCMPESTRI;
10814 X86CC = X86::COND_S;
10815 break;
10816 case Intrinsic::x86_sse42_pcmpistriz128:
10817 Opcode = X86ISD::PCMPISTRI;
10818 X86CC = X86::COND_E;
10819 break;
10820 case Intrinsic::x86_sse42_pcmpestriz128:
10821 Opcode = X86ISD::PCMPESTRI;
10822 X86CC = X86::COND_E;
10823 break;
10824 }
10825 SmallVector<SDValue, 5> NewOps;
10826 NewOps.append(Op->op_begin()+1, Op->op_end());
10827 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10828 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10829 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10830 DAG.getConstant(X86CC, MVT::i8),
10831 SDValue(PCMP.getNode(), 1));
10832 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10833 }
Craig Topper6d688152012-08-14 07:43:25 +000010834
Craig Topper4feb6472012-08-06 06:22:36 +000010835 case Intrinsic::x86_sse42_pcmpistri128:
10836 case Intrinsic::x86_sse42_pcmpestri128: {
10837 unsigned Opcode;
10838 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10839 Opcode = X86ISD::PCMPISTRI;
10840 else
10841 Opcode = X86ISD::PCMPESTRI;
10842
10843 SmallVector<SDValue, 5> NewOps;
10844 NewOps.append(Op->op_begin()+1, Op->op_end());
10845 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10846 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10847 }
Craig Topper0e292372012-08-24 04:03:22 +000010848 case Intrinsic::x86_fma_vfmadd_ps:
10849 case Intrinsic::x86_fma_vfmadd_pd:
10850 case Intrinsic::x86_fma_vfmsub_ps:
10851 case Intrinsic::x86_fma_vfmsub_pd:
10852 case Intrinsic::x86_fma_vfnmadd_ps:
10853 case Intrinsic::x86_fma_vfnmadd_pd:
10854 case Intrinsic::x86_fma_vfnmsub_ps:
10855 case Intrinsic::x86_fma_vfnmsub_pd:
10856 case Intrinsic::x86_fma_vfmaddsub_ps:
10857 case Intrinsic::x86_fma_vfmaddsub_pd:
10858 case Intrinsic::x86_fma_vfmsubadd_ps:
10859 case Intrinsic::x86_fma_vfmsubadd_pd:
10860 case Intrinsic::x86_fma_vfmadd_ps_256:
10861 case Intrinsic::x86_fma_vfmadd_pd_256:
10862 case Intrinsic::x86_fma_vfmsub_ps_256:
10863 case Intrinsic::x86_fma_vfmsub_pd_256:
10864 case Intrinsic::x86_fma_vfnmadd_ps_256:
10865 case Intrinsic::x86_fma_vfnmadd_pd_256:
10866 case Intrinsic::x86_fma_vfnmsub_ps_256:
10867 case Intrinsic::x86_fma_vfnmsub_pd_256:
10868 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10869 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10870 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10871 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010872 unsigned Opc;
10873 switch (IntNo) {
10874 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10875 case Intrinsic::x86_fma_vfmadd_ps:
10876 case Intrinsic::x86_fma_vfmadd_pd:
10877 case Intrinsic::x86_fma_vfmadd_ps_256:
10878 case Intrinsic::x86_fma_vfmadd_pd_256:
10879 Opc = X86ISD::FMADD;
10880 break;
10881 case Intrinsic::x86_fma_vfmsub_ps:
10882 case Intrinsic::x86_fma_vfmsub_pd:
10883 case Intrinsic::x86_fma_vfmsub_ps_256:
10884 case Intrinsic::x86_fma_vfmsub_pd_256:
10885 Opc = X86ISD::FMSUB;
10886 break;
10887 case Intrinsic::x86_fma_vfnmadd_ps:
10888 case Intrinsic::x86_fma_vfnmadd_pd:
10889 case Intrinsic::x86_fma_vfnmadd_ps_256:
10890 case Intrinsic::x86_fma_vfnmadd_pd_256:
10891 Opc = X86ISD::FNMADD;
10892 break;
10893 case Intrinsic::x86_fma_vfnmsub_ps:
10894 case Intrinsic::x86_fma_vfnmsub_pd:
10895 case Intrinsic::x86_fma_vfnmsub_ps_256:
10896 case Intrinsic::x86_fma_vfnmsub_pd_256:
10897 Opc = X86ISD::FNMSUB;
10898 break;
10899 case Intrinsic::x86_fma_vfmaddsub_ps:
10900 case Intrinsic::x86_fma_vfmaddsub_pd:
10901 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10902 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10903 Opc = X86ISD::FMADDSUB;
10904 break;
10905 case Intrinsic::x86_fma_vfmsubadd_ps:
10906 case Intrinsic::x86_fma_vfmsubadd_pd:
10907 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10908 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10909 Opc = X86ISD::FMSUBADD;
10910 break;
10911 }
10912
10913 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10914 Op.getOperand(2), Op.getOperand(3));
10915 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010916 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010917}
Evan Cheng72261582005-12-20 06:22:03 +000010918
Craig Topper55b24052012-09-11 06:15:32 +000010919static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010920 DebugLoc dl = Op.getDebugLoc();
10921 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10922 switch (IntNo) {
10923 default: return SDValue(); // Don't custom lower most intrinsics.
10924
10925 // RDRAND intrinsics.
10926 case Intrinsic::x86_rdrand_16:
10927 case Intrinsic::x86_rdrand_32:
10928 case Intrinsic::x86_rdrand_64: {
10929 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010930 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10931 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010932
10933 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10934 // return the value from Rand, which is always 0, casted to i32.
10935 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10936 DAG.getConstant(1, Op->getValueType(1)),
10937 DAG.getConstant(X86::COND_B, MVT::i32),
10938 SDValue(Result.getNode(), 1) };
10939 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10940 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10941 Ops, 4);
10942
10943 // Return { result, isValid, chain }.
10944 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010945 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010946 }
10947 }
10948}
10949
Dan Gohmand858e902010-04-17 15:26:15 +000010950SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10951 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010952 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10953 MFI->setReturnAddressIsTaken(true);
10954
Bill Wendling64e87322009-01-16 19:25:27 +000010955 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010956 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010957 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010958
10959 if (Depth > 0) {
10960 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10961 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010962 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10963 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10964 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010965 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010966 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010967 }
10968
10969 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010970 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010971 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010972 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010973}
10974
Dan Gohmand858e902010-04-17 15:26:15 +000010975SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010976 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10977 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010978
Owen Andersone50ed302009-08-10 22:56:29 +000010979 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010980 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010981 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10982 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010983 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010984 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010985 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10986 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010987 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010988 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010989}
10990
Dan Gohman475871a2008-07-27 21:46:04 +000010991SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010992 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010993 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010994}
10995
Dan Gohmand858e902010-04-17 15:26:15 +000010996SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010997 SDValue Chain = Op.getOperand(0);
10998 SDValue Offset = Op.getOperand(1);
10999 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011000 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011001
Dan Gohmand8816272010-08-11 18:14:00 +000011002 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
11003 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
11004 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011005 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011006
Dan Gohmand8816272010-08-11 18:14:00 +000011007 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011008 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011009 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011010 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11011 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011012 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011013
Dale Johannesene4d209d2009-02-03 20:21:25 +000011014 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011015 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011016 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011017}
11018
Michael Liao6c0e04c2012-10-15 22:39:43 +000011019SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11020 SelectionDAG &DAG) const {
11021 DebugLoc DL = Op.getDebugLoc();
11022 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11023 DAG.getVTList(MVT::i32, MVT::Other),
11024 Op.getOperand(0), Op.getOperand(1));
11025}
11026
11027SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11028 SelectionDAG &DAG) const {
11029 DebugLoc DL = Op.getDebugLoc();
11030 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11031 Op.getOperand(0), Op.getOperand(1));
11032}
11033
Craig Topper55b24052012-09-11 06:15:32 +000011034static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011035 return Op.getOperand(0);
11036}
11037
11038SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11039 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011040 SDValue Root = Op.getOperand(0);
11041 SDValue Trmp = Op.getOperand(1); // trampoline
11042 SDValue FPtr = Op.getOperand(2); // nested function
11043 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011044 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011045
Dan Gohman69de1932008-02-06 22:27:42 +000011046 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011047 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011048
11049 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011050 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011051
11052 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011053 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11054 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011055
Michael Liao7abf67a2012-10-04 19:50:43 +000011056 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11057 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011058
11059 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11060
11061 // Load the pointer to the nested function into R11.
11062 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011063 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011064 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011065 Addr, MachinePointerInfo(TrmpAddr),
11066 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011067
Owen Anderson825b72b2009-08-11 20:47:22 +000011068 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11069 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011070 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11071 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011072 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011073
11074 // Load the 'nest' parameter value into R10.
11075 // R10 is specified in X86CallingConv.td
11076 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011077 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11078 DAG.getConstant(10, MVT::i64));
11079 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011080 Addr, MachinePointerInfo(TrmpAddr, 10),
11081 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011082
Owen Anderson825b72b2009-08-11 20:47:22 +000011083 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11084 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011085 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11086 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011087 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011088
11089 // Jump to the nested function.
11090 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011091 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11092 DAG.getConstant(20, MVT::i64));
11093 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011094 Addr, MachinePointerInfo(TrmpAddr, 20),
11095 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011096
11097 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011098 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11099 DAG.getConstant(22, MVT::i64));
11100 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011101 MachinePointerInfo(TrmpAddr, 22),
11102 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011103
Duncan Sands4a544a72011-09-06 13:37:06 +000011104 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011105 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011106 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011107 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011108 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011109 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011110
11111 switch (CC) {
11112 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011113 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011114 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011115 case CallingConv::X86_StdCall: {
11116 // Pass 'nest' parameter in ECX.
11117 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011118 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011119
11120 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011121 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011122 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011123
Chris Lattner58d74912008-03-12 17:45:29 +000011124 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011125 unsigned InRegCount = 0;
11126 unsigned Idx = 1;
11127
11128 for (FunctionType::param_iterator I = FTy->param_begin(),
11129 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011130 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011131 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011132 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011133
11134 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011135 report_fatal_error("Nest register in use - reduce number of inreg"
11136 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011137 }
11138 }
11139 break;
11140 }
11141 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011142 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011143 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011144 // Pass 'nest' parameter in EAX.
11145 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011146 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011147 break;
11148 }
11149
Dan Gohman475871a2008-07-27 21:46:04 +000011150 SDValue OutChains[4];
11151 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011152
Owen Anderson825b72b2009-08-11 20:47:22 +000011153 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11154 DAG.getConstant(10, MVT::i32));
11155 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011156
Chris Lattnera62fe662010-02-05 19:20:30 +000011157 // This is storing the opcode for MOV32ri.
11158 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011159 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011160 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011161 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011162 Trmp, MachinePointerInfo(TrmpAddr),
11163 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011164
Owen Anderson825b72b2009-08-11 20:47:22 +000011165 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11166 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011167 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11168 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011169 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011170
Chris Lattnera62fe662010-02-05 19:20:30 +000011171 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011172 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11173 DAG.getConstant(5, MVT::i32));
11174 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011175 MachinePointerInfo(TrmpAddr, 5),
11176 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011177
Owen Anderson825b72b2009-08-11 20:47:22 +000011178 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11179 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011180 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11181 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011182 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011183
Duncan Sands4a544a72011-09-06 13:37:06 +000011184 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011185 }
11186}
11187
Dan Gohmand858e902010-04-17 15:26:15 +000011188SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11189 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011190 /*
11191 The rounding mode is in bits 11:10 of FPSR, and has the following
11192 settings:
11193 00 Round to nearest
11194 01 Round to -inf
11195 10 Round to +inf
11196 11 Round to 0
11197
11198 FLT_ROUNDS, on the other hand, expects the following:
11199 -1 Undefined
11200 0 Round to 0
11201 1 Round to nearest
11202 2 Round to +inf
11203 3 Round to -inf
11204
11205 To perform the conversion, we do:
11206 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11207 */
11208
11209 MachineFunction &MF = DAG.getMachineFunction();
11210 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011211 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011212 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011213 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011214 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011215
11216 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011217 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011218 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011219
Chris Lattner2156b792010-09-22 01:11:26 +000011220 MachineMemOperand *MMO =
11221 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11222 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011223
Chris Lattner2156b792010-09-22 01:11:26 +000011224 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11225 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11226 DAG.getVTList(MVT::Other),
11227 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011228
11229 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011230 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011231 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011232
11233 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011234 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011235 DAG.getNode(ISD::SRL, DL, MVT::i16,
11236 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011237 CWD, DAG.getConstant(0x800, MVT::i16)),
11238 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011239 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011240 DAG.getNode(ISD::SRL, DL, MVT::i16,
11241 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011242 CWD, DAG.getConstant(0x400, MVT::i16)),
11243 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011244
Dan Gohman475871a2008-07-27 21:46:04 +000011245 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011246 DAG.getNode(ISD::AND, DL, MVT::i16,
11247 DAG.getNode(ISD::ADD, DL, MVT::i16,
11248 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011249 DAG.getConstant(1, MVT::i16)),
11250 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011251
Duncan Sands83ec4b62008-06-06 12:08:01 +000011252 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011253 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011254}
11255
Craig Topper55b24052012-09-11 06:15:32 +000011256static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011257 EVT VT = Op.getValueType();
11258 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011259 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011260 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011261
11262 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011263 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011264 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011265 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011266 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011267 }
Evan Cheng18efe262007-12-14 02:13:44 +000011268
Evan Cheng152804e2007-12-14 08:30:15 +000011269 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011270 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011271 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011272
11273 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011274 SDValue Ops[] = {
11275 Op,
11276 DAG.getConstant(NumBits+NumBits-1, OpVT),
11277 DAG.getConstant(X86::COND_E, MVT::i8),
11278 Op.getValue(1)
11279 };
11280 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011281
11282 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011283 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011284
Owen Anderson825b72b2009-08-11 20:47:22 +000011285 if (VT == MVT::i8)
11286 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011287 return Op;
11288}
11289
Craig Topper55b24052012-09-11 06:15:32 +000011290static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011291 EVT VT = Op.getValueType();
11292 EVT OpVT = VT;
11293 unsigned NumBits = VT.getSizeInBits();
11294 DebugLoc dl = Op.getDebugLoc();
11295
11296 Op = Op.getOperand(0);
11297 if (VT == MVT::i8) {
11298 // Zero extend to i32 since there is not an i8 bsr.
11299 OpVT = MVT::i32;
11300 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11301 }
11302
11303 // Issue a bsr (scan bits in reverse).
11304 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11305 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11306
11307 // And xor with NumBits-1.
11308 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11309
11310 if (VT == MVT::i8)
11311 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11312 return Op;
11313}
11314
Craig Topper55b24052012-09-11 06:15:32 +000011315static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011316 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011317 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011318 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011319 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011320
11321 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011322 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011323 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011324
11325 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011326 SDValue Ops[] = {
11327 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011328 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011329 DAG.getConstant(X86::COND_E, MVT::i8),
11330 Op.getValue(1)
11331 };
Chandler Carruth77821022011-12-24 12:12:34 +000011332 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011333}
11334
Craig Topper13894fa2011-08-24 06:14:18 +000011335// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11336// ones, and then concatenate the result back.
11337static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011338 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011339
Craig Topper7a9a28b2012-08-12 02:23:29 +000011340 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011341 "Unsupported value type for operation");
11342
Craig Topper66ddd152012-04-27 22:54:43 +000011343 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011344 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011345
11346 // Extract the LHS vectors
11347 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011348 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11349 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011350
11351 // Extract the RHS vectors
11352 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011353 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11354 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011355
11356 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11357 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11358
11359 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11360 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11361 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11362}
11363
Craig Topper55b24052012-09-11 06:15:32 +000011364static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011365 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011366 Op.getValueType().isInteger() &&
11367 "Only handle AVX 256-bit vector integer operation");
11368 return Lower256IntArith(Op, DAG);
11369}
11370
Craig Topper55b24052012-09-11 06:15:32 +000011371static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011372 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011373 Op.getValueType().isInteger() &&
11374 "Only handle AVX 256-bit vector integer operation");
11375 return Lower256IntArith(Op, DAG);
11376}
11377
Craig Topper55b24052012-09-11 06:15:32 +000011378static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11379 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011380 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011381 EVT VT = Op.getValueType();
11382
11383 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011384 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011385 return Lower256IntArith(Op, DAG);
11386
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011387 SDValue A = Op.getOperand(0);
11388 SDValue B = Op.getOperand(1);
11389
11390 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11391 if (VT == MVT::v4i32) {
11392 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11393 "Should not custom lower when pmuldq is available!");
11394
11395 // Extract the odd parts.
11396 const int UnpackMask[] = { 1, -1, 3, -1 };
11397 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11398 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11399
11400 // Multiply the even parts.
11401 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11402 // Now multiply odd parts.
11403 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11404
11405 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11406 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11407
11408 // Merge the two vectors back together with a shuffle. This expands into 2
11409 // shuffles.
11410 const int ShufMask[] = { 0, 4, 2, 6 };
11411 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11412 }
11413
Craig Topper5b209e82012-02-05 03:14:49 +000011414 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11415 "Only know how to lower V2I64/V4I64 multiply");
11416
Craig Topper5b209e82012-02-05 03:14:49 +000011417 // Ahi = psrlqi(a, 32);
11418 // Bhi = psrlqi(b, 32);
11419 //
11420 // AloBlo = pmuludq(a, b);
11421 // AloBhi = pmuludq(a, Bhi);
11422 // AhiBlo = pmuludq(Ahi, b);
11423
11424 // AloBhi = psllqi(AloBhi, 32);
11425 // AhiBlo = psllqi(AhiBlo, 32);
11426 // return AloBlo + AloBhi + AhiBlo;
11427
Craig Topper5b209e82012-02-05 03:14:49 +000011428 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011429
Craig Topper5b209e82012-02-05 03:14:49 +000011430 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11431 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011432
Craig Topper5b209e82012-02-05 03:14:49 +000011433 // Bit cast to 32-bit vectors for MULUDQ
11434 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11435 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11436 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11437 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11438 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011439
Craig Topper5b209e82012-02-05 03:14:49 +000011440 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11441 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11442 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011443
Craig Topper5b209e82012-02-05 03:14:49 +000011444 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11445 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011446
Dale Johannesene4d209d2009-02-03 20:21:25 +000011447 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011448 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011449}
11450
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011451SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11452 EVT VT = Op.getValueType();
11453 EVT EltTy = VT.getVectorElementType();
11454 unsigned NumElts = VT.getVectorNumElements();
11455 SDValue N0 = Op.getOperand(0);
11456 DebugLoc dl = Op.getDebugLoc();
11457
11458 // Lower sdiv X, pow2-const.
11459 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11460 if (!C)
11461 return SDValue();
11462
11463 APInt SplatValue, SplatUndef;
11464 unsigned MinSplatBits;
11465 bool HasAnyUndefs;
11466 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11467 return SDValue();
11468
11469 if ((SplatValue != 0) &&
11470 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11471 unsigned lg2 = SplatValue.countTrailingZeros();
11472 // Splat the sign bit.
11473 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11474 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11475 // Add (N0 < 0) ? abs2 - 1 : 0;
11476 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11477 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11478 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11479 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11480 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11481
11482 // If we're dividing by a positive value, we're done. Otherwise, we must
11483 // negate the result.
11484 if (SplatValue.isNonNegative())
11485 return SRA;
11486
11487 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11488 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11489 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11490 }
11491 return SDValue();
11492}
11493
Michael Liao4b7ab122013-03-20 02:20:36 +000011494static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11495 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011496 EVT VT = Op.getValueType();
11497 DebugLoc dl = Op.getDebugLoc();
11498 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011499 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011500
Nadav Rotem43012222011-05-11 08:12:09 +000011501 // Optimize shl/srl/sra with constant shift amount.
11502 if (isSplatVector(Amt.getNode())) {
11503 SDValue SclrAmt = Amt->getOperand(0);
11504 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11505 uint64_t ShiftAmt = C->getZExtValue();
11506
Craig Toppered2e13d2012-01-22 19:15:14 +000011507 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011508 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011509 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11510 if (Op.getOpcode() == ISD::SHL)
11511 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11512 DAG.getConstant(ShiftAmt, MVT::i32));
11513 if (Op.getOpcode() == ISD::SRL)
11514 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11515 DAG.getConstant(ShiftAmt, MVT::i32));
11516 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11517 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11518 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011519 }
11520
Craig Toppered2e13d2012-01-22 19:15:14 +000011521 if (VT == MVT::v16i8) {
11522 if (Op.getOpcode() == ISD::SHL) {
11523 // Make a large shift.
11524 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11525 DAG.getConstant(ShiftAmt, MVT::i32));
11526 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11527 // Zero out the rightmost bits.
11528 SmallVector<SDValue, 16> V(16,
11529 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11530 MVT::i8));
11531 return DAG.getNode(ISD::AND, dl, VT, SHL,
11532 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011533 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011534 if (Op.getOpcode() == ISD::SRL) {
11535 // Make a large shift.
11536 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11537 DAG.getConstant(ShiftAmt, MVT::i32));
11538 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11539 // Zero out the leftmost bits.
11540 SmallVector<SDValue, 16> V(16,
11541 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11542 MVT::i8));
11543 return DAG.getNode(ISD::AND, dl, VT, SRL,
11544 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11545 }
11546 if (Op.getOpcode() == ISD::SRA) {
11547 if (ShiftAmt == 7) {
11548 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011549 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011550 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011551 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011552
Craig Toppered2e13d2012-01-22 19:15:14 +000011553 // R s>> a === ((R u>> a) ^ m) - m
11554 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11555 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11556 MVT::i8));
11557 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11558 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11559 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11560 return Res;
11561 }
Craig Topper731dfd02012-04-23 03:42:40 +000011562 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011563 }
Craig Topper46154eb2011-11-11 07:39:23 +000011564
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011565 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011566 if (Op.getOpcode() == ISD::SHL) {
11567 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011568 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11569 DAG.getConstant(ShiftAmt, MVT::i32));
11570 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011571 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011572 SmallVector<SDValue, 32> V(32,
11573 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11574 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011575 return DAG.getNode(ISD::AND, dl, VT, SHL,
11576 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011577 }
Craig Topper0d86d462011-11-20 00:12:05 +000011578 if (Op.getOpcode() == ISD::SRL) {
11579 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011580 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11581 DAG.getConstant(ShiftAmt, MVT::i32));
11582 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011583 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011584 SmallVector<SDValue, 32> V(32,
11585 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11586 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011587 return DAG.getNode(ISD::AND, dl, VT, SRL,
11588 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11589 }
11590 if (Op.getOpcode() == ISD::SRA) {
11591 if (ShiftAmt == 7) {
11592 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011593 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011594 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011595 }
11596
11597 // R s>> a === ((R u>> a) ^ m) - m
11598 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11599 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11600 MVT::i8));
11601 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11602 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11603 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11604 return Res;
11605 }
Craig Topper731dfd02012-04-23 03:42:40 +000011606 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011607 }
Nadav Rotem43012222011-05-11 08:12:09 +000011608 }
11609 }
11610
Michael Liao4b7ab122013-03-20 02:20:36 +000011611 return SDValue();
11612}
11613
11614SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11615
11616 EVT VT = Op.getValueType();
11617 DebugLoc dl = Op.getDebugLoc();
11618 SDValue R = Op.getOperand(0);
11619 SDValue Amt = Op.getOperand(1);
11620 SDValue V;
11621
11622 if (!Subtarget->hasSSE2())
11623 return SDValue();
11624
11625 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
11626 if (V.getNode())
11627 return V;
11628
Nadav Rotem43012222011-05-11 08:12:09 +000011629 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011630 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011631 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011632
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011633 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011634 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011635 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11636 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11637 }
Nadav Rotem43012222011-05-11 08:12:09 +000011638 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011639 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011640
Nate Begeman51409212010-07-28 00:21:48 +000011641 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011642 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011643 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011644
Lang Hames8b99c1e2011-12-17 01:08:46 +000011645 // Turn 'a' into a mask suitable for VSELECT
11646 SDValue VSelM = DAG.getConstant(0x80, VT);
11647 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011648 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011649
Lang Hames8b99c1e2011-12-17 01:08:46 +000011650 SDValue CM1 = DAG.getConstant(0x0f, VT);
11651 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011652
Lang Hames8b99c1e2011-12-17 01:08:46 +000011653 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11654 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011655 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11656 DAG.getConstant(4, MVT::i32), DAG);
11657 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011658 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11659
Nate Begeman51409212010-07-28 00:21:48 +000011660 // a += a
11661 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011662 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011663 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011664
Lang Hames8b99c1e2011-12-17 01:08:46 +000011665 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11666 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011667 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11668 DAG.getConstant(2, MVT::i32), DAG);
11669 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011670 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11671
Nate Begeman51409212010-07-28 00:21:48 +000011672 // a += a
11673 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011674 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011675 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011676
Lang Hames8b99c1e2011-12-17 01:08:46 +000011677 // return VSELECT(r, r+r, a);
11678 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011679 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011680 return R;
11681 }
Craig Topper46154eb2011-11-11 07:39:23 +000011682
11683 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011684 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011685 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011686 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11687 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11688
11689 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011690 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11691 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011692
11693 // Recreate the shift amount vectors
11694 SDValue Amt1, Amt2;
11695 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11696 // Constant shift amount
11697 SmallVector<SDValue, 4> Amt1Csts;
11698 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011699 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011700 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011701 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011702 Amt2Csts.push_back(Amt->getOperand(i));
11703
11704 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11705 &Amt1Csts[0], NumElems/2);
11706 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11707 &Amt2Csts[0], NumElems/2);
11708 } else {
11709 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011710 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11711 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011712 }
11713
11714 // Issue new vector shifts for the smaller types
11715 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11716 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11717
11718 // Concatenate the result back
11719 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11720 }
11721
Nate Begeman51409212010-07-28 00:21:48 +000011722 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011723}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011724
Craig Topper55b24052012-09-11 06:15:32 +000011725static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011726 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11727 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011728 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11729 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011730 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011731 SDValue LHS = N->getOperand(0);
11732 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011733 unsigned BaseOp = 0;
11734 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011735 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011736 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011737 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011738 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011739 // A subtract of one will be selected as a INC. Note that INC doesn't
11740 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011741 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11742 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011743 BaseOp = X86ISD::INC;
11744 Cond = X86::COND_O;
11745 break;
11746 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011747 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011748 Cond = X86::COND_O;
11749 break;
11750 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011751 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011752 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011753 break;
11754 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011755 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11756 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11758 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011759 BaseOp = X86ISD::DEC;
11760 Cond = X86::COND_O;
11761 break;
11762 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011763 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011764 Cond = X86::COND_O;
11765 break;
11766 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011767 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011768 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011769 break;
11770 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011771 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011772 Cond = X86::COND_O;
11773 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011774 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11775 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11776 MVT::i32);
11777 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011778
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011779 SDValue SetCC =
11780 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11781 DAG.getConstant(X86::COND_O, MVT::i32),
11782 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011783
Dan Gohman6e5fda22011-07-22 18:45:15 +000011784 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011785 }
Bill Wendling74c37652008-12-09 22:08:41 +000011786 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011787
Bill Wendling61edeb52008-12-02 01:06:39 +000011788 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011789 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011790 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011791
Bill Wendling61edeb52008-12-02 01:06:39 +000011792 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011793 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11794 DAG.getConstant(Cond, MVT::i32),
11795 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011796
Dan Gohman6e5fda22011-07-22 18:45:15 +000011797 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011798}
11799
Chad Rosier30450e82011-12-22 22:35:21 +000011800SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11801 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011802 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011803 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11804 EVT VT = Op.getValueType();
11805
Craig Toppered2e13d2012-01-22 19:15:14 +000011806 if (!Subtarget->hasSSE2() || !VT.isVector())
11807 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011808
Craig Toppered2e13d2012-01-22 19:15:14 +000011809 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11810 ExtraVT.getScalarType().getSizeInBits();
11811 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11812
11813 switch (VT.getSimpleVT().SimpleTy) {
11814 default: return SDValue();
11815 case MVT::v8i32:
11816 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011817 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011818 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011819 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011820 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011821 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011822
Craig Toppered2e13d2012-01-22 19:15:14 +000011823 // Extract the LHS vectors
11824 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011825 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11826 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011827
Craig Toppered2e13d2012-01-22 19:15:14 +000011828 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11829 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011830
Craig Toppered2e13d2012-01-22 19:15:14 +000011831 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011832 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011833 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11834 ExtraNumElems/2);
11835 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011836
Craig Toppered2e13d2012-01-22 19:15:14 +000011837 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11838 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011839
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011840 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011841 }
11842 // fall through
11843 case MVT::v4i32:
11844 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000011845 // (sext (vzext x)) -> (vsext x)
11846 SDValue Op0 = Op.getOperand(0);
11847 SDValue Op00 = Op0.getOperand(0);
11848 SDValue Tmp1;
11849 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
11850 if (Op0.getOpcode() == ISD::BITCAST &&
11851 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
11852 Tmp1 = LowerVectorIntExtend(Op00, DAG);
11853 if (Tmp1.getNode()) {
11854 SDValue Tmp1Op0 = Tmp1.getOperand(0);
11855 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
11856 "This optimization is invalid without a VZEXT.");
11857 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
11858 }
11859
11860 // If the above didn't work, then just use Shift-Left + Shift-Right.
11861 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000011862 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011863 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011864 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011865}
11866
Craig Topper55b24052012-09-11 06:15:32 +000011867static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11868 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011869 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011870
Eric Christopher77ed1352011-07-08 00:04:56 +000011871 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11872 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011873 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011874 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011875 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011876 SDValue Ops[] = {
11877 DAG.getRegister(X86::ESP, MVT::i32), // Base
11878 DAG.getTargetConstant(1, MVT::i8), // Scale
11879 DAG.getRegister(0, MVT::i32), // Index
11880 DAG.getTargetConstant(0, MVT::i32), // Disp
11881 DAG.getRegister(0, MVT::i32), // Segment.
11882 Zero,
11883 Chain
11884 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011885 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011886 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11887 array_lengthof(Ops));
11888 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011889 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011890
Eric Christopher9a9d2752010-07-22 02:48:34 +000011891 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011892 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011893 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011894
Chris Lattner132929a2010-08-14 17:26:09 +000011895 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11896 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11897 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11898 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011899
Chris Lattner132929a2010-08-14 17:26:09 +000011900 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11901 if (!Op1 && !Op2 && !Op3 && Op4)
11902 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011903
Chris Lattner132929a2010-08-14 17:26:09 +000011904 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11905 if (Op1 && !Op2 && !Op3 && !Op4)
11906 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011907
11908 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011909 // (MFENCE)>;
11910 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011911}
11912
Craig Topper55b24052012-09-11 06:15:32 +000011913static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11914 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011915 DebugLoc dl = Op.getDebugLoc();
11916 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11917 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11918 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11919 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11920
11921 // The only fence that needs an instruction is a sequentially-consistent
11922 // cross-thread fence.
11923 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11924 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11925 // no-sse2). There isn't any reason to disable it if the target processor
11926 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011927 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011928 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11929
11930 SDValue Chain = Op.getOperand(0);
11931 SDValue Zero = DAG.getConstant(0, MVT::i32);
11932 SDValue Ops[] = {
11933 DAG.getRegister(X86::ESP, MVT::i32), // Base
11934 DAG.getTargetConstant(1, MVT::i8), // Scale
11935 DAG.getRegister(0, MVT::i32), // Index
11936 DAG.getTargetConstant(0, MVT::i32), // Disp
11937 DAG.getRegister(0, MVT::i32), // Segment.
11938 Zero,
11939 Chain
11940 };
11941 SDNode *Res =
11942 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11943 array_lengthof(Ops));
11944 return SDValue(Res, 0);
11945 }
11946
11947 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11948 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11949}
11950
Craig Topper55b24052012-09-11 06:15:32 +000011951static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11952 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011953 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011954 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011955 unsigned Reg = 0;
11956 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011957 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011958 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011959 case MVT::i8: Reg = X86::AL; size = 1; break;
11960 case MVT::i16: Reg = X86::AX; size = 2; break;
11961 case MVT::i32: Reg = X86::EAX; size = 4; break;
11962 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011963 assert(Subtarget->is64Bit() && "Node not type legal!");
11964 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011965 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011966 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011967 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011968 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011969 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011970 Op.getOperand(1),
11971 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011972 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011973 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011974 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011975 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11976 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11977 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011978 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011979 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011980 return cpOut;
11981}
11982
Craig Topper55b24052012-09-11 06:15:32 +000011983static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11984 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011985 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011986 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011987 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011988 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011989 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011990 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11991 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011992 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011993 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11994 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011995 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011996 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011997 rdx.getValue(1)
11998 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011999 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012000}
12001
Craig Topper55b24052012-09-11 06:15:32 +000012002SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012003 EVT SrcVT = Op.getOperand(0).getValueType();
12004 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012005 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012006 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012007 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012008 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012009 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012010 // i64 <=> MMX conversions are Legal.
12011 if (SrcVT==MVT::i64 && DstVT.isVector())
12012 return Op;
12013 if (DstVT==MVT::i64 && SrcVT.isVector())
12014 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012015 // MMX <=> MMX conversions are Legal.
12016 if (SrcVT.isVector() && DstVT.isVector())
12017 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012018 // All other conversions need to be expanded.
12019 return SDValue();
12020}
Chris Lattner5b856542010-12-20 00:59:46 +000012021
Craig Topper55b24052012-09-11 06:15:32 +000012022static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012023 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012024 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012025 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012026 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012027 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012028 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012029 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012030 Node->getOperand(0),
12031 Node->getOperand(1), negOp,
12032 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012033 cast<AtomicSDNode>(Node)->getAlignment(),
12034 cast<AtomicSDNode>(Node)->getOrdering(),
12035 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012036}
12037
Eli Friedman327236c2011-08-24 20:50:09 +000012038static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12039 SDNode *Node = Op.getNode();
12040 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012041 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012042
12043 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012044 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12045 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12046 // (The only way to get a 16-byte store is cmpxchg16b)
12047 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12048 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12049 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012050 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12051 cast<AtomicSDNode>(Node)->getMemoryVT(),
12052 Node->getOperand(0),
12053 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012054 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012055 cast<AtomicSDNode>(Node)->getOrdering(),
12056 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012057 return Swap.getValue(1);
12058 }
12059 // Other atomic stores have a simple pattern.
12060 return Op;
12061}
12062
Chris Lattner5b856542010-12-20 00:59:46 +000012063static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12064 EVT VT = Op.getNode()->getValueType(0);
12065
12066 // Let legalize expand this if it isn't a legal type yet.
12067 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12068 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012069
Chris Lattner5b856542010-12-20 00:59:46 +000012070 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012071
Chris Lattner5b856542010-12-20 00:59:46 +000012072 unsigned Opc;
12073 bool ExtraOp = false;
12074 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012075 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012076 case ISD::ADDC: Opc = X86ISD::ADD; break;
12077 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12078 case ISD::SUBC: Opc = X86ISD::SUB; break;
12079 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12080 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012081
Chris Lattner5b856542010-12-20 00:59:46 +000012082 if (!ExtraOp)
12083 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12084 Op.getOperand(1));
12085 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12086 Op.getOperand(1), Op.getOperand(2));
12087}
12088
Evan Cheng8688a582013-01-29 02:32:37 +000012089SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012090 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012091
Evan Cheng8688a582013-01-29 02:32:37 +000012092 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12093 // which returns the values in two XMM registers.
12094 DebugLoc dl = Op.getDebugLoc();
12095 SDValue Arg = Op.getOperand(0);
12096 EVT ArgVT = Arg.getValueType();
12097 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012098
Evan Cheng8688a582013-01-29 02:32:37 +000012099 ArgListTy Args;
12100 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012101
Evan Cheng8688a582013-01-29 02:32:37 +000012102 Entry.Node = Arg;
12103 Entry.Ty = ArgTy;
12104 Entry.isSExt = false;
12105 Entry.isZExt = false;
12106 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012107
12108 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12109 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12110 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012111 const char *LibcallName = (ArgVT == MVT::f64)
12112 ? "__sincos_stret" : "__sincosf_stret";
12113 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012114
Evan Cheng8688a582013-01-29 02:32:37 +000012115 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12116 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012117 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12118 false, false, false, false, 0,
12119 CallingConv::C, /*isTaillCall=*/false,
12120 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12121 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012122 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012123 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012124}
12125
Evan Cheng0db9fe62006-04-25 20:13:52 +000012126/// LowerOperation - Provide custom lowering hooks for some operations.
12127///
Dan Gohmand858e902010-04-17 15:26:15 +000012128SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012129 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012130 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012131 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012132 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12133 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12134 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012135 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012136 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012137 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012138 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012139 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12140 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12141 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012142 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12143 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012144 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12145 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12146 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012147 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012148 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012149 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012150 case ISD::SHL_PARTS:
12151 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012152 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012153 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012154 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012155 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012156 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12157 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12158 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012159 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012160 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012161 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012162 case ISD::FABS: return LowerFABS(Op, DAG);
12163 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012164 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012165 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012166 case ISD::SETCC: return LowerSETCC(Op, DAG);
12167 case ISD::SELECT: return LowerSELECT(Op, DAG);
12168 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012169 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012170 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012171 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012172 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012173 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012174 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012175 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12176 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012177 case ISD::FRAME_TO_ARGS_OFFSET:
12178 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012179 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012180 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012181 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12182 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012183 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12184 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012185 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012186 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012187 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012188 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012189 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012190 case ISD::SRA:
12191 case ISD::SRL:
12192 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012193 case ISD::SADDO:
12194 case ISD::UADDO:
12195 case ISD::SSUBO:
12196 case ISD::USUBO:
12197 case ISD::SMULO:
12198 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012199 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012200 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012201 case ISD::ADDC:
12202 case ISD::ADDE:
12203 case ISD::SUBC:
12204 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012205 case ISD::ADD: return LowerADD(Op, DAG);
12206 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012207 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012208 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012209 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012210}
12211
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012212static void ReplaceATOMIC_LOAD(SDNode *Node,
12213 SmallVectorImpl<SDValue> &Results,
12214 SelectionDAG &DAG) {
12215 DebugLoc dl = Node->getDebugLoc();
12216 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12217
12218 // Convert wide load -> cmpxchg8b/cmpxchg16b
12219 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12220 // (The only way to get a 16-byte load is cmpxchg16b)
12221 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012222 SDValue Zero = DAG.getConstant(0, VT);
12223 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012224 Node->getOperand(0),
12225 Node->getOperand(1), Zero, Zero,
12226 cast<AtomicSDNode>(Node)->getMemOperand(),
12227 cast<AtomicSDNode>(Node)->getOrdering(),
12228 cast<AtomicSDNode>(Node)->getSynchScope());
12229 Results.push_back(Swap.getValue(0));
12230 Results.push_back(Swap.getValue(1));
12231}
12232
Craig Topperc0878702012-08-17 06:55:11 +000012233static void
Duncan Sands1607f052008-12-01 11:39:25 +000012234ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012235 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012236 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012237 assert (Node->getValueType(0) == MVT::i64 &&
12238 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012239
12240 SDValue Chain = Node->getOperand(0);
12241 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012242 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012243 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012244 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012245 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012246 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012247 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012248 SDValue Result =
12249 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12250 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012251 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012252 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012253 Results.push_back(Result.getValue(2));
12254}
12255
Duncan Sands126d9072008-07-04 11:47:58 +000012256/// ReplaceNodeResults - Replace a node with an illegal result type
12257/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012258void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12259 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012260 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012261 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012262 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012263 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012264 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012265 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012266 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012267 case ISD::ADDC:
12268 case ISD::ADDE:
12269 case ISD::SUBC:
12270 case ISD::SUBE:
12271 // We don't want to expand or promote these.
12272 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012273 case ISD::FP_TO_SINT:
12274 case ISD::FP_TO_UINT: {
12275 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12276
12277 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12278 return;
12279
Eli Friedman948e95a2009-05-23 09:59:16 +000012280 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012281 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012282 SDValue FIST = Vals.first, StackSlot = Vals.second;
12283 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012284 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012285 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012286 if (StackSlot.getNode() != 0)
12287 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12288 MachinePointerInfo(),
12289 false, false, false, 0));
12290 else
12291 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012292 }
12293 return;
12294 }
Michael Liao991b6a22012-10-24 04:09:32 +000012295 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012296 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12297 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012298 N->getValueType(0) != MVT::v2f32)
12299 return;
12300 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12301 N->getOperand(0));
12302 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12303 MVT::f64);
12304 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12305 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12306 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12307 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12308 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12309 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12310 return;
12311 }
Michael Liao44c2d612012-10-10 16:53:28 +000012312 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012313 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12314 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012315 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12316 Results.push_back(V);
12317 return;
12318 }
Duncan Sands1607f052008-12-01 11:39:25 +000012319 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012320 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012321 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012322 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012323 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012324 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012325 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012326 eax.getValue(2));
12327 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12328 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012329 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012330 Results.push_back(edx.getValue(1));
12331 return;
12332 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012333 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012334 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012335 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012336 bool Regs64bit = T == MVT::i128;
12337 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012338 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012339 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12340 DAG.getConstant(0, HalfT));
12341 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12342 DAG.getConstant(1, HalfT));
12343 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12344 Regs64bit ? X86::RAX : X86::EAX,
12345 cpInL, SDValue());
12346 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12347 Regs64bit ? X86::RDX : X86::EDX,
12348 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012349 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012350 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12351 DAG.getConstant(0, HalfT));
12352 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12353 DAG.getConstant(1, HalfT));
12354 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12355 Regs64bit ? X86::RBX : X86::EBX,
12356 swapInL, cpInH.getValue(1));
12357 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012358 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012359 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012360 SDValue Ops[] = { swapInH.getValue(0),
12361 N->getOperand(1),
12362 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012363 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012364 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012365 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12366 X86ISD::LCMPXCHG8_DAG;
12367 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012368 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012369 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12370 Regs64bit ? X86::RAX : X86::EAX,
12371 HalfT, Result.getValue(1));
12372 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12373 Regs64bit ? X86::RDX : X86::EDX,
12374 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012375 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012376 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012377 Results.push_back(cpOutH.getValue(1));
12378 return;
12379 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012380 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012381 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012382 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012383 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012384 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012385 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012386 case ISD::ATOMIC_LOAD_MAX:
12387 case ISD::ATOMIC_LOAD_MIN:
12388 case ISD::ATOMIC_LOAD_UMAX:
12389 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012390 case ISD::ATOMIC_SWAP: {
12391 unsigned Opc;
12392 switch (N->getOpcode()) {
12393 default: llvm_unreachable("Unexpected opcode");
12394 case ISD::ATOMIC_LOAD_ADD:
12395 Opc = X86ISD::ATOMADD64_DAG;
12396 break;
12397 case ISD::ATOMIC_LOAD_AND:
12398 Opc = X86ISD::ATOMAND64_DAG;
12399 break;
12400 case ISD::ATOMIC_LOAD_NAND:
12401 Opc = X86ISD::ATOMNAND64_DAG;
12402 break;
12403 case ISD::ATOMIC_LOAD_OR:
12404 Opc = X86ISD::ATOMOR64_DAG;
12405 break;
12406 case ISD::ATOMIC_LOAD_SUB:
12407 Opc = X86ISD::ATOMSUB64_DAG;
12408 break;
12409 case ISD::ATOMIC_LOAD_XOR:
12410 Opc = X86ISD::ATOMXOR64_DAG;
12411 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012412 case ISD::ATOMIC_LOAD_MAX:
12413 Opc = X86ISD::ATOMMAX64_DAG;
12414 break;
12415 case ISD::ATOMIC_LOAD_MIN:
12416 Opc = X86ISD::ATOMMIN64_DAG;
12417 break;
12418 case ISD::ATOMIC_LOAD_UMAX:
12419 Opc = X86ISD::ATOMUMAX64_DAG;
12420 break;
12421 case ISD::ATOMIC_LOAD_UMIN:
12422 Opc = X86ISD::ATOMUMIN64_DAG;
12423 break;
Craig Topperc0878702012-08-17 06:55:11 +000012424 case ISD::ATOMIC_SWAP:
12425 Opc = X86ISD::ATOMSWAP64_DAG;
12426 break;
12427 }
12428 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012429 return;
Craig Topperc0878702012-08-17 06:55:11 +000012430 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012431 case ISD::ATOMIC_LOAD:
12432 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012433 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012434}
12435
Evan Cheng72261582005-12-20 06:22:03 +000012436const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12437 switch (Opcode) {
12438 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012439 case X86ISD::BSF: return "X86ISD::BSF";
12440 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012441 case X86ISD::SHLD: return "X86ISD::SHLD";
12442 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012443 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012444 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012445 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012446 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012447 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012448 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012449 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12450 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12451 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012452 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012453 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012454 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012455 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012456 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012457 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012458 case X86ISD::COMI: return "X86ISD::COMI";
12459 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012460 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012461 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012462 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12463 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012464 case X86ISD::CMOV: return "X86ISD::CMOV";
12465 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012466 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012467 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12468 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012469 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012470 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012471 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012472 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012473 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012474 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12475 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012476 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012477 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012478 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012479 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012480 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012481 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012482 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012483 case X86ISD::HADD: return "X86ISD::HADD";
12484 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012485 case X86ISD::FHADD: return "X86ISD::FHADD";
12486 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012487 case X86ISD::UMAX: return "X86ISD::UMAX";
12488 case X86ISD::UMIN: return "X86ISD::UMIN";
12489 case X86ISD::SMAX: return "X86ISD::SMAX";
12490 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012491 case X86ISD::FMAX: return "X86ISD::FMAX";
12492 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012493 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12494 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012495 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12496 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012497 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012498 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012499 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012500 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12501 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012502 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012503 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012504 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012505 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012506 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12507 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012508 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12509 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12510 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12511 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12512 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12513 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012514 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012515 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012516 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012517 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12518 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012519 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012520 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012521 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12522 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012523 case X86ISD::VSHL: return "X86ISD::VSHL";
12524 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012525 case X86ISD::VSRA: return "X86ISD::VSRA";
12526 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12527 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12528 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012529 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012530 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12531 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012532 case X86ISD::ADD: return "X86ISD::ADD";
12533 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012534 case X86ISD::ADC: return "X86ISD::ADC";
12535 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012536 case X86ISD::SMUL: return "X86ISD::SMUL";
12537 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012538 case X86ISD::INC: return "X86ISD::INC";
12539 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012540 case X86ISD::OR: return "X86ISD::OR";
12541 case X86ISD::XOR: return "X86ISD::XOR";
12542 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012543 case X86ISD::BLSI: return "X86ISD::BLSI";
12544 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12545 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012546 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012547 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012548 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012549 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012550 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12551 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012552 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012553 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012554 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012555 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012556 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012557 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12558 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012559 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12560 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12561 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012562 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12563 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012564 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12565 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012566 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012567 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012568 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012569 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12570 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012571 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012572 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012573 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012574 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012575 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012576 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012577 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012578 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012579 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012580 case X86ISD::FMADD: return "X86ISD::FMADD";
12581 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12582 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12583 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12584 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12585 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012586 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12587 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Evan Cheng72261582005-12-20 06:22:03 +000012588 }
12589}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012590
Chris Lattnerc9addb72007-03-30 23:15:24 +000012591// isLegalAddressingMode - Return true if the addressing mode represented
12592// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012593bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012594 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012595 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012596 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012597 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012598
Chris Lattnerc9addb72007-03-30 23:15:24 +000012599 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012600 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012601 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012602
Chris Lattnerc9addb72007-03-30 23:15:24 +000012603 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012604 unsigned GVFlags =
12605 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012606
Chris Lattnerdfed4132009-07-10 07:38:24 +000012607 // If a reference to this global requires an extra load, we can't fold it.
12608 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012609 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012610
Chris Lattnerdfed4132009-07-10 07:38:24 +000012611 // If BaseGV requires a register for the PIC base, we cannot also have a
12612 // BaseReg specified.
12613 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012614 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012615
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012616 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012617 if ((M != CodeModel::Small || R != Reloc::Static) &&
12618 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012619 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012620 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012621
Chris Lattnerc9addb72007-03-30 23:15:24 +000012622 switch (AM.Scale) {
12623 case 0:
12624 case 1:
12625 case 2:
12626 case 4:
12627 case 8:
12628 // These scales always work.
12629 break;
12630 case 3:
12631 case 5:
12632 case 9:
12633 // These scales are formed with basereg+scalereg. Only accept if there is
12634 // no basereg yet.
12635 if (AM.HasBaseReg)
12636 return false;
12637 break;
12638 default: // Other stuff never works.
12639 return false;
12640 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012641
Chris Lattnerc9addb72007-03-30 23:15:24 +000012642 return true;
12643}
12644
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012645bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012646 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012647 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012648 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12649 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012650 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012651}
12652
Evan Cheng70e10d32012-07-17 06:53:39 +000012653bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012654 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012655}
12656
12657bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012658 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012659 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012660}
12661
Owen Andersone50ed302009-08-10 22:56:29 +000012662bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012663 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012664 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012665 unsigned NumBits1 = VT1.getSizeInBits();
12666 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012667 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012668}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012669
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012670bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012671 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012672 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012673}
12674
Owen Andersone50ed302009-08-10 22:56:29 +000012675bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012676 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012677 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012678}
12679
Evan Cheng2766a472012-12-06 19:13:27 +000012680bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12681 EVT VT1 = Val.getValueType();
12682 if (isZExtFree(VT1, VT2))
12683 return true;
12684
12685 if (Val.getOpcode() != ISD::LOAD)
12686 return false;
12687
12688 if (!VT1.isSimple() || !VT1.isInteger() ||
12689 !VT2.isSimple() || !VT2.isInteger())
12690 return false;
12691
12692 switch (VT1.getSimpleVT().SimpleTy) {
12693 default: break;
12694 case MVT::i8:
12695 case MVT::i16:
12696 case MVT::i32:
12697 // X86 has 8, 16, and 32-bit zero-extending loads.
12698 return true;
12699 }
12700
12701 return false;
12702}
12703
Owen Andersone50ed302009-08-10 22:56:29 +000012704bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012705 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012706 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012707}
12708
Evan Cheng60c07e12006-07-05 22:17:51 +000012709/// isShuffleMaskLegal - Targets can use this to indicate that they only
12710/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12711/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12712/// are assumed to be legal.
12713bool
Eric Christopherfd179292009-08-27 18:07:15 +000012714X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012715 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012716 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012717 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012718 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012719
Nate Begemana09008b2009-10-19 02:17:23 +000012720 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012721 return (VT.getVectorNumElements() == 2 ||
12722 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12723 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012724 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012725 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012726 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12727 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012728 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012729 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12730 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12731 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12732 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012733}
12734
Dan Gohman7d8143f2008-04-09 20:09:42 +000012735bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012736X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012737 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012738 unsigned NumElts = VT.getVectorNumElements();
12739 // FIXME: This collection of masks seems suspect.
12740 if (NumElts == 2)
12741 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012742 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012743 return (isMOVLMask(Mask, VT) ||
12744 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012745 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12746 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012747 }
12748 return false;
12749}
12750
12751//===----------------------------------------------------------------------===//
12752// X86 Scheduler Hooks
12753//===----------------------------------------------------------------------===//
12754
Michael Liaobe02a902012-11-08 07:28:54 +000012755/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012756static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12757 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012758 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012759
12760 const BasicBlock *BB = MBB->getBasicBlock();
12761 MachineFunction::iterator I = MBB;
12762 ++I;
12763
12764 // For the v = xbegin(), we generate
12765 //
12766 // thisMBB:
12767 // xbegin sinkMBB
12768 //
12769 // mainMBB:
12770 // eax = -1
12771 //
12772 // sinkMBB:
12773 // v = eax
12774
12775 MachineBasicBlock *thisMBB = MBB;
12776 MachineFunction *MF = MBB->getParent();
12777 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12778 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12779 MF->insert(I, mainMBB);
12780 MF->insert(I, sinkMBB);
12781
12782 // Transfer the remainder of BB and its successor edges to sinkMBB.
12783 sinkMBB->splice(sinkMBB->begin(), MBB,
12784 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12785 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12786
12787 // thisMBB:
12788 // xbegin sinkMBB
12789 // # fallthrough to mainMBB
12790 // # abortion to sinkMBB
12791 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12792 thisMBB->addSuccessor(mainMBB);
12793 thisMBB->addSuccessor(sinkMBB);
12794
12795 // mainMBB:
12796 // EAX = -1
12797 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
12798 mainMBB->addSuccessor(sinkMBB);
12799
12800 // sinkMBB:
12801 // EAX is live into the sinkMBB
12802 sinkMBB->addLiveIn(X86::EAX);
12803 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12804 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
12805 .addReg(X86::EAX);
12806
12807 MI->eraseFromParent();
12808 return sinkMBB;
12809}
12810
Michael Liaob118a072012-09-20 03:06:15 +000012811// Get CMPXCHG opcode for the specified data type.
12812static unsigned getCmpXChgOpcode(EVT VT) {
12813 switch (VT.getSimpleVT().SimpleTy) {
12814 case MVT::i8: return X86::LCMPXCHG8;
12815 case MVT::i16: return X86::LCMPXCHG16;
12816 case MVT::i32: return X86::LCMPXCHG32;
12817 case MVT::i64: return X86::LCMPXCHG64;
12818 default:
12819 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000012820 }
Michael Liaob118a072012-09-20 03:06:15 +000012821 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000012822}
12823
Michael Liaob118a072012-09-20 03:06:15 +000012824// Get LOAD opcode for the specified data type.
12825static unsigned getLoadOpcode(EVT VT) {
12826 switch (VT.getSimpleVT().SimpleTy) {
12827 case MVT::i8: return X86::MOV8rm;
12828 case MVT::i16: return X86::MOV16rm;
12829 case MVT::i32: return X86::MOV32rm;
12830 case MVT::i64: return X86::MOV64rm;
12831 default:
12832 break;
12833 }
12834 llvm_unreachable("Invalid operand size!");
12835}
12836
12837// Get opcode of the non-atomic one from the specified atomic instruction.
12838static unsigned getNonAtomicOpcode(unsigned Opc) {
12839 switch (Opc) {
12840 case X86::ATOMAND8: return X86::AND8rr;
12841 case X86::ATOMAND16: return X86::AND16rr;
12842 case X86::ATOMAND32: return X86::AND32rr;
12843 case X86::ATOMAND64: return X86::AND64rr;
12844 case X86::ATOMOR8: return X86::OR8rr;
12845 case X86::ATOMOR16: return X86::OR16rr;
12846 case X86::ATOMOR32: return X86::OR32rr;
12847 case X86::ATOMOR64: return X86::OR64rr;
12848 case X86::ATOMXOR8: return X86::XOR8rr;
12849 case X86::ATOMXOR16: return X86::XOR16rr;
12850 case X86::ATOMXOR32: return X86::XOR32rr;
12851 case X86::ATOMXOR64: return X86::XOR64rr;
12852 }
12853 llvm_unreachable("Unhandled atomic-load-op opcode!");
12854}
12855
12856// Get opcode of the non-atomic one from the specified atomic instruction with
12857// extra opcode.
12858static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
12859 unsigned &ExtraOpc) {
12860 switch (Opc) {
12861 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
12862 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
12863 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
12864 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012865 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012866 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
12867 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
12868 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012869 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012870 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
12871 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
12872 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012873 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012874 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
12875 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
12876 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000012877 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000012878 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
12879 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
12880 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
12881 }
12882 llvm_unreachable("Unhandled atomic-load-op opcode!");
12883}
12884
12885// Get opcode of the non-atomic one from the specified atomic instruction for
12886// 64-bit data type on 32-bit target.
12887static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
12888 switch (Opc) {
12889 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
12890 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
12891 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
12892 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
12893 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
12894 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012895 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
12896 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
12897 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
12898 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000012899 }
12900 llvm_unreachable("Unhandled atomic-load-op opcode!");
12901}
12902
12903// Get opcode of the non-atomic one from the specified atomic instruction for
12904// 64-bit data type on 32-bit target with extra opcode.
12905static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
12906 unsigned &HiOpc,
12907 unsigned &ExtraOpc) {
12908 switch (Opc) {
12909 case X86::ATOMNAND6432:
12910 ExtraOpc = X86::NOT32r;
12911 HiOpc = X86::AND32rr;
12912 return X86::AND32rr;
12913 }
12914 llvm_unreachable("Unhandled atomic-load-op opcode!");
12915}
12916
12917// Get pseudo CMOV opcode from the specified data type.
12918static unsigned getPseudoCMOVOpc(EVT VT) {
12919 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000012920 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000012921 case MVT::i16: return X86::CMOV_GR16;
12922 case MVT::i32: return X86::CMOV_GR32;
12923 default:
12924 break;
12925 }
12926 llvm_unreachable("Unknown CMOV opcode!");
12927}
12928
12929// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
12930// They will be translated into a spin-loop or compare-exchange loop from
12931//
12932// ...
12933// dst = atomic-fetch-op MI.addr, MI.val
12934// ...
12935//
12936// to
12937//
12938// ...
Michael Liaoc537f792013-03-06 00:17:04 +000012939// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000012940// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000012941// t4 = phi(t1, t3 / loop)
12942// t2 = OP MI.val, t4
12943// EAX = t4
12944// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
12945// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000012946// JNE loop
12947// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000012948// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000012949// ...
Mon P Wang63307c32008-05-05 19:05:59 +000012950MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000012951X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
12952 MachineBasicBlock *MBB) const {
12953 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12954 DebugLoc DL = MI->getDebugLoc();
12955
12956 MachineFunction *MF = MBB->getParent();
12957 MachineRegisterInfo &MRI = MF->getRegInfo();
12958
12959 const BasicBlock *BB = MBB->getBasicBlock();
12960 MachineFunction::iterator I = MBB;
12961 ++I;
12962
Michael Liao13d08bf2013-01-22 21:47:38 +000012963 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000012964 "Unexpected number of operands");
12965
12966 assert(MI->hasOneMemOperand() &&
12967 "Expected atomic-load-op to have one memoperand");
12968
12969 // Memory Reference
12970 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12971 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12972
12973 unsigned DstReg, SrcReg;
12974 unsigned MemOpndSlot;
12975
12976 unsigned CurOp = 0;
12977
12978 DstReg = MI->getOperand(CurOp++).getReg();
12979 MemOpndSlot = CurOp;
12980 CurOp += X86::AddrNumOperands;
12981 SrcReg = MI->getOperand(CurOp++).getReg();
12982
12983 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000012984 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000012985 unsigned t1 = MRI.createVirtualRegister(RC);
12986 unsigned t2 = MRI.createVirtualRegister(RC);
12987 unsigned t3 = MRI.createVirtualRegister(RC);
12988 unsigned t4 = MRI.createVirtualRegister(RC);
12989 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000012990
12991 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
12992 unsigned LOADOpc = getLoadOpcode(VT);
12993
12994 // For the atomic load-arith operator, we generate
12995 //
12996 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000012997 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000012998 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000012999 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013000 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013001 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013002 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013003 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013004 // JNE mainMBB
13005 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013006 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013007
13008 MachineBasicBlock *thisMBB = MBB;
13009 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13010 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13011 MF->insert(I, mainMBB);
13012 MF->insert(I, sinkMBB);
13013
13014 MachineInstrBuilder MIB;
13015
13016 // Transfer the remainder of BB and its successor edges to sinkMBB.
13017 sinkMBB->splice(sinkMBB->begin(), MBB,
13018 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13019 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13020
13021 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013022 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13023 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13024 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13025 if (NewMO.isReg())
13026 NewMO.setIsKill(false);
13027 MIB.addOperand(NewMO);
13028 }
13029 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13030 unsigned flags = (*MMOI)->getFlags();
13031 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13032 MachineMemOperand *MMO =
13033 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13034 (*MMOI)->getSize(),
13035 (*MMOI)->getBaseAlignment(),
13036 (*MMOI)->getTBAAInfo(),
13037 (*MMOI)->getRanges());
13038 MIB.addMemOperand(MMO);
13039 }
Michael Liaob118a072012-09-20 03:06:15 +000013040
13041 thisMBB->addSuccessor(mainMBB);
13042
13043 // mainMBB:
13044 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013045
Michael Liaoc537f792013-03-06 00:17:04 +000013046 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013047 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13048 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013049
Michael Liaob118a072012-09-20 03:06:15 +000013050 unsigned Opc = MI->getOpcode();
13051 switch (Opc) {
13052 default:
13053 llvm_unreachable("Unhandled atomic-load-op opcode!");
13054 case X86::ATOMAND8:
13055 case X86::ATOMAND16:
13056 case X86::ATOMAND32:
13057 case X86::ATOMAND64:
13058 case X86::ATOMOR8:
13059 case X86::ATOMOR16:
13060 case X86::ATOMOR32:
13061 case X86::ATOMOR64:
13062 case X86::ATOMXOR8:
13063 case X86::ATOMXOR16:
13064 case X86::ATOMXOR32:
13065 case X86::ATOMXOR64: {
13066 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013067 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13068 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013069 break;
13070 }
13071 case X86::ATOMNAND8:
13072 case X86::ATOMNAND16:
13073 case X86::ATOMNAND32:
13074 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013075 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013076 unsigned NOTOpc;
13077 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013078 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13079 .addReg(t4);
13080 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013081 break;
13082 }
Michael Liao08382492012-09-21 03:00:17 +000013083 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013084 case X86::ATOMMAX16:
13085 case X86::ATOMMAX32:
13086 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013087 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013088 case X86::ATOMMIN16:
13089 case X86::ATOMMIN32:
13090 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013091 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013092 case X86::ATOMUMAX16:
13093 case X86::ATOMUMAX32:
13094 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013095 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013096 case X86::ATOMUMIN16:
13097 case X86::ATOMUMIN32:
13098 case X86::ATOMUMIN64: {
13099 unsigned CMPOpc;
13100 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13101
13102 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13103 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013104 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013105
13106 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013107 if (VT != MVT::i8) {
13108 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013109 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013110 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013111 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013112 } else {
13113 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013114 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13115 const TargetRegisterClass *RC32 =
13116 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013117 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13118 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013119 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013120
13121 unsigned Undef = MRI.createVirtualRegister(RC32);
13122 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13123
13124 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13125 .addReg(Undef)
13126 .addReg(SrcReg)
13127 .addImm(X86::sub_8bit);
13128 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13129 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013130 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013131 .addImm(X86::sub_8bit);
13132
Michael Liaoc537f792013-03-06 00:17:04 +000013133 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013134 .addReg(SrcReg32)
13135 .addReg(AccReg32);
13136
Michael Liaoc537f792013-03-06 00:17:04 +000013137 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13138 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013139 }
Michael Liaob118a072012-09-20 03:06:15 +000013140 } else {
13141 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013142 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013143 "Invalid atomic-load-op transformation!");
13144 unsigned SelOpc = getPseudoCMOVOpc(VT);
13145 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13146 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013147 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13148 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013149 .addImm(CC);
13150 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013151 // Replace the original PHI node as mainMBB is changed after CMOV
13152 // lowering.
13153 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13154 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13155 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013156 }
13157 break;
13158 }
13159 }
13160
Michael Liaoc537f792013-03-06 00:17:04 +000013161 // Copy PhyReg back from virtual register.
13162 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13163 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013164
13165 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013166 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13167 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13168 if (NewMO.isReg())
13169 NewMO.setIsKill(false);
13170 MIB.addOperand(NewMO);
13171 }
13172 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013173 MIB.setMemRefs(MMOBegin, MMOEnd);
13174
Michael Liaoc537f792013-03-06 00:17:04 +000013175 // Copy PhyReg back to virtual register.
13176 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13177 .addReg(PhyReg);
13178
Michael Liaob118a072012-09-20 03:06:15 +000013179 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13180
13181 mainMBB->addSuccessor(origMainMBB);
13182 mainMBB->addSuccessor(sinkMBB);
13183
13184 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013185 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13186 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013187 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013188
13189 MI->eraseFromParent();
13190 return sinkMBB;
13191}
13192
13193// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13194// instructions. They will be translated into a spin-loop or compare-exchange
13195// 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// t1L = LOAD [MI.addr + 0]
13205// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013206// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013207// t4L = phi(t1L, t3L / loop)
13208// t4H = phi(t1H, t3H / loop)
13209// t2L = OP MI.val.lo, t4L
13210// t2H = OP MI.val.hi, t4H
13211// EAX = t4L
13212// EDX = t4H
13213// EBX = t2L
13214// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013215// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013216// t3L = EAX
13217// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013218// JNE loop
13219// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013220// dstL = t3L
13221// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013222// ...
13223MachineBasicBlock *
13224X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13225 MachineBasicBlock *MBB) const {
13226 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13227 DebugLoc DL = MI->getDebugLoc();
13228
13229 MachineFunction *MF = MBB->getParent();
13230 MachineRegisterInfo &MRI = MF->getRegInfo();
13231
13232 const BasicBlock *BB = MBB->getBasicBlock();
13233 MachineFunction::iterator I = MBB;
13234 ++I;
13235
Michael Liao13d08bf2013-01-22 21:47:38 +000013236 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013237 "Unexpected number of operands");
13238
13239 assert(MI->hasOneMemOperand() &&
13240 "Expected atomic-load-op32 to have one memoperand");
13241
13242 // Memory Reference
13243 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13244 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13245
13246 unsigned DstLoReg, DstHiReg;
13247 unsigned SrcLoReg, SrcHiReg;
13248 unsigned MemOpndSlot;
13249
13250 unsigned CurOp = 0;
13251
13252 DstLoReg = MI->getOperand(CurOp++).getReg();
13253 DstHiReg = MI->getOperand(CurOp++).getReg();
13254 MemOpndSlot = CurOp;
13255 CurOp += X86::AddrNumOperands;
13256 SrcLoReg = MI->getOperand(CurOp++).getReg();
13257 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013258
Craig Topperc9099502012-04-20 06:31:50 +000013259 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013260 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013261
Michael Liaoc537f792013-03-06 00:17:04 +000013262 unsigned t1L = MRI.createVirtualRegister(RC);
13263 unsigned t1H = MRI.createVirtualRegister(RC);
13264 unsigned t2L = MRI.createVirtualRegister(RC);
13265 unsigned t2H = MRI.createVirtualRegister(RC);
13266 unsigned t3L = MRI.createVirtualRegister(RC);
13267 unsigned t3H = MRI.createVirtualRegister(RC);
13268 unsigned t4L = MRI.createVirtualRegister(RC);
13269 unsigned t4H = MRI.createVirtualRegister(RC);
13270
Michael Liaob118a072012-09-20 03:06:15 +000013271 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13272 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013273
Michael Liaob118a072012-09-20 03:06:15 +000013274 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013275 //
Michael Liaob118a072012-09-20 03:06:15 +000013276 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013277 // t1L = LOAD [MI.addr + 0]
13278 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013279 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013280 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13281 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13282 // t2L = OP MI.val.lo, t4L
13283 // t2H = OP MI.val.hi, t4H
13284 // EBX = t2L
13285 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013286 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013287 // t3L = EAX
13288 // t3H = EDX
13289 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013290 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013291 // dstL = t3L
13292 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013293
Mon P Wang63307c32008-05-05 19:05:59 +000013294 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013295 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13296 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13297 MF->insert(I, mainMBB);
13298 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013299
Michael Liaob118a072012-09-20 03:06:15 +000013300 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013301
Michael Liaob118a072012-09-20 03:06:15 +000013302 // Transfer the remainder of BB and its successor edges to sinkMBB.
13303 sinkMBB->splice(sinkMBB->begin(), MBB,
13304 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13305 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013306
Michael Liaob118a072012-09-20 03:06:15 +000013307 // thisMBB:
13308 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013309 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013310 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013311 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13312 if (NewMO.isReg())
13313 NewMO.setIsKill(false);
13314 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013315 }
Michael Liaoc537f792013-03-06 00:17:04 +000013316 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13317 unsigned flags = (*MMOI)->getFlags();
13318 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13319 MachineMemOperand *MMO =
13320 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13321 (*MMOI)->getSize(),
13322 (*MMOI)->getBaseAlignment(),
13323 (*MMOI)->getTBAAInfo(),
13324 (*MMOI)->getRanges());
13325 MIB.addMemOperand(MMO);
13326 };
13327 MachineInstr *LowMI = MIB;
13328
13329 // Hi
13330 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13331 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13332 if (i == X86::AddrDisp) {
13333 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13334 } else {
13335 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13336 if (NewMO.isReg())
13337 NewMO.setIsKill(false);
13338 MIB.addOperand(NewMO);
13339 }
13340 }
13341 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013342
Michael Liaob118a072012-09-20 03:06:15 +000013343 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013344
Michael Liaob118a072012-09-20 03:06:15 +000013345 // mainMBB:
13346 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013347
Michael Liaoc537f792013-03-06 00:17:04 +000013348 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013349 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13350 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13351 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13352 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013353
Michael Liaob118a072012-09-20 03:06:15 +000013354 unsigned Opc = MI->getOpcode();
13355 switch (Opc) {
13356 default:
13357 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13358 case X86::ATOMAND6432:
13359 case X86::ATOMOR6432:
13360 case X86::ATOMXOR6432:
13361 case X86::ATOMADD6432:
13362 case X86::ATOMSUB6432: {
13363 unsigned HiOpc;
13364 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013365 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13366 .addReg(SrcLoReg);
13367 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13368 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013369 break;
13370 }
13371 case X86::ATOMNAND6432: {
13372 unsigned HiOpc, NOTOpc;
13373 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013374 unsigned TmpL = MRI.createVirtualRegister(RC);
13375 unsigned TmpH = MRI.createVirtualRegister(RC);
13376 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13377 .addReg(t4L);
13378 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13379 .addReg(t4H);
13380 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13381 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013382 break;
13383 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013384 case X86::ATOMMAX6432:
13385 case X86::ATOMMIN6432:
13386 case X86::ATOMUMAX6432:
13387 case X86::ATOMUMIN6432: {
13388 unsigned HiOpc;
13389 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13390 unsigned cL = MRI.createVirtualRegister(RC8);
13391 unsigned cH = MRI.createVirtualRegister(RC8);
13392 unsigned cL32 = MRI.createVirtualRegister(RC);
13393 unsigned cH32 = MRI.createVirtualRegister(RC);
13394 unsigned cc = MRI.createVirtualRegister(RC);
13395 // cl := cmp src_lo, lo
13396 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013397 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013398 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13399 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13400 // ch := cmp src_hi, hi
13401 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013402 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013403 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13404 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13405 // cc := if (src_hi == hi) ? cl : ch;
13406 if (Subtarget->hasCMov()) {
13407 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13408 .addReg(cH32).addReg(cL32);
13409 } else {
13410 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13411 .addReg(cH32).addReg(cL32)
13412 .addImm(X86::COND_E);
13413 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13414 }
13415 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13416 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013417 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13418 .addReg(SrcLoReg).addReg(t4L);
13419 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13420 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013421 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013422 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13423 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013424 .addImm(X86::COND_NE);
13425 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013426 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13427 // 2nd CMOV lowering.
13428 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013429 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13430 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013431 .addImm(X86::COND_NE);
13432 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013433 // Replace the original PHI node as mainMBB is changed after CMOV
13434 // lowering.
13435 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13436 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13437 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13438 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13439 PhiL->eraseFromParent();
13440 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013441 }
13442 break;
13443 }
Michael Liaob118a072012-09-20 03:06:15 +000013444 case X86::ATOMSWAP6432: {
13445 unsigned HiOpc;
13446 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013447 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13448 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013449 break;
13450 }
13451 }
Mon P Wang63307c32008-05-05 19:05:59 +000013452
Michael Liaob118a072012-09-20 03:06:15 +000013453 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013454 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13455 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013456 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013457 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13458 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013459
Michael Liaob118a072012-09-20 03:06:15 +000013460 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013461 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13462 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13463 if (NewMO.isReg())
13464 NewMO.setIsKill(false);
13465 MIB.addOperand(NewMO);
13466 }
Michael Liaob118a072012-09-20 03:06:15 +000013467 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013468
Michael Liaoc537f792013-03-06 00:17:04 +000013469 // Copy EDX:EAX back to t3H:t3L
13470 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13471 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13472
Michael Liaob118a072012-09-20 03:06:15 +000013473 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013474
Michael Liaob118a072012-09-20 03:06:15 +000013475 mainMBB->addSuccessor(origMainMBB);
13476 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013477
Michael Liaob118a072012-09-20 03:06:15 +000013478 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013479 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13480 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013481 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013482 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13483 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013484 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013485
Michael Liaob118a072012-09-20 03:06:15 +000013486 MI->eraseFromParent();
13487 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013488}
13489
Eric Christopherf83a5de2009-08-27 18:08:16 +000013490// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013491// or XMM0_V32I8 in AVX all of this code can be replaced with that
13492// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013493static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13494 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013495 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013496 switch (MI->getOpcode()) {
13497 default: llvm_unreachable("illegal opcode!");
13498 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13499 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13500 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13501 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13502 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13503 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13504 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13505 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013506 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013507
Craig Topper8aae8dd2012-11-10 08:57:41 +000013508 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013509 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013510
Craig Topper52ea2452012-11-10 09:25:36 +000013511 unsigned NumArgs = MI->getNumOperands();
13512 for (unsigned i = 1; i < NumArgs; ++i) {
13513 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013514 if (!(Op.isReg() && Op.isImplicit()))
13515 MIB.addOperand(Op);
13516 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013517 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013518 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13519
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013520 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013521 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013522 .addReg(X86::XMM0);
13523
Dan Gohman14152b42010-07-06 20:24:04 +000013524 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013525 return BB;
13526}
13527
Craig Topper9c7ae012012-11-10 01:23:36 +000013528// FIXME: Custom handling because TableGen doesn't support multiple implicit
13529// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013530static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13531 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013532 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013533 switch (MI->getOpcode()) {
13534 default: llvm_unreachable("illegal opcode!");
13535 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13536 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13537 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13538 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13539 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13540 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13541 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13542 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013543 }
13544
Craig Topper8aae8dd2012-11-10 08:57:41 +000013545 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013546 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013547
Craig Topper52ea2452012-11-10 09:25:36 +000013548 unsigned NumArgs = MI->getNumOperands(); // remove the results
13549 for (unsigned i = 1; i < NumArgs; ++i) {
13550 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013551 if (!(Op.isReg() && Op.isImplicit()))
13552 MIB.addOperand(Op);
13553 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013554 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013555 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13556
13557 BuildMI(*BB, MI, dl,
13558 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13559 .addReg(X86::ECX);
13560
13561 MI->eraseFromParent();
13562 return BB;
13563}
13564
Craig Topper2da36912012-11-11 22:45:02 +000013565static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13566 const TargetInstrInfo *TII,
13567 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013568 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013569
Eric Christopher228232b2010-11-30 07:20:12 +000013570 // Address into RAX/EAX, other two args into ECX, EDX.
13571 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13572 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13573 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13574 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013575 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013576
Eric Christopher228232b2010-11-30 07:20:12 +000013577 unsigned ValOps = X86::AddrNumOperands;
13578 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13579 .addReg(MI->getOperand(ValOps).getReg());
13580 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13581 .addReg(MI->getOperand(ValOps+1).getReg());
13582
13583 // The instruction doesn't actually take any operands though.
13584 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013585
Eric Christopher228232b2010-11-30 07:20:12 +000013586 MI->eraseFromParent(); // The pseudo is gone now.
13587 return BB;
13588}
13589
13590MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013591X86TargetLowering::EmitVAARG64WithCustomInserter(
13592 MachineInstr *MI,
13593 MachineBasicBlock *MBB) const {
13594 // Emit va_arg instruction on X86-64.
13595
13596 // Operands to this pseudo-instruction:
13597 // 0 ) Output : destination address (reg)
13598 // 1-5) Input : va_list address (addr, i64mem)
13599 // 6 ) ArgSize : Size (in bytes) of vararg type
13600 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13601 // 8 ) Align : Alignment of type
13602 // 9 ) EFLAGS (implicit-def)
13603
13604 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13605 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13606
13607 unsigned DestReg = MI->getOperand(0).getReg();
13608 MachineOperand &Base = MI->getOperand(1);
13609 MachineOperand &Scale = MI->getOperand(2);
13610 MachineOperand &Index = MI->getOperand(3);
13611 MachineOperand &Disp = MI->getOperand(4);
13612 MachineOperand &Segment = MI->getOperand(5);
13613 unsigned ArgSize = MI->getOperand(6).getImm();
13614 unsigned ArgMode = MI->getOperand(7).getImm();
13615 unsigned Align = MI->getOperand(8).getImm();
13616
13617 // Memory Reference
13618 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13619 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13620 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13621
13622 // Machine Information
13623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13624 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13625 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13626 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13627 DebugLoc DL = MI->getDebugLoc();
13628
13629 // struct va_list {
13630 // i32 gp_offset
13631 // i32 fp_offset
13632 // i64 overflow_area (address)
13633 // i64 reg_save_area (address)
13634 // }
13635 // sizeof(va_list) = 24
13636 // alignment(va_list) = 8
13637
13638 unsigned TotalNumIntRegs = 6;
13639 unsigned TotalNumXMMRegs = 8;
13640 bool UseGPOffset = (ArgMode == 1);
13641 bool UseFPOffset = (ArgMode == 2);
13642 unsigned MaxOffset = TotalNumIntRegs * 8 +
13643 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13644
13645 /* Align ArgSize to a multiple of 8 */
13646 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13647 bool NeedsAlign = (Align > 8);
13648
13649 MachineBasicBlock *thisMBB = MBB;
13650 MachineBasicBlock *overflowMBB;
13651 MachineBasicBlock *offsetMBB;
13652 MachineBasicBlock *endMBB;
13653
13654 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13655 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13656 unsigned OffsetReg = 0;
13657
13658 if (!UseGPOffset && !UseFPOffset) {
13659 // If we only pull from the overflow region, we don't create a branch.
13660 // We don't need to alter control flow.
13661 OffsetDestReg = 0; // unused
13662 OverflowDestReg = DestReg;
13663
13664 offsetMBB = NULL;
13665 overflowMBB = thisMBB;
13666 endMBB = thisMBB;
13667 } else {
13668 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13669 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13670 // If not, pull from overflow_area. (branch to overflowMBB)
13671 //
13672 // thisMBB
13673 // | .
13674 // | .
13675 // offsetMBB overflowMBB
13676 // | .
13677 // | .
13678 // endMBB
13679
13680 // Registers for the PHI in endMBB
13681 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13682 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13683
13684 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13685 MachineFunction *MF = MBB->getParent();
13686 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13687 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13688 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13689
13690 MachineFunction::iterator MBBIter = MBB;
13691 ++MBBIter;
13692
13693 // Insert the new basic blocks
13694 MF->insert(MBBIter, offsetMBB);
13695 MF->insert(MBBIter, overflowMBB);
13696 MF->insert(MBBIter, endMBB);
13697
13698 // Transfer the remainder of MBB and its successor edges to endMBB.
13699 endMBB->splice(endMBB->begin(), thisMBB,
13700 llvm::next(MachineBasicBlock::iterator(MI)),
13701 thisMBB->end());
13702 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13703
13704 // Make offsetMBB and overflowMBB successors of thisMBB
13705 thisMBB->addSuccessor(offsetMBB);
13706 thisMBB->addSuccessor(overflowMBB);
13707
13708 // endMBB is a successor of both offsetMBB and overflowMBB
13709 offsetMBB->addSuccessor(endMBB);
13710 overflowMBB->addSuccessor(endMBB);
13711
13712 // Load the offset value into a register
13713 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13714 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13715 .addOperand(Base)
13716 .addOperand(Scale)
13717 .addOperand(Index)
13718 .addDisp(Disp, UseFPOffset ? 4 : 0)
13719 .addOperand(Segment)
13720 .setMemRefs(MMOBegin, MMOEnd);
13721
13722 // Check if there is enough room left to pull this argument.
13723 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13724 .addReg(OffsetReg)
13725 .addImm(MaxOffset + 8 - ArgSizeA8);
13726
13727 // Branch to "overflowMBB" if offset >= max
13728 // Fall through to "offsetMBB" otherwise
13729 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13730 .addMBB(overflowMBB);
13731 }
13732
13733 // In offsetMBB, emit code to use the reg_save_area.
13734 if (offsetMBB) {
13735 assert(OffsetReg != 0);
13736
13737 // Read the reg_save_area address.
13738 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13739 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13740 .addOperand(Base)
13741 .addOperand(Scale)
13742 .addOperand(Index)
13743 .addDisp(Disp, 16)
13744 .addOperand(Segment)
13745 .setMemRefs(MMOBegin, MMOEnd);
13746
13747 // Zero-extend the offset
13748 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13749 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13750 .addImm(0)
13751 .addReg(OffsetReg)
13752 .addImm(X86::sub_32bit);
13753
13754 // Add the offset to the reg_save_area to get the final address.
13755 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13756 .addReg(OffsetReg64)
13757 .addReg(RegSaveReg);
13758
13759 // Compute the offset for the next argument
13760 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13761 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13762 .addReg(OffsetReg)
13763 .addImm(UseFPOffset ? 16 : 8);
13764
13765 // Store it back into the va_list.
13766 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13767 .addOperand(Base)
13768 .addOperand(Scale)
13769 .addOperand(Index)
13770 .addDisp(Disp, UseFPOffset ? 4 : 0)
13771 .addOperand(Segment)
13772 .addReg(NextOffsetReg)
13773 .setMemRefs(MMOBegin, MMOEnd);
13774
13775 // Jump to endMBB
13776 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13777 .addMBB(endMBB);
13778 }
13779
13780 //
13781 // Emit code to use overflow area
13782 //
13783
13784 // Load the overflow_area address into a register.
13785 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13786 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13787 .addOperand(Base)
13788 .addOperand(Scale)
13789 .addOperand(Index)
13790 .addDisp(Disp, 8)
13791 .addOperand(Segment)
13792 .setMemRefs(MMOBegin, MMOEnd);
13793
13794 // If we need to align it, do so. Otherwise, just copy the address
13795 // to OverflowDestReg.
13796 if (NeedsAlign) {
13797 // Align the overflow address
13798 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
13799 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
13800
13801 // aligned_addr = (addr + (align-1)) & ~(align-1)
13802 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
13803 .addReg(OverflowAddrReg)
13804 .addImm(Align-1);
13805
13806 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
13807 .addReg(TmpReg)
13808 .addImm(~(uint64_t)(Align-1));
13809 } else {
13810 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
13811 .addReg(OverflowAddrReg);
13812 }
13813
13814 // Compute the next overflow address after this argument.
13815 // (the overflow address should be kept 8-byte aligned)
13816 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
13817 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
13818 .addReg(OverflowDestReg)
13819 .addImm(ArgSizeA8);
13820
13821 // Store the new overflow address.
13822 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
13823 .addOperand(Base)
13824 .addOperand(Scale)
13825 .addOperand(Index)
13826 .addDisp(Disp, 8)
13827 .addOperand(Segment)
13828 .addReg(NextAddrReg)
13829 .setMemRefs(MMOBegin, MMOEnd);
13830
13831 // If we branched, emit the PHI to the front of endMBB.
13832 if (offsetMBB) {
13833 BuildMI(*endMBB, endMBB->begin(), DL,
13834 TII->get(X86::PHI), DestReg)
13835 .addReg(OffsetDestReg).addMBB(offsetMBB)
13836 .addReg(OverflowDestReg).addMBB(overflowMBB);
13837 }
13838
13839 // Erase the pseudo instruction
13840 MI->eraseFromParent();
13841
13842 return endMBB;
13843}
13844
13845MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000013846X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
13847 MachineInstr *MI,
13848 MachineBasicBlock *MBB) const {
13849 // Emit code to save XMM registers to the stack. The ABI says that the
13850 // number of registers to save is given in %al, so it's theoretically
13851 // possible to do an indirect jump trick to avoid saving all of them,
13852 // however this code takes a simpler approach and just executes all
13853 // of the stores if %al is non-zero. It's less code, and it's probably
13854 // easier on the hardware branch predictor, and stores aren't all that
13855 // expensive anyway.
13856
13857 // Create the new basic blocks. One block contains all the XMM stores,
13858 // and one block is the final destination regardless of whether any
13859 // stores were performed.
13860 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13861 MachineFunction *F = MBB->getParent();
13862 MachineFunction::iterator MBBIter = MBB;
13863 ++MBBIter;
13864 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
13865 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
13866 F->insert(MBBIter, XMMSaveMBB);
13867 F->insert(MBBIter, EndMBB);
13868
Dan Gohman14152b42010-07-06 20:24:04 +000013869 // Transfer the remainder of MBB and its successor edges to EndMBB.
13870 EndMBB->splice(EndMBB->begin(), MBB,
13871 llvm::next(MachineBasicBlock::iterator(MI)),
13872 MBB->end());
13873 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
13874
Dan Gohmand6708ea2009-08-15 01:38:56 +000013875 // The original block will now fall through to the XMM save block.
13876 MBB->addSuccessor(XMMSaveMBB);
13877 // The XMMSaveMBB will fall through to the end block.
13878 XMMSaveMBB->addSuccessor(EndMBB);
13879
13880 // Now add the instructions.
13881 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13882 DebugLoc DL = MI->getDebugLoc();
13883
13884 unsigned CountReg = MI->getOperand(0).getReg();
13885 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
13886 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
13887
13888 if (!Subtarget->isTargetWin64()) {
13889 // If %al is 0, branch around the XMM save block.
13890 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000013891 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013892 MBB->addSuccessor(EndMBB);
13893 }
13894
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000013895 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000013896 // In the XMM save block, save all the XMM argument registers.
13897 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
13898 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000013899 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000013900 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000013901 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000013902 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000013903 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013904 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000013905 .addFrameIndex(RegSaveFrameIndex)
13906 .addImm(/*Scale=*/1)
13907 .addReg(/*IndexReg=*/0)
13908 .addImm(/*Disp=*/Offset)
13909 .addReg(/*Segment=*/0)
13910 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000013911 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000013912 }
13913
Dan Gohman14152b42010-07-06 20:24:04 +000013914 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000013915
13916 return EndMBB;
13917}
Mon P Wang63307c32008-05-05 19:05:59 +000013918
Lang Hames6e3f7e42012-02-03 01:13:49 +000013919// The EFLAGS operand of SelectItr might be missing a kill marker
13920// because there were multiple uses of EFLAGS, and ISel didn't know
13921// which to mark. Figure out whether SelectItr should have had a
13922// kill marker, and set it if it should. Returns the correct kill
13923// marker value.
13924static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
13925 MachineBasicBlock* BB,
13926 const TargetRegisterInfo* TRI) {
13927 // Scan forward through BB for a use/def of EFLAGS.
13928 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
13929 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000013930 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013931 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000013932 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000013933 if (mi.definesRegister(X86::EFLAGS))
13934 break; // Should have kill-flag - update below.
13935 }
13936
13937 // If we hit the end of the block, check whether EFLAGS is live into a
13938 // successor.
13939 if (miI == BB->end()) {
13940 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
13941 sEnd = BB->succ_end();
13942 sItr != sEnd; ++sItr) {
13943 MachineBasicBlock* succ = *sItr;
13944 if (succ->isLiveIn(X86::EFLAGS))
13945 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000013946 }
13947 }
13948
Lang Hames6e3f7e42012-02-03 01:13:49 +000013949 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
13950 // out. SelectMI should have a kill flag on EFLAGS.
13951 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000013952 return true;
13953}
13954
Evan Cheng60c07e12006-07-05 22:17:51 +000013955MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000013956X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013957 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000013958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13959 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000013960
Chris Lattner52600972009-09-02 05:57:00 +000013961 // To "insert" a SELECT_CC instruction, we actually have to insert the
13962 // diamond control-flow pattern. The incoming instruction knows the
13963 // destination vreg to set, the condition code register to branch on, the
13964 // true/false values to select between, and a branch opcode to use.
13965 const BasicBlock *LLVM_BB = BB->getBasicBlock();
13966 MachineFunction::iterator It = BB;
13967 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000013968
Chris Lattner52600972009-09-02 05:57:00 +000013969 // thisMBB:
13970 // ...
13971 // TrueVal = ...
13972 // cmpTY ccX, r1, r2
13973 // bCC copy1MBB
13974 // fallthrough --> copy0MBB
13975 MachineBasicBlock *thisMBB = BB;
13976 MachineFunction *F = BB->getParent();
13977 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
13978 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000013979 F->insert(It, copy0MBB);
13980 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000013981
Bill Wendling730c07e2010-06-25 20:48:10 +000013982 // If the EFLAGS register isn't dead in the terminator, then claim that it's
13983 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000013984 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13985 if (!MI->killsRegister(X86::EFLAGS) &&
13986 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
13987 copy0MBB->addLiveIn(X86::EFLAGS);
13988 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000013989 }
13990
Dan Gohman14152b42010-07-06 20:24:04 +000013991 // Transfer the remainder of BB and its successor edges to sinkMBB.
13992 sinkMBB->splice(sinkMBB->begin(), BB,
13993 llvm::next(MachineBasicBlock::iterator(MI)),
13994 BB->end());
13995 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
13996
13997 // Add the true and fallthrough blocks as its successors.
13998 BB->addSuccessor(copy0MBB);
13999 BB->addSuccessor(sinkMBB);
14000
14001 // Create the conditional branch instruction.
14002 unsigned Opc =
14003 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14004 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14005
Chris Lattner52600972009-09-02 05:57:00 +000014006 // copy0MBB:
14007 // %FalseValue = ...
14008 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014009 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014010
Chris Lattner52600972009-09-02 05:57:00 +000014011 // sinkMBB:
14012 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14013 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014014 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14015 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014016 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14017 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14018
Dan Gohman14152b42010-07-06 20:24:04 +000014019 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014020 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014021}
14022
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014023MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014024X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14025 bool Is64Bit) const {
14026 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14027 DebugLoc DL = MI->getDebugLoc();
14028 MachineFunction *MF = BB->getParent();
14029 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14030
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014031 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014032
14033 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14034 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14035
14036 // BB:
14037 // ... [Till the alloca]
14038 // If stacklet is not large enough, jump to mallocMBB
14039 //
14040 // bumpMBB:
14041 // Allocate by subtracting from RSP
14042 // Jump to continueMBB
14043 //
14044 // mallocMBB:
14045 // Allocate by call to runtime
14046 //
14047 // continueMBB:
14048 // ...
14049 // [rest of original BB]
14050 //
14051
14052 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14053 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14054 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14055
14056 MachineRegisterInfo &MRI = MF->getRegInfo();
14057 const TargetRegisterClass *AddrRegClass =
14058 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14059
14060 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14061 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14062 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014063 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014064 sizeVReg = MI->getOperand(1).getReg(),
14065 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14066
14067 MachineFunction::iterator MBBIter = BB;
14068 ++MBBIter;
14069
14070 MF->insert(MBBIter, bumpMBB);
14071 MF->insert(MBBIter, mallocMBB);
14072 MF->insert(MBBIter, continueMBB);
14073
14074 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14075 (MachineBasicBlock::iterator(MI)), BB->end());
14076 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14077
14078 // Add code to the main basic block to check if the stack limit has been hit,
14079 // and if so, jump to mallocMBB otherwise to bumpMBB.
14080 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014081 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014082 .addReg(tmpSPVReg).addReg(sizeVReg);
14083 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014084 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014085 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014086 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14087
14088 // bumpMBB simply decreases the stack pointer, since we know the current
14089 // stacklet has enough space.
14090 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014091 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014092 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014093 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014094 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14095
14096 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014097 const uint32_t *RegMask =
14098 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014099 if (Is64Bit) {
14100 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14101 .addReg(sizeVReg);
14102 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014103 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014104 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014105 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014106 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014107 } else {
14108 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14109 .addImm(12);
14110 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14111 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014112 .addExternalSymbol("__morestack_allocate_stack_space")
14113 .addRegMask(RegMask)
14114 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014115 }
14116
14117 if (!Is64Bit)
14118 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14119 .addImm(16);
14120
14121 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14122 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14123 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14124
14125 // Set up the CFG correctly.
14126 BB->addSuccessor(bumpMBB);
14127 BB->addSuccessor(mallocMBB);
14128 mallocMBB->addSuccessor(continueMBB);
14129 bumpMBB->addSuccessor(continueMBB);
14130
14131 // Take care of the PHI nodes.
14132 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14133 MI->getOperand(0).getReg())
14134 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14135 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14136
14137 // Delete the original pseudo instruction.
14138 MI->eraseFromParent();
14139
14140 // And we're done.
14141 return continueMBB;
14142}
14143
14144MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014145X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014146 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014147 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14148 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014149
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014150 assert(!Subtarget->isTargetEnvMacho());
14151
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014152 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14153 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014154
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014155 if (Subtarget->isTargetWin64()) {
14156 if (Subtarget->isTargetCygMing()) {
14157 // ___chkstk(Mingw64):
14158 // Clobbers R10, R11, RAX and EFLAGS.
14159 // Updates RSP.
14160 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14161 .addExternalSymbol("___chkstk")
14162 .addReg(X86::RAX, RegState::Implicit)
14163 .addReg(X86::RSP, RegState::Implicit)
14164 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14165 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14166 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14167 } else {
14168 // __chkstk(MSVCRT): does not update stack pointer.
14169 // Clobbers R10, R11 and EFLAGS.
14170 // FIXME: RAX(allocated size) might be reused and not killed.
14171 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14172 .addExternalSymbol("__chkstk")
14173 .addReg(X86::RAX, RegState::Implicit)
14174 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14175 // RAX has the offset to subtracted from RSP.
14176 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14177 .addReg(X86::RSP)
14178 .addReg(X86::RAX);
14179 }
14180 } else {
14181 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014182 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14183
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014184 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14185 .addExternalSymbol(StackProbeSymbol)
14186 .addReg(X86::EAX, RegState::Implicit)
14187 .addReg(X86::ESP, RegState::Implicit)
14188 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14189 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14190 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14191 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014192
Dan Gohman14152b42010-07-06 20:24:04 +000014193 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014194 return BB;
14195}
Chris Lattner52600972009-09-02 05:57:00 +000014196
14197MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014198X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14199 MachineBasicBlock *BB) const {
14200 // This is pretty easy. We're taking the value that we received from
14201 // our load from the relocation, sticking it in either RDI (x86-64)
14202 // or EAX and doing an indirect call. The return value will then
14203 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014204 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014205 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014206 DebugLoc DL = MI->getDebugLoc();
14207 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014208
14209 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014210 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014211
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014212 // Get a register mask for the lowered call.
14213 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14214 // proper register mask.
14215 const uint32_t *RegMask =
14216 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014217 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014218 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14219 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014220 .addReg(X86::RIP)
14221 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014222 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014223 MI->getOperand(3).getTargetFlags())
14224 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014225 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014226 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014227 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014228 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014229 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14230 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014231 .addReg(0)
14232 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014233 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014234 MI->getOperand(3).getTargetFlags())
14235 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014236 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014237 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014238 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014239 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014240 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14241 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014242 .addReg(TII->getGlobalBaseReg(F))
14243 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014244 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014245 MI->getOperand(3).getTargetFlags())
14246 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014247 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014248 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014249 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014250 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014251
Dan Gohman14152b42010-07-06 20:24:04 +000014252 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014253 return BB;
14254}
14255
14256MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014257X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14258 MachineBasicBlock *MBB) const {
14259 DebugLoc DL = MI->getDebugLoc();
14260 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14261
14262 MachineFunction *MF = MBB->getParent();
14263 MachineRegisterInfo &MRI = MF->getRegInfo();
14264
14265 const BasicBlock *BB = MBB->getBasicBlock();
14266 MachineFunction::iterator I = MBB;
14267 ++I;
14268
14269 // Memory Reference
14270 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14271 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14272
14273 unsigned DstReg;
14274 unsigned MemOpndSlot = 0;
14275
14276 unsigned CurOp = 0;
14277
14278 DstReg = MI->getOperand(CurOp++).getReg();
14279 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14280 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14281 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14282 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14283
14284 MemOpndSlot = CurOp;
14285
14286 MVT PVT = getPointerTy();
14287 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14288 "Invalid Pointer Size!");
14289
14290 // For v = setjmp(buf), we generate
14291 //
14292 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014293 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014294 // SjLjSetup restoreMBB
14295 //
14296 // mainMBB:
14297 // v_main = 0
14298 //
14299 // sinkMBB:
14300 // v = phi(main, restore)
14301 //
14302 // restoreMBB:
14303 // v_restore = 1
14304
14305 MachineBasicBlock *thisMBB = MBB;
14306 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14307 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14308 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14309 MF->insert(I, mainMBB);
14310 MF->insert(I, sinkMBB);
14311 MF->push_back(restoreMBB);
14312
14313 MachineInstrBuilder MIB;
14314
14315 // Transfer the remainder of BB and its successor edges to sinkMBB.
14316 sinkMBB->splice(sinkMBB->begin(), MBB,
14317 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14318 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14319
14320 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014321 unsigned PtrStoreOpc = 0;
14322 unsigned LabelReg = 0;
14323 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14324 Reloc::Model RM = getTargetMachine().getRelocationModel();
14325 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14326 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014327
Michael Liao281ae5a2012-10-17 02:22:27 +000014328 // Prepare IP either in reg or imm.
14329 if (!UseImmLabel) {
14330 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14331 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14332 LabelReg = MRI.createVirtualRegister(PtrRC);
14333 if (Subtarget->is64Bit()) {
14334 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14335 .addReg(X86::RIP)
14336 .addImm(0)
14337 .addReg(0)
14338 .addMBB(restoreMBB)
14339 .addReg(0);
14340 } else {
14341 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14342 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14343 .addReg(XII->getGlobalBaseReg(MF))
14344 .addImm(0)
14345 .addReg(0)
14346 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14347 .addReg(0);
14348 }
14349 } else
14350 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014351 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014352 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014353 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14354 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014355 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014356 else
14357 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14358 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014359 if (!UseImmLabel)
14360 MIB.addReg(LabelReg);
14361 else
14362 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014363 MIB.setMemRefs(MMOBegin, MMOEnd);
14364 // Setup
14365 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14366 .addMBB(restoreMBB);
14367 MIB.addRegMask(RegInfo->getNoPreservedMask());
14368 thisMBB->addSuccessor(mainMBB);
14369 thisMBB->addSuccessor(restoreMBB);
14370
14371 // mainMBB:
14372 // EAX = 0
14373 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14374 mainMBB->addSuccessor(sinkMBB);
14375
14376 // sinkMBB:
14377 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14378 TII->get(X86::PHI), DstReg)
14379 .addReg(mainDstReg).addMBB(mainMBB)
14380 .addReg(restoreDstReg).addMBB(restoreMBB);
14381
14382 // restoreMBB:
14383 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14384 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14385 restoreMBB->addSuccessor(sinkMBB);
14386
14387 MI->eraseFromParent();
14388 return sinkMBB;
14389}
14390
14391MachineBasicBlock *
14392X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14393 MachineBasicBlock *MBB) const {
14394 DebugLoc DL = MI->getDebugLoc();
14395 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14396
14397 MachineFunction *MF = MBB->getParent();
14398 MachineRegisterInfo &MRI = MF->getRegInfo();
14399
14400 // Memory Reference
14401 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14402 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14403
14404 MVT PVT = getPointerTy();
14405 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14406 "Invalid Pointer Size!");
14407
14408 const TargetRegisterClass *RC =
14409 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14410 unsigned Tmp = MRI.createVirtualRegister(RC);
14411 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14412 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14413 unsigned SP = RegInfo->getStackRegister();
14414
14415 MachineInstrBuilder MIB;
14416
Michael Liao281ae5a2012-10-17 02:22:27 +000014417 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14418 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014419
14420 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14421 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14422
14423 // Reload FP
14424 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14425 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14426 MIB.addOperand(MI->getOperand(i));
14427 MIB.setMemRefs(MMOBegin, MMOEnd);
14428 // Reload IP
14429 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14430 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14431 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014432 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014433 else
14434 MIB.addOperand(MI->getOperand(i));
14435 }
14436 MIB.setMemRefs(MMOBegin, MMOEnd);
14437 // Reload SP
14438 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14439 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14440 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014441 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014442 else
14443 MIB.addOperand(MI->getOperand(i));
14444 }
14445 MIB.setMemRefs(MMOBegin, MMOEnd);
14446 // Jump
14447 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14448
14449 MI->eraseFromParent();
14450 return MBB;
14451}
14452
14453MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014454X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014455 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014456 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014457 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014458 case X86::TAILJMPd64:
14459 case X86::TAILJMPr64:
14460 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014461 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014462 case X86::TCRETURNdi64:
14463 case X86::TCRETURNri64:
14464 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014465 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014466 case X86::WIN_ALLOCA:
14467 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014468 case X86::SEG_ALLOCA_32:
14469 return EmitLoweredSegAlloca(MI, BB, false);
14470 case X86::SEG_ALLOCA_64:
14471 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014472 case X86::TLSCall_32:
14473 case X86::TLSCall_64:
14474 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014475 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014476 case X86::CMOV_FR32:
14477 case X86::CMOV_FR64:
14478 case X86::CMOV_V4F32:
14479 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014480 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014481 case X86::CMOV_V8F32:
14482 case X86::CMOV_V4F64:
14483 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014484 case X86::CMOV_GR16:
14485 case X86::CMOV_GR32:
14486 case X86::CMOV_RFP32:
14487 case X86::CMOV_RFP64:
14488 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014489 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014490
Dale Johannesen849f2142007-07-03 00:53:03 +000014491 case X86::FP32_TO_INT16_IN_MEM:
14492 case X86::FP32_TO_INT32_IN_MEM:
14493 case X86::FP32_TO_INT64_IN_MEM:
14494 case X86::FP64_TO_INT16_IN_MEM:
14495 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014496 case X86::FP64_TO_INT64_IN_MEM:
14497 case X86::FP80_TO_INT16_IN_MEM:
14498 case X86::FP80_TO_INT32_IN_MEM:
14499 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014500 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14501 DebugLoc DL = MI->getDebugLoc();
14502
Evan Cheng60c07e12006-07-05 22:17:51 +000014503 // Change the floating point control register to use "round towards zero"
14504 // mode when truncating to an integer value.
14505 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014506 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014507 addFrameReference(BuildMI(*BB, MI, DL,
14508 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014509
14510 // Load the old value of the high byte of the control word...
14511 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014512 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014513 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014514 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014515
14516 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014517 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014518 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014519
14520 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014521 addFrameReference(BuildMI(*BB, MI, DL,
14522 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014523
14524 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014525 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014526 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014527
14528 // Get the X86 opcode to use.
14529 unsigned Opc;
14530 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014531 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014532 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14533 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14534 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14535 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14536 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14537 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014538 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14539 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14540 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014541 }
14542
14543 X86AddressMode AM;
14544 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014545 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014546 AM.BaseType = X86AddressMode::RegBase;
14547 AM.Base.Reg = Op.getReg();
14548 } else {
14549 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014550 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014551 }
14552 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014553 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014554 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014555 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014556 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014557 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014558 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014559 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014560 AM.GV = Op.getGlobal();
14561 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014562 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014563 }
Dan Gohman14152b42010-07-06 20:24:04 +000014564 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014565 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014566
14567 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014568 addFrameReference(BuildMI(*BB, MI, DL,
14569 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014570
Dan Gohman14152b42010-07-06 20:24:04 +000014571 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014572 return BB;
14573 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014574 // String/text processing lowering.
14575 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014576 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014577 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014578 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014579 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014580 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014581 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014582 case X86::VPCMPESTRM128MEM:
14583 assert(Subtarget->hasSSE42() &&
14584 "Target must have SSE4.2 or AVX features enabled");
14585 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014586
14587 // String/text processing lowering.
14588 case X86::PCMPISTRIREG:
14589 case X86::VPCMPISTRIREG:
14590 case X86::PCMPISTRIMEM:
14591 case X86::VPCMPISTRIMEM:
14592 case X86::PCMPESTRIREG:
14593 case X86::VPCMPESTRIREG:
14594 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014595 case X86::VPCMPESTRIMEM:
14596 assert(Subtarget->hasSSE42() &&
14597 "Target must have SSE4.2 or AVX features enabled");
14598 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014599
Craig Topper8aae8dd2012-11-10 08:57:41 +000014600 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014601 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014602 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014603
Michael Liaobe02a902012-11-08 07:28:54 +000014604 // xbegin
14605 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014606 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014607
Craig Topper8aae8dd2012-11-10 08:57:41 +000014608 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014609 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014610 case X86::ATOMAND16:
14611 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014612 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014613 // Fall through
14614 case X86::ATOMOR8:
14615 case X86::ATOMOR16:
14616 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014617 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014618 // Fall through
14619 case X86::ATOMXOR16:
14620 case X86::ATOMXOR8:
14621 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014622 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014623 // Fall through
14624 case X86::ATOMNAND8:
14625 case X86::ATOMNAND16:
14626 case X86::ATOMNAND32:
14627 case X86::ATOMNAND64:
14628 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014629 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014630 case X86::ATOMMAX16:
14631 case X86::ATOMMAX32:
14632 case X86::ATOMMAX64:
14633 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014634 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014635 case X86::ATOMMIN16:
14636 case X86::ATOMMIN32:
14637 case X86::ATOMMIN64:
14638 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014639 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014640 case X86::ATOMUMAX16:
14641 case X86::ATOMUMAX32:
14642 case X86::ATOMUMAX64:
14643 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014644 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014645 case X86::ATOMUMIN16:
14646 case X86::ATOMUMIN32:
14647 case X86::ATOMUMIN64:
14648 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014649
14650 // This group does 64-bit operations on a 32-bit host.
14651 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014652 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014653 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014654 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014655 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014656 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014657 case X86::ATOMMAX6432:
14658 case X86::ATOMMIN6432:
14659 case X86::ATOMUMAX6432:
14660 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014661 case X86::ATOMSWAP6432:
14662 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014663
Dan Gohmand6708ea2009-08-15 01:38:56 +000014664 case X86::VASTART_SAVE_XMM_REGS:
14665 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014666
14667 case X86::VAARG_64:
14668 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014669
14670 case X86::EH_SjLj_SetJmp32:
14671 case X86::EH_SjLj_SetJmp64:
14672 return emitEHSjLjSetJmp(MI, BB);
14673
14674 case X86::EH_SjLj_LongJmp32:
14675 case X86::EH_SjLj_LongJmp64:
14676 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014677 }
14678}
14679
14680//===----------------------------------------------------------------------===//
14681// X86 Optimization Hooks
14682//===----------------------------------------------------------------------===//
14683
Dan Gohman475871a2008-07-27 21:46:04 +000014684void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014685 APInt &KnownZero,
14686 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014687 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014688 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014689 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014690 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014691 assert((Opc >= ISD::BUILTIN_OP_END ||
14692 Opc == ISD::INTRINSIC_WO_CHAIN ||
14693 Opc == ISD::INTRINSIC_W_CHAIN ||
14694 Opc == ISD::INTRINSIC_VOID) &&
14695 "Should use MaskedValueIsZero if you don't know whether Op"
14696 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014697
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014698 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014699 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014700 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014701 case X86ISD::ADD:
14702 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014703 case X86ISD::ADC:
14704 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014705 case X86ISD::SMUL:
14706 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014707 case X86ISD::INC:
14708 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014709 case X86ISD::OR:
14710 case X86ISD::XOR:
14711 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014712 // These nodes' second result is a boolean.
14713 if (Op.getResNo() == 0)
14714 break;
14715 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014716 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014717 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014718 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014719 case ISD::INTRINSIC_WO_CHAIN: {
14720 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14721 unsigned NumLoBits = 0;
14722 switch (IntId) {
14723 default: break;
14724 case Intrinsic::x86_sse_movmsk_ps:
14725 case Intrinsic::x86_avx_movmsk_ps_256:
14726 case Intrinsic::x86_sse2_movmsk_pd:
14727 case Intrinsic::x86_avx_movmsk_pd_256:
14728 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014729 case Intrinsic::x86_sse2_pmovmskb_128:
14730 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014731 // High bits of movmskp{s|d}, pmovmskb are known zero.
14732 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014733 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014734 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14735 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14736 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14737 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14738 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14739 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014740 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014741 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014742 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014743 break;
14744 }
14745 }
14746 break;
14747 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014748 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014749}
Chris Lattner259e97c2006-01-31 19:43:35 +000014750
Owen Andersonbc146b02010-09-21 20:42:50 +000014751unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14752 unsigned Depth) const {
14753 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14754 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14755 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014756
Owen Andersonbc146b02010-09-21 20:42:50 +000014757 // Fallback case.
14758 return 1;
14759}
14760
Evan Cheng206ee9d2006-07-07 08:33:52 +000014761/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014762/// node is a GlobalAddress + offset.
14763bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014764 const GlobalValue* &GA,
14765 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014766 if (N->getOpcode() == X86ISD::Wrapper) {
14767 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014768 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014769 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014770 return true;
14771 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014772 }
Evan Chengad4196b2008-05-12 19:56:52 +000014773 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014774}
14775
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014776/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14777/// same as extracting the high 128-bit part of 256-bit vector and then
14778/// inserting the result into the low part of a new 256-bit vector
14779static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14780 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014781 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014782
14783 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014784 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014785 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14786 SVOp->getMaskElt(j) >= 0)
14787 return false;
14788
14789 return true;
14790}
14791
14792/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14793/// same as extracting the low 128-bit part of 256-bit vector and then
14794/// inserting the result into the high part of a new 256-bit vector
14795static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
14796 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014797 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014798
14799 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000014800 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014801 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14802 SVOp->getMaskElt(j) >= 0)
14803 return false;
14804
14805 return true;
14806}
14807
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014808/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
14809static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000014810 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014811 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014812 DebugLoc dl = N->getDebugLoc();
14813 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
14814 SDValue V1 = SVOp->getOperand(0);
14815 SDValue V2 = SVOp->getOperand(1);
14816 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014817 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014818
14819 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
14820 V2.getOpcode() == ISD::CONCAT_VECTORS) {
14821 //
14822 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000014823 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014824 // V UNDEF BUILD_VECTOR UNDEF
14825 // \ / \ /
14826 // CONCAT_VECTOR CONCAT_VECTOR
14827 // \ /
14828 // \ /
14829 // RESULT: V + zero extended
14830 //
14831 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
14832 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
14833 V1.getOperand(1).getOpcode() != ISD::UNDEF)
14834 return SDValue();
14835
14836 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
14837 return SDValue();
14838
14839 // To match the shuffle mask, the first half of the mask should
14840 // be exactly the first vector, and all the rest a splat with the
14841 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000014842 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014843 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
14844 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
14845 return SDValue();
14846
Chad Rosier3d1161e2012-01-03 21:05:52 +000014847 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
14848 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000014849 if (Ld->hasNUsesOfValue(1, 0)) {
14850 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
14851 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
14852 SDValue ResNode =
14853 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
14854 Ld->getMemoryVT(),
14855 Ld->getPointerInfo(),
14856 Ld->getAlignment(),
14857 false/*isVolatile*/, true/*ReadMem*/,
14858 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000014859
14860 // Make sure the newly-created LOAD is in the same position as Ld in
14861 // terms of dependency. We create a TokenFactor for Ld and ResNode,
14862 // and update uses of Ld's output chain to use the TokenFactor.
14863 if (Ld->hasAnyUseOfValue(1)) {
14864 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
14865 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
14866 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
14867 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
14868 SDValue(ResNode.getNode(), 1));
14869 }
14870
Chad Rosier42726832012-05-07 18:47:44 +000014871 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
14872 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000014873 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000014874
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014875 // Emit a zeroed vector and insert the desired subvector on its
14876 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014877 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000014878 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014879 return DCI.CombineTo(N, InsV);
14880 }
14881
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014882 //===--------------------------------------------------------------------===//
14883 // Combine some shuffles into subvector extracts and inserts:
14884 //
14885
14886 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
14887 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014888 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
14889 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014890 return DCI.CombineTo(N, InsV);
14891 }
14892
14893 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
14894 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000014895 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
14896 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014897 return DCI.CombineTo(N, InsV);
14898 }
14899
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014900 return SDValue();
14901}
14902
14903/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000014904static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014905 TargetLowering::DAGCombinerInfo &DCI,
14906 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000014907 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000014908 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000014909
Mon P Wanga0fd0d52010-12-19 23:55:53 +000014910 // Don't create instructions with illegal types after legalize types has run.
14911 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14912 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
14913 return SDValue();
14914
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014915 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014916 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000014917 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000014918 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014919
14920 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000014921 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000014922 return SDValue();
14923
14924 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
14925 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
14926 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000014927 SmallVector<SDValue, 16> Elts;
14928 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000014929 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000014930
Nate Begemanfdea31a2010-03-24 20:49:50 +000014931 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000014932}
Evan Chengd880b972008-05-09 21:53:03 +000014933
Nadav Roteme12bf182013-01-04 17:35:21 +000014934/// PerformTruncateCombine - Converts truncate operation to
14935/// a sequence of vector shuffle operations.
14936/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000014937static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
14938 TargetLowering::DAGCombinerInfo &DCI,
14939 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000014940 return SDValue();
14941}
14942
Craig Topper89f4e662012-03-20 07:17:59 +000014943/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
14944/// specific shuffle of a load can be folded into a single element load.
14945/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
14946/// shuffles have been customed lowered so we need to handle those here.
14947static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
14948 TargetLowering::DAGCombinerInfo &DCI) {
14949 if (DCI.isBeforeLegalizeOps())
14950 return SDValue();
14951
14952 SDValue InVec = N->getOperand(0);
14953 SDValue EltNo = N->getOperand(1);
14954
14955 if (!isa<ConstantSDNode>(EltNo))
14956 return SDValue();
14957
14958 EVT VT = InVec.getValueType();
14959
14960 bool HasShuffleIntoBitcast = false;
14961 if (InVec.getOpcode() == ISD::BITCAST) {
14962 // Don't duplicate a load with other uses.
14963 if (!InVec.hasOneUse())
14964 return SDValue();
14965 EVT BCVT = InVec.getOperand(0).getValueType();
14966 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
14967 return SDValue();
14968 InVec = InVec.getOperand(0);
14969 HasShuffleIntoBitcast = true;
14970 }
14971
14972 if (!isTargetShuffle(InVec.getOpcode()))
14973 return SDValue();
14974
14975 // Don't duplicate a load with other uses.
14976 if (!InVec.hasOneUse())
14977 return SDValue();
14978
14979 SmallVector<int, 16> ShuffleMask;
14980 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000014981 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
14982 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000014983 return SDValue();
14984
14985 // Select the input vector, guarding against out of range extract vector.
14986 unsigned NumElems = VT.getVectorNumElements();
14987 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
14988 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
14989 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
14990 : InVec.getOperand(1);
14991
14992 // If inputs to shuffle are the same for both ops, then allow 2 uses
14993 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
14994
14995 if (LdNode.getOpcode() == ISD::BITCAST) {
14996 // Don't duplicate a load with other uses.
14997 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
14998 return SDValue();
14999
15000 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15001 LdNode = LdNode.getOperand(0);
15002 }
15003
15004 if (!ISD::isNormalLoad(LdNode.getNode()))
15005 return SDValue();
15006
15007 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15008
15009 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15010 return SDValue();
15011
15012 if (HasShuffleIntoBitcast) {
15013 // If there's a bitcast before the shuffle, check if the load type and
15014 // alignment is valid.
15015 unsigned Align = LN0->getAlignment();
15016 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015017 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015018 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15019
15020 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15021 return SDValue();
15022 }
15023
15024 // All checks match so transform back to vector_shuffle so that DAG combiner
15025 // can finish the job
15026 DebugLoc dl = N->getDebugLoc();
15027
15028 // Create shuffle node taking into account the case that its a unary shuffle
15029 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15030 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15031 InVec.getOperand(0), Shuffle,
15032 &ShuffleMask[0]);
15033 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15034 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15035 EltNo);
15036}
15037
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015038/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15039/// generation and convert it from being a bunch of shuffles and extracts
15040/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015041static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015042 TargetLowering::DAGCombinerInfo &DCI) {
15043 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15044 if (NewOp.getNode())
15045 return NewOp;
15046
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015047 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015048 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15049 // from mmx to v2i32 has a single usage.
15050 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15051 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15052 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
15053 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
15054 N->getValueType(0),
15055 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015056
15057 // Only operate on vectors of 4 elements, where the alternative shuffling
15058 // gets to be more expensive.
15059 if (InputVector.getValueType() != MVT::v4i32)
15060 return SDValue();
15061
15062 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15063 // single use which is a sign-extend or zero-extend, and all elements are
15064 // used.
15065 SmallVector<SDNode *, 4> Uses;
15066 unsigned ExtractedElements = 0;
15067 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15068 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15069 if (UI.getUse().getResNo() != InputVector.getResNo())
15070 return SDValue();
15071
15072 SDNode *Extract = *UI;
15073 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15074 return SDValue();
15075
15076 if (Extract->getValueType(0) != MVT::i32)
15077 return SDValue();
15078 if (!Extract->hasOneUse())
15079 return SDValue();
15080 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15081 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15082 return SDValue();
15083 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15084 return SDValue();
15085
15086 // Record which element was extracted.
15087 ExtractedElements |=
15088 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15089
15090 Uses.push_back(Extract);
15091 }
15092
15093 // If not all the elements were used, this may not be worthwhile.
15094 if (ExtractedElements != 15)
15095 return SDValue();
15096
15097 // Ok, we've now decided to do the transformation.
15098 DebugLoc dl = InputVector.getDebugLoc();
15099
15100 // Store the value to a temporary stack slot.
15101 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015102 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15103 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015104
15105 // Replace each use (extract) with a load of the appropriate element.
15106 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15107 UE = Uses.end(); UI != UE; ++UI) {
15108 SDNode *Extract = *UI;
15109
Nadav Rotem86694292011-05-17 08:31:57 +000015110 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015111 SDValue Idx = Extract->getOperand(1);
15112 unsigned EltSize =
15113 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15114 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015115 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015116 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15117
Nadav Rotem86694292011-05-17 08:31:57 +000015118 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015119 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015120
15121 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015122 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015123 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015124 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015125
15126 // Replace the exact with the load.
15127 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15128 }
15129
15130 // The replacement was made in place; don't return anything.
15131 return SDValue();
15132}
15133
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015134/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15135static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15136 SDValue RHS, SelectionDAG &DAG,
15137 const X86Subtarget *Subtarget) {
15138 if (!VT.isVector())
15139 return 0;
15140
15141 switch (VT.getSimpleVT().SimpleTy) {
15142 default: return 0;
15143 case MVT::v32i8:
15144 case MVT::v16i16:
15145 case MVT::v8i32:
15146 if (!Subtarget->hasAVX2())
15147 return 0;
15148 case MVT::v16i8:
15149 case MVT::v8i16:
15150 case MVT::v4i32:
15151 if (!Subtarget->hasSSE2())
15152 return 0;
15153 }
15154
15155 // SSE2 has only a small subset of the operations.
15156 bool hasUnsigned = Subtarget->hasSSE41() ||
15157 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15158 bool hasSigned = Subtarget->hasSSE41() ||
15159 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15160
15161 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15162
15163 // Check for x CC y ? x : y.
15164 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15165 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15166 switch (CC) {
15167 default: break;
15168 case ISD::SETULT:
15169 case ISD::SETULE:
15170 return hasUnsigned ? X86ISD::UMIN : 0;
15171 case ISD::SETUGT:
15172 case ISD::SETUGE:
15173 return hasUnsigned ? X86ISD::UMAX : 0;
15174 case ISD::SETLT:
15175 case ISD::SETLE:
15176 return hasSigned ? X86ISD::SMIN : 0;
15177 case ISD::SETGT:
15178 case ISD::SETGE:
15179 return hasSigned ? X86ISD::SMAX : 0;
15180 }
15181 // Check for x CC y ? y : x -- a min/max with reversed arms.
15182 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15183 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15184 switch (CC) {
15185 default: break;
15186 case ISD::SETULT:
15187 case ISD::SETULE:
15188 return hasUnsigned ? X86ISD::UMAX : 0;
15189 case ISD::SETUGT:
15190 case ISD::SETUGE:
15191 return hasUnsigned ? X86ISD::UMIN : 0;
15192 case ISD::SETLT:
15193 case ISD::SETLE:
15194 return hasSigned ? X86ISD::SMAX : 0;
15195 case ISD::SETGT:
15196 case ISD::SETGE:
15197 return hasSigned ? X86ISD::SMIN : 0;
15198 }
15199 }
15200
15201 return 0;
15202}
15203
Duncan Sands6bcd2192011-09-17 16:49:39 +000015204/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15205/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015206static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015207 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015208 const X86Subtarget *Subtarget) {
15209 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015210 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015211 // Get the LHS/RHS of the select.
15212 SDValue LHS = N->getOperand(1);
15213 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015214 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015215
Dan Gohman670e5392009-09-21 18:03:22 +000015216 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015217 // instructions match the semantics of the common C idiom x<y?x:y but not
15218 // x<=y?x:y, because of how they handle negative zero (which can be
15219 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015220 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15221 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015222 (Subtarget->hasSSE2() ||
15223 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015224 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015225
Chris Lattner47b4ce82009-03-11 05:48:52 +000015226 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015227 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015228 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15229 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015230 switch (CC) {
15231 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015232 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015233 // Converting this to a min would handle NaNs incorrectly, and swapping
15234 // the operands would cause it to handle comparisons between positive
15235 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015236 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015237 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015238 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15239 break;
15240 std::swap(LHS, RHS);
15241 }
Dan Gohman670e5392009-09-21 18:03:22 +000015242 Opcode = X86ISD::FMIN;
15243 break;
15244 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015245 // Converting this to a min would handle comparisons between positive
15246 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015247 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015248 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15249 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015250 Opcode = X86ISD::FMIN;
15251 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015252 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015253 // Converting this to a min would handle both negative zeros and NaNs
15254 // incorrectly, but we can swap the operands to fix both.
15255 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015256 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015257 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015258 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015259 Opcode = X86ISD::FMIN;
15260 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015261
Dan Gohman670e5392009-09-21 18:03:22 +000015262 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015263 // Converting this to a max would handle comparisons between positive
15264 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015265 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015266 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015267 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015268 Opcode = X86ISD::FMAX;
15269 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015270 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015271 // Converting this to a max would handle NaNs incorrectly, and swapping
15272 // the operands would cause it to handle comparisons between positive
15273 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015274 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015275 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015276 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15277 break;
15278 std::swap(LHS, RHS);
15279 }
Dan Gohman670e5392009-09-21 18:03:22 +000015280 Opcode = X86ISD::FMAX;
15281 break;
15282 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015283 // Converting this to a max would handle both negative zeros and NaNs
15284 // incorrectly, but we can swap the operands to fix both.
15285 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015286 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015287 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015288 case ISD::SETGE:
15289 Opcode = X86ISD::FMAX;
15290 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015291 }
Dan Gohman670e5392009-09-21 18:03:22 +000015292 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015293 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15294 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015295 switch (CC) {
15296 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015297 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015298 // Converting this to a min would handle comparisons between positive
15299 // and negative zero incorrectly, and swapping the operands would
15300 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015301 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015302 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015303 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015304 break;
15305 std::swap(LHS, RHS);
15306 }
Dan Gohman670e5392009-09-21 18:03:22 +000015307 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015308 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015309 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015310 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015311 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015312 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15313 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015314 Opcode = X86ISD::FMIN;
15315 break;
15316 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015317 // Converting this to a min would handle both negative zeros and NaNs
15318 // incorrectly, but we can swap the operands to fix both.
15319 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015320 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015321 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015322 case ISD::SETGE:
15323 Opcode = X86ISD::FMIN;
15324 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015325
Dan Gohman670e5392009-09-21 18:03:22 +000015326 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015327 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015328 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015329 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015330 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015331 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015332 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015333 // Converting this to a max would handle comparisons between positive
15334 // and negative zero incorrectly, and swapping the operands would
15335 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015336 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015337 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015338 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015339 break;
15340 std::swap(LHS, RHS);
15341 }
Dan Gohman670e5392009-09-21 18:03:22 +000015342 Opcode = X86ISD::FMAX;
15343 break;
15344 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015345 // Converting this to a max would handle both negative zeros and NaNs
15346 // incorrectly, but we can swap the operands to fix both.
15347 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015348 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015349 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015350 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015351 Opcode = X86ISD::FMAX;
15352 break;
15353 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015354 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015355
Chris Lattner47b4ce82009-03-11 05:48:52 +000015356 if (Opcode)
15357 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015358 }
Eric Christopherfd179292009-08-27 18:07:15 +000015359
Chris Lattnerd1980a52009-03-12 06:52:53 +000015360 // If this is a select between two integer constants, try to do some
15361 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015362 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15363 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015364 // Don't do this for crazy integer types.
15365 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15366 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015367 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015368 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015369
Chris Lattnercee56e72009-03-13 05:53:31 +000015370 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015371 // Efficiently invertible.
15372 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15373 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15374 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15375 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015376 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015377 }
Eric Christopherfd179292009-08-27 18:07:15 +000015378
Chris Lattnerd1980a52009-03-12 06:52:53 +000015379 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015380 if (FalseC->getAPIntValue() == 0 &&
15381 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015382 if (NeedsCondInvert) // Invert the condition if needed.
15383 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15384 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015385
Chris Lattnerd1980a52009-03-12 06:52:53 +000015386 // Zero extend the condition if needed.
15387 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015388
Chris Lattnercee56e72009-03-13 05:53:31 +000015389 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015390 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015391 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015392 }
Eric Christopherfd179292009-08-27 18:07:15 +000015393
Chris Lattner97a29a52009-03-13 05:22:11 +000015394 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015395 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015396 if (NeedsCondInvert) // Invert the condition if needed.
15397 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15398 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015399
Chris Lattner97a29a52009-03-13 05:22:11 +000015400 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015401 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15402 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015403 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015404 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015405 }
Eric Christopherfd179292009-08-27 18:07:15 +000015406
Chris Lattnercee56e72009-03-13 05:53:31 +000015407 // Optimize cases that will turn into an LEA instruction. This requires
15408 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015409 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015410 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015411 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015412
Chris Lattnercee56e72009-03-13 05:53:31 +000015413 bool isFastMultiplier = false;
15414 if (Diff < 10) {
15415 switch ((unsigned char)Diff) {
15416 default: break;
15417 case 1: // result = add base, cond
15418 case 2: // result = lea base( , cond*2)
15419 case 3: // result = lea base(cond, cond*2)
15420 case 4: // result = lea base( , cond*4)
15421 case 5: // result = lea base(cond, cond*4)
15422 case 8: // result = lea base( , cond*8)
15423 case 9: // result = lea base(cond, cond*8)
15424 isFastMultiplier = true;
15425 break;
15426 }
15427 }
Eric Christopherfd179292009-08-27 18:07:15 +000015428
Chris Lattnercee56e72009-03-13 05:53:31 +000015429 if (isFastMultiplier) {
15430 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15431 if (NeedsCondInvert) // Invert the condition if needed.
15432 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15433 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015434
Chris Lattnercee56e72009-03-13 05:53:31 +000015435 // Zero extend the condition if needed.
15436 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15437 Cond);
15438 // Scale the condition by the difference.
15439 if (Diff != 1)
15440 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15441 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015442
Chris Lattnercee56e72009-03-13 05:53:31 +000015443 // Add the base if non-zero.
15444 if (FalseC->getAPIntValue() != 0)
15445 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15446 SDValue(FalseC, 0));
15447 return Cond;
15448 }
Eric Christopherfd179292009-08-27 18:07:15 +000015449 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015450 }
15451 }
Eric Christopherfd179292009-08-27 18:07:15 +000015452
Evan Cheng56f582d2012-01-04 01:41:39 +000015453 // Canonicalize max and min:
15454 // (x > y) ? x : y -> (x >= y) ? x : y
15455 // (x < y) ? x : y -> (x <= y) ? x : y
15456 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15457 // the need for an extra compare
15458 // against zero. e.g.
15459 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15460 // subl %esi, %edi
15461 // testl %edi, %edi
15462 // movl $0, %eax
15463 // cmovgl %edi, %eax
15464 // =>
15465 // xorl %eax, %eax
15466 // subl %esi, $edi
15467 // cmovsl %eax, %edi
15468 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15469 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15470 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15471 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15472 switch (CC) {
15473 default: break;
15474 case ISD::SETLT:
15475 case ISD::SETGT: {
15476 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15477 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15478 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15479 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15480 }
15481 }
15482 }
15483
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015484 // Match VSELECTs into subs with unsigned saturation.
15485 if (!DCI.isBeforeLegalize() &&
15486 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15487 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15488 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15489 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15490 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15491
15492 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15493 // left side invert the predicate to simplify logic below.
15494 SDValue Other;
15495 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15496 Other = RHS;
15497 CC = ISD::getSetCCInverse(CC, true);
15498 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15499 Other = LHS;
15500 }
15501
15502 if (Other.getNode() && Other->getNumOperands() == 2 &&
15503 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15504 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15505 SDValue CondRHS = Cond->getOperand(1);
15506
15507 // Look for a general sub with unsigned saturation first.
15508 // x >= y ? x-y : 0 --> subus x, y
15509 // x > y ? x-y : 0 --> subus x, y
15510 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15511 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15512 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15513
15514 // If the RHS is a constant we have to reverse the const canonicalization.
15515 // x > C-1 ? x+-C : 0 --> subus x, C
15516 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15517 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15518 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015519 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015520 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015521 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015522 }
15523
15524 // Another special case: If C was a sign bit, the sub has been
15525 // canonicalized into a xor.
15526 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15527 // it's safe to decanonicalize the xor?
15528 // x s< 0 ? x^C : 0 --> subus x, C
15529 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15530 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15531 isSplatVector(OpRHS.getNode())) {
15532 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15533 if (A.isSignBit())
15534 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15535 }
15536 }
15537 }
15538
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015539 // Try to match a min/max vector operation.
15540 if (!DCI.isBeforeLegalize() &&
15541 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15542 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15543 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15544
Nadav Rotemcc616562012-01-15 19:27:55 +000015545 // If we know that this node is legal then we know that it is going to be
15546 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15547 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15548 // to simplify previous instructions.
15549 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15550 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015551 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015552 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015553
15554 // Don't optimize vector selects that map to mask-registers.
15555 if (BitWidth == 1)
15556 return SDValue();
15557
Nadav Rotemcc616562012-01-15 19:27:55 +000015558 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15559 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15560
15561 APInt KnownZero, KnownOne;
15562 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15563 DCI.isBeforeLegalizeOps());
15564 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15565 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15566 DCI.CommitTargetLoweringOpt(TLO);
15567 }
15568
Dan Gohman475871a2008-07-27 21:46:04 +000015569 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015570}
15571
Michael Liao2a33cec2012-08-10 19:58:13 +000015572// Check whether a boolean test is testing a boolean value generated by
15573// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15574// code.
15575//
15576// Simplify the following patterns:
15577// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15578// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15579// to (Op EFLAGS Cond)
15580//
15581// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15582// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15583// to (Op EFLAGS !Cond)
15584//
15585// where Op could be BRCOND or CMOV.
15586//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015587static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015588 // Quit if not CMP and SUB with its value result used.
15589 if (Cmp.getOpcode() != X86ISD::CMP &&
15590 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15591 return SDValue();
15592
15593 // Quit if not used as a boolean value.
15594 if (CC != X86::COND_E && CC != X86::COND_NE)
15595 return SDValue();
15596
15597 // Check CMP operands. One of them should be 0 or 1 and the other should be
15598 // an SetCC or extended from it.
15599 SDValue Op1 = Cmp.getOperand(0);
15600 SDValue Op2 = Cmp.getOperand(1);
15601
15602 SDValue SetCC;
15603 const ConstantSDNode* C = 0;
15604 bool needOppositeCond = (CC == X86::COND_E);
15605
15606 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15607 SetCC = Op2;
15608 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15609 SetCC = Op1;
15610 else // Quit if all operands are not constants.
15611 return SDValue();
15612
15613 if (C->getZExtValue() == 1)
15614 needOppositeCond = !needOppositeCond;
15615 else if (C->getZExtValue() != 0)
15616 // Quit if the constant is neither 0 or 1.
15617 return SDValue();
15618
15619 // Skip 'zext' node.
15620 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
15621 SetCC = SetCC.getOperand(0);
15622
Michael Liao7fdc66b2012-09-10 16:36:16 +000015623 switch (SetCC.getOpcode()) {
15624 case X86ISD::SETCC:
15625 // Set the condition code or opposite one if necessary.
15626 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15627 if (needOppositeCond)
15628 CC = X86::GetOppositeBranchCondition(CC);
15629 return SetCC.getOperand(1);
15630 case X86ISD::CMOV: {
15631 // Check whether false/true value has canonical one, i.e. 0 or 1.
15632 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15633 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15634 // Quit if true value is not a constant.
15635 if (!TVal)
15636 return SDValue();
15637 // Quit if false value is not a constant.
15638 if (!FVal) {
15639 // A special case for rdrand, where 0 is set if false cond is found.
15640 SDValue Op = SetCC.getOperand(0);
15641 if (Op.getOpcode() != X86ISD::RDRAND)
15642 return SDValue();
15643 }
15644 // Quit if false value is not the constant 0 or 1.
15645 bool FValIsFalse = true;
15646 if (FVal && FVal->getZExtValue() != 0) {
15647 if (FVal->getZExtValue() != 1)
15648 return SDValue();
15649 // If FVal is 1, opposite cond is needed.
15650 needOppositeCond = !needOppositeCond;
15651 FValIsFalse = false;
15652 }
15653 // Quit if TVal is not the constant opposite of FVal.
15654 if (FValIsFalse && TVal->getZExtValue() != 1)
15655 return SDValue();
15656 if (!FValIsFalse && TVal->getZExtValue() != 0)
15657 return SDValue();
15658 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15659 if (needOppositeCond)
15660 CC = X86::GetOppositeBranchCondition(CC);
15661 return SetCC.getOperand(3);
15662 }
15663 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015664
Michael Liao7fdc66b2012-09-10 16:36:16 +000015665 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015666}
15667
Chris Lattnerd1980a52009-03-12 06:52:53 +000015668/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15669static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015670 TargetLowering::DAGCombinerInfo &DCI,
15671 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015672 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015673
Chris Lattnerd1980a52009-03-12 06:52:53 +000015674 // If the flag operand isn't dead, don't touch this CMOV.
15675 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15676 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015677
Evan Chengb5a55d92011-05-24 01:48:22 +000015678 SDValue FalseOp = N->getOperand(0);
15679 SDValue TrueOp = N->getOperand(1);
15680 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15681 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015682
Evan Chengb5a55d92011-05-24 01:48:22 +000015683 if (CC == X86::COND_E || CC == X86::COND_NE) {
15684 switch (Cond.getOpcode()) {
15685 default: break;
15686 case X86ISD::BSR:
15687 case X86ISD::BSF:
15688 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15689 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15690 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15691 }
15692 }
15693
Michael Liao2a33cec2012-08-10 19:58:13 +000015694 SDValue Flags;
15695
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015696 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015697 if (Flags.getNode() &&
15698 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015699 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015700 SDValue Ops[] = { FalseOp, TrueOp,
15701 DAG.getConstant(CC, MVT::i8), Flags };
15702 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15703 Ops, array_lengthof(Ops));
15704 }
15705
Chris Lattnerd1980a52009-03-12 06:52:53 +000015706 // If this is a select between two integer constants, try to do some
15707 // optimizations. Note that the operands are ordered the opposite of SELECT
15708 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015709 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15710 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015711 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15712 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015713 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15714 CC = X86::GetOppositeBranchCondition(CC);
15715 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015716 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015717 }
Eric Christopherfd179292009-08-27 18:07:15 +000015718
Chris Lattnerd1980a52009-03-12 06:52:53 +000015719 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015720 // This is efficient for any integer data type (including i8/i16) and
15721 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015722 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015723 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15724 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015725
Chris Lattnerd1980a52009-03-12 06:52:53 +000015726 // Zero extend the condition if needed.
15727 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015728
Chris Lattnerd1980a52009-03-12 06:52:53 +000015729 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15730 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015731 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015732 if (N->getNumValues() == 2) // Dead flag value?
15733 return DCI.CombineTo(N, Cond, SDValue());
15734 return Cond;
15735 }
Eric Christopherfd179292009-08-27 18:07:15 +000015736
Chris Lattnercee56e72009-03-13 05:53:31 +000015737 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15738 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015739 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015740 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15741 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015742
Chris Lattner97a29a52009-03-13 05:22:11 +000015743 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015744 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15745 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015746 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15747 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015748
Chris Lattner97a29a52009-03-13 05:22:11 +000015749 if (N->getNumValues() == 2) // Dead flag value?
15750 return DCI.CombineTo(N, Cond, SDValue());
15751 return Cond;
15752 }
Eric Christopherfd179292009-08-27 18:07:15 +000015753
Chris Lattnercee56e72009-03-13 05:53:31 +000015754 // Optimize cases that will turn into an LEA instruction. This requires
15755 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015756 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015757 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015758 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015759
Chris Lattnercee56e72009-03-13 05:53:31 +000015760 bool isFastMultiplier = false;
15761 if (Diff < 10) {
15762 switch ((unsigned char)Diff) {
15763 default: break;
15764 case 1: // result = add base, cond
15765 case 2: // result = lea base( , cond*2)
15766 case 3: // result = lea base(cond, cond*2)
15767 case 4: // result = lea base( , cond*4)
15768 case 5: // result = lea base(cond, cond*4)
15769 case 8: // result = lea base( , cond*8)
15770 case 9: // result = lea base(cond, cond*8)
15771 isFastMultiplier = true;
15772 break;
15773 }
15774 }
Eric Christopherfd179292009-08-27 18:07:15 +000015775
Chris Lattnercee56e72009-03-13 05:53:31 +000015776 if (isFastMultiplier) {
15777 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015778 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15779 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015780 // Zero extend the condition if needed.
15781 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15782 Cond);
15783 // Scale the condition by the difference.
15784 if (Diff != 1)
15785 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15786 DAG.getConstant(Diff, Cond.getValueType()));
15787
15788 // Add the base if non-zero.
15789 if (FalseC->getAPIntValue() != 0)
15790 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15791 SDValue(FalseC, 0));
15792 if (N->getNumValues() == 2) // Dead flag value?
15793 return DCI.CombineTo(N, Cond, SDValue());
15794 return Cond;
15795 }
Eric Christopherfd179292009-08-27 18:07:15 +000015796 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015797 }
15798 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015799
15800 // Handle these cases:
15801 // (select (x != c), e, c) -> select (x != c), e, x),
15802 // (select (x == c), c, e) -> select (x == c), x, e)
15803 // where the c is an integer constant, and the "select" is the combination
15804 // of CMOV and CMP.
15805 //
15806 // The rationale for this change is that the conditional-move from a constant
15807 // needs two instructions, however, conditional-move from a register needs
15808 // only one instruction.
15809 //
15810 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
15811 // some instruction-combining opportunities. This opt needs to be
15812 // postponed as late as possible.
15813 //
15814 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
15815 // the DCI.xxxx conditions are provided to postpone the optimization as
15816 // late as possible.
15817
15818 ConstantSDNode *CmpAgainst = 0;
15819 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
15820 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000015821 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015822
15823 if (CC == X86::COND_NE &&
15824 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
15825 CC = X86::GetOppositeBranchCondition(CC);
15826 std::swap(TrueOp, FalseOp);
15827 }
15828
15829 if (CC == X86::COND_E &&
15830 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
15831 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
15832 DAG.getConstant(CC, MVT::i8), Cond };
15833 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
15834 array_lengthof(Ops));
15835 }
15836 }
15837 }
15838
Chris Lattnerd1980a52009-03-12 06:52:53 +000015839 return SDValue();
15840}
15841
Evan Cheng0b0cd912009-03-28 05:57:29 +000015842/// PerformMulCombine - Optimize a single multiply with constant into two
15843/// in order to implement it with two cheaper instructions, e.g.
15844/// LEA + SHL, LEA + LEA.
15845static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
15846 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000015847 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15848 return SDValue();
15849
Owen Andersone50ed302009-08-10 22:56:29 +000015850 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000015851 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000015852 return SDValue();
15853
15854 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
15855 if (!C)
15856 return SDValue();
15857 uint64_t MulAmt = C->getZExtValue();
15858 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
15859 return SDValue();
15860
15861 uint64_t MulAmt1 = 0;
15862 uint64_t MulAmt2 = 0;
15863 if ((MulAmt % 9) == 0) {
15864 MulAmt1 = 9;
15865 MulAmt2 = MulAmt / 9;
15866 } else if ((MulAmt % 5) == 0) {
15867 MulAmt1 = 5;
15868 MulAmt2 = MulAmt / 5;
15869 } else if ((MulAmt % 3) == 0) {
15870 MulAmt1 = 3;
15871 MulAmt2 = MulAmt / 3;
15872 }
15873 if (MulAmt2 &&
15874 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
15875 DebugLoc DL = N->getDebugLoc();
15876
15877 if (isPowerOf2_64(MulAmt2) &&
15878 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
15879 // If second multiplifer is pow2, issue it first. We want the multiply by
15880 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
15881 // is an add.
15882 std::swap(MulAmt1, MulAmt2);
15883
15884 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000015885 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015886 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000015887 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000015888 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015889 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000015890 DAG.getConstant(MulAmt1, VT));
15891
Eric Christopherfd179292009-08-27 18:07:15 +000015892 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000015893 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000015894 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000015895 else
Evan Cheng73f24c92009-03-30 21:36:47 +000015896 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000015897 DAG.getConstant(MulAmt2, VT));
15898
15899 // Do not add new nodes to DAG combiner worklist.
15900 DCI.CombineTo(N, NewMul, false);
15901 }
15902 return SDValue();
15903}
15904
Evan Chengad9c0a32009-12-15 00:53:42 +000015905static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
15906 SDValue N0 = N->getOperand(0);
15907 SDValue N1 = N->getOperand(1);
15908 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
15909 EVT VT = N0.getValueType();
15910
15911 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
15912 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015913 if (VT.isInteger() && !VT.isVector() &&
15914 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000015915 N0.getOperand(1).getOpcode() == ISD::Constant) {
15916 SDValue N00 = N0.getOperand(0);
15917 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
15918 ((N00.getOpcode() == ISD::ANY_EXTEND ||
15919 N00.getOpcode() == ISD::ZERO_EXTEND) &&
15920 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
15921 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
15922 APInt ShAmt = N1C->getAPIntValue();
15923 Mask = Mask.shl(ShAmt);
15924 if (Mask != 0)
15925 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
15926 N00, DAG.getConstant(Mask, VT));
15927 }
15928 }
15929
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015930 // Hardware support for vector shifts is sparse which makes us scalarize the
15931 // vector operations in many cases. Also, on sandybridge ADD is faster than
15932 // shl.
15933 // (shl V, 1) -> add V,V
15934 if (isSplatVector(N1.getNode())) {
15935 assert(N0.getValueType().isVector() && "Invalid vector shift type");
15936 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
15937 // We shift all of the values by one. In many cases we do not have
15938 // hardware support for this operation. This is better expressed as an ADD
15939 // of two values.
15940 if (N1C && (1 == N1C->getZExtValue())) {
15941 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
15942 }
15943 }
15944
Evan Chengad9c0a32009-12-15 00:53:42 +000015945 return SDValue();
15946}
Evan Cheng0b0cd912009-03-28 05:57:29 +000015947
Nate Begeman740ab032009-01-26 00:52:55 +000015948/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
15949/// when possible.
15950static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000015951 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000015952 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000015953 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000015954 if (N->getOpcode() == ISD::SHL) {
15955 SDValue V = PerformSHLCombine(N, DAG);
15956 if (V.getNode()) return V;
15957 }
Evan Chengad9c0a32009-12-15 00:53:42 +000015958
Nate Begeman740ab032009-01-26 00:52:55 +000015959 // On X86 with SSE2 support, we can transform this to a vector shift if
15960 // all elements are shifted by the same amount. We can't do this in legalize
15961 // because the a constant vector is typically transformed to a constant pool
15962 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000015963 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015964 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015965
Craig Topper7be5dfd2011-11-12 09:58:49 +000015966 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015967 (!Subtarget->hasInt256() ||
Craig Topper7be5dfd2011-11-12 09:58:49 +000015968 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000015969 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000015970
Mon P Wang3becd092009-01-28 08:12:05 +000015971 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000015972 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000015973 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000015974 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000015975 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
15976 unsigned NumElts = VT.getVectorNumElements();
15977 unsigned i = 0;
15978 for (; i != NumElts; ++i) {
15979 SDValue Arg = ShAmtOp.getOperand(i);
15980 if (Arg.getOpcode() == ISD::UNDEF) continue;
15981 BaseShAmt = Arg;
15982 break;
15983 }
Craig Topper37c26772012-01-17 04:44:50 +000015984 // Handle the case where the build_vector is all undef
15985 // FIXME: Should DAG allow this?
15986 if (i == NumElts)
15987 return SDValue();
15988
Mon P Wang3becd092009-01-28 08:12:05 +000015989 for (; i != NumElts; ++i) {
15990 SDValue Arg = ShAmtOp.getOperand(i);
15991 if (Arg.getOpcode() == ISD::UNDEF) continue;
15992 if (Arg != BaseShAmt) {
15993 return SDValue();
15994 }
15995 }
15996 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000015997 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000015998 SDValue InVec = ShAmtOp.getOperand(0);
15999 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
16000 unsigned NumElts = InVec.getValueType().getVectorNumElements();
16001 unsigned i = 0;
16002 for (; i != NumElts; ++i) {
16003 SDValue Arg = InVec.getOperand(i);
16004 if (Arg.getOpcode() == ISD::UNDEF) continue;
16005 BaseShAmt = Arg;
16006 break;
16007 }
16008 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
16009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000016010 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000016011 if (C->getZExtValue() == SplatIdx)
16012 BaseShAmt = InVec.getOperand(1);
16013 }
16014 }
Mon P Wang845b1892012-02-01 22:15:20 +000016015 if (BaseShAmt.getNode() == 0) {
16016 // Don't create instructions with illegal types after legalize
16017 // types has run.
16018 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
16019 !DCI.isBeforeLegalize())
16020 return SDValue();
16021
Mon P Wangefa42202009-09-03 19:56:25 +000016022 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
16023 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000016024 }
Mon P Wang3becd092009-01-28 08:12:05 +000016025 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000016026 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016027
Mon P Wangefa42202009-09-03 19:56:25 +000016028 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000016029 if (EltVT.bitsGT(MVT::i32))
16030 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
16031 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000016032 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000016033
Nate Begemanc2fd67f2009-01-26 03:15:31 +000016034 // The shift amount is identical so we can do a vector shift.
16035 SDValue ValOp = N->getOperand(0);
16036 switch (N->getOpcode()) {
16037 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000016038 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000016039 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000016040 switch (VT.getSimpleVT().SimpleTy) {
16041 default: return SDValue();
16042 case MVT::v2i64:
16043 case MVT::v4i32:
16044 case MVT::v8i16:
16045 case MVT::v4i64:
16046 case MVT::v8i32:
16047 case MVT::v16i16:
16048 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
16049 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000016050 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000016051 switch (VT.getSimpleVT().SimpleTy) {
16052 default: return SDValue();
16053 case MVT::v4i32:
16054 case MVT::v8i16:
16055 case MVT::v8i32:
16056 case MVT::v16i16:
16057 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
16058 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000016059 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000016060 switch (VT.getSimpleVT().SimpleTy) {
16061 default: return SDValue();
16062 case MVT::v2i64:
16063 case MVT::v4i32:
16064 case MVT::v8i16:
16065 case MVT::v4i64:
16066 case MVT::v8i32:
16067 case MVT::v16i16:
16068 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
16069 }
Nate Begeman740ab032009-01-26 00:52:55 +000016070 }
Nate Begeman740ab032009-01-26 00:52:55 +000016071}
16072
Stuart Hastings865f0932011-06-03 23:53:54 +000016073// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16074// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16075// and friends. Likewise for OR -> CMPNEQSS.
16076static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16077 TargetLowering::DAGCombinerInfo &DCI,
16078 const X86Subtarget *Subtarget) {
16079 unsigned opcode;
16080
16081 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16082 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016083 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016084 SDValue N0 = N->getOperand(0);
16085 SDValue N1 = N->getOperand(1);
16086 SDValue CMP0 = N0->getOperand(1);
16087 SDValue CMP1 = N1->getOperand(1);
16088 DebugLoc DL = N->getDebugLoc();
16089
16090 // The SETCCs should both refer to the same CMP.
16091 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16092 return SDValue();
16093
16094 SDValue CMP00 = CMP0->getOperand(0);
16095 SDValue CMP01 = CMP0->getOperand(1);
16096 EVT VT = CMP00.getValueType();
16097
16098 if (VT == MVT::f32 || VT == MVT::f64) {
16099 bool ExpectingFlags = false;
16100 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016101 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016102 !ExpectingFlags && UI != UE; ++UI)
16103 switch (UI->getOpcode()) {
16104 default:
16105 case ISD::BR_CC:
16106 case ISD::BRCOND:
16107 case ISD::SELECT:
16108 ExpectingFlags = true;
16109 break;
16110 case ISD::CopyToReg:
16111 case ISD::SIGN_EXTEND:
16112 case ISD::ZERO_EXTEND:
16113 case ISD::ANY_EXTEND:
16114 break;
16115 }
16116
16117 if (!ExpectingFlags) {
16118 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16119 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16120
16121 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16122 X86::CondCode tmp = cc0;
16123 cc0 = cc1;
16124 cc1 = tmp;
16125 }
16126
16127 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16128 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16129 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16130 X86ISD::NodeType NTOperator = is64BitFP ?
16131 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16132 // FIXME: need symbolic constants for these magic numbers.
16133 // See X86ATTInstPrinter.cpp:printSSECC().
16134 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16135 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16136 DAG.getConstant(x86cc, MVT::i8));
16137 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16138 OnesOrZeroesF);
16139 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16140 DAG.getConstant(1, MVT::i32));
16141 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16142 return OneBitOfTruth;
16143 }
16144 }
16145 }
16146 }
16147 return SDValue();
16148}
16149
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016150/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16151/// so it can be folded inside ANDNP.
16152static bool CanFoldXORWithAllOnes(const SDNode *N) {
16153 EVT VT = N->getValueType(0);
16154
16155 // Match direct AllOnes for 128 and 256-bit vectors
16156 if (ISD::isBuildVectorAllOnes(N))
16157 return true;
16158
16159 // Look through a bit convert.
16160 if (N->getOpcode() == ISD::BITCAST)
16161 N = N->getOperand(0).getNode();
16162
16163 // Sometimes the operand may come from a insert_subvector building a 256-bit
16164 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016165 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016166 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16167 SDValue V1 = N->getOperand(0);
16168 SDValue V2 = N->getOperand(1);
16169
16170 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16171 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16172 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16173 ISD::isBuildVectorAllOnes(V2.getNode()))
16174 return true;
16175 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016176
16177 return false;
16178}
16179
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016180// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16181// register. In most cases we actually compare or select YMM-sized registers
16182// and mixing the two types creates horrible code. This method optimizes
16183// some of the transition sequences.
16184static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16185 TargetLowering::DAGCombinerInfo &DCI,
16186 const X86Subtarget *Subtarget) {
16187 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016188 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016189 return SDValue();
16190
16191 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16192 N->getOpcode() == ISD::ZERO_EXTEND ||
16193 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16194
16195 SDValue Narrow = N->getOperand(0);
16196 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016197 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016198 return SDValue();
16199
16200 if (Narrow->getOpcode() != ISD::XOR &&
16201 Narrow->getOpcode() != ISD::AND &&
16202 Narrow->getOpcode() != ISD::OR)
16203 return SDValue();
16204
16205 SDValue N0 = Narrow->getOperand(0);
16206 SDValue N1 = Narrow->getOperand(1);
16207 DebugLoc DL = Narrow->getDebugLoc();
16208
16209 // The Left side has to be a trunc.
16210 if (N0.getOpcode() != ISD::TRUNCATE)
16211 return SDValue();
16212
16213 // The type of the truncated inputs.
16214 EVT WideVT = N0->getOperand(0)->getValueType(0);
16215 if (WideVT != VT)
16216 return SDValue();
16217
16218 // The right side has to be a 'trunc' or a constant vector.
16219 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16220 bool RHSConst = (isSplatVector(N1.getNode()) &&
16221 isa<ConstantSDNode>(N1->getOperand(0)));
16222 if (!RHSTrunc && !RHSConst)
16223 return SDValue();
16224
16225 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16226
16227 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16228 return SDValue();
16229
16230 // Set N0 and N1 to hold the inputs to the new wide operation.
16231 N0 = N0->getOperand(0);
16232 if (RHSConst) {
16233 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16234 N1->getOperand(0));
16235 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16236 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16237 } else if (RHSTrunc) {
16238 N1 = N1->getOperand(0);
16239 }
16240
16241 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016242 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016243 unsigned Opcode = N->getOpcode();
16244 switch (Opcode) {
16245 case ISD::ANY_EXTEND:
16246 return Op;
16247 case ISD::ZERO_EXTEND: {
16248 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16249 APInt Mask = APInt::getAllOnesValue(InBits);
16250 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16251 return DAG.getNode(ISD::AND, DL, VT,
16252 Op, DAG.getConstant(Mask, VT));
16253 }
16254 case ISD::SIGN_EXTEND:
16255 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16256 Op, DAG.getValueType(NarrowVT));
16257 default:
16258 llvm_unreachable("Unexpected opcode");
16259 }
16260}
16261
Nate Begemanb65c1752010-12-17 22:55:37 +000016262static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16263 TargetLowering::DAGCombinerInfo &DCI,
16264 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016265 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016266 if (DCI.isBeforeLegalizeOps())
16267 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016268
Stuart Hastings865f0932011-06-03 23:53:54 +000016269 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16270 if (R.getNode())
16271 return R;
16272
Craig Topperb926afc2012-12-17 05:12:30 +000016273 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016274 // BLSI is X & (-X)
16275 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016276 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16277 SDValue N0 = N->getOperand(0);
16278 SDValue N1 = N->getOperand(1);
16279 DebugLoc DL = N->getDebugLoc();
16280
Craig Topperb4c94572011-10-21 06:55:01 +000016281 // Check LHS for neg
16282 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16283 isZero(N0.getOperand(0)))
16284 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16285
16286 // Check RHS for neg
16287 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16288 isZero(N1.getOperand(0)))
16289 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16290
16291 // Check LHS for X-1
16292 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16293 isAllOnes(N0.getOperand(1)))
16294 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16295
16296 // Check RHS for X-1
16297 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16298 isAllOnes(N1.getOperand(1)))
16299 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16300
Craig Topper54a11172011-10-14 07:06:56 +000016301 return SDValue();
16302 }
16303
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016304 // Want to form ANDNP nodes:
16305 // 1) In the hopes of then easily combining them with OR and AND nodes
16306 // to form PBLEND/PSIGN.
16307 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016308 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016309 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016310
Nate Begemanb65c1752010-12-17 22:55:37 +000016311 SDValue N0 = N->getOperand(0);
16312 SDValue N1 = N->getOperand(1);
16313 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016314
Nate Begemanb65c1752010-12-17 22:55:37 +000016315 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016316 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016317 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16318 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016319 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016320
16321 // Check RHS for vnot
16322 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016323 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16324 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016325 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016326
Nate Begemanb65c1752010-12-17 22:55:37 +000016327 return SDValue();
16328}
16329
Evan Cheng760d1942010-01-04 21:22:48 +000016330static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016331 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016332 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016333 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016334 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016335 return SDValue();
16336
Stuart Hastings865f0932011-06-03 23:53:54 +000016337 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16338 if (R.getNode())
16339 return R;
16340
Evan Cheng760d1942010-01-04 21:22:48 +000016341 SDValue N0 = N->getOperand(0);
16342 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016343
Nate Begemanb65c1752010-12-17 22:55:37 +000016344 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016345 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016346 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016347 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016348 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016349
Craig Topper1666cb62011-11-19 07:07:26 +000016350 // Canonicalize pandn to RHS
16351 if (N0.getOpcode() == X86ISD::ANDNP)
16352 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016353 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016354 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16355 SDValue Mask = N1.getOperand(0);
16356 SDValue X = N1.getOperand(1);
16357 SDValue Y;
16358 if (N0.getOperand(0) == Mask)
16359 Y = N0.getOperand(1);
16360 if (N0.getOperand(1) == Mask)
16361 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016362
Craig Topper1666cb62011-11-19 07:07:26 +000016363 // Check to see if the mask appeared in both the AND and ANDNP and
16364 if (!Y.getNode())
16365 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016366
Craig Topper1666cb62011-11-19 07:07:26 +000016367 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016368 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016369 if (Mask.getOpcode() == ISD::BITCAST)
16370 Mask = Mask.getOperand(0);
16371 if (X.getOpcode() == ISD::BITCAST)
16372 X = X.getOperand(0);
16373 if (Y.getOpcode() == ISD::BITCAST)
16374 Y = Y.getOperand(0);
16375
Craig Topper1666cb62011-11-19 07:07:26 +000016376 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016377
Craig Toppered2e13d2012-01-22 19:15:14 +000016378 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016379 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16380 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016381 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000016382 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000016383
16384 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000016385 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000016386 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16387 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
16388 if ((SraAmt + 1) != EltBits)
16389 return SDValue();
16390
16391 DebugLoc DL = N->getDebugLoc();
16392
16393 // Now we know we at least have a plendvb with the mask val. See if
16394 // we can form a psignb/w/d.
16395 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016396 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16397 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016398 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16399 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16400 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016401 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016402 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016403 }
16404 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016405 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016406 return SDValue();
16407
16408 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16409
16410 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16411 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16412 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016413 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016414 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016415 }
16416 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016417
Craig Topper1666cb62011-11-19 07:07:26 +000016418 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16419 return SDValue();
16420
Nate Begemanb65c1752010-12-17 22:55:37 +000016421 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016422 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16423 std::swap(N0, N1);
16424 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16425 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016426 if (!N0.hasOneUse() || !N1.hasOneUse())
16427 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016428
16429 SDValue ShAmt0 = N0.getOperand(1);
16430 if (ShAmt0.getValueType() != MVT::i8)
16431 return SDValue();
16432 SDValue ShAmt1 = N1.getOperand(1);
16433 if (ShAmt1.getValueType() != MVT::i8)
16434 return SDValue();
16435 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16436 ShAmt0 = ShAmt0.getOperand(0);
16437 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16438 ShAmt1 = ShAmt1.getOperand(0);
16439
16440 DebugLoc DL = N->getDebugLoc();
16441 unsigned Opc = X86ISD::SHLD;
16442 SDValue Op0 = N0.getOperand(0);
16443 SDValue Op1 = N1.getOperand(0);
16444 if (ShAmt0.getOpcode() == ISD::SUB) {
16445 Opc = X86ISD::SHRD;
16446 std::swap(Op0, Op1);
16447 std::swap(ShAmt0, ShAmt1);
16448 }
16449
Evan Cheng8b1190a2010-04-28 01:18:01 +000016450 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016451 if (ShAmt1.getOpcode() == ISD::SUB) {
16452 SDValue Sum = ShAmt1.getOperand(0);
16453 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016454 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16455 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16456 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16457 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016458 return DAG.getNode(Opc, DL, VT,
16459 Op0, Op1,
16460 DAG.getNode(ISD::TRUNCATE, DL,
16461 MVT::i8, ShAmt0));
16462 }
16463 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16464 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16465 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016466 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016467 return DAG.getNode(Opc, DL, VT,
16468 N0.getOperand(0), N1.getOperand(0),
16469 DAG.getNode(ISD::TRUNCATE, DL,
16470 MVT::i8, ShAmt0));
16471 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016472
Evan Cheng760d1942010-01-04 21:22:48 +000016473 return SDValue();
16474}
16475
Manman Ren92363622012-06-07 22:39:10 +000016476// Generate NEG and CMOV for integer abs.
16477static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16478 EVT VT = N->getValueType(0);
16479
16480 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16481 // 8-bit integer abs to NEG and CMOV.
16482 if (VT.isInteger() && VT.getSizeInBits() == 8)
16483 return SDValue();
16484
16485 SDValue N0 = N->getOperand(0);
16486 SDValue N1 = N->getOperand(1);
16487 DebugLoc DL = N->getDebugLoc();
16488
16489 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16490 // and change it to SUB and CMOV.
16491 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16492 N0.getOpcode() == ISD::ADD &&
16493 N0.getOperand(1) == N1 &&
16494 N1.getOpcode() == ISD::SRA &&
16495 N1.getOperand(0) == N0.getOperand(0))
16496 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16497 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16498 // Generate SUB & CMOV.
16499 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16500 DAG.getConstant(0, VT), N0.getOperand(0));
16501
16502 SDValue Ops[] = { N0.getOperand(0), Neg,
16503 DAG.getConstant(X86::COND_GE, MVT::i8),
16504 SDValue(Neg.getNode(), 1) };
16505 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16506 Ops, array_lengthof(Ops));
16507 }
16508 return SDValue();
16509}
16510
Craig Topper3738ccd2011-12-27 06:27:23 +000016511// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016512static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16513 TargetLowering::DAGCombinerInfo &DCI,
16514 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016515 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016516 if (DCI.isBeforeLegalizeOps())
16517 return SDValue();
16518
Manman Ren45d53b82012-06-08 18:58:26 +000016519 if (Subtarget->hasCMov()) {
16520 SDValue RV = performIntegerAbsCombine(N, DAG);
16521 if (RV.getNode())
16522 return RV;
16523 }
Manman Ren92363622012-06-07 22:39:10 +000016524
16525 // Try forming BMI if it is available.
16526 if (!Subtarget->hasBMI())
16527 return SDValue();
16528
Craig Topperb4c94572011-10-21 06:55:01 +000016529 if (VT != MVT::i32 && VT != MVT::i64)
16530 return SDValue();
16531
Craig Topper3738ccd2011-12-27 06:27:23 +000016532 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16533
Craig Topperb4c94572011-10-21 06:55:01 +000016534 // Create BLSMSK instructions by finding X ^ (X-1)
16535 SDValue N0 = N->getOperand(0);
16536 SDValue N1 = N->getOperand(1);
16537 DebugLoc DL = N->getDebugLoc();
16538
16539 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16540 isAllOnes(N0.getOperand(1)))
16541 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16542
16543 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16544 isAllOnes(N1.getOperand(1)))
16545 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16546
16547 return SDValue();
16548}
16549
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016550/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16551static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016552 TargetLowering::DAGCombinerInfo &DCI,
16553 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016554 LoadSDNode *Ld = cast<LoadSDNode>(N);
16555 EVT RegVT = Ld->getValueType(0);
16556 EVT MemVT = Ld->getMemoryVT();
16557 DebugLoc dl = Ld->getDebugLoc();
16558 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016559 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016560
16561 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016562 unsigned Alignment = Ld->getAlignment();
Nadav Rotemba958652013-01-19 08:38:41 +000016563 bool IsAligned = Alignment == 0 || Alignment == MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016564
16565 // On Sandybridge unaligned 256bit loads are inefficient.
16566 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016567 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016568 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016569 if (NumElems < 2)
16570 return SDValue();
16571
Nadav Rotem48177ac2013-01-18 23:10:30 +000016572 SDValue Ptr = Ld->getBasePtr();
16573 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16574
16575 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16576 NumElems/2);
16577 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16578 Ld->getPointerInfo(), Ld->isVolatile(),
16579 Ld->isNonTemporal(), Ld->isInvariant(),
16580 Alignment);
16581 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16582 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16583 Ld->getPointerInfo(), Ld->isVolatile(),
16584 Ld->isNonTemporal(), Ld->isInvariant(),
Nadav Rotemba958652013-01-19 08:38:41 +000016585 std::max(Alignment/2U, 1U));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016586 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16587 Load1.getValue(1),
16588 Load2.getValue(1));
16589
16590 SDValue NewVec = DAG.getUNDEF(RegVT);
16591 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16592 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16593 return DCI.CombineTo(N, NewVec, TF, true);
16594 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016595
Nadav Rotemca6f2962011-09-18 19:00:23 +000016596 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016597 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16598 // expansion is still better than scalar code.
16599 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16600 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016601 // TODO: It is possible to support ZExt by zeroing the undef values
16602 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016603 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16604 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016605 assert(MemVT != RegVT && "Cannot extend to the same type");
16606 assert(MemVT.isVector() && "Must load a vector from memory");
16607
16608 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016609 unsigned MemSz = MemVT.getSizeInBits();
16610 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016611
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016612 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16613 return SDValue();
16614
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016615 // All sizes must be a power of two.
16616 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16617 return SDValue();
16618
16619 // Attempt to load the original value using scalar loads.
16620 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016621 MVT SclrLoadTy = MVT::i8;
16622 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16623 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16624 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016625 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016626 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016627 }
16628 }
16629
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016630 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16631 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16632 (64 <= MemSz))
16633 SclrLoadTy = MVT::f64;
16634
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016635 // Calculate the number of scalar loads that we need to perform
16636 // in order to load our vector from memory.
16637 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016638 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16639 return SDValue();
16640
16641 unsigned loadRegZize = RegSz;
16642 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16643 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016644
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016645 // Represent our vector as a sequence of elements which are the
16646 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016647 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016648 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016649
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016650 // Represent the data using the same element type that is stored in
16651 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016652 EVT WideVecVT =
16653 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016654 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016655
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016656 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16657 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016658
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016659 // We can't shuffle using an illegal type.
16660 if (!TLI.isTypeLegal(WideVecVT))
16661 return SDValue();
16662
16663 SmallVector<SDValue, 8> Chains;
16664 SDValue Ptr = Ld->getBasePtr();
16665 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16666 TLI.getPointerTy());
16667 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16668
16669 for (unsigned i = 0; i < NumLoads; ++i) {
16670 // Perform a single load.
16671 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16672 Ptr, Ld->getPointerInfo(),
16673 Ld->isVolatile(), Ld->isNonTemporal(),
16674 Ld->isInvariant(), Ld->getAlignment());
16675 Chains.push_back(ScalarLoad.getValue(1));
16676 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16677 // another round of DAGCombining.
16678 if (i == 0)
16679 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16680 else
16681 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16682 ScalarLoad, DAG.getIntPtrConstant(i));
16683
16684 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16685 }
16686
16687 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16688 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016689
16690 // Bitcast the loaded value to a vector of the original element type, in
16691 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016692 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016693 unsigned SizeRatio = RegSz/MemSz;
16694
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016695 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016696 // If we have SSE4.1 we can directly emit a VSEXT node.
16697 if (Subtarget->hasSSE41()) {
16698 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16699 return DCI.CombineTo(N, Sext, TF, true);
16700 }
16701
16702 // Otherwise we'll shuffle the small elements in the high bits of the
16703 // larger type and perform an arithmetic shift. If the shift is not legal
16704 // it's better to scalarize.
16705 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16706 return SDValue();
16707
16708 // Redistribute the loaded elements into the different locations.
16709 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16710 for (unsigned i = 0; i != NumElems; ++i)
16711 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16712
16713 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16714 DAG.getUNDEF(WideVecVT),
16715 &ShuffleVec[0]);
16716
16717 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16718
16719 // Build the arithmetic shift.
16720 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16721 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016722 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16723 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016724
16725 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016726 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016727
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016728 // Redistribute the loaded elements into the different locations.
16729 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016730 for (unsigned i = 0; i != NumElems; ++i)
16731 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016732
16733 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016734 DAG.getUNDEF(WideVecVT),
16735 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016736
16737 // Bitcast to the requested type.
16738 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16739 // Replace the original load with the new sequence
16740 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016741 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016742 }
16743
16744 return SDValue();
16745}
16746
Chris Lattner149a4e52008-02-22 02:09:43 +000016747/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016748static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016749 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016750 StoreSDNode *St = cast<StoreSDNode>(N);
16751 EVT VT = St->getValue().getValueType();
16752 EVT StVT = St->getMemoryVT();
16753 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016754 SDValue StoredVal = St->getOperand(1);
16755 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotemba958652013-01-19 08:38:41 +000016756 unsigned Alignment = St->getAlignment();
16757 bool IsAligned = Alignment == 0 || Alignment == VT.getSizeInBits()/8;
Nadav Rotem5e742a32011-08-11 16:41:21 +000016758
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016759 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016760 // On Sandy Bridge, 256-bit memory operations are executed by two
16761 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16762 // memory operation.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016763 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016764 StVT == VT && !IsAligned) {
16765 unsigned NumElems = VT.getVectorNumElements();
16766 if (NumElems < 2)
16767 return SDValue();
16768
16769 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16770 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016771
16772 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16773 SDValue Ptr0 = St->getBasePtr();
16774 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16775
16776 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16777 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016778 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016779 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16780 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016781 St->isNonTemporal(),
16782 std::max(Alignment/2U, 1U));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016783 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16784 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016785
16786 // Optimize trunc store (of multiple scalars) to shuffle and store.
16787 // First, pack all of the elements in one place. Next, store to memory
16788 // in fewer chunks.
16789 if (St->isTruncatingStore() && VT.isVector()) {
16790 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16791 unsigned NumElems = VT.getVectorNumElements();
16792 assert(StVT != VT && "Cannot truncate to the same type");
16793 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16794 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16795
16796 // From, To sizes and ElemCount must be pow of two
16797 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016798 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016799 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016800 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016801
Nadav Rotem614061b2011-08-10 19:30:14 +000016802 unsigned SizeRatio = FromSz / ToSz;
16803
16804 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16805
16806 // Create a type on which we perform the shuffle
16807 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16808 StVT.getScalarType(), NumElems*SizeRatio);
16809
16810 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16811
16812 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16813 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016814 for (unsigned i = 0; i != NumElems; ++i)
16815 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016816
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016817 // Can't shuffle using an illegal type.
16818 if (!TLI.isTypeLegal(WideVecVT))
16819 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016820
16821 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016822 DAG.getUNDEF(WideVecVT),
16823 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016824 // At this point all of the data is stored at the bottom of the
16825 // register. We now need to save it to mem.
16826
16827 // Find the largest store unit
16828 MVT StoreType = MVT::i8;
16829 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16830 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16831 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016832 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016833 StoreType = Tp;
16834 }
16835
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016836 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16837 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16838 (64 <= NumElems * ToSz))
16839 StoreType = MVT::f64;
16840
Nadav Rotem614061b2011-08-10 19:30:14 +000016841 // Bitcast the original vector into a vector of store-size units
16842 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016843 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016844 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16845 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16846 SmallVector<SDValue, 8> Chains;
16847 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16848 TLI.getPointerTy());
16849 SDValue Ptr = St->getBasePtr();
16850
16851 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016852 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016853 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16854 StoreType, ShuffWide,
16855 DAG.getIntPtrConstant(i));
16856 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16857 St->getPointerInfo(), St->isVolatile(),
16858 St->isNonTemporal(), St->getAlignment());
16859 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16860 Chains.push_back(Ch);
16861 }
16862
16863 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16864 Chains.size());
16865 }
16866
Chris Lattner149a4e52008-02-22 02:09:43 +000016867 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16868 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016869 // A preferable solution to the general problem is to figure out the right
16870 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016871
16872 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016873 if (VT.getSizeInBits() != 64)
16874 return SDValue();
16875
Devang Patel578efa92009-06-05 21:57:13 +000016876 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016877 bool NoImplicitFloatOps = F->getAttributes().
16878 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016879 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016880 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016881 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016882 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016883 isa<LoadSDNode>(St->getValue()) &&
16884 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16885 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016886 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016887 LoadSDNode *Ld = 0;
16888 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016889 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016890 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016891 // Must be a store of a load. We currently handle two cases: the load
16892 // is a direct child, and it's under an intervening TokenFactor. It is
16893 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016894 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016895 Ld = cast<LoadSDNode>(St->getChain());
16896 else if (St->getValue().hasOneUse() &&
16897 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000016898 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016899 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000016900 TokenFactorIndex = i;
16901 Ld = cast<LoadSDNode>(St->getValue());
16902 } else
16903 Ops.push_back(ChainVal->getOperand(i));
16904 }
16905 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000016906
Evan Cheng536e6672009-03-12 05:59:15 +000016907 if (!Ld || !ISD::isNormalLoad(Ld))
16908 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016909
Evan Cheng536e6672009-03-12 05:59:15 +000016910 // If this is not the MMX case, i.e. we are just turning i64 load/store
16911 // into f64 load/store, avoid the transformation if there are multiple
16912 // uses of the loaded value.
16913 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
16914 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016915
Evan Cheng536e6672009-03-12 05:59:15 +000016916 DebugLoc LdDL = Ld->getDebugLoc();
16917 DebugLoc StDL = N->getDebugLoc();
16918 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
16919 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
16920 // pair instead.
16921 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016922 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000016923 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
16924 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016925 Ld->isNonTemporal(), Ld->isInvariant(),
16926 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016927 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000016928 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000016929 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000016930 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000016931 Ops.size());
16932 }
Evan Cheng536e6672009-03-12 05:59:15 +000016933 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000016934 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016935 St->isVolatile(), St->isNonTemporal(),
16936 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000016937 }
Evan Cheng536e6672009-03-12 05:59:15 +000016938
16939 // Otherwise, lower to two pairs of 32-bit loads / stores.
16940 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016941 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
16942 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016943
Owen Anderson825b72b2009-08-11 20:47:22 +000016944 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016945 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016946 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016947 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000016948 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000016949 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000016950 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000016951 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000016952 MinAlign(Ld->getAlignment(), 4));
16953
16954 SDValue NewChain = LoLd.getValue(1);
16955 if (TokenFactorIndex != -1) {
16956 Ops.push_back(LoLd);
16957 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000016958 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000016959 Ops.size());
16960 }
16961
16962 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000016963 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
16964 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000016965
16966 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016967 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000016968 St->isVolatile(), St->isNonTemporal(),
16969 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000016970 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000016971 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000016972 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000016973 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000016974 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000016975 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000016976 }
Dan Gohman475871a2008-07-27 21:46:04 +000016977 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000016978}
16979
Duncan Sands17470be2011-09-22 20:15:48 +000016980/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
16981/// and return the operands for the horizontal operation in LHS and RHS. A
16982/// horizontal operation performs the binary operation on successive elements
16983/// of its first operand, then on successive elements of its second operand,
16984/// returning the resulting values in a vector. For example, if
16985/// A = < float a0, float a1, float a2, float a3 >
16986/// and
16987/// B = < float b0, float b1, float b2, float b3 >
16988/// then the result of doing a horizontal operation on A and B is
16989/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
16990/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
16991/// A horizontal-op B, for some already available A and B, and if so then LHS is
16992/// set to A, RHS to B, and the routine returns 'true'.
16993/// Note that the binary operation should have the property that if one of the
16994/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000016995static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000016996 // Look for the following pattern: if
16997 // A = < float a0, float a1, float a2, float a3 >
16998 // B = < float b0, float b1, float b2, float b3 >
16999 // and
17000 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17001 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17002 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17003 // which is A horizontal-op B.
17004
17005 // At least one of the operands should be a vector shuffle.
17006 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17007 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17008 return false;
17009
17010 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017011
17012 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17013 "Unsupported vector type for horizontal add/sub");
17014
17015 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17016 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017017 unsigned NumElts = VT.getVectorNumElements();
17018 unsigned NumLanes = VT.getSizeInBits()/128;
17019 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017020 assert((NumLaneElts % 2 == 0) &&
17021 "Vector type should have an even number of elements in each lane");
17022 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017023
17024 // View LHS in the form
17025 // LHS = VECTOR_SHUFFLE A, B, LMask
17026 // If LHS is not a shuffle then pretend it is the shuffle
17027 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17028 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17029 // type VT.
17030 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017031 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017032 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17033 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17034 A = LHS.getOperand(0);
17035 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17036 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017037 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17038 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017039 } else {
17040 if (LHS.getOpcode() != ISD::UNDEF)
17041 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017042 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017043 LMask[i] = i;
17044 }
17045
17046 // Likewise, view RHS in the form
17047 // RHS = VECTOR_SHUFFLE C, D, RMask
17048 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017049 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017050 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17051 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17052 C = RHS.getOperand(0);
17053 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17054 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017055 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17056 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017057 } else {
17058 if (RHS.getOpcode() != ISD::UNDEF)
17059 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017060 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017061 RMask[i] = i;
17062 }
17063
17064 // Check that the shuffles are both shuffling the same vectors.
17065 if (!(A == C && B == D) && !(A == D && B == C))
17066 return false;
17067
17068 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17069 if (!A.getNode() && !B.getNode())
17070 return false;
17071
17072 // If A and B occur in reverse order in RHS, then "swap" them (which means
17073 // rewriting the mask).
17074 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017075 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017076
17077 // At this point LHS and RHS are equivalent to
17078 // LHS = VECTOR_SHUFFLE A, B, LMask
17079 // RHS = VECTOR_SHUFFLE A, B, RMask
17080 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017081 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017082 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017083
Craig Topperf8363302011-12-02 08:18:41 +000017084 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017085 if (LIdx < 0 || RIdx < 0 ||
17086 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17087 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017088 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017089
Craig Topperf8363302011-12-02 08:18:41 +000017090 // Check that successive elements are being operated on. If not, this is
17091 // not a horizontal operation.
17092 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17093 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017094 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017095 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017096 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017097 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017098 }
17099
17100 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17101 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17102 return true;
17103}
17104
17105/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17106static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17107 const X86Subtarget *Subtarget) {
17108 EVT VT = N->getValueType(0);
17109 SDValue LHS = N->getOperand(0);
17110 SDValue RHS = N->getOperand(1);
17111
17112 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017113 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017114 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017115 isHorizontalBinOp(LHS, RHS, true))
17116 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
17117 return SDValue();
17118}
17119
17120/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17121static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17122 const X86Subtarget *Subtarget) {
17123 EVT VT = N->getValueType(0);
17124 SDValue LHS = N->getOperand(0);
17125 SDValue RHS = N->getOperand(1);
17126
17127 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017128 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017129 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017130 isHorizontalBinOp(LHS, RHS, false))
17131 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
17132 return SDValue();
17133}
17134
Chris Lattner6cf73262008-01-25 06:14:17 +000017135/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17136/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017137static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017138 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17139 // F[X]OR(0.0, x) -> x
17140 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017141 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17142 if (C->getValueAPF().isPosZero())
17143 return N->getOperand(1);
17144 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17145 if (C->getValueAPF().isPosZero())
17146 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017147 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017148}
17149
Nadav Rotemd60cb112012-08-19 13:06:16 +000017150/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17151/// X86ISD::FMAX nodes.
17152static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17153 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17154
17155 // Only perform optimizations if UnsafeMath is used.
17156 if (!DAG.getTarget().Options.UnsafeFPMath)
17157 return SDValue();
17158
17159 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017160 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017161 unsigned NewOp = 0;
17162 switch (N->getOpcode()) {
17163 default: llvm_unreachable("unknown opcode");
17164 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17165 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17166 }
17167
17168 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17169 N->getOperand(0), N->getOperand(1));
17170}
17171
Chris Lattneraf723b92008-01-25 05:46:26 +000017172/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017173static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017174 // FAND(0.0, x) -> 0.0
17175 // FAND(x, 0.0) -> 0.0
17176 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17177 if (C->getValueAPF().isPosZero())
17178 return N->getOperand(0);
17179 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17180 if (C->getValueAPF().isPosZero())
17181 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017182 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017183}
17184
Dan Gohmane5af2d32009-01-29 01:59:02 +000017185static SDValue PerformBTCombine(SDNode *N,
17186 SelectionDAG &DAG,
17187 TargetLowering::DAGCombinerInfo &DCI) {
17188 // BT ignores high bits in the bit index operand.
17189 SDValue Op1 = N->getOperand(1);
17190 if (Op1.hasOneUse()) {
17191 unsigned BitWidth = Op1.getValueSizeInBits();
17192 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17193 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017194 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17195 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017196 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017197 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17198 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17199 DCI.CommitTargetLoweringOpt(TLO);
17200 }
17201 return SDValue();
17202}
Chris Lattner83e6c992006-10-04 06:57:07 +000017203
Eli Friedman7a5e5552009-06-07 06:52:44 +000017204static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17205 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017206 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017207 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017208 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017209 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017210 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017211 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017212 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017213 }
17214 return SDValue();
17215}
17216
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017217static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
17218 const X86Subtarget *Subtarget) {
17219 EVT VT = N->getValueType(0);
17220 if (!VT.isVector())
17221 return SDValue();
17222
17223 SDValue N0 = N->getOperand(0);
17224 SDValue N1 = N->getOperand(1);
17225 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
17226 DebugLoc dl = N->getDebugLoc();
17227
17228 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17229 // both SSE and AVX2 since there is no sign-extended shift right
17230 // operation on a vector with 64-bit elements.
17231 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17232 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17233 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17234 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17235 SDValue N00 = N0.getOperand(0);
17236
17237 // EXTLOAD has a better solution on AVX2,
17238 // it may be replaced with X86ISD::VSEXT node.
17239 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17240 if (!ISD::isNormalLoad(N00.getNode()))
17241 return SDValue();
17242
17243 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
17244 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
17245 N00, N1);
17246 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17247 }
17248 }
17249 return SDValue();
17250}
17251
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017252static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17253 TargetLowering::DAGCombinerInfo &DCI,
17254 const X86Subtarget *Subtarget) {
17255 if (!DCI.isBeforeLegalizeOps())
17256 return SDValue();
17257
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017258 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017259 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017260
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017261 EVT VT = N->getValueType(0);
17262 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017263 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17264 if (R.getNode())
17265 return R;
17266 }
17267
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017268 return SDValue();
17269}
17270
Michael Liaof6c24ee2012-08-10 14:39:24 +000017271static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017272 const X86Subtarget* Subtarget) {
17273 DebugLoc dl = N->getDebugLoc();
17274 EVT VT = N->getValueType(0);
17275
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017276 // Let legalize expand this if it isn't a legal type yet.
17277 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17278 return SDValue();
17279
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017280 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017281 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17282 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017283 return SDValue();
17284
17285 SDValue A = N->getOperand(0);
17286 SDValue B = N->getOperand(1);
17287 SDValue C = N->getOperand(2);
17288
17289 bool NegA = (A.getOpcode() == ISD::FNEG);
17290 bool NegB = (B.getOpcode() == ISD::FNEG);
17291 bool NegC = (C.getOpcode() == ISD::FNEG);
17292
Michael Liaof6c24ee2012-08-10 14:39:24 +000017293 // Negative multiplication when NegA xor NegB
17294 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017295 if (NegA)
17296 A = A.getOperand(0);
17297 if (NegB)
17298 B = B.getOperand(0);
17299 if (NegC)
17300 C = C.getOperand(0);
17301
17302 unsigned Opcode;
17303 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017304 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017305 else
Craig Topperbf404372012-08-31 15:40:30 +000017306 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17307
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017308 return DAG.getNode(Opcode, dl, VT, A, B, C);
17309}
17310
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017311static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017312 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017313 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017314 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17315 // (and (i32 x86isd::setcc_carry), 1)
17316 // This eliminates the zext. This transformation is necessary because
17317 // ISD::SETCC is always legalized to i8.
17318 DebugLoc dl = N->getDebugLoc();
17319 SDValue N0 = N->getOperand(0);
17320 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017321
Evan Cheng2e489c42009-12-16 00:53:11 +000017322 if (N0.getOpcode() == ISD::AND &&
17323 N0.hasOneUse() &&
17324 N0.getOperand(0).hasOneUse()) {
17325 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017326 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17327 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17328 if (!C || C->getZExtValue() != 1)
17329 return SDValue();
17330 return DAG.getNode(ISD::AND, dl, VT,
17331 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17332 N00.getOperand(0), N00.getOperand(1)),
17333 DAG.getConstant(1, VT));
17334 }
17335 }
17336
Craig Topper5a529e42013-01-18 06:44:29 +000017337 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017338 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17339 if (R.getNode())
17340 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017341 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017342
Evan Cheng2e489c42009-12-16 00:53:11 +000017343 return SDValue();
17344}
17345
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017346// Optimize x == -y --> x+y == 0
17347// x != -y --> x+y != 0
17348static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17349 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17350 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017351 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017352
17353 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17354 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17355 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17356 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17357 LHS.getValueType(), RHS, LHS.getOperand(1));
17358 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17359 addV, DAG.getConstant(0, addV.getValueType()), CC);
17360 }
17361 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17362 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17363 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17364 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17365 RHS.getValueType(), LHS, RHS.getOperand(1));
17366 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17367 addV, DAG.getConstant(0, addV.getValueType()), CC);
17368 }
17369 return SDValue();
17370}
17371
Eric Christophere187e252013-01-31 00:50:48 +000017372// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17373// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017374// an all-ones bit which is more useful than 0/1 in some cases.
17375static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17376 return DAG.getNode(ISD::AND, DL, MVT::i8,
17377 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17378 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17379 DAG.getConstant(1, MVT::i8));
17380}
17381
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017382// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017383static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17384 TargetLowering::DAGCombinerInfo &DCI,
17385 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017386 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017387 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17388 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017389
Shuxin Yanga5526a92012-10-31 23:11:48 +000017390 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017391 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017392 // materializing "setb reg".
17393 //
17394 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17395 // cannot take an immediate as its first operand.
17396 //
Eric Christophere187e252013-01-31 00:50:48 +000017397 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017398 EFLAGS.getValueType().isInteger() &&
17399 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17400 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17401 EFLAGS.getNode()->getVTList(),
17402 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17403 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17404 return MaterializeSETB(DL, NewEFLAGS, DAG);
17405 }
17406 }
17407
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017408 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17409 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17410 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017411 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017412 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017413
Michael Liao2a33cec2012-08-10 19:58:13 +000017414 SDValue Flags;
17415
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017416 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17417 if (Flags.getNode()) {
17418 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17419 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17420 }
17421
Michael Liao2a33cec2012-08-10 19:58:13 +000017422 return SDValue();
17423}
17424
17425// Optimize branch condition evaluation.
17426//
17427static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17428 TargetLowering::DAGCombinerInfo &DCI,
17429 const X86Subtarget *Subtarget) {
17430 DebugLoc DL = N->getDebugLoc();
17431 SDValue Chain = N->getOperand(0);
17432 SDValue Dest = N->getOperand(1);
17433 SDValue EFLAGS = N->getOperand(3);
17434 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17435
17436 SDValue Flags;
17437
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017438 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17439 if (Flags.getNode()) {
17440 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17441 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17442 Flags);
17443 }
17444
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017445 return SDValue();
17446}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017447
Benjamin Kramer1396c402011-06-18 11:09:41 +000017448static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17449 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017450 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017451 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017452
17453 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017454 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017455 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017456 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017457 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17458 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17459 }
17460
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017461 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17462 // a 32-bit target where SSE doesn't support i64->FP operations.
17463 if (Op0.getOpcode() == ISD::LOAD) {
17464 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17465 EVT VT = Ld->getValueType(0);
17466 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17467 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17468 !XTLI->getSubtarget()->is64Bit() &&
17469 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017470 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17471 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017472 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17473 return FILDChain;
17474 }
17475 }
17476 return SDValue();
17477}
17478
Chris Lattner23a01992010-12-20 01:37:09 +000017479// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17480static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17481 X86TargetLowering::DAGCombinerInfo &DCI) {
17482 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17483 // the result is either zero or one (depending on the input carry bit).
17484 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17485 if (X86::isZeroNode(N->getOperand(0)) &&
17486 X86::isZeroNode(N->getOperand(1)) &&
17487 // We don't have a good way to replace an EFLAGS use, so only do this when
17488 // dead right now.
17489 SDValue(N, 1).use_empty()) {
17490 DebugLoc DL = N->getDebugLoc();
17491 EVT VT = N->getValueType(0);
17492 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17493 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17494 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17495 DAG.getConstant(X86::COND_B,MVT::i8),
17496 N->getOperand(2)),
17497 DAG.getConstant(1, VT));
17498 return DCI.CombineTo(N, Res1, CarryOut);
17499 }
17500
17501 return SDValue();
17502}
17503
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017504// fold (add Y, (sete X, 0)) -> adc 0, Y
17505// (add Y, (setne X, 0)) -> sbb -1, Y
17506// (sub (sete X, 0), Y) -> sbb 0, Y
17507// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017508static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017509 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017510
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017511 // Look through ZExts.
17512 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17513 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17514 return SDValue();
17515
17516 SDValue SetCC = Ext.getOperand(0);
17517 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17518 return SDValue();
17519
17520 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17521 if (CC != X86::COND_E && CC != X86::COND_NE)
17522 return SDValue();
17523
17524 SDValue Cmp = SetCC.getOperand(1);
17525 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017526 !X86::isZeroNode(Cmp.getOperand(1)) ||
17527 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017528 return SDValue();
17529
17530 SDValue CmpOp0 = Cmp.getOperand(0);
17531 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17532 DAG.getConstant(1, CmpOp0.getValueType()));
17533
17534 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17535 if (CC == X86::COND_NE)
17536 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17537 DL, OtherVal.getValueType(), OtherVal,
17538 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17539 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17540 DL, OtherVal.getValueType(), OtherVal,
17541 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17542}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017543
Craig Topper54f952a2011-11-19 09:02:40 +000017544/// PerformADDCombine - Do target-specific dag combines on integer adds.
17545static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17546 const X86Subtarget *Subtarget) {
17547 EVT VT = N->getValueType(0);
17548 SDValue Op0 = N->getOperand(0);
17549 SDValue Op1 = N->getOperand(1);
17550
17551 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017552 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017553 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017554 isHorizontalBinOp(Op0, Op1, true))
17555 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17556
17557 return OptimizeConditionalInDecrement(N, DAG);
17558}
17559
17560static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17561 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017562 SDValue Op0 = N->getOperand(0);
17563 SDValue Op1 = N->getOperand(1);
17564
17565 // X86 can't encode an immediate LHS of a sub. See if we can push the
17566 // negation into a preceding instruction.
17567 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017568 // If the RHS of the sub is a XOR with one use and a constant, invert the
17569 // immediate. Then add one to the LHS of the sub so we can turn
17570 // X-Y -> X+~Y+1, saving one register.
17571 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17572 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017573 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017574 EVT VT = Op0.getValueType();
17575 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17576 Op1.getOperand(0),
17577 DAG.getConstant(~XorC, VT));
17578 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017579 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017580 }
17581 }
17582
Craig Topper54f952a2011-11-19 09:02:40 +000017583 // Try to synthesize horizontal adds from adds of shuffles.
17584 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017585 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017586 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017587 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017588 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17589
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017590 return OptimizeConditionalInDecrement(N, DAG);
17591}
17592
Michael Liaod9d09602012-10-23 17:34:00 +000017593/// performVZEXTCombine - Performs build vector combines
17594static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17595 TargetLowering::DAGCombinerInfo &DCI,
17596 const X86Subtarget *Subtarget) {
17597 // (vzext (bitcast (vzext (x)) -> (vzext x)
17598 SDValue In = N->getOperand(0);
17599 while (In.getOpcode() == ISD::BITCAST)
17600 In = In.getOperand(0);
17601
17602 if (In.getOpcode() != X86ISD::VZEXT)
17603 return SDValue();
17604
Nadav Rotemb39a5522013-02-14 18:20:48 +000017605 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0),
17606 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017607}
17608
Dan Gohman475871a2008-07-27 21:46:04 +000017609SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017610 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017611 SelectionDAG &DAG = DCI.DAG;
17612 switch (N->getOpcode()) {
17613 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017614 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017615 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017616 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017617 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017618 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017619 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17620 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017621 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017622 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017623 case ISD::SHL:
17624 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017625 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017626 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017627 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017628 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017629 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017630 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017631 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017632 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17633 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017634 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017635 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017636 case X86ISD::FMIN:
17637 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017638 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017639 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017640 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017641 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017642 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017643 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017644 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017645 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017646 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017647 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017648 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017649 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017650 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017651 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017652 case X86ISD::UNPCKH:
17653 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017654 case X86ISD::MOVHLPS:
17655 case X86ISD::MOVLHPS:
17656 case X86ISD::PSHUFD:
17657 case X86ISD::PSHUFHW:
17658 case X86ISD::PSHUFLW:
17659 case X86ISD::MOVSS:
17660 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017661 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017662 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017663 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017664 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017665 }
17666
Dan Gohman475871a2008-07-27 21:46:04 +000017667 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017668}
17669
Evan Chenge5b51ac2010-04-17 06:13:15 +000017670/// isTypeDesirableForOp - Return true if the target has native support for
17671/// the specified value type and it is 'desirable' to use the type for the
17672/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17673/// instruction encodings are longer and some i16 instructions are slow.
17674bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17675 if (!isTypeLegal(VT))
17676 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017677 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017678 return true;
17679
17680 switch (Opc) {
17681 default:
17682 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017683 case ISD::LOAD:
17684 case ISD::SIGN_EXTEND:
17685 case ISD::ZERO_EXTEND:
17686 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017687 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017688 case ISD::SRL:
17689 case ISD::SUB:
17690 case ISD::ADD:
17691 case ISD::MUL:
17692 case ISD::AND:
17693 case ISD::OR:
17694 case ISD::XOR:
17695 return false;
17696 }
17697}
17698
17699/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017700/// beneficial for dag combiner to promote the specified node. If true, it
17701/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017702bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017703 EVT VT = Op.getValueType();
17704 if (VT != MVT::i16)
17705 return false;
17706
Evan Cheng4c26e932010-04-19 19:29:22 +000017707 bool Promote = false;
17708 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017709 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017710 default: break;
17711 case ISD::LOAD: {
17712 LoadSDNode *LD = cast<LoadSDNode>(Op);
17713 // If the non-extending load has a single use and it's not live out, then it
17714 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017715 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17716 Op.hasOneUse()*/) {
17717 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17718 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17719 // The only case where we'd want to promote LOAD (rather then it being
17720 // promoted as an operand is when it's only use is liveout.
17721 if (UI->getOpcode() != ISD::CopyToReg)
17722 return false;
17723 }
17724 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017725 Promote = true;
17726 break;
17727 }
17728 case ISD::SIGN_EXTEND:
17729 case ISD::ZERO_EXTEND:
17730 case ISD::ANY_EXTEND:
17731 Promote = true;
17732 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017733 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017734 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017735 SDValue N0 = Op.getOperand(0);
17736 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017737 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017738 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017739 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017740 break;
17741 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017742 case ISD::ADD:
17743 case ISD::MUL:
17744 case ISD::AND:
17745 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017746 case ISD::XOR:
17747 Commute = true;
17748 // fallthrough
17749 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017750 SDValue N0 = Op.getOperand(0);
17751 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017752 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017753 return false;
17754 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017755 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017756 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017757 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017758 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017759 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017760 }
17761 }
17762
17763 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017764 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017765}
17766
Evan Cheng60c07e12006-07-05 22:17:51 +000017767//===----------------------------------------------------------------------===//
17768// X86 Inline Assembly Support
17769//===----------------------------------------------------------------------===//
17770
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017771namespace {
17772 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017773 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017774 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017775
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017776 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017777 StringRef piece(*args[i]);
17778 if (!s.startswith(piece)) // Check if the piece matches.
17779 return false;
17780
17781 s = s.substr(piece.size());
17782 StringRef::size_type pos = s.find_first_not_of(" \t");
17783 if (pos == 0) // We matched a prefix.
17784 return false;
17785
17786 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017787 }
17788
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017789 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017790 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017791 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017792}
17793
Chris Lattnerb8105652009-07-20 17:51:36 +000017794bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17795 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017796
17797 std::string AsmStr = IA->getAsmString();
17798
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017799 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17800 if (!Ty || Ty->getBitWidth() % 16 != 0)
17801 return false;
17802
Chris Lattnerb8105652009-07-20 17:51:36 +000017803 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017804 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017805 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017806
17807 switch (AsmPieces.size()) {
17808 default: return false;
17809 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017810 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017811 // we will turn this bswap into something that will be lowered to logical
17812 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17813 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017814 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017815 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17816 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17817 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17818 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17819 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17820 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017821 // No need to check constraints, nothing other than the equivalent of
17822 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017823 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017824 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017825
Chris Lattnerb8105652009-07-20 17:51:36 +000017826 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017827 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017828 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017829 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17830 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017831 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017832 const std::string &ConstraintsStr = IA->getConstraintString();
17833 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017834 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000017835 if (AsmPieces.size() == 4 &&
17836 AsmPieces[0] == "~{cc}" &&
17837 AsmPieces[1] == "~{dirflag}" &&
17838 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017839 AsmPieces[3] == "~{fpsr}")
17840 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017841 }
17842 break;
17843 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017844 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017845 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017846 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17847 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17848 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017849 AsmPieces.clear();
17850 const std::string &ConstraintsStr = IA->getConstraintString();
17851 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017852 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017853 if (AsmPieces.size() == 4 &&
17854 AsmPieces[0] == "~{cc}" &&
17855 AsmPieces[1] == "~{dirflag}" &&
17856 AsmPieces[2] == "~{flags}" &&
17857 AsmPieces[3] == "~{fpsr}")
17858 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017859 }
Evan Cheng55d42002011-01-08 01:24:27 +000017860
17861 if (CI->getType()->isIntegerTy(64)) {
17862 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17863 if (Constraints.size() >= 2 &&
17864 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17865 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17866 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017867 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17868 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17869 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017870 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017871 }
17872 }
17873 break;
17874 }
17875 return false;
17876}
17877
Chris Lattnerf4dff842006-07-11 02:54:03 +000017878/// getConstraintType - Given a constraint letter, return the type of
17879/// constraint it is for this target.
17880X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017881X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17882 if (Constraint.size() == 1) {
17883 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017884 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017885 case 'q':
17886 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017887 case 'f':
17888 case 't':
17889 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017890 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017891 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017892 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017893 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017894 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017895 case 'a':
17896 case 'b':
17897 case 'c':
17898 case 'd':
17899 case 'S':
17900 case 'D':
17901 case 'A':
17902 return C_Register;
17903 case 'I':
17904 case 'J':
17905 case 'K':
17906 case 'L':
17907 case 'M':
17908 case 'N':
17909 case 'G':
17910 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000017911 case 'e':
17912 case 'Z':
17913 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000017914 default:
17915 break;
17916 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000017917 }
Chris Lattner4234f572007-03-25 02:14:49 +000017918 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000017919}
17920
John Thompson44ab89e2010-10-29 17:29:13 +000017921/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000017922/// This object must already have been set up with the operand type
17923/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000017924TargetLowering::ConstraintWeight
17925 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000017926 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000017927 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017928 Value *CallOperandVal = info.CallOperandVal;
17929 // If we don't have a value, we can't do a match,
17930 // but allow it at the lowest weight.
17931 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000017932 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000017933 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000017934 // Look at the constraint type.
17935 switch (*constraint) {
17936 default:
John Thompson44ab89e2010-10-29 17:29:13 +000017937 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
17938 case 'R':
17939 case 'q':
17940 case 'Q':
17941 case 'a':
17942 case 'b':
17943 case 'c':
17944 case 'd':
17945 case 'S':
17946 case 'D':
17947 case 'A':
17948 if (CallOperandVal->getType()->isIntegerTy())
17949 weight = CW_SpecificReg;
17950 break;
17951 case 'f':
17952 case 't':
17953 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017954 if (type->isFloatingPointTy())
17955 weight = CW_SpecificReg;
17956 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017957 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000017958 if (type->isX86_MMXTy() && Subtarget->hasMMX())
17959 weight = CW_SpecificReg;
17960 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017961 case 'x':
17962 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000017963 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017964 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000017965 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017966 break;
17967 case 'I':
17968 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
17969 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000017970 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000017971 }
17972 break;
John Thompson44ab89e2010-10-29 17:29:13 +000017973 case 'J':
17974 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17975 if (C->getZExtValue() <= 63)
17976 weight = CW_Constant;
17977 }
17978 break;
17979 case 'K':
17980 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17981 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
17982 weight = CW_Constant;
17983 }
17984 break;
17985 case 'L':
17986 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17987 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
17988 weight = CW_Constant;
17989 }
17990 break;
17991 case 'M':
17992 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17993 if (C->getZExtValue() <= 3)
17994 weight = CW_Constant;
17995 }
17996 break;
17997 case 'N':
17998 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
17999 if (C->getZExtValue() <= 0xff)
18000 weight = CW_Constant;
18001 }
18002 break;
18003 case 'G':
18004 case 'C':
18005 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18006 weight = CW_Constant;
18007 }
18008 break;
18009 case 'e':
18010 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18011 if ((C->getSExtValue() >= -0x80000000LL) &&
18012 (C->getSExtValue() <= 0x7fffffffLL))
18013 weight = CW_Constant;
18014 }
18015 break;
18016 case 'Z':
18017 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18018 if (C->getZExtValue() <= 0xffffffff)
18019 weight = CW_Constant;
18020 }
18021 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018022 }
18023 return weight;
18024}
18025
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018026/// LowerXConstraint - try to replace an X constraint, which matches anything,
18027/// with another that has more specific requirements based on the type of the
18028/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018029const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018030LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018031 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18032 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018033 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018034 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018035 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018036 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018037 return "x";
18038 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018039
Chris Lattner5e764232008-04-26 23:02:14 +000018040 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018041}
18042
Chris Lattner48884cd2007-08-25 00:47:38 +000018043/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18044/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018045void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018046 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018047 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018048 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018049 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018050
Eric Christopher100c8332011-06-02 23:16:42 +000018051 // Only support length 1 constraints for now.
18052 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018053
Eric Christopher100c8332011-06-02 23:16:42 +000018054 char ConstraintLetter = Constraint[0];
18055 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018056 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018057 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018058 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018059 if (C->getZExtValue() <= 31) {
18060 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018061 break;
18062 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018063 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018064 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018065 case 'J':
18066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018067 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018068 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18069 break;
18070 }
18071 }
18072 return;
18073 case 'K':
18074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018075 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018076 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18077 break;
18078 }
18079 }
18080 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018081 case 'N':
18082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018083 if (C->getZExtValue() <= 255) {
18084 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018085 break;
18086 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018087 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018088 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018089 case 'e': {
18090 // 32-bit signed value
18091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018092 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18093 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018094 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018095 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018096 break;
18097 }
18098 // FIXME gcc accepts some relocatable values here too, but only in certain
18099 // memory models; it's complicated.
18100 }
18101 return;
18102 }
18103 case 'Z': {
18104 // 32-bit unsigned value
18105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018106 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18107 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018108 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18109 break;
18110 }
18111 }
18112 // FIXME gcc accepts some relocatable values here too, but only in certain
18113 // memory models; it's complicated.
18114 return;
18115 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018116 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018117 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018118 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018119 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018120 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018121 break;
18122 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018123
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018124 // In any sort of PIC mode addresses need to be computed at runtime by
18125 // adding in a register or some sort of table lookup. These can't
18126 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018127 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018128 return;
18129
Chris Lattnerdc43a882007-05-03 16:52:29 +000018130 // If we are in non-pic codegen mode, we allow the address of a global (with
18131 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018132 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018133 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018134
Chris Lattner49921962009-05-08 18:23:14 +000018135 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18136 while (1) {
18137 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18138 Offset += GA->getOffset();
18139 break;
18140 } else if (Op.getOpcode() == ISD::ADD) {
18141 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18142 Offset += C->getZExtValue();
18143 Op = Op.getOperand(0);
18144 continue;
18145 }
18146 } else if (Op.getOpcode() == ISD::SUB) {
18147 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18148 Offset += -C->getZExtValue();
18149 Op = Op.getOperand(0);
18150 continue;
18151 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018152 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018153
Chris Lattner49921962009-05-08 18:23:14 +000018154 // Otherwise, this isn't something we can handle, reject it.
18155 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018156 }
Eric Christopherfd179292009-08-27 18:07:15 +000018157
Dan Gohman46510a72010-04-15 01:51:59 +000018158 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018159 // If we require an extra load to get this address, as in PIC mode, we
18160 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018161 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18162 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018163 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018164
Devang Patel0d881da2010-07-06 22:08:15 +000018165 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
18166 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018167 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018168 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018169 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018170
Gabor Greifba36cb52008-08-28 21:40:38 +000018171 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018172 Ops.push_back(Result);
18173 return;
18174 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018175 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018176}
18177
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018178std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018179X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000018180 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018181 // First, see if this is a constraint that directly corresponds to an LLVM
18182 // register class.
18183 if (Constraint.size() == 1) {
18184 // GCC Constraint Letters
18185 switch (Constraint[0]) {
18186 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018187 // TODO: Slight differences here in allocation order and leaving
18188 // RIP in the class. Do they matter any more here than they do
18189 // in the normal allocation?
18190 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18191 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018192 if (VT == MVT::i32 || VT == MVT::f32)
18193 return std::make_pair(0U, &X86::GR32RegClass);
18194 if (VT == MVT::i16)
18195 return std::make_pair(0U, &X86::GR16RegClass);
18196 if (VT == MVT::i8 || VT == MVT::i1)
18197 return std::make_pair(0U, &X86::GR8RegClass);
18198 if (VT == MVT::i64 || VT == MVT::f64)
18199 return std::make_pair(0U, &X86::GR64RegClass);
18200 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018201 }
18202 // 32-bit fallthrough
18203 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018204 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018205 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18206 if (VT == MVT::i16)
18207 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18208 if (VT == MVT::i8 || VT == MVT::i1)
18209 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18210 if (VT == MVT::i64)
18211 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018212 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018213 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018214 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018215 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018216 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018217 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018218 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018219 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018220 return std::make_pair(0U, &X86::GR32RegClass);
18221 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018222 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018223 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018224 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018225 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018226 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018227 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018228 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18229 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018230 case 'f': // FP Stack registers.
18231 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18232 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018233 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018234 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018235 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018236 return std::make_pair(0U, &X86::RFP64RegClass);
18237 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018238 case 'y': // MMX_REGS if MMX allowed.
18239 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018240 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018241 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018242 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018243 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018244 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018245 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018246
Owen Anderson825b72b2009-08-11 20:47:22 +000018247 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018248 default: break;
18249 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018250 case MVT::f32:
18251 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018252 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018253 case MVT::f64:
18254 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018255 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018256 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018257 case MVT::v16i8:
18258 case MVT::v8i16:
18259 case MVT::v4i32:
18260 case MVT::v2i64:
18261 case MVT::v4f32:
18262 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018263 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018264 // AVX types.
18265 case MVT::v32i8:
18266 case MVT::v16i16:
18267 case MVT::v8i32:
18268 case MVT::v4i64:
18269 case MVT::v8f32:
18270 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018271 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018272 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018273 break;
18274 }
18275 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018276
Chris Lattnerf76d1802006-07-31 23:26:50 +000018277 // Use the default implementation in TargetLowering to convert the register
18278 // constraint into a member of a register class.
18279 std::pair<unsigned, const TargetRegisterClass*> Res;
18280 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018281
18282 // Not found as a standard register?
18283 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018284 // Map st(0) -> st(7) -> ST0
18285 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18286 tolower(Constraint[1]) == 's' &&
18287 tolower(Constraint[2]) == 't' &&
18288 Constraint[3] == '(' &&
18289 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18290 Constraint[5] == ')' &&
18291 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018292
Chris Lattner56d77c72009-09-13 22:41:48 +000018293 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018294 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018295 return Res;
18296 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018297
Chris Lattner56d77c72009-09-13 22:41:48 +000018298 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018299 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018300 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018301 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018302 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018303 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018304
18305 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018306 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018307 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018308 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018309 return Res;
18310 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018311
Dale Johannesen330169f2008-11-13 21:52:36 +000018312 // 'A' means EAX + EDX.
18313 if (Constraint == "A") {
18314 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018315 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018316 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018317 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018318 return Res;
18319 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018320
Chris Lattnerf76d1802006-07-31 23:26:50 +000018321 // Otherwise, check to see if this is a register class of the wrong value
18322 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18323 // turn into {ax},{dx}.
18324 if (Res.second->hasType(VT))
18325 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018326
Chris Lattnerf76d1802006-07-31 23:26:50 +000018327 // All of the single-register GCC register classes map their values onto
18328 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18329 // really want an 8-bit or 32-bit register, map to the appropriate register
18330 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018331 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018332 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018333 unsigned DestReg = 0;
18334 switch (Res.first) {
18335 default: break;
18336 case X86::AX: DestReg = X86::AL; break;
18337 case X86::DX: DestReg = X86::DL; break;
18338 case X86::CX: DestReg = X86::CL; break;
18339 case X86::BX: DestReg = X86::BL; break;
18340 }
18341 if (DestReg) {
18342 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018343 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018344 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018345 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018346 unsigned DestReg = 0;
18347 switch (Res.first) {
18348 default: break;
18349 case X86::AX: DestReg = X86::EAX; break;
18350 case X86::DX: DestReg = X86::EDX; break;
18351 case X86::CX: DestReg = X86::ECX; break;
18352 case X86::BX: DestReg = X86::EBX; break;
18353 case X86::SI: DestReg = X86::ESI; break;
18354 case X86::DI: DestReg = X86::EDI; break;
18355 case X86::BP: DestReg = X86::EBP; break;
18356 case X86::SP: DestReg = X86::ESP; break;
18357 }
18358 if (DestReg) {
18359 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018360 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018361 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018362 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018363 unsigned DestReg = 0;
18364 switch (Res.first) {
18365 default: break;
18366 case X86::AX: DestReg = X86::RAX; break;
18367 case X86::DX: DestReg = X86::RDX; break;
18368 case X86::CX: DestReg = X86::RCX; break;
18369 case X86::BX: DestReg = X86::RBX; break;
18370 case X86::SI: DestReg = X86::RSI; break;
18371 case X86::DI: DestReg = X86::RDI; break;
18372 case X86::BP: DestReg = X86::RBP; break;
18373 case X86::SP: DestReg = X86::RSP; break;
18374 }
18375 if (DestReg) {
18376 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018377 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018378 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018379 }
Craig Topperc9099502012-04-20 06:31:50 +000018380 } else if (Res.second == &X86::FR32RegClass ||
18381 Res.second == &X86::FR64RegClass ||
18382 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018383 // Handle references to XMM physical registers that got mapped into the
18384 // wrong class. This can happen with constraints like {xmm0} where the
18385 // target independent register mapper will just pick the first match it can
18386 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018387
18388 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018389 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018390 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018391 Res.second = &X86::FR64RegClass;
18392 else if (X86::VR128RegClass.hasType(VT))
18393 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018394 else if (X86::VR256RegClass.hasType(VT))
18395 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018396 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018397
Chris Lattnerf76d1802006-07-31 23:26:50 +000018398 return Res;
18399}